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.

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