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.

460 lines
12 KiB

  1. package statedb
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "io/ioutil"
  6. "math/big"
  7. "os"
  8. "strings"
  9. "testing"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  12. "github.com/hermeznetwork/hermez-node/common"
  13. "github.com/hermeznetwork/tracerr"
  14. "github.com/iden3/go-iden3-crypto/babyjub"
  15. "github.com/iden3/go-merkletree/db"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. )
  19. func newAccount(t *testing.T, i int) *common.Account {
  20. var sk babyjub.PrivateKey
  21. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  22. require.Nil(t, err)
  23. pk := sk.Public()
  24. key, err := ethCrypto.GenerateKey()
  25. require.Nil(t, err)
  26. address := ethCrypto.PubkeyToAddress(key.PublicKey)
  27. return &common.Account{
  28. Idx: common.Idx(256 + i),
  29. TokenID: common.TokenID(i),
  30. Nonce: common.Nonce(i),
  31. Balance: big.NewInt(1000),
  32. PublicKey: pk,
  33. EthAddr: address,
  34. }
  35. }
  36. func TestNewStateDBIntermediateState(t *testing.T) {
  37. dir, err := ioutil.TempDir("", "tmpdb")
  38. require.Nil(t, err)
  39. defer assert.Nil(t, os.RemoveAll(dir))
  40. sdb, err := NewStateDB(dir, TypeTxSelector, 0)
  41. assert.Nil(t, err)
  42. // test values
  43. k0 := []byte("testkey0")
  44. k1 := []byte("testkey1")
  45. v0 := []byte("testvalue0")
  46. v1 := []byte("testvalue1")
  47. // store some data
  48. tx, err := sdb.db.NewTx()
  49. assert.Nil(t, err)
  50. err = tx.Put(k0, v0)
  51. assert.Nil(t, err)
  52. err = tx.Commit()
  53. assert.Nil(t, err)
  54. v, err := sdb.db.Get(k0)
  55. assert.Nil(t, err)
  56. assert.Equal(t, v0, v)
  57. // call NewStateDB which should get the db at the last checkpoint state
  58. // executing a Reset (discarding the last 'testkey0'&'testvalue0' data)
  59. sdb, err = NewStateDB(dir, TypeTxSelector, 0)
  60. assert.Nil(t, err)
  61. v, err = sdb.db.Get(k0)
  62. assert.NotNil(t, err)
  63. assert.Equal(t, db.ErrNotFound, tracerr.Unwrap(err))
  64. assert.Nil(t, v)
  65. // store the same data from the beginning that has ben lost since last NewStateDB
  66. tx, err = sdb.db.NewTx()
  67. assert.Nil(t, err)
  68. err = tx.Put(k0, v0)
  69. assert.Nil(t, err)
  70. err = tx.Commit()
  71. assert.Nil(t, err)
  72. v, err = sdb.db.Get(k0)
  73. assert.Nil(t, err)
  74. assert.Equal(t, v0, v)
  75. // make checkpoints with the current state
  76. bn, err := sdb.GetCurrentBatch()
  77. assert.Nil(t, err)
  78. assert.Equal(t, common.BatchNum(0), bn)
  79. err = sdb.MakeCheckpoint()
  80. assert.Nil(t, err)
  81. bn, err = sdb.GetCurrentBatch()
  82. assert.Nil(t, err)
  83. assert.Equal(t, common.BatchNum(1), bn)
  84. // write more data
  85. tx, err = sdb.db.NewTx()
  86. assert.Nil(t, err)
  87. err = tx.Put(k1, v1)
  88. assert.Nil(t, err)
  89. err = tx.Commit()
  90. assert.Nil(t, err)
  91. v, err = sdb.db.Get(k1)
  92. assert.Nil(t, err)
  93. assert.Equal(t, v1, v)
  94. // call NewStateDB which should get the db at the last checkpoint state
  95. // executing a Reset (discarding the last 'testkey1'&'testvalue1' data)
  96. sdb, err = NewStateDB(dir, TypeTxSelector, 0)
  97. assert.Nil(t, err)
  98. v, err = sdb.db.Get(k0)
  99. assert.Nil(t, err)
  100. assert.Equal(t, v0, v)
  101. v, err = sdb.db.Get(k1)
  102. assert.NotNil(t, err)
  103. assert.Equal(t, db.ErrNotFound, tracerr.Unwrap(err))
  104. assert.Nil(t, v)
  105. }
  106. func TestStateDBWithoutMT(t *testing.T) {
  107. dir, err := ioutil.TempDir("", "tmpdb")
  108. require.Nil(t, err)
  109. defer assert.Nil(t, os.RemoveAll(dir))
  110. sdb, err := NewStateDB(dir, TypeTxSelector, 0)
  111. assert.Nil(t, err)
  112. // create test accounts
  113. var accounts []*common.Account
  114. for i := 0; i < 4; i++ {
  115. accounts = append(accounts, newAccount(t, i))
  116. }
  117. // get non-existing account, expecting an error
  118. unexistingAccount := common.Idx(1)
  119. _, err = sdb.GetAccount(unexistingAccount)
  120. assert.NotNil(t, err)
  121. assert.Equal(t, db.ErrNotFound, tracerr.Unwrap(err))
  122. // add test accounts
  123. for i := 0; i < len(accounts); i++ {
  124. _, err = sdb.CreateAccount(accounts[i].Idx, accounts[i])
  125. assert.Nil(t, err)
  126. }
  127. for i := 0; i < len(accounts); i++ {
  128. existingAccount := accounts[i].Idx
  129. accGetted, err := sdb.GetAccount(existingAccount)
  130. assert.Nil(t, err)
  131. assert.Equal(t, accounts[i], accGetted)
  132. }
  133. // try already existing idx and get error
  134. existingAccount := common.Idx(256)
  135. _, err = sdb.GetAccount(existingAccount) // check that exist
  136. assert.Nil(t, err)
  137. _, err = sdb.CreateAccount(common.Idx(256), accounts[1]) // check that can not be created twice
  138. assert.NotNil(t, err)
  139. assert.Equal(t, ErrAccountAlreadyExists, tracerr.Unwrap(err))
  140. // update accounts
  141. for i := 0; i < len(accounts); i++ {
  142. accounts[i].Nonce = accounts[i].Nonce + 1
  143. existingAccount = common.Idx(i)
  144. _, err = sdb.UpdateAccount(existingAccount, accounts[i])
  145. assert.Nil(t, err)
  146. }
  147. _, err = sdb.MTGetProof(common.Idx(1))
  148. assert.NotNil(t, err)
  149. assert.Equal(t, ErrStateDBWithoutMT, tracerr.Unwrap(err))
  150. }
  151. func TestStateDBWithMT(t *testing.T) {
  152. dir, err := ioutil.TempDir("", "tmpdb")
  153. require.Nil(t, err)
  154. defer assert.Nil(t, os.RemoveAll(dir))
  155. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  156. assert.Nil(t, err)
  157. // create test accounts
  158. var accounts []*common.Account
  159. for i := 0; i < 20; i++ {
  160. accounts = append(accounts, newAccount(t, i))
  161. }
  162. // get non-existing account, expecting an error
  163. _, err = sdb.GetAccount(common.Idx(1))
  164. assert.NotNil(t, err)
  165. assert.Equal(t, db.ErrNotFound, tracerr.Unwrap(err))
  166. // add test accounts
  167. for i := 0; i < len(accounts); i++ {
  168. _, err = sdb.CreateAccount(accounts[i].Idx, accounts[i])
  169. assert.Nil(t, err)
  170. }
  171. for i := 0; i < len(accounts); i++ {
  172. accGetted, err := sdb.GetAccount(accounts[i].Idx)
  173. assert.Nil(t, err)
  174. assert.Equal(t, accounts[i], accGetted)
  175. }
  176. // try already existing idx and get error
  177. _, err = sdb.GetAccount(common.Idx(256)) // check that exist
  178. assert.Nil(t, err)
  179. _, err = sdb.CreateAccount(common.Idx(256), accounts[1]) // check that can not be created twice
  180. assert.NotNil(t, err)
  181. assert.Equal(t, ErrAccountAlreadyExists, tracerr.Unwrap(err))
  182. _, err = sdb.MTGetProof(common.Idx(256))
  183. assert.Nil(t, err)
  184. // update accounts
  185. for i := 0; i < len(accounts); i++ {
  186. accounts[i].Nonce = accounts[i].Nonce + 1
  187. _, err = sdb.UpdateAccount(accounts[i].Idx, accounts[i])
  188. assert.Nil(t, err)
  189. }
  190. a, err := sdb.GetAccount(common.Idx(256)) // check that account value has been updated
  191. assert.Nil(t, err)
  192. assert.Equal(t, accounts[0].Nonce, a.Nonce)
  193. }
  194. func TestCheckpoints(t *testing.T) {
  195. dir, err := ioutil.TempDir("", "sdb")
  196. require.Nil(t, err)
  197. defer assert.Nil(t, os.RemoveAll(dir))
  198. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  199. assert.Nil(t, err)
  200. // create test accounts
  201. var accounts []*common.Account
  202. for i := 0; i < 10; i++ {
  203. accounts = append(accounts, newAccount(t, i))
  204. }
  205. // add test accounts
  206. for i := 0; i < len(accounts); i++ {
  207. _, err = sdb.CreateAccount(accounts[i].Idx, accounts[i])
  208. assert.Nil(t, err)
  209. }
  210. // do checkpoints and check that currentBatch is correct
  211. err = sdb.MakeCheckpoint()
  212. assert.Nil(t, err)
  213. cb, err := sdb.GetCurrentBatch()
  214. assert.Nil(t, err)
  215. assert.Equal(t, common.BatchNum(1), cb)
  216. for i := 1; i < 10; i++ {
  217. err = sdb.MakeCheckpoint()
  218. assert.Nil(t, err)
  219. cb, err = sdb.GetCurrentBatch()
  220. assert.Nil(t, err)
  221. assert.Equal(t, common.BatchNum(i+1), cb)
  222. }
  223. // printCheckpoints(t, sdb.path)
  224. // reset checkpoint
  225. err = sdb.Reset(3)
  226. assert.Nil(t, err)
  227. // check that reset can be repeated (as there exist the 'current' and
  228. // 'BatchNum3', from where the 'current' is a copy)
  229. err = sdb.Reset(3)
  230. require.Nil(t, err)
  231. // check that currentBatch is as expected after Reset
  232. cb, err = sdb.GetCurrentBatch()
  233. assert.Nil(t, err)
  234. assert.Equal(t, common.BatchNum(3), cb)
  235. // advance one checkpoint and check that currentBatch is fine
  236. err = sdb.MakeCheckpoint()
  237. assert.Nil(t, err)
  238. cb, err = sdb.GetCurrentBatch()
  239. assert.Nil(t, err)
  240. assert.Equal(t, common.BatchNum(4), cb)
  241. err = sdb.DeleteCheckpoint(common.BatchNum(9))
  242. assert.Nil(t, err)
  243. err = sdb.DeleteCheckpoint(common.BatchNum(10))
  244. assert.Nil(t, err)
  245. err = sdb.DeleteCheckpoint(common.BatchNum(9)) // does not exist, should return err
  246. assert.NotNil(t, err)
  247. err = sdb.DeleteCheckpoint(common.BatchNum(11)) // does not exist, should return err
  248. assert.NotNil(t, err)
  249. // Create a LocalStateDB from the initial StateDB
  250. dirLocal, err := ioutil.TempDir("", "ldb")
  251. require.Nil(t, err)
  252. defer assert.Nil(t, os.RemoveAll(dirLocal))
  253. ldb, err := NewLocalStateDB(dirLocal, sdb, TypeBatchBuilder, 32)
  254. assert.Nil(t, err)
  255. // get checkpoint 4 from sdb (StateDB) to ldb (LocalStateDB)
  256. err = ldb.Reset(4, true)
  257. assert.Nil(t, err)
  258. // check that currentBatch is 4 after the Reset
  259. cb, err = ldb.GetCurrentBatch()
  260. assert.Nil(t, err)
  261. assert.Equal(t, common.BatchNum(4), cb)
  262. // advance one checkpoint in ldb
  263. err = ldb.MakeCheckpoint()
  264. assert.Nil(t, err)
  265. cb, err = ldb.GetCurrentBatch()
  266. assert.Nil(t, err)
  267. assert.Equal(t, common.BatchNum(5), cb)
  268. // Create a 2nd LocalStateDB from the initial StateDB
  269. dirLocal2, err := ioutil.TempDir("", "ldb2")
  270. require.Nil(t, err)
  271. defer assert.Nil(t, os.RemoveAll(dirLocal2))
  272. ldb2, err := NewLocalStateDB(dirLocal2, sdb, TypeBatchBuilder, 32)
  273. assert.Nil(t, err)
  274. // get checkpoint 4 from sdb (StateDB) to ldb (LocalStateDB)
  275. err = ldb2.Reset(4, true)
  276. assert.Nil(t, err)
  277. // check that currentBatch is 4 after the Reset
  278. cb, err = ldb2.GetCurrentBatch()
  279. assert.Nil(t, err)
  280. assert.Equal(t, common.BatchNum(4), cb)
  281. // advance one checkpoint in ldb2
  282. err = ldb2.MakeCheckpoint()
  283. assert.Nil(t, err)
  284. cb, err = ldb2.GetCurrentBatch()
  285. assert.Nil(t, err)
  286. assert.Equal(t, common.BatchNum(5), cb)
  287. debug := false
  288. if debug {
  289. printCheckpoints(t, sdb.path)
  290. printCheckpoints(t, ldb.path)
  291. printCheckpoints(t, ldb2.path)
  292. }
  293. }
  294. func TestStateDBGetAccounts(t *testing.T) {
  295. dir, err := ioutil.TempDir("", "tmpdb")
  296. require.Nil(t, err)
  297. sdb, err := NewStateDB(dir, TypeTxSelector, 0)
  298. assert.Nil(t, err)
  299. // create test accounts
  300. var accounts []common.Account
  301. for i := 0; i < 16; i++ {
  302. account := newAccount(t, i)
  303. accounts = append(accounts, *account)
  304. }
  305. // add test accounts
  306. for i := range accounts {
  307. _, err = sdb.CreateAccount(accounts[i].Idx, &accounts[i])
  308. require.Nil(t, err)
  309. }
  310. dbAccounts, err := sdb.GetAccounts()
  311. require.Nil(t, err)
  312. assert.Equal(t, accounts, dbAccounts)
  313. }
  314. func printCheckpoints(t *testing.T, path string) {
  315. files, err := ioutil.ReadDir(path)
  316. assert.Nil(t, err)
  317. fmt.Println(path)
  318. for _, f := range files {
  319. fmt.Println(" " + f.Name())
  320. }
  321. }
  322. func bigFromStr(h string, u int) *big.Int {
  323. if u == 16 {
  324. h = strings.TrimPrefix(h, "0x")
  325. }
  326. b, ok := new(big.Int).SetString(h, u)
  327. if !ok {
  328. panic("bigFromStr err")
  329. }
  330. return b
  331. }
  332. func TestCheckAccountsTreeTestVectors(t *testing.T) {
  333. dir, err := ioutil.TempDir("", "tmpdb")
  334. require.Nil(t, err)
  335. defer assert.Nil(t, os.RemoveAll(dir))
  336. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  337. require.Nil(t, err)
  338. ay0 := new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(253), nil), big.NewInt(1))
  339. // test value from js version (compatibility-canary)
  340. assert.Equal(t, "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", (hex.EncodeToString(ay0.Bytes())))
  341. bjj0, err := babyjub.PointFromSignAndY(true, ay0)
  342. require.Nil(t, err)
  343. ay1 := bigFromStr("00", 16)
  344. bjj1, err := babyjub.PointFromSignAndY(false, ay1)
  345. require.Nil(t, err)
  346. ay2 := bigFromStr("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7", 16)
  347. bjj2, err := babyjub.PointFromSignAndY(false, ay2)
  348. require.Nil(t, err)
  349. ay3 := bigFromStr("0x10", 16) // 0x10=16
  350. bjj3, err := babyjub.PointFromSignAndY(false, ay3)
  351. require.Nil(t, err)
  352. accounts := []*common.Account{
  353. {
  354. Idx: 1,
  355. TokenID: 0xFFFFFFFF,
  356. PublicKey: (*babyjub.PublicKey)(bjj0),
  357. EthAddr: ethCommon.HexToAddress("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
  358. Nonce: common.Nonce(0xFFFFFFFFFF),
  359. Balance: bigFromStr("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16),
  360. },
  361. {
  362. Idx: 100,
  363. TokenID: 0,
  364. PublicKey: (*babyjub.PublicKey)(bjj1),
  365. EthAddr: ethCommon.HexToAddress("0x00"),
  366. Nonce: common.Nonce(0),
  367. Balance: bigFromStr("0", 10),
  368. },
  369. {
  370. Idx: 0xFFFFFFFFFFFF,
  371. TokenID: 3,
  372. PublicKey: (*babyjub.PublicKey)(bjj2),
  373. EthAddr: ethCommon.HexToAddress("0xA3C88ac39A76789437AED31B9608da72e1bbfBF9"),
  374. Nonce: common.Nonce(129),
  375. Balance: bigFromStr("42000000000000000000", 10),
  376. },
  377. {
  378. Idx: 10000,
  379. TokenID: 1000,
  380. PublicKey: (*babyjub.PublicKey)(bjj3),
  381. EthAddr: ethCommon.HexToAddress("0x64"),
  382. Nonce: common.Nonce(1900),
  383. Balance: bigFromStr("14000000000000000000", 10),
  384. },
  385. }
  386. for i := 0; i < len(accounts); i++ {
  387. _, err = accounts[i].HashValue()
  388. require.Nil(t, err)
  389. _, err = sdb.CreateAccount(accounts[i].Idx, accounts[i])
  390. require.Nil(t, err)
  391. }
  392. // root value generated by js version:
  393. assert.Equal(t, "17298264051379321456969039521810887093935433569451713402227686942080129181291", sdb.mt.Root().BigInt().String())
  394. }