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.

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