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.

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