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.

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