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.

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