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.

93 lines
2.8 KiB

  1. package statedb
  2. import (
  3. "io/ioutil"
  4. "strings"
  5. "testing"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/hermeznetwork/hermez-node/test"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestProcessTxs(t *testing.T) {
  12. dir, err := ioutil.TempDir("", "tmpdb")
  13. require.Nil(t, err)
  14. sdb, err := NewStateDB(dir, true, 32)
  15. assert.Nil(t, err)
  16. // generate test transactions from test.SetTest0 code
  17. parser := test.NewParser(strings.NewReader(test.SetTest0))
  18. instructions, err := parser.Parse()
  19. assert.Nil(t, err)
  20. l1Txs, coordinatorL1Txs, poolL2Txs := test.GenerateTestTxs(t, instructions)
  21. assert.Equal(t, 29, len(l1Txs[0]))
  22. assert.Equal(t, 0, len(coordinatorL1Txs[0]))
  23. assert.Equal(t, 21, len(poolL2Txs[0]))
  24. // iterate for each batch
  25. for i := 0; i < len(l1Txs); i++ {
  26. l2Txs := common.PoolL2TxsToL2Txs(poolL2Txs[i])
  27. _, _, err := sdb.ProcessTxs(true, l1Txs[i], coordinatorL1Txs[i], l2Txs)
  28. require.Nil(t, err)
  29. }
  30. acc, err := sdb.GetAccount(common.Idx(1))
  31. assert.Nil(t, err)
  32. assert.Equal(t, "23", acc.Balance.String())
  33. }
  34. func TestProcessTxsBatchByBatch(t *testing.T) {
  35. dir, err := ioutil.TempDir("", "tmpdb")
  36. require.Nil(t, err)
  37. sdb, err := NewStateDB(dir, true, 32)
  38. assert.Nil(t, err)
  39. // generate test transactions from test.SetTest0 code
  40. parser := test.NewParser(strings.NewReader(test.SetTest0))
  41. instructions, err := parser.Parse()
  42. assert.Nil(t, err)
  43. l1Txs, coordinatorL1Txs, poolL2Txs := test.GenerateTestTxs(t, instructions)
  44. assert.Equal(t, 29, len(l1Txs[0]))
  45. assert.Equal(t, 0, len(coordinatorL1Txs[0]))
  46. assert.Equal(t, 21, len(poolL2Txs[0]))
  47. assert.Equal(t, 5, len(l1Txs[1]))
  48. assert.Equal(t, 1, len(coordinatorL1Txs[1]))
  49. assert.Equal(t, 55, len(poolL2Txs[1]))
  50. assert.Equal(t, 10, len(l1Txs[2]))
  51. assert.Equal(t, 0, len(coordinatorL1Txs[2]))
  52. assert.Equal(t, 7, len(poolL2Txs[2]))
  53. // use first batch
  54. l2txs := common.PoolL2TxsToL2Txs(poolL2Txs[0])
  55. _, exitInfos, err := sdb.ProcessTxs(true, l1Txs[0], coordinatorL1Txs[0], l2txs)
  56. require.Nil(t, err)
  57. assert.Equal(t, 0, len(exitInfos))
  58. acc, err := sdb.GetAccount(common.Idx(1))
  59. assert.Nil(t, err)
  60. assert.Equal(t, "28", acc.Balance.String())
  61. // use second batch
  62. l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[1])
  63. _, exitInfos, err = sdb.ProcessTxs(true, l1Txs[1], coordinatorL1Txs[1], l2txs)
  64. require.Nil(t, err)
  65. assert.Equal(t, 5, len(exitInfos))
  66. acc, err = sdb.GetAccount(common.Idx(1))
  67. assert.Nil(t, err)
  68. assert.Equal(t, "48", acc.Balance.String())
  69. // use third batch
  70. l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[2])
  71. _, exitInfos, err = sdb.ProcessTxs(true, l1Txs[2], coordinatorL1Txs[2], l2txs)
  72. require.Nil(t, err)
  73. assert.Equal(t, 1, len(exitInfos))
  74. acc, err = sdb.GetAccount(common.Idx(1))
  75. assert.Nil(t, err)
  76. assert.Equal(t, "23", acc.Balance.String())
  77. }