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.

155 lines
5.5 KiB

  1. package statedb
  2. import (
  3. "io/ioutil"
  4. "testing"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/hermeznetwork/hermez-node/eth"
  7. "github.com/hermeznetwork/hermez-node/test/transakcio"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestProcessTxsSynchronizer(t *testing.T) {
  12. // TODO once TTGL is updated, use the blockchain L2Tx (not PoolL2Tx) for
  13. // the Synchronizer tests
  14. dir, err := ioutil.TempDir("", "tmpdb")
  15. require.Nil(t, err)
  16. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  17. assert.Nil(t, err)
  18. // generate test transactions from test.SetTest0 code
  19. tc := transakcio.NewTestContext(eth.RollupConstMaxL1UserTx)
  20. blocks, err := tc.GenerateBlocks(transakcio.SetBlockchain0)
  21. require.Nil(t, err)
  22. assert.Equal(t, 29, len(blocks[0].L1UserTxs))
  23. assert.Equal(t, 0, len(blocks[0].Batches[0].L1CoordinatorTxs))
  24. assert.Equal(t, 21, len(blocks[0].Batches[1].L2Txs))
  25. assert.Equal(t, 1, len(blocks[1].Batches[0].L1CoordinatorTxs))
  26. assert.Equal(t, 59, len(blocks[1].Batches[0].L2Txs))
  27. assert.Equal(t, 1, len(blocks[1].Batches[1].L1CoordinatorTxs))
  28. assert.Equal(t, 8, len(blocks[1].Batches[1].L2Txs))
  29. // use first batch
  30. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[0].L2Txs)
  31. _, exitInfos, err := sdb.ProcessTxs(blocks[0].L1UserTxs, blocks[0].Batches[0].L1CoordinatorTxs, l2Txs)
  32. require.Nil(t, err)
  33. assert.Equal(t, 0, len(exitInfos))
  34. acc, err := sdb.GetAccount(common.Idx(256))
  35. require.Nil(t, err)
  36. assert.Equal(t, "50", acc.Balance.String())
  37. // second batch of first block
  38. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[1].L2Txs)
  39. _, exitInfos, err = sdb.ProcessTxs(nil, blocks[0].Batches[1].L1CoordinatorTxs, l2Txs)
  40. require.Nil(t, err)
  41. // TODO once TTGL is updated, add a check that a input poolL2Tx with
  42. // Nonce & TokenID =0, after ProcessTxs call has the expected value
  43. assert.Equal(t, 0, len(exitInfos))
  44. acc, err = sdb.GetAccount(common.Idx(256))
  45. require.Nil(t, err)
  46. assert.Equal(t, "28", acc.Balance.String())
  47. // use second batch
  48. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Batches[0].L2Txs)
  49. _, exitInfos, err = sdb.ProcessTxs(nil, blocks[1].Batches[0].L1CoordinatorTxs, l2Txs)
  50. require.Nil(t, err)
  51. assert.Equal(t, 4, len(exitInfos)) // the 'ForceExit(1)' is not computed yet, as the batch is without L1UserTxs
  52. acc, err = sdb.GetAccount(common.Idx(256))
  53. require.Nil(t, err)
  54. assert.Equal(t, "53", acc.Balance.String())
  55. // use third batch
  56. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Batches[1].L2Txs)
  57. _, exitInfos, err = sdb.ProcessTxs(blocks[1].L1UserTxs, blocks[1].Batches[1].L1CoordinatorTxs, l2Txs)
  58. require.Nil(t, err)
  59. assert.Equal(t, 2, len(exitInfos)) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5'
  60. acc, err = sdb.GetAccount(common.Idx(256))
  61. assert.Nil(t, err)
  62. assert.Equal(t, "73", acc.Balance.String())
  63. }
  64. /*
  65. WIP
  66. func TestProcessTxsBatchBuilder(t *testing.T) {
  67. dir, err := ioutil.TempDir("", "tmpdb")
  68. require.Nil(t, err)
  69. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  70. assert.Nil(t, err)
  71. // generate test transactions from test.SetTest0 code
  72. tc := transakcio.NewTestContext()
  73. blocks := tc.GenerateBlocks(transakcio.SetBlockchain0)
  74. assert.Equal(t, 29, len(blocks[0].Batches[0].L1UserTxs))
  75. assert.Equal(t, 0, len(blocks[0].Batches[0].L1CoordinatorTxs))
  76. assert.Equal(t, 21, len(blocks[0].Batches[0].L2Txs))
  77. assert.Equal(t, 1, len(blocks[0].Batches[1].L1UserTxs))
  78. assert.Equal(t, 1, len(blocks[0].Batches[1].L1CoordinatorTxs))
  79. assert.Equal(t, 59, len(blocks[0].Batches[1].L2Txs))
  80. assert.Equal(t, 9, len(blocks[0].Batches[2].L1UserTxs))
  81. assert.Equal(t, 0, len(blocks[0].Batches[2].L1CoordinatorTxs))
  82. assert.Equal(t, 8, len(blocks[0].Batches[2].L2Txs))
  83. // use first batch
  84. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[0].L2Txs)
  85. _, exitInfos, err := sdb.ProcessTxs(blocks[0].Batches[0].L1UserTxs, blocks[0].Batches[0].L1CoordinatorTxs, l2Txs)
  86. require.Nil(t, err)
  87. assert.Equal(t, 0, len(exitInfos))
  88. acc, err := sdb.GetAccount(common.Idx(256))
  89. assert.Nil(t, err)
  90. assert.Equal(t, "28", acc.Balance.String())
  91. // use second batch
  92. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[1].L2Txs)
  93. _, exitInfos, err = sdb.ProcessTxs(blocks[0].Batches[1].L1UserTxs, blocks[0].Batches[1].L1CoordinatorTxs, l2Txs)
  94. require.Nil(t, err)
  95. assert.Equal(t, 5, len(exitInfos))
  96. acc, err = sdb.GetAccount(common.Idx(256))
  97. require.Nil(t, err)
  98. assert.Equal(t, "48", acc.Balance.String())
  99. // use third batch
  100. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs)
  101. _, exitInfos, err = sdb.ProcessTxs(blocks[0].Batches[2].L1UserTxs, blocks[0].Batches[2].L1CoordinatorTxs, l2Txs)
  102. require.Nil(t, err)
  103. assert.Equal(t, 1, len(exitInfos))
  104. acc, err = sdb.GetAccount(common.Idx(256))
  105. assert.Nil(t, err)
  106. assert.Equal(t, "23", acc.Balance.String())
  107. }
  108. func TestZKInputsGeneration(t *testing.T) {
  109. dir, err := ioutil.TempDir("", "tmpdb")
  110. require.Nil(t, err)
  111. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  112. assert.Nil(t, err)
  113. // generate test transactions from test.SetTest0 code
  114. tc := transakcio.NewTestContext()
  115. blocks := tc.GenerateBlocks(transakcio.SetBlockchain0)
  116. assert.Equal(t, 29, len(blocks[0].Batches[0].L1UserTxs))
  117. assert.Equal(t, 0, len(blocks[0].Batches[0].L1CoordinatorTxs))
  118. assert.Equal(t, 21, len(blocks[0].Batches[0].L2Txs))
  119. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[0].L2Txs)
  120. zki, _, err := sdb.ProcessTxs(blocks[0].Batches[0].L1UserTxs, blocks[0].Batches[0].L1CoordinatorTxs, l2Txs)
  121. require.Nil(t, err)
  122. s, err := json.Marshal(zki)
  123. require.Nil(t, err)
  124. debug:=true
  125. if debug {
  126. fmt.Println(string(s))
  127. }
  128. }
  129. */