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.

144 lines
3.8 KiB

  1. package statedb
  2. import (
  3. "encoding/hex"
  4. "io/ioutil"
  5. "math/big"
  6. "testing"
  7. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/iden3/go-iden3-crypto/babyjub"
  10. "github.com/iden3/go-merkletree/db"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func newAccount(t *testing.T, i int) *common.Account {
  15. var sk babyjub.PrivateKey
  16. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  17. require.Nil(t, err)
  18. pk := sk.Public()
  19. key, err := ethCrypto.GenerateKey()
  20. require.Nil(t, err)
  21. address := ethCrypto.PubkeyToAddress(key.PublicKey)
  22. return &common.Account{
  23. TokenID: common.TokenID(i),
  24. Nonce: uint64(i),
  25. Balance: big.NewInt(1000),
  26. PublicKey: pk,
  27. EthAddr: address,
  28. }
  29. }
  30. func TestStateDBWithoutMT(t *testing.T) {
  31. dir, err := ioutil.TempDir("", "tmpdb")
  32. require.Nil(t, err)
  33. sdb, err := NewStateDB(dir, false, false, 0)
  34. assert.Nil(t, err)
  35. // create test accounts
  36. var accounts []*common.Account
  37. for i := 0; i < 100; i++ {
  38. accounts = append(accounts, newAccount(t, i))
  39. }
  40. // get non-existing account, expecting an error
  41. _, err = sdb.GetAccount(common.Idx(1))
  42. assert.NotNil(t, err)
  43. assert.Equal(t, db.ErrNotFound, err)
  44. // add test accounts
  45. for i := 0; i < len(accounts); i++ {
  46. err = sdb.CreateAccount(common.Idx(i), accounts[i])
  47. assert.Nil(t, err)
  48. }
  49. for i := 0; i < len(accounts); i++ {
  50. accGetted, err := sdb.GetAccount(common.Idx(i))
  51. assert.Nil(t, err)
  52. assert.Equal(t, accounts[i], accGetted)
  53. }
  54. // try already existing idx and get error
  55. _, err = sdb.GetAccount(common.Idx(1)) // check that exist
  56. assert.Nil(t, err)
  57. err = sdb.CreateAccount(common.Idx(1), accounts[1]) // check that can not be created twice
  58. assert.NotNil(t, err)
  59. assert.Equal(t, ErrAccountAlreadyExists, err)
  60. // update accounts
  61. for i := 0; i < len(accounts); i++ {
  62. accounts[i].Nonce = accounts[i].Nonce + 1
  63. err = sdb.UpdateAccount(common.Idx(i), accounts[i])
  64. assert.Nil(t, err)
  65. }
  66. // check that can not call MerkleTree methods of the StateDB
  67. _, err = sdb.MTCreateAccount(common.Idx(1), accounts[1])
  68. assert.NotNil(t, err)
  69. assert.Equal(t, ErrStateDBWithoutMT, err)
  70. _, err = sdb.MTUpdateAccount(common.Idx(1), accounts[1])
  71. assert.NotNil(t, err)
  72. assert.Equal(t, ErrStateDBWithoutMT, err)
  73. _, err = sdb.MTGetProof(common.Idx(1))
  74. assert.NotNil(t, err)
  75. assert.Equal(t, ErrStateDBWithoutMT, err)
  76. }
  77. func TestStateDBWithMT(t *testing.T) {
  78. dir, err := ioutil.TempDir("", "tmpdb")
  79. require.Nil(t, err)
  80. sdb, err := NewStateDB(dir, false, true, 32)
  81. assert.Nil(t, err)
  82. // create test accounts
  83. var accounts []*common.Account
  84. for i := 0; i < 100; i++ {
  85. accounts = append(accounts, newAccount(t, i))
  86. }
  87. // get non-existing account, expecting an error
  88. _, err = sdb.GetAccount(common.Idx(1))
  89. assert.NotNil(t, err)
  90. assert.Equal(t, db.ErrNotFound, err)
  91. // add test accounts
  92. for i := 0; i < len(accounts); i++ {
  93. _, err = sdb.MTCreateAccount(common.Idx(i), accounts[i])
  94. assert.Nil(t, err)
  95. }
  96. for i := 0; i < len(accounts); i++ {
  97. accGetted, err := sdb.GetAccount(common.Idx(i))
  98. assert.Nil(t, err)
  99. assert.Equal(t, accounts[i], accGetted)
  100. }
  101. // try already existing idx and get error
  102. _, err = sdb.GetAccount(common.Idx(1)) // check that exist
  103. assert.Nil(t, err)
  104. _, err = sdb.MTCreateAccount(common.Idx(1), accounts[1]) // check that can not be created twice
  105. assert.NotNil(t, err)
  106. assert.Equal(t, ErrAccountAlreadyExists, err)
  107. _, err = sdb.MTGetProof(common.Idx(1))
  108. assert.Nil(t, err)
  109. // update accounts
  110. for i := 0; i < len(accounts); i++ {
  111. accounts[i].Nonce = accounts[i].Nonce + 1
  112. _, err = sdb.MTUpdateAccount(common.Idx(i), accounts[i])
  113. assert.Nil(t, err)
  114. }
  115. a, err := sdb.GetAccount(common.Idx(1)) // check that account value has been updated
  116. assert.Nil(t, err)
  117. assert.Equal(t, accounts[1].Nonce, a.Nonce)
  118. }