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.

246 lines
9.3 KiB

  1. package statedb
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/hermeznetwork/hermez-node/eth"
  10. "github.com/hermeznetwork/hermez-node/log"
  11. "github.com/hermeznetwork/hermez-node/test/til"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. )
  15. func TestProcessTxsSynchronizer(t *testing.T) {
  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.SetBlockchain0 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, 62, 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("block:0 batch:0, 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. assert.Equal(t, 0, len(ptOut.CollectedFees))
  44. log.Debug("block:0 batch:1")
  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. assert.Equal(t, 4, len(ptOut.CollectedFees))
  51. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  52. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(1)].String())
  53. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  54. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  55. acc, err := sdb.GetAccount(idxA1)
  56. require.Nil(t, err)
  57. assert.Equal(t, "50", acc.Balance.String())
  58. log.Debug("block:0 batch:2")
  59. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs)
  60. ptOut, err = sdb.ProcessTxs(coordIdxs, nil, blocks[0].Batches[2].L1CoordinatorTxs, l2Txs)
  61. require.Nil(t, err)
  62. assert.Equal(t, 0, len(ptOut.ExitInfos))
  63. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  64. assert.Equal(t, 4, len(ptOut.CollectedFees))
  65. assert.Equal(t, "2", ptOut.CollectedFees[common.TokenID(0)].String())
  66. assert.Equal(t, "1", ptOut.CollectedFees[common.TokenID(1)].String())
  67. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  68. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  69. acc, err = sdb.GetAccount(idxA1)
  70. require.Nil(t, err)
  71. assert.Equal(t, "35", acc.Balance.String())
  72. log.Debug("block:1 batch:0")
  73. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Batches[0].L2Txs)
  74. // before processing expect l2Txs[0:2].Nonce==0
  75. assert.Equal(t, common.Nonce(0), l2Txs[0].Nonce)
  76. assert.Equal(t, common.Nonce(0), l2Txs[1].Nonce)
  77. assert.Equal(t, common.Nonce(0), l2Txs[2].Nonce)
  78. ptOut, err = sdb.ProcessTxs(coordIdxs, nil, blocks[1].Batches[0].L1CoordinatorTxs, l2Txs)
  79. require.Nil(t, err)
  80. // after processing expect l2Txs[0:2].Nonce!=0 and has expected value
  81. assert.Equal(t, common.Nonce(6), l2Txs[0].Nonce)
  82. assert.Equal(t, common.Nonce(7), l2Txs[1].Nonce)
  83. assert.Equal(t, common.Nonce(8), l2Txs[2].Nonce)
  84. assert.Equal(t, 4, len(ptOut.ExitInfos)) // the 'ForceExit(1)' is not computed yet, as the batch is without L1UserTxs
  85. assert.Equal(t, 1, len(ptOut.CreatedAccounts))
  86. assert.Equal(t, 4, len(ptOut.CollectedFees))
  87. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  88. assert.Equal(t, "1", ptOut.CollectedFees[common.TokenID(1)].String())
  89. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  90. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  91. acc, err = sdb.GetAccount(idxA1)
  92. require.Nil(t, err)
  93. assert.Equal(t, "57", acc.Balance.String())
  94. log.Debug("block:1 batch:1")
  95. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Batches[1].L2Txs)
  96. ptOut, err = sdb.ProcessTxs(coordIdxs, blocks[1].L1UserTxs, blocks[1].Batches[1].L1CoordinatorTxs, l2Txs)
  97. require.Nil(t, err)
  98. assert.Equal(t, 2, len(ptOut.ExitInfos)) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5'
  99. assert.Equal(t, 1, len(ptOut.CreatedAccounts))
  100. assert.Equal(t, 4, len(ptOut.CollectedFees))
  101. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  102. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(1)].String())
  103. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  104. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  105. acc, err = sdb.GetAccount(idxA1)
  106. assert.Nil(t, err)
  107. assert.Equal(t, "82", acc.Balance.String())
  108. idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
  109. acc, err = sdb.GetAccount(idxB0)
  110. require.Nil(t, err)
  111. assert.Equal(t, "51", acc.Balance.String())
  112. // get balance of Coordinator account for TokenID==0
  113. acc, err = sdb.GetAccount(common.Idx(256))
  114. require.Nil(t, err)
  115. assert.Equal(t, "2", acc.Balance.String())
  116. }
  117. func TestProcessTxsBatchBuilder(t *testing.T) {
  118. dir, err := ioutil.TempDir("", "tmpdb")
  119. require.Nil(t, err)
  120. defer assert.Nil(t, os.RemoveAll(dir))
  121. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  122. assert.Nil(t, err)
  123. // generate test transactions from test.SetBlockchain0 code
  124. tc := til.NewContext(eth.RollupConstMaxL1UserTx)
  125. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  126. require.Nil(t, err)
  127. // Coordinator Idx where to send the fees
  128. coordIdxs := []common.Idx{256, 257, 258, 259}
  129. // Idx of user 'A'
  130. idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
  131. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  132. // to create the Coordinator accounts to receive the fees
  133. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  134. ptOut, err := sdb.ProcessTxs(nil, nil, blocks[0].Batches[0].L1CoordinatorTxs, nil)
  135. require.Nil(t, err)
  136. // expect 0 at CreatedAccount, as is only computed when StateDB.Type==TypeSynchronizer
  137. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  138. log.Debug("block:0 batch:1")
  139. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[1].L2Txs)
  140. ptOut, err = sdb.ProcessTxs(coordIdxs, blocks[0].L1UserTxs, blocks[0].Batches[1].L1CoordinatorTxs, l2Txs)
  141. require.Nil(t, err)
  142. assert.Equal(t, 0, len(ptOut.ExitInfos))
  143. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  144. acc, err := sdb.GetAccount(idxA1)
  145. require.Nil(t, err)
  146. assert.Equal(t, "50", acc.Balance.String())
  147. log.Debug("block:0 batch:2")
  148. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs)
  149. ptOut, err = sdb.ProcessTxs(coordIdxs, nil, blocks[0].Batches[2].L1CoordinatorTxs, l2Txs)
  150. require.Nil(t, err)
  151. assert.Equal(t, 0, len(ptOut.ExitInfos))
  152. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  153. acc, err = sdb.GetAccount(idxA1)
  154. require.Nil(t, err)
  155. assert.Equal(t, "35", acc.Balance.String())
  156. log.Debug("block:1 batch:0")
  157. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Batches[0].L2Txs)
  158. _, err = sdb.ProcessTxs(coordIdxs, nil, blocks[1].Batches[0].L1CoordinatorTxs, l2Txs)
  159. require.Nil(t, err)
  160. acc, err = sdb.GetAccount(idxA1)
  161. require.Nil(t, err)
  162. assert.Equal(t, "57", acc.Balance.String())
  163. log.Debug("block:1 batch:1")
  164. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Batches[1].L2Txs)
  165. _, err = sdb.ProcessTxs(coordIdxs, blocks[1].L1UserTxs, blocks[1].Batches[1].L1CoordinatorTxs, l2Txs)
  166. require.Nil(t, err)
  167. acc, err = sdb.GetAccount(idxA1)
  168. assert.Nil(t, err)
  169. assert.Equal(t, "82", acc.Balance.String())
  170. idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
  171. acc, err = sdb.GetAccount(idxB0)
  172. require.Nil(t, err)
  173. assert.Equal(t, "51", acc.Balance.String())
  174. // get balance of Coordinator account for TokenID==0
  175. acc, err = sdb.GetAccount(common.Idx(256))
  176. require.Nil(t, err)
  177. assert.Equal(t, common.TokenID(0), acc.TokenID)
  178. assert.Equal(t, "2", acc.Balance.String())
  179. acc, err = sdb.GetAccount(common.Idx(257))
  180. require.Nil(t, err)
  181. assert.Equal(t, common.TokenID(1), acc.TokenID)
  182. assert.Equal(t, "2", acc.Balance.String())
  183. }
  184. func TestZKInputsGeneration(t *testing.T) {
  185. dir, err := ioutil.TempDir("", "tmpdb")
  186. require.Nil(t, err)
  187. defer assert.Nil(t, os.RemoveAll(dir))
  188. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  189. assert.Nil(t, err)
  190. // generate test transactions from test.SetBlockchain0 code
  191. tc := til.NewContext(eth.RollupConstMaxL1UserTx)
  192. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  193. require.Nil(t, err)
  194. // Coordinator Idx where to send the fees
  195. coordIdxs := []common.Idx{256, 257, 258, 259}
  196. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  197. _, err = sdb.ProcessTxs(nil, nil, blocks[0].Batches[0].L1CoordinatorTxs, nil)
  198. require.Nil(t, err)
  199. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[1].L2Txs)
  200. ptOut, err := sdb.ProcessTxs(coordIdxs, blocks[0].L1UserTxs, blocks[0].Batches[1].L1CoordinatorTxs, l2Txs)
  201. require.Nil(t, err)
  202. s, err := json.Marshal(ptOut.ZKInputs)
  203. require.Nil(t, err)
  204. debug := false
  205. if debug {
  206. fmt.Println(string(s))
  207. }
  208. }