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.

145 lines
5.1 KiB

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