You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.8 KiB

  1. package statedb
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/hermeznetwork/hermez-node/eth"
  8. "github.com/hermeznetwork/hermez-node/log"
  9. "github.com/hermeznetwork/hermez-node/test/til"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. // TODO: Test CollectedFees output
  14. func TestProcessTxsSynchronizer(t *testing.T) {
  15. // TODO once TTGL is updated, use the blockchain L2Tx (not PoolL2Tx) for
  16. // the Synchronizer tests
  17. dir, err := ioutil.TempDir("", "tmpdb")
  18. require.Nil(t, err)
  19. defer assert.Nil(t, os.RemoveAll(dir))
  20. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  21. assert.Nil(t, err)
  22. // generate test transactions from test.SetTest0 code
  23. tc := til.NewContext(eth.RollupConstMaxL1UserTx)
  24. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  25. require.Nil(t, err)
  26. assert.Equal(t, 31, len(blocks[0].L1UserTxs))
  27. assert.Equal(t, 4, len(blocks[0].Batches[0].L1CoordinatorTxs))
  28. assert.Equal(t, 0, len(blocks[0].Batches[1].L1CoordinatorTxs))
  29. assert.Equal(t, 22, len(blocks[0].Batches[2].L2Txs))
  30. assert.Equal(t, 1, len(blocks[1].Batches[0].L1CoordinatorTxs))
  31. assert.Equal(t, 59, len(blocks[1].Batches[0].L2Txs))
  32. assert.Equal(t, 1, len(blocks[1].Batches[1].L1CoordinatorTxs))
  33. assert.Equal(t, 8, len(blocks[1].Batches[1].L2Txs))
  34. // Coordinator Idx where to send the fees
  35. coordIdxs := []common.Idx{256, 257, 258, 259}
  36. // Idx of user 'A'
  37. idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
  38. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  39. // to create the Coordinator accounts to receive the fees
  40. log.Debug("1st batch, 1st block, only L1CoordinatorTxs")
  41. ptOut, err := sdb.ProcessTxs(nil, nil, blocks[0].Batches[0].L1CoordinatorTxs, nil)
  42. require.Nil(t, err)
  43. assert.Equal(t, 4, len(ptOut.CreatedAccounts))
  44. log.Debug("2nd batch, 1st block")
  45. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[1].L2Txs)
  46. ptOut, err = sdb.ProcessTxs(coordIdxs, blocks[0].L1UserTxs, blocks[0].Batches[1].L1CoordinatorTxs, l2Txs)
  47. require.Nil(t, err)
  48. assert.Equal(t, 0, len(ptOut.ExitInfos))
  49. assert.Equal(t, 31, len(ptOut.CreatedAccounts))
  50. acc, err := sdb.GetAccount(idxA1)
  51. require.Nil(t, err)
  52. assert.Equal(t, "50", acc.Balance.String())
  53. log.Debug("3rd batch, 1st block")
  54. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs)
  55. ptOut, err = sdb.ProcessTxs(coordIdxs, nil, blocks[0].Batches[2].L1CoordinatorTxs, l2Txs)
  56. require.Nil(t, err)
  57. // TODO once TTGL is updated, add a check that a input poolL2Tx with
  58. // Nonce & TokenID =0, after ProcessTxs call has the expected value
  59. assert.Equal(t, 0, len(ptOut.ExitInfos))
  60. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  61. acc, err = sdb.GetAccount(idxA1)
  62. require.Nil(t, err)
  63. assert.Equal(t, "28", acc.Balance.String())
  64. log.Debug("1st batch, 2nd block")
  65. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Batches[0].L2Txs)
  66. ptOut, err = sdb.ProcessTxs(coordIdxs, nil, blocks[1].Batches[0].L1CoordinatorTxs, l2Txs)
  67. require.Nil(t, err)
  68. assert.Equal(t, 4, len(ptOut.ExitInfos)) // the 'ForceExit(1)' is not computed yet, as the batch is without L1UserTxs
  69. assert.Equal(t, 1, len(ptOut.CreatedAccounts))
  70. acc, err = sdb.GetAccount(idxA1)
  71. require.Nil(t, err)
  72. assert.Equal(t, "53", acc.Balance.String())
  73. log.Debug("2nd batch, 2nd block")
  74. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Batches[1].L2Txs)
  75. ptOut, err = sdb.ProcessTxs(coordIdxs, blocks[1].L1UserTxs, blocks[1].Batches[1].L1CoordinatorTxs, l2Txs)
  76. require.Nil(t, err)
  77. assert.Equal(t, 2, len(ptOut.ExitInfos)) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5'
  78. assert.Equal(t, 1, len(ptOut.CreatedAccounts))
  79. acc, err = sdb.GetAccount(idxA1)
  80. assert.Nil(t, err)
  81. assert.Equal(t, "78", acc.Balance.String())
  82. idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
  83. acc, err = sdb.GetAccount(idxB0)
  84. require.Nil(t, err)
  85. assert.Equal(t, "51", acc.Balance.String())
  86. // get balance of Coordinator account for TokenID==0
  87. acc, err = sdb.GetAccount(common.Idx(256))
  88. require.Nil(t, err)
  89. assert.Equal(t, "2", acc.Balance.String())
  90. }
  91. /*
  92. WIP
  93. func TestProcessTxsBatchBuilder(t *testing.T) {
  94. dir, err := ioutil.TempDir("", "tmpdb")
  95. require.Nil(t, err)
  96. defer assert.Nil(t, os.RemoveAll(dir))
  97. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  98. assert.Nil(t, err)
  99. // generate test transactions from test.SetTest0 code
  100. tc := til.NewContext()
  101. blocks := tc.GenerateBlocks(til.SetBlockchain0)
  102. assert.Equal(t, 29, len(blocks[0].Batches[0].L1UserTxs))
  103. assert.Equal(t, 0, len(blocks[0].Batches[0].L1CoordinatorTxs))
  104. assert.Equal(t, 21, len(blocks[0].Batches[0].L2Txs))
  105. assert.Equal(t, 1, len(blocks[0].Batches[1].L1UserTxs))
  106. assert.Equal(t, 1, len(blocks[0].Batches[1].L1CoordinatorTxs))
  107. assert.Equal(t, 59, len(blocks[0].Batches[1].L2Txs))
  108. assert.Equal(t, 9, len(blocks[0].Batches[2].L1UserTxs))
  109. assert.Equal(t, 0, len(blocks[0].Batches[2].L1CoordinatorTxs))
  110. assert.Equal(t, 8, len(blocks[0].Batches[2].L2Txs))
  111. // Idx of user 'A'
  112. idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
  113. // use first batch
  114. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[0].L2Txs)
  115. _, exitInfos, err := sdb.ProcessTxs(coordIdxs, blocks[0].Batches[0].L1UserTxs, blocks[0].Batches[0].L1CoordinatorTxs, l2Txs)
  116. require.Nil(t, err)
  117. assert.Equal(t, 0, len(exitInfos))
  118. acc, err := sdb.GetAccount(idxA1)
  119. assert.Nil(t, err)
  120. assert.Equal(t, "28", acc.Balance.String())
  121. // use second batch
  122. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[1].L2Txs)
  123. _, exitInfos, err = sdb.ProcessTxs(coordIdxs, blocks[0].Batches[1].L1UserTxs, blocks[0].Batches[1].L1CoordinatorTxs, l2Txs)
  124. require.Nil(t, err)
  125. assert.Equal(t, 5, len(exitInfos))
  126. acc, err = sdb.GetAccount(idxA1)
  127. require.Nil(t, err)
  128. assert.Equal(t, "48", acc.Balance.String())
  129. // use third batch
  130. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs)
  131. _, exitInfos, err = sdb.ProcessTxs(coordIdxs, blocks[0].Batches[2].L1UserTxs, blocks[0].Batches[2].L1CoordinatorTxs, l2Txs)
  132. require.Nil(t, err)
  133. assert.Equal(t, 1, len(exitInfos))
  134. acc, err = sdb.GetAccount(idxA1)
  135. assert.Nil(t, err)
  136. assert.Equal(t, "23", acc.Balance.String())
  137. }
  138. func TestZKInputsGeneration(t *testing.T) {
  139. dir, err := ioutil.TempDir("", "tmpdb")
  140. require.Nil(t, err)
  141. defer assert.Nil(t, os.RemoveAll(dir))
  142. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  143. assert.Nil(t, err)
  144. // generate test transactions from test.SetTest0 code
  145. tc := til.NewContext()
  146. blocks := tc.GenerateBlocks(til.SetBlockchain0)
  147. assert.Equal(t, 29, len(blocks[0].Batches[0].L1UserTxs))
  148. assert.Equal(t, 0, len(blocks[0].Batches[0].L1CoordinatorTxs))
  149. assert.Equal(t, 21, len(blocks[0].Batches[0].L2Txs))
  150. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[0].L2Txs)
  151. zki, _, err := sdb.ProcessTxs(coordIdxs, blocks[0].Batches[0].L1UserTxs, blocks[0].Batches[0].L1CoordinatorTxs, l2Txs)
  152. require.Nil(t, err)
  153. s, err := json.Marshal(zki)
  154. require.Nil(t, err)
  155. debug:=true
  156. if debug {
  157. fmt.Println(string(s))
  158. }
  159. }
  160. */