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.

184 lines
5.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, TypeBatchBuilder, 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(l1Txs[i], coordinatorL1Txs[i], poolL2Txs[i])
  31. require.Nil(t, err)
  32. }
  33. accountIdx := common.Idx(256)
  34. acc, err := sdb.GetAccount(&accountIdx)
  35. assert.Nil(t, err)
  36. assert.Equal(t, "23", acc.Balance.String())
  37. }
  38. func TestProcessTxsSynchronizer(t *testing.T) {
  39. // TODO once TTGL is updated, use the blockchain L2Tx (not PoolL2Tx) for
  40. // the Synchronizer tests
  41. dir, err := ioutil.TempDir("", "tmpdb")
  42. require.Nil(t, err)
  43. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  44. assert.Nil(t, err)
  45. // generate test transactions from test.SetTest0 code
  46. parser := test.NewParser(strings.NewReader(test.SetTest0))
  47. instructions, err := parser.Parse()
  48. assert.Nil(t, err)
  49. l1Txs, coordinatorL1Txs, poolL2Txs, _ := test.GenerateTestTxs(t, instructions)
  50. assert.Equal(t, 29, len(l1Txs[0]))
  51. assert.Equal(t, 0, len(coordinatorL1Txs[0]))
  52. assert.Equal(t, 21, len(poolL2Txs[0]))
  53. assert.Equal(t, 5, len(l1Txs[1]))
  54. assert.Equal(t, 1, len(coordinatorL1Txs[1]))
  55. assert.Equal(t, 55, len(poolL2Txs[1]))
  56. assert.Equal(t, 10, len(l1Txs[2]))
  57. assert.Equal(t, 0, len(coordinatorL1Txs[2]))
  58. assert.Equal(t, 7, len(poolL2Txs[2]))
  59. // use first batch
  60. // l2txs := common.PoolL2TxsToL2Txs(poolL2Txs[0])
  61. _, exitInfos, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
  62. require.Nil(t, err)
  63. // TODO once TTGL is updated, add a check that a input poolL2Tx with
  64. // Nonce & TokenID =0, after ProcessTxs call has the expected value
  65. assert.Equal(t, 0, len(exitInfos))
  66. accountIdx := common.Idx(256)
  67. acc, err := sdb.GetAccount(&accountIdx)
  68. assert.Nil(t, err)
  69. assert.Equal(t, "28", acc.Balance.String())
  70. // use second batch
  71. // l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[1])
  72. _, exitInfos, err = sdb.ProcessTxs(l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1])
  73. require.Nil(t, err)
  74. assert.Equal(t, 5, len(exitInfos))
  75. acc, err = sdb.GetAccount(&accountIdx)
  76. require.Nil(t, err)
  77. assert.Equal(t, "48", acc.Balance.String())
  78. // use third batch
  79. // l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[2])
  80. _, exitInfos, err = sdb.ProcessTxs(l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2])
  81. require.Nil(t, err)
  82. assert.Equal(t, 1, len(exitInfos))
  83. acc, err = sdb.GetAccount(&accountIdx)
  84. assert.Nil(t, err)
  85. assert.Equal(t, "23", acc.Balance.String())
  86. }
  87. func TestProcessTxsBatchBuilder(t *testing.T) {
  88. dir, err := ioutil.TempDir("", "tmpdb")
  89. require.Nil(t, err)
  90. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  91. assert.Nil(t, err)
  92. // generate test transactions from test.SetTest0 code
  93. parser := test.NewParser(strings.NewReader(test.SetTest0))
  94. instructions, err := parser.Parse()
  95. assert.Nil(t, err)
  96. l1Txs, coordinatorL1Txs, poolL2Txs, _ := test.GenerateTestTxs(t, instructions)
  97. assert.Equal(t, 29, len(l1Txs[0]))
  98. assert.Equal(t, 0, len(coordinatorL1Txs[0]))
  99. assert.Equal(t, 21, len(poolL2Txs[0]))
  100. assert.Equal(t, 5, len(l1Txs[1]))
  101. assert.Equal(t, 1, len(coordinatorL1Txs[1]))
  102. assert.Equal(t, 55, len(poolL2Txs[1]))
  103. assert.Equal(t, 10, len(l1Txs[2]))
  104. assert.Equal(t, 0, len(coordinatorL1Txs[2]))
  105. assert.Equal(t, 7, len(poolL2Txs[2]))
  106. // use first batch
  107. // l2txs := common.PoolL2TxsToL2Txs(poolL2Txs[0])
  108. _, exitInfos, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
  109. require.Nil(t, err)
  110. assert.Equal(t, 0, len(exitInfos))
  111. accountIdx := common.Idx(256)
  112. acc, err := sdb.GetAccount(&accountIdx)
  113. assert.Nil(t, err)
  114. assert.Equal(t, "28", acc.Balance.String())
  115. // use second batch
  116. // l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[1])
  117. _, exitInfos, err = sdb.ProcessTxs(l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1])
  118. require.Nil(t, err)
  119. assert.Equal(t, 5, len(exitInfos))
  120. acc, err = sdb.GetAccount(&accountIdx)
  121. require.Nil(t, err)
  122. assert.Equal(t, "48", acc.Balance.String())
  123. // use third batch
  124. // l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[2])
  125. _, exitInfos, err = sdb.ProcessTxs(l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2])
  126. require.Nil(t, err)
  127. assert.Equal(t, 1, len(exitInfos))
  128. acc, err = sdb.GetAccount(&accountIdx)
  129. assert.Nil(t, err)
  130. assert.Equal(t, "23", acc.Balance.String())
  131. }
  132. func TestZKInputsGeneration(t *testing.T) {
  133. dir, err := ioutil.TempDir("", "tmpdb")
  134. require.Nil(t, err)
  135. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  136. assert.Nil(t, err)
  137. // generate test transactions from test.SetTest0 code
  138. parser := test.NewParser(strings.NewReader(test.SetTest0))
  139. instructions, err := parser.Parse()
  140. assert.Nil(t, err)
  141. l1Txs, coordinatorL1Txs, poolL2Txs, _ := test.GenerateTestTxs(t, instructions)
  142. assert.Equal(t, 29, len(l1Txs[0]))
  143. assert.Equal(t, 0, len(coordinatorL1Txs[0]))
  144. assert.Equal(t, 21, len(poolL2Txs[0]))
  145. zki, _, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
  146. require.Nil(t, err)
  147. s, err := json.Marshal(zki)
  148. require.Nil(t, err)
  149. if debug {
  150. fmt.Println(string(s))
  151. }
  152. }