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.

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