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.

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