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.

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