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.

804 lines
23 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package arbo
  2. import (
  3. "encoding/hex"
  4. "math"
  5. "math/big"
  6. "runtime"
  7. "testing"
  8. "time"
  9. qt "github.com/frankban/quicktest"
  10. "go.vocdoni.io/dvote/db"
  11. "go.vocdoni.io/dvote/db/badgerdb"
  12. )
  13. func checkRootBIString(c *qt.C, tree *Tree, expected string) {
  14. root, err := tree.Root()
  15. c.Assert(err, qt.IsNil)
  16. rootBI := BytesToBigInt(root)
  17. c.Check(rootBI.String(), qt.Equals, expected)
  18. }
  19. func TestDBTx(t *testing.T) {
  20. c := qt.New(t)
  21. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  22. c.Assert(err, qt.IsNil)
  23. wTx := database.WriteTx()
  24. _, err = wTx.Get([]byte("a"))
  25. c.Assert(err, qt.Equals, db.ErrKeyNotFound)
  26. err = wTx.Set([]byte("a"), []byte("b"))
  27. c.Assert(err, qt.IsNil)
  28. v, err := wTx.Get([]byte("a"))
  29. c.Assert(err, qt.IsNil)
  30. c.Assert(v, qt.DeepEquals, []byte("b"))
  31. }
  32. func TestAddTestVectors(t *testing.T) {
  33. c := qt.New(t)
  34. // Poseidon test vectors generated using https://github.com/iden3/circomlib smt.js
  35. testVectorsPoseidon := []string{
  36. "0000000000000000000000000000000000000000000000000000000000000000",
  37. "13578938674299138072471463694055224830892726234048532520316387704878000008795",
  38. "5412393676474193513566895793055462193090331607895808993925969873307089394741",
  39. "14204494359367183802864593755198662203838502594566452929175967972147978322084",
  40. }
  41. testAdd(c, HashFunctionPoseidon, testVectorsPoseidon)
  42. testVectorsSha256 := []string{
  43. "0000000000000000000000000000000000000000000000000000000000000000",
  44. "46910109172468462938850740851377282682950237270676610513794735904325820156367",
  45. "59481735341404520835410489183267411392292882901306595567679529387376287440550",
  46. "20573794434149960984975763118181266662429997821552560184909083010514790081771",
  47. }
  48. testAdd(c, HashFunctionSha256, testVectorsSha256)
  49. }
  50. func testAdd(c *qt.C, hashFunc HashFunction, testVectors []string) {
  51. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  52. c.Assert(err, qt.IsNil)
  53. tree, err := NewTree(database, 256, hashFunc)
  54. c.Assert(err, qt.IsNil)
  55. defer tree.db.Close() //nolint:errcheck
  56. root, err := tree.Root()
  57. c.Assert(err, qt.IsNil)
  58. c.Check(hex.EncodeToString(root), qt.Equals, testVectors[0])
  59. bLen := 32
  60. err = tree.Add(
  61. BigIntToBytes(bLen, big.NewInt(1)),
  62. BigIntToBytes(bLen, big.NewInt(2)))
  63. c.Assert(err, qt.IsNil)
  64. checkRootBIString(c, tree, testVectors[1])
  65. err = tree.Add(
  66. BigIntToBytes(bLen, big.NewInt(33)),
  67. BigIntToBytes(bLen, big.NewInt(44)))
  68. c.Assert(err, qt.IsNil)
  69. checkRootBIString(c, tree, testVectors[2])
  70. err = tree.Add(
  71. BigIntToBytes(bLen, big.NewInt(1234)),
  72. BigIntToBytes(bLen, big.NewInt(9876)))
  73. c.Assert(err, qt.IsNil)
  74. checkRootBIString(c, tree, testVectors[3])
  75. }
  76. func TestAddBatch(t *testing.T) {
  77. c := qt.New(t)
  78. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  79. c.Assert(err, qt.IsNil)
  80. tree, err := NewTree(database, 256, HashFunctionPoseidon)
  81. c.Assert(err, qt.IsNil)
  82. defer tree.db.Close() //nolint:errcheck
  83. bLen := 32
  84. for i := 0; i < 1000; i++ {
  85. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  86. v := BigIntToBytes(bLen, big.NewInt(0))
  87. if err := tree.Add(k, v); err != nil {
  88. t.Fatal(err)
  89. }
  90. }
  91. checkRootBIString(c, tree,
  92. "296519252211642170490407814696803112091039265640052570497930797516015811235")
  93. database, err = badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  94. c.Assert(err, qt.IsNil)
  95. tree2, err := NewTree(database, 256, HashFunctionPoseidon)
  96. c.Assert(err, qt.IsNil)
  97. defer tree2.db.Close() //nolint:errcheck
  98. var keys, values [][]byte
  99. for i := 0; i < 1000; i++ {
  100. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  101. v := BigIntToBytes(bLen, big.NewInt(0))
  102. keys = append(keys, k)
  103. values = append(values, v)
  104. }
  105. indexes, err := tree2.AddBatch(keys, values)
  106. c.Assert(err, qt.IsNil)
  107. c.Check(len(indexes), qt.Equals, 0)
  108. checkRootBIString(c, tree2,
  109. "296519252211642170490407814696803112091039265640052570497930797516015811235")
  110. }
  111. func TestAddDifferentOrder(t *testing.T) {
  112. c := qt.New(t)
  113. database1, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  114. c.Assert(err, qt.IsNil)
  115. tree1, err := NewTree(database1, 256, HashFunctionPoseidon)
  116. c.Assert(err, qt.IsNil)
  117. defer tree1.db.Close() //nolint:errcheck
  118. bLen := 32
  119. for i := 0; i < 16; i++ {
  120. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  121. v := BigIntToBytes(bLen, big.NewInt(0))
  122. if err := tree1.Add(k, v); err != nil {
  123. t.Fatal(err)
  124. }
  125. }
  126. database2, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  127. c.Assert(err, qt.IsNil)
  128. tree2, err := NewTree(database2, 256, HashFunctionPoseidon)
  129. c.Assert(err, qt.IsNil)
  130. defer tree2.db.Close() //nolint:errcheck
  131. for i := 16 - 1; i >= 0; i-- {
  132. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  133. v := BigIntToBytes(bLen, big.NewInt(0))
  134. if err := tree2.Add(k, v); err != nil {
  135. t.Fatal(err)
  136. }
  137. }
  138. root1, err := tree1.Root()
  139. c.Assert(err, qt.IsNil)
  140. root2, err := tree2.Root()
  141. c.Assert(err, qt.IsNil)
  142. c.Check(hex.EncodeToString(root2), qt.Equals, hex.EncodeToString(root1))
  143. c.Check(hex.EncodeToString(root1), qt.Equals,
  144. "3b89100bec24da9275c87bc188740389e1d5accfc7d88ba5688d7fa96a00d82f")
  145. }
  146. func TestAddRepeatedIndex(t *testing.T) {
  147. c := qt.New(t)
  148. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  149. c.Assert(err, qt.IsNil)
  150. tree, err := NewTree(database, 256, HashFunctionPoseidon)
  151. c.Assert(err, qt.IsNil)
  152. defer tree.db.Close() //nolint:errcheck
  153. bLen := 32
  154. k := BigIntToBytes(bLen, big.NewInt(int64(3)))
  155. v := BigIntToBytes(bLen, big.NewInt(int64(12)))
  156. err = tree.Add(k, v)
  157. c.Assert(err, qt.IsNil)
  158. err = tree.Add(k, v) // repeating same key-value
  159. c.Check(err, qt.Equals, ErrKeyAlreadyExists)
  160. }
  161. func TestUpdate(t *testing.T) {
  162. c := qt.New(t)
  163. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  164. c.Assert(err, qt.IsNil)
  165. tree, err := NewTree(database, 256, HashFunctionPoseidon)
  166. c.Assert(err, qt.IsNil)
  167. defer tree.db.Close() //nolint:errcheck
  168. bLen := 32
  169. k := BigIntToBytes(bLen, big.NewInt(int64(20)))
  170. v := BigIntToBytes(bLen, big.NewInt(int64(12)))
  171. if err := tree.Add(k, v); err != nil {
  172. t.Fatal(err)
  173. }
  174. v = BigIntToBytes(bLen, big.NewInt(int64(11)))
  175. err = tree.Update(k, v)
  176. c.Assert(err, qt.IsNil)
  177. gettedKey, gettedValue, err := tree.Get(k)
  178. c.Assert(err, qt.IsNil)
  179. c.Check(gettedKey, qt.DeepEquals, k)
  180. c.Check(gettedValue, qt.DeepEquals, v)
  181. // add more leafs to the tree to do another test
  182. for i := 0; i < 16; i++ {
  183. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  184. v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
  185. if err := tree.Add(k, v); err != nil {
  186. t.Fatal(err)
  187. }
  188. }
  189. k = BigIntToBytes(bLen, big.NewInt(int64(3)))
  190. v = BigIntToBytes(bLen, big.NewInt(int64(11)))
  191. // check that before the Update, value for 3 is !=11
  192. gettedKey, gettedValue, err = tree.Get(k)
  193. c.Assert(err, qt.IsNil)
  194. c.Check(gettedKey, qt.DeepEquals, k)
  195. c.Check(gettedValue, qt.Not(qt.DeepEquals), v)
  196. c.Check(gettedValue, qt.DeepEquals, BigIntToBytes(bLen, big.NewInt(6)))
  197. err = tree.Update(k, v)
  198. c.Assert(err, qt.IsNil)
  199. // check that after Update, the value for 3 is ==11
  200. gettedKey, gettedValue, err = tree.Get(k)
  201. c.Assert(err, qt.IsNil)
  202. c.Check(gettedKey, qt.DeepEquals, k)
  203. c.Check(gettedValue, qt.DeepEquals, v)
  204. c.Check(gettedValue, qt.DeepEquals, BigIntToBytes(bLen, big.NewInt(11)))
  205. }
  206. func TestAux(t *testing.T) { // TODO split in proper tests
  207. c := qt.New(t)
  208. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  209. c.Assert(err, qt.IsNil)
  210. tree, err := NewTree(database, 256, HashFunctionPoseidon)
  211. c.Assert(err, qt.IsNil)
  212. defer tree.db.Close() //nolint:errcheck
  213. bLen := 32
  214. k := BigIntToBytes(bLen, big.NewInt(int64(1)))
  215. v := BigIntToBytes(bLen, big.NewInt(int64(0)))
  216. err = tree.Add(k, v)
  217. c.Assert(err, qt.IsNil)
  218. k = BigIntToBytes(bLen, big.NewInt(int64(256)))
  219. err = tree.Add(k, v)
  220. c.Assert(err, qt.IsNil)
  221. k = BigIntToBytes(bLen, big.NewInt(int64(257)))
  222. err = tree.Add(k, v)
  223. c.Assert(err, qt.IsNil)
  224. k = BigIntToBytes(bLen, big.NewInt(int64(515)))
  225. err = tree.Add(k, v)
  226. c.Assert(err, qt.IsNil)
  227. k = BigIntToBytes(bLen, big.NewInt(int64(770)))
  228. err = tree.Add(k, v)
  229. c.Assert(err, qt.IsNil)
  230. k = BigIntToBytes(bLen, big.NewInt(int64(388)))
  231. err = tree.Add(k, v)
  232. c.Assert(err, qt.IsNil)
  233. k = BigIntToBytes(bLen, big.NewInt(int64(900)))
  234. err = tree.Add(k, v)
  235. c.Assert(err, qt.IsNil)
  236. //
  237. // err = tree.PrintGraphviz(nil)
  238. // c.Assert(err, qt.IsNil)
  239. }
  240. func TestGet(t *testing.T) {
  241. c := qt.New(t)
  242. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  243. c.Assert(err, qt.IsNil)
  244. tree, err := NewTree(database, 256, HashFunctionPoseidon)
  245. c.Assert(err, qt.IsNil)
  246. defer tree.db.Close() //nolint:errcheck
  247. bLen := 32
  248. for i := 0; i < 10; i++ {
  249. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  250. v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
  251. if err := tree.Add(k, v); err != nil {
  252. t.Fatal(err)
  253. }
  254. }
  255. k := BigIntToBytes(bLen, big.NewInt(int64(7)))
  256. gettedKey, gettedValue, err := tree.Get(k)
  257. c.Assert(err, qt.IsNil)
  258. c.Check(gettedKey, qt.DeepEquals, k)
  259. c.Check(gettedValue, qt.DeepEquals, BigIntToBytes(bLen, big.NewInt(int64(7*2))))
  260. }
  261. func TestGenProofAndVerify(t *testing.T) {
  262. c := qt.New(t)
  263. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  264. c.Assert(err, qt.IsNil)
  265. tree, err := NewTree(database, 256, HashFunctionPoseidon)
  266. c.Assert(err, qt.IsNil)
  267. defer tree.db.Close() //nolint:errcheck
  268. bLen := 32
  269. for i := 0; i < 10; i++ {
  270. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  271. v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
  272. if err := tree.Add(k, v); err != nil {
  273. t.Fatal(err)
  274. }
  275. }
  276. k := BigIntToBytes(bLen, big.NewInt(int64(7)))
  277. v := BigIntToBytes(bLen, big.NewInt(int64(14)))
  278. kAux, proofV, siblings, existence, err := tree.GenProof(k)
  279. c.Assert(err, qt.IsNil)
  280. c.Assert(proofV, qt.DeepEquals, v)
  281. c.Assert(k, qt.DeepEquals, kAux)
  282. c.Assert(existence, qt.IsTrue)
  283. root, err := tree.Root()
  284. c.Assert(err, qt.IsNil)
  285. verif, err := CheckProof(tree.hashFunction, k, v, root, siblings)
  286. c.Assert(err, qt.IsNil)
  287. c.Check(verif, qt.IsTrue)
  288. }
  289. func TestDumpAndImportDump(t *testing.T) {
  290. c := qt.New(t)
  291. database1, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  292. c.Assert(err, qt.IsNil)
  293. tree1, err := NewTree(database1, 256, HashFunctionPoseidon)
  294. c.Assert(err, qt.IsNil)
  295. defer tree1.db.Close() //nolint:errcheck
  296. bLen := 32
  297. for i := 0; i < 16; i++ {
  298. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  299. v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
  300. if err := tree1.Add(k, v); err != nil {
  301. t.Fatal(err)
  302. }
  303. }
  304. e, err := tree1.Dump(nil)
  305. c.Assert(err, qt.IsNil)
  306. database2, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  307. c.Assert(err, qt.IsNil)
  308. tree2, err := NewTree(database2, 256, HashFunctionPoseidon)
  309. c.Assert(err, qt.IsNil)
  310. defer tree2.db.Close() //nolint:errcheck
  311. err = tree2.ImportDump(e)
  312. c.Assert(err, qt.IsNil)
  313. root1, err := tree1.Root()
  314. c.Assert(err, qt.IsNil)
  315. root2, err := tree2.Root()
  316. c.Assert(err, qt.IsNil)
  317. c.Check(root2, qt.DeepEquals, root1)
  318. c.Check(hex.EncodeToString(root2), qt.Equals,
  319. "0d93aaa3362b2f999f15e15728f123087c2eee716f01c01f56e23aae07f09f08")
  320. }
  321. func TestRWMutex(t *testing.T) {
  322. c := qt.New(t)
  323. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  324. c.Assert(err, qt.IsNil)
  325. tree, err := NewTree(database, 256, HashFunctionPoseidon)
  326. c.Assert(err, qt.IsNil)
  327. defer tree.db.Close() //nolint:errcheck
  328. bLen := 32
  329. var keys, values [][]byte
  330. for i := 0; i < 1000; i++ {
  331. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  332. v := BigIntToBytes(bLen, big.NewInt(0))
  333. keys = append(keys, k)
  334. values = append(values, v)
  335. }
  336. go func() {
  337. _, err = tree.AddBatch(keys, values)
  338. if err != nil {
  339. panic(err)
  340. }
  341. }()
  342. time.Sleep(500 * time.Millisecond)
  343. k := BigIntToBytes(bLen, big.NewInt(int64(99999)))
  344. v := BigIntToBytes(bLen, big.NewInt(int64(99999)))
  345. if err := tree.Add(k, v); err != nil {
  346. t.Fatal(err)
  347. }
  348. }
  349. // TODO UPDATE
  350. // func TestSetGetNLeafs(t *testing.T) {
  351. // c := qt.New(t)
  352. // database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  353. // c.Assert(err, qt.IsNil)
  354. // tree, err := NewTree(database, 100, HashFunctionPoseidon)
  355. // c.Assert(err, qt.IsNil)
  356. //
  357. // // 0
  358. // tree.dbBatch = tree.db.NewBatch()
  359. //
  360. // err = tree.setNLeafs(0)
  361. // c.Assert(err, qt.IsNil)
  362. //
  363. // err = tree.dbBatch.Write()
  364. // c.Assert(err, qt.IsNil)
  365. //
  366. // n, err := tree.GetNLeafs()
  367. // c.Assert(err, qt.IsNil)
  368. // c.Assert(n, qt.Equals, 0)
  369. //
  370. // // 1024
  371. // tree.dbBatch = tree.db.NewBatch()
  372. //
  373. // err = tree.setNLeafs(1024)
  374. // c.Assert(err, qt.IsNil)
  375. //
  376. // err = tree.dbBatch.Write()
  377. // c.Assert(err, qt.IsNil)
  378. //
  379. // n, err = tree.GetNLeafs()
  380. // c.Assert(err, qt.IsNil)
  381. // c.Assert(n, qt.Equals, 1024)
  382. //
  383. // // 2**64 -1
  384. // tree.dbBatch = tree.db.NewBatch()
  385. //
  386. // maxUint := ^uint(0)
  387. // maxInt := int(maxUint >> 1)
  388. //
  389. // err = tree.setNLeafs(maxInt)
  390. // c.Assert(err, qt.IsNil)
  391. //
  392. // err = tree.dbBatch.Write()
  393. // c.Assert(err, qt.IsNil)
  394. //
  395. // n, err = tree.GetNLeafs()
  396. // c.Assert(err, qt.IsNil)
  397. // c.Assert(n, qt.Equals, maxInt)
  398. // }
  399. func TestAddBatchFullyUsed(t *testing.T) {
  400. c := qt.New(t)
  401. database1, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  402. c.Assert(err, qt.IsNil)
  403. tree1, err := NewTree(database1, 4, HashFunctionPoseidon)
  404. c.Assert(err, qt.IsNil)
  405. database2, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  406. c.Assert(err, qt.IsNil)
  407. tree2, err := NewTree(database2, 4, HashFunctionPoseidon)
  408. c.Assert(err, qt.IsNil)
  409. var keys, values [][]byte
  410. for i := 0; i < 16; i++ {
  411. k := BigIntToBytes(1, big.NewInt(int64(i)))
  412. v := k
  413. keys = append(keys, k)
  414. values = append(values, v)
  415. // add one by one expecting no error
  416. err := tree1.Add(k, v)
  417. c.Assert(err, qt.IsNil)
  418. }
  419. invalids, err := tree2.AddBatch(keys, values)
  420. c.Assert(err, qt.IsNil)
  421. c.Assert(0, qt.Equals, len(invalids))
  422. root1, err := tree1.Root()
  423. c.Assert(err, qt.IsNil)
  424. root2, err := tree2.Root()
  425. c.Assert(err, qt.IsNil)
  426. c.Assert(root1, qt.DeepEquals, root2)
  427. // get all key-values and check that are equal between both trees
  428. for i := 0; i < 16; i++ {
  429. auxK1, auxV1, err := tree1.Get(BigIntToBytes(1, big.NewInt(int64(i))))
  430. c.Assert(err, qt.IsNil)
  431. auxK2, auxV2, err := tree2.Get(BigIntToBytes(1, big.NewInt(int64(i))))
  432. c.Assert(err, qt.IsNil)
  433. c.Assert(auxK1, qt.DeepEquals, auxK2)
  434. c.Assert(auxV1, qt.DeepEquals, auxV2)
  435. }
  436. // try adding one more key to both trees (through Add & AddBatch) and
  437. // expect not being added due the tree is already full
  438. k := BigIntToBytes(1, big.NewInt(int64(16)))
  439. v := k
  440. err = tree1.Add(k, v)
  441. c.Assert(err, qt.Equals, ErrMaxVirtualLevel)
  442. invalids, err = tree2.AddBatch([][]byte{k}, [][]byte{v})
  443. c.Assert(err, qt.IsNil)
  444. c.Assert(1, qt.Equals, len(invalids))
  445. }
  446. func TestSetRoot(t *testing.T) {
  447. c := qt.New(t)
  448. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  449. c.Assert(err, qt.IsNil)
  450. tree, err := NewTree(database, 256, HashFunctionPoseidon)
  451. c.Assert(err, qt.IsNil)
  452. expectedRoot := "13742386369878513332697380582061714160370929283209286127733983161245560237407"
  453. // fill the tree
  454. bLen := 32
  455. var keys, values [][]byte
  456. for i := 0; i < 1000; i++ {
  457. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  458. v := BigIntToBytes(bLen, big.NewInt(int64(i)))
  459. keys = append(keys, k)
  460. values = append(values, v)
  461. }
  462. indexes, err := tree.AddBatch(keys, values)
  463. c.Assert(err, qt.IsNil)
  464. c.Check(len(indexes), qt.Equals, 0)
  465. checkRootBIString(c, tree,
  466. expectedRoot)
  467. // add one more k-v
  468. k := BigIntToBytes(bLen, big.NewInt(1000))
  469. v := BigIntToBytes(bLen, big.NewInt(1000))
  470. err = tree.Add(k, v)
  471. c.Assert(err, qt.IsNil)
  472. checkRootBIString(c, tree,
  473. "10747149055773881257049574592162159501044114324358186833013814735296193179713")
  474. // do a SetRoot, and expect the same root than the original tree
  475. pastRootBI, ok := new(big.Int).SetString(expectedRoot, 10)
  476. c.Assert(ok, qt.IsTrue)
  477. pastRoot := BigIntToBytes(32, pastRootBI)
  478. err = tree.SetRoot(pastRoot)
  479. c.Assert(err, qt.IsNil)
  480. checkRootBIString(c, tree, expectedRoot)
  481. // check that the tree can be updated
  482. err = tree.Add([]byte("test"), []byte("test"))
  483. c.Assert(err, qt.IsNil)
  484. err = tree.Update([]byte("test"), []byte("test"))
  485. c.Assert(err, qt.IsNil)
  486. // check that the k-v '1000' does not exist in the new tree
  487. _, _, err = tree.Get(k)
  488. c.Assert(err, qt.Equals, ErrKeyNotFound)
  489. // check that can be set an empty root
  490. err = tree.SetRoot(tree.emptyHash)
  491. c.Assert(err, qt.IsNil)
  492. }
  493. func TestSnapshot(t *testing.T) {
  494. c := qt.New(t)
  495. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  496. c.Assert(err, qt.IsNil)
  497. tree, err := NewTree(database, 256, HashFunctionPoseidon)
  498. c.Assert(err, qt.IsNil)
  499. // fill the tree
  500. bLen := 32
  501. var keys, values [][]byte
  502. for i := 0; i < 1000; i++ {
  503. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  504. v := BigIntToBytes(bLen, big.NewInt(int64(i)))
  505. keys = append(keys, k)
  506. values = append(values, v)
  507. }
  508. indexes, err := tree.AddBatch(keys, values)
  509. c.Assert(err, qt.IsNil)
  510. c.Check(len(indexes), qt.Equals, 0)
  511. checkRootBIString(c, tree,
  512. "13742386369878513332697380582061714160370929283209286127733983161245560237407")
  513. // do a snapshot, and expect the same root than the original tree
  514. snapshotTree, err := tree.Snapshot(nil)
  515. c.Assert(err, qt.IsNil)
  516. checkRootBIString(c, snapshotTree,
  517. "13742386369878513332697380582061714160370929283209286127733983161245560237407")
  518. // check that the snapshotTree can not be updated
  519. _, err = snapshotTree.AddBatch(keys, values)
  520. c.Assert(err, qt.Equals, ErrSnapshotNotEditable)
  521. err = snapshotTree.Add([]byte("test"), []byte("test"))
  522. c.Assert(err, qt.Equals, ErrSnapshotNotEditable)
  523. err = snapshotTree.Update([]byte("test"), []byte("test"))
  524. c.Assert(err, qt.Equals, ErrSnapshotNotEditable)
  525. err = snapshotTree.ImportDump(nil)
  526. c.Assert(err, qt.Equals, ErrSnapshotNotEditable)
  527. // update the original tree by adding a new key-value, and check that
  528. // snapshotTree still has the old root, but the original tree has a new
  529. // root
  530. err = tree.Add([]byte("test"), []byte("test"))
  531. c.Assert(err, qt.IsNil)
  532. checkRootBIString(c, snapshotTree,
  533. "13742386369878513332697380582061714160370929283209286127733983161245560237407")
  534. checkRootBIString(c, tree,
  535. "1025190963769001718196479367844646783678188389989148142691917685159698888868")
  536. }
  537. func TestGetFromSnapshotExpectArboErrKeyNotFound(t *testing.T) {
  538. c := qt.New(t)
  539. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  540. c.Assert(err, qt.IsNil)
  541. tree, err := NewTree(database, 256, HashFunctionPoseidon)
  542. c.Assert(err, qt.IsNil)
  543. defer tree.db.Close() //nolint:errcheck
  544. bLen := 32
  545. k := BigIntToBytes(bLen, big.NewInt(int64(3)))
  546. root, err := tree.Root()
  547. c.Assert(err, qt.IsNil)
  548. tree, err = tree.Snapshot(root)
  549. c.Assert(err, qt.IsNil)
  550. _, _, err = tree.Get(k)
  551. c.Assert(err, qt.Equals, ErrKeyNotFound) // and not equal to db.ErrKeyNotFound
  552. }
  553. func TestKeyLen(t *testing.T) {
  554. c := qt.New(t)
  555. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  556. c.Assert(err, qt.IsNil)
  557. // maxLevels is 100, keyPath length = ceil(maxLevels/8) = 13
  558. maxLevels := 100
  559. tree, err := NewTree(database, maxLevels, HashFunctionBlake2b)
  560. c.Assert(err, qt.IsNil)
  561. // expect no errors when adding a key of only 4 bytes (when the
  562. // required length of keyPath for 100 levels would be 13 bytes)
  563. bLen := 4
  564. k := BigIntToBytes(bLen, big.NewInt(1))
  565. v := BigIntToBytes(bLen, big.NewInt(1))
  566. err = tree.Add(k, v)
  567. c.Assert(err, qt.IsNil)
  568. err = tree.Update(k, v)
  569. c.Assert(err, qt.IsNil)
  570. _, _, _, _, err = tree.GenProof(k)
  571. c.Assert(err, qt.IsNil)
  572. _, _, err = tree.Get(k)
  573. c.Assert(err, qt.IsNil)
  574. k = BigIntToBytes(bLen, big.NewInt(2))
  575. v = BigIntToBytes(bLen, big.NewInt(2))
  576. invalids, err := tree.AddBatch([][]byte{k}, [][]byte{v})
  577. c.Assert(err, qt.IsNil)
  578. c.Assert(len(invalids), qt.Equals, 0)
  579. // expect errors when adding a key bigger than maximum capacity of the
  580. // tree: ceil(maxLevels/8)
  581. maxLevels = 32
  582. database, err = badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  583. c.Assert(err, qt.IsNil)
  584. tree, err = NewTree(database, maxLevels, HashFunctionBlake2b)
  585. c.Assert(err, qt.IsNil)
  586. maxKeyLen := int(math.Ceil(float64(maxLevels) / float64(8))) //nolint:gomnd
  587. k = BigIntToBytes(maxKeyLen+1, big.NewInt(1))
  588. v = BigIntToBytes(maxKeyLen+1, big.NewInt(1))
  589. expectedErrMsg := "len(k) can not be bigger than ceil(maxLevels/8)," +
  590. " where len(k): 5, maxLevels: 32, max key len=ceil(maxLevels/8): 4." +
  591. " Might need a bigger tree depth (maxLevels>=40) in order to input" +
  592. " keys of length 5"
  593. err = tree.Add(k, v)
  594. c.Assert(err.Error(), qt.Equals, expectedErrMsg)
  595. err = tree.Update(k, v)
  596. c.Assert(err.Error(), qt.Equals, expectedErrMsg)
  597. _, _, _, _, err = tree.GenProof(k)
  598. c.Assert(err.Error(), qt.Equals, expectedErrMsg)
  599. _, _, err = tree.Get(k)
  600. c.Assert(err.Error(), qt.Equals, expectedErrMsg)
  601. // check AddBatch with few key-values
  602. invalids, err = tree.AddBatch([][]byte{k}, [][]byte{v})
  603. c.Assert(err, qt.IsNil)
  604. c.Assert(len(invalids), qt.Equals, 1)
  605. // check AddBatch with many key-values
  606. nCPU := flp2(runtime.NumCPU())
  607. nKVs := nCPU + 1
  608. var ks, vs [][]byte
  609. for i := 0; i < nKVs; i++ {
  610. ks = append(ks, BigIntToBytes(maxKeyLen+1, big.NewInt(1)))
  611. vs = append(vs, BigIntToBytes(maxKeyLen+1, big.NewInt(1)))
  612. }
  613. invalids, err = tree.AddBatch(ks, vs)
  614. c.Assert(err, qt.IsNil)
  615. c.Assert(len(invalids), qt.Equals, nKVs)
  616. // check that with maxKeyLen it can be added
  617. k = BigIntToBytes(maxKeyLen, big.NewInt(1))
  618. err = tree.Add(k, v)
  619. c.Assert(err, qt.IsNil)
  620. // check CheckProof check with key longer
  621. kAux, vAux, packedSiblings, existence, err := tree.GenProof(k)
  622. c.Assert(err, qt.IsNil)
  623. c.Assert(existence, qt.IsTrue)
  624. root, err := tree.Root()
  625. c.Assert(err, qt.IsNil)
  626. verif, err := CheckProof(tree.HashFunction(), kAux, vAux, root, packedSiblings)
  627. c.Assert(err, qt.IsNil)
  628. c.Assert(verif, qt.IsTrue)
  629. // use a similar key but with one zero, expect that CheckProof fails on
  630. // the verification
  631. kAux = append(kAux, 0)
  632. verif, err = CheckProof(tree.HashFunction(), kAux, vAux, root, packedSiblings)
  633. c.Assert(err, qt.IsNil)
  634. c.Assert(verif, qt.IsFalse)
  635. }
  636. func TestKeyLenBiggerThan32(t *testing.T) {
  637. c := qt.New(t)
  638. maxLevels := 264
  639. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  640. c.Assert(err, qt.IsNil)
  641. tree, err := NewTree(database, maxLevels, HashFunctionBlake2b)
  642. c.Assert(err, qt.IsNil)
  643. bLen := 33
  644. err = tree.Add(
  645. randomBytes(bLen),
  646. randomBytes(bLen))
  647. c.Assert(err, qt.IsNil)
  648. // 2nd key that we add, will find a node with len(key)==32 (due the
  649. // hash output size, expect that next Add does not give any error, as
  650. // internally it will use a keyPath of size corresponent to the
  651. // maxLevels size of the tree
  652. err = tree.Add(
  653. randomBytes(bLen),
  654. randomBytes(bLen))
  655. c.Assert(err, qt.IsNil)
  656. }
  657. func BenchmarkAdd(b *testing.B) {
  658. bLen := 32 // for both Poseidon & Sha256
  659. // prepare inputs
  660. var ks, vs [][]byte
  661. for i := 0; i < 1000; i++ {
  662. k := BigIntToBytes(bLen, big.NewInt(int64(i)))
  663. v := BigIntToBytes(bLen, big.NewInt(int64(i)))
  664. ks = append(ks, k)
  665. vs = append(vs, v)
  666. }
  667. b.Run("Poseidon", func(b *testing.B) {
  668. benchmarkAdd(b, HashFunctionPoseidon, ks, vs)
  669. })
  670. b.Run("Sha256", func(b *testing.B) {
  671. benchmarkAdd(b, HashFunctionSha256, ks, vs)
  672. })
  673. }
  674. func benchmarkAdd(b *testing.B, hashFunc HashFunction, ks, vs [][]byte) {
  675. c := qt.New(b)
  676. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  677. c.Assert(err, qt.IsNil)
  678. tree, err := NewTree(database, 140, hashFunc)
  679. c.Assert(err, qt.IsNil)
  680. defer tree.db.Close() //nolint:errcheck
  681. for i := 0; i < len(ks); i++ {
  682. if err := tree.Add(ks[i], vs[i]); err != nil {
  683. b.Fatal(err)
  684. }
  685. }
  686. }