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.

465 lines
13 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, 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.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.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, TypeTxSelector, 0)
  61. assert.NoError(t, err)
  62. v, err = sdb.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.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.Get(k0)
  74. assert.NoError(t, err)
  75. assert.Equal(t, v0, v)
  76. // make checkpoints with the current state
  77. bn, err := sdb.GetCurrentBatch()
  78. assert.NoError(t, err)
  79. assert.Equal(t, common.BatchNum(0), bn)
  80. err = sdb.MakeCheckpoint()
  81. assert.NoError(t, err)
  82. bn, err = sdb.GetCurrentBatch()
  83. assert.NoError(t, err)
  84. assert.Equal(t, common.BatchNum(1), bn)
  85. // write more data
  86. tx, err = sdb.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.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, TypeTxSelector, 0)
  98. assert.NoError(t, err)
  99. v, err = sdb.db.Get(k0)
  100. assert.NoError(t, err)
  101. assert.Equal(t, v0, v)
  102. v, err = sdb.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, 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, 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. func TestCheckpoints(t *testing.T) {
  196. dir, err := ioutil.TempDir("", "sdb")
  197. require.NoError(t, err)
  198. defer assert.NoError(t, os.RemoveAll(dir))
  199. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  200. assert.NoError(t, err)
  201. // create test accounts
  202. var accounts []*common.Account
  203. for i := 0; i < 10; i++ {
  204. accounts = append(accounts, newAccount(t, i))
  205. }
  206. // add test accounts
  207. for i := 0; i < len(accounts); i++ {
  208. _, err = sdb.CreateAccount(accounts[i].Idx, accounts[i])
  209. assert.NoError(t, err)
  210. }
  211. // do checkpoints and check that currentBatch is correct
  212. err = sdb.MakeCheckpoint()
  213. assert.NoError(t, err)
  214. cb, err := sdb.GetCurrentBatch()
  215. assert.NoError(t, err)
  216. assert.Equal(t, common.BatchNum(1), cb)
  217. for i := 1; i < 10; i++ {
  218. err = sdb.MakeCheckpoint()
  219. assert.NoError(t, err)
  220. cb, err = sdb.GetCurrentBatch()
  221. assert.NoError(t, err)
  222. assert.Equal(t, common.BatchNum(i+1), cb)
  223. }
  224. // printCheckpoints(t, sdb.path)
  225. // reset checkpoint
  226. err = sdb.Reset(3)
  227. assert.NoError(t, err)
  228. // check that reset can be repeated (as there exist the 'current' and
  229. // 'BatchNum3', from where the 'current' is a copy)
  230. err = sdb.Reset(3)
  231. require.NoError(t, err)
  232. // check that currentBatch is as expected after Reset
  233. cb, err = sdb.GetCurrentBatch()
  234. assert.NoError(t, err)
  235. assert.Equal(t, common.BatchNum(3), cb)
  236. // advance one checkpoint and check that currentBatch is fine
  237. err = sdb.MakeCheckpoint()
  238. assert.NoError(t, err)
  239. cb, err = sdb.GetCurrentBatch()
  240. assert.NoError(t, err)
  241. assert.Equal(t, common.BatchNum(4), cb)
  242. err = sdb.DeleteCheckpoint(common.BatchNum(9))
  243. assert.NoError(t, err)
  244. err = sdb.DeleteCheckpoint(common.BatchNum(10))
  245. assert.NoError(t, err)
  246. err = sdb.DeleteCheckpoint(common.BatchNum(9)) // does not exist, should return err
  247. assert.NotNil(t, err)
  248. err = sdb.DeleteCheckpoint(common.BatchNum(11)) // does not exist, should return err
  249. assert.NotNil(t, err)
  250. // Create a LocalStateDB from the initial StateDB
  251. dirLocal, err := ioutil.TempDir("", "ldb")
  252. require.NoError(t, err)
  253. defer assert.NoError(t, os.RemoveAll(dirLocal))
  254. ldb, err := NewLocalStateDB(dirLocal, sdb, TypeBatchBuilder, 32)
  255. assert.NoError(t, err)
  256. // get checkpoint 4 from sdb (StateDB) to ldb (LocalStateDB)
  257. err = ldb.Reset(4, true)
  258. assert.NoError(t, err)
  259. // check that currentBatch is 4 after the Reset
  260. cb, err = ldb.GetCurrentBatch()
  261. assert.NoError(t, err)
  262. assert.Equal(t, common.BatchNum(4), cb)
  263. // advance one checkpoint in ldb
  264. err = ldb.MakeCheckpoint()
  265. assert.NoError(t, err)
  266. cb, err = ldb.GetCurrentBatch()
  267. assert.NoError(t, err)
  268. assert.Equal(t, common.BatchNum(5), cb)
  269. // Create a 2nd LocalStateDB from the initial StateDB
  270. dirLocal2, err := ioutil.TempDir("", "ldb2")
  271. require.NoError(t, err)
  272. defer assert.NoError(t, os.RemoveAll(dirLocal2))
  273. ldb2, err := NewLocalStateDB(dirLocal2, sdb, TypeBatchBuilder, 32)
  274. assert.NoError(t, err)
  275. // get checkpoint 4 from sdb (StateDB) to ldb (LocalStateDB)
  276. err = ldb2.Reset(4, true)
  277. assert.NoError(t, err)
  278. // check that currentBatch is 4 after the Reset
  279. cb, err = ldb2.GetCurrentBatch()
  280. assert.NoError(t, err)
  281. assert.Equal(t, common.BatchNum(4), cb)
  282. // advance one checkpoint in ldb2
  283. err = ldb2.MakeCheckpoint()
  284. assert.NoError(t, err)
  285. cb, err = ldb2.GetCurrentBatch()
  286. assert.NoError(t, err)
  287. assert.Equal(t, common.BatchNum(5), cb)
  288. debug := false
  289. if debug {
  290. printCheckpoints(t, sdb.path)
  291. printCheckpoints(t, ldb.path)
  292. printCheckpoints(t, ldb2.path)
  293. }
  294. }
  295. func TestStateDBGetAccounts(t *testing.T) {
  296. dir, err := ioutil.TempDir("", "tmpdb")
  297. require.NoError(t, err)
  298. sdb, err := NewStateDB(dir, TypeTxSelector, 0)
  299. assert.NoError(t, err)
  300. // create test accounts
  301. var accounts []common.Account
  302. for i := 0; i < 16; i++ {
  303. account := newAccount(t, i)
  304. accounts = append(accounts, *account)
  305. }
  306. // add test accounts
  307. for i := range accounts {
  308. _, err = sdb.CreateAccount(accounts[i].Idx, &accounts[i])
  309. require.NoError(t, err)
  310. }
  311. dbAccounts, err := sdb.GetAccounts()
  312. require.NoError(t, err)
  313. assert.Equal(t, accounts, dbAccounts)
  314. }
  315. func printCheckpoints(t *testing.T, path string) {
  316. files, err := ioutil.ReadDir(path)
  317. assert.NoError(t, err)
  318. fmt.Println(path)
  319. for _, f := range files {
  320. fmt.Println(" " + f.Name())
  321. }
  322. }
  323. func bigFromStr(h string, u int) *big.Int {
  324. if u == 16 {
  325. h = strings.TrimPrefix(h, "0x")
  326. }
  327. b, ok := new(big.Int).SetString(h, u)
  328. if !ok {
  329. panic("bigFromStr err")
  330. }
  331. return b
  332. }
  333. func TestCheckAccountsTreeTestVectors(t *testing.T) {
  334. dir, err := ioutil.TempDir("", "tmpdb")
  335. require.NoError(t, err)
  336. defer assert.NoError(t, os.RemoveAll(dir))
  337. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  338. require.NoError(t, err)
  339. ay0 := new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(253), nil), big.NewInt(1))
  340. // test value from js version (compatibility-canary)
  341. assert.Equal(t, "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", (hex.EncodeToString(ay0.Bytes())))
  342. bjjPoint0Comp := babyjub.PackSignY(true, ay0)
  343. bjj0 := babyjub.PublicKeyComp(bjjPoint0Comp)
  344. ay1 := bigFromStr("00", 16)
  345. bjjPoint1Comp := babyjub.PackSignY(false, ay1)
  346. bjj1 := babyjub.PublicKeyComp(bjjPoint1Comp)
  347. ay2 := bigFromStr("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7", 16)
  348. bjjPoint2Comp := babyjub.PackSignY(false, ay2)
  349. bjj2 := babyjub.PublicKeyComp(bjjPoint2Comp)
  350. ay3 := bigFromStr("0x10", 16) // 0x10=16
  351. bjjPoint3Comp := babyjub.PackSignY(false, ay3)
  352. require.NoError(t, err)
  353. bjj3 := babyjub.PublicKeyComp(bjjPoint3Comp)
  354. accounts := []*common.Account{
  355. {
  356. Idx: 1,
  357. TokenID: 0xFFFFFFFF,
  358. PublicKey: bjj0,
  359. EthAddr: ethCommon.HexToAddress("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
  360. Nonce: common.Nonce(0xFFFFFFFFFF),
  361. Balance: bigFromStr("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16),
  362. },
  363. {
  364. Idx: 100,
  365. TokenID: 0,
  366. PublicKey: bjj1,
  367. EthAddr: ethCommon.HexToAddress("0x00"),
  368. Nonce: common.Nonce(0),
  369. Balance: bigFromStr("0", 10),
  370. },
  371. {
  372. Idx: 0xFFFFFFFFFFFF,
  373. TokenID: 3,
  374. PublicKey: bjj2,
  375. EthAddr: ethCommon.HexToAddress("0xA3C88ac39A76789437AED31B9608da72e1bbfBF9"),
  376. Nonce: common.Nonce(129),
  377. Balance: bigFromStr("42000000000000000000", 10),
  378. },
  379. {
  380. Idx: 10000,
  381. TokenID: 1000,
  382. PublicKey: bjj3,
  383. EthAddr: ethCommon.HexToAddress("0x64"),
  384. Nonce: common.Nonce(1900),
  385. Balance: bigFromStr("14000000000000000000", 10),
  386. },
  387. }
  388. for i := 0; i < len(accounts); i++ {
  389. _, err = accounts[i].HashValue()
  390. require.NoError(t, err)
  391. _, err = sdb.CreateAccount(accounts[i].Idx, accounts[i])
  392. if err != nil {
  393. log.Error(err)
  394. }
  395. require.NoError(t, err)
  396. }
  397. // root value generated by js version:
  398. assert.Equal(t, "17298264051379321456969039521810887093935433569451713402227686942080129181291", sdb.mt.Root().BigInt().String())
  399. }