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.

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