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.

124 lines
3.6 KiB

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