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.

556 lines
17 KiB

4 years ago
  1. package merkletree
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "math/big"
  7. "testing"
  8. "github.com/iden3/go-merkletree/db/memory"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. var debug = false
  13. type Fatalable interface {
  14. Fatal(args ...interface{})
  15. }
  16. func newTestingMerkle(f Fatalable, numLevels int) *MerkleTree {
  17. mt, err := NewMerkleTree(memory.NewMemoryStorage(), numLevels)
  18. if err != nil {
  19. f.Fatal(err)
  20. return nil
  21. }
  22. return mt
  23. }
  24. func TestHashParsers(t *testing.T) {
  25. h0 := NewHashFromBigInt(big.NewInt(0))
  26. assert.Equal(t, "0", h0.String())
  27. h1 := NewHashFromBigInt(big.NewInt(1))
  28. assert.Equal(t, "1", h1.String())
  29. h10 := NewHashFromBigInt(big.NewInt(10))
  30. assert.Equal(t, "10", h10.String())
  31. h7l := NewHashFromBigInt(big.NewInt(1234567))
  32. assert.Equal(t, "1234567", h7l.String())
  33. h8l := NewHashFromBigInt(big.NewInt(12345678))
  34. assert.Equal(t, "12345678...", h8l.String())
  35. b, ok := new(big.Int).SetString("4932297968297298434239270129193057052722409868268166443802652458940273154854", 10)
  36. assert.True(t, ok)
  37. h := NewHashFromBigInt(b)
  38. assert.Equal(t, "4932297968297298434239270129193057052722409868268166443802652458940273154854", h.BigInt().String())
  39. assert.Equal(t, "49322979...", h.String())
  40. assert.Equal(t, "0ae794eb9c3d8bbb9002e993fc2ed301dcbd2af5508ed072c375e861f1aa5b26", h.Hex())
  41. }
  42. func TestNewTree(t *testing.T) {
  43. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  44. assert.Nil(t, err)
  45. assert.Equal(t, "0", mt.Root().String())
  46. // test vectors generated using https://github.com/iden3/circomlib smt.js
  47. err = mt.Add(big.NewInt(1), big.NewInt(2))
  48. assert.Nil(t, err)
  49. assert.Equal(t, "4932297968297298434239270129193057052722409868268166443802652458940273154854", mt.Root().BigInt().String())
  50. err = mt.Add(big.NewInt(33), big.NewInt(44))
  51. assert.Nil(t, err)
  52. assert.Equal(t, "13563340744765267202993741297198970774200042973817962221376874695587906013050", mt.Root().BigInt().String())
  53. err = mt.Add(big.NewInt(1234), big.NewInt(9876))
  54. assert.Nil(t, err)
  55. assert.Equal(t, "16970503620176669663662021947486532860010370357132361783766545149750777353066", mt.Root().BigInt().String())
  56. proof, err := mt.GenerateProof(big.NewInt(33), nil)
  57. assert.Nil(t, err)
  58. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(33), big.NewInt(44)))
  59. assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(33), big.NewInt(45)))
  60. }
  61. func TestAddDifferentOrder(t *testing.T) {
  62. mt1 := newTestingMerkle(t, 140)
  63. defer mt1.db.Close()
  64. for i := 0; i < 16; i++ {
  65. k := big.NewInt(int64(i))
  66. v := big.NewInt(0)
  67. if err := mt1.Add(k, v); err != nil {
  68. t.Fatal(err)
  69. }
  70. }
  71. mt2 := newTestingMerkle(t, 140)
  72. defer mt2.db.Close()
  73. for i := 16 - 1; i >= 0; i-- {
  74. k := big.NewInt(int64(i))
  75. v := big.NewInt(0)
  76. if err := mt2.Add(k, v); err != nil {
  77. t.Fatal(err)
  78. }
  79. }
  80. assert.Equal(t, mt1.Root().Hex(), mt2.Root().Hex())
  81. assert.Equal(t, "0967b777d660e54aa3a0f0f3405bb962504d3d69d6b930146cd212dff9913bee", mt1.Root().Hex())
  82. }
  83. func TestAddRepeatedIndex(t *testing.T) {
  84. mt := newTestingMerkle(t, 140)
  85. defer mt.db.Close()
  86. k := big.NewInt(int64(3))
  87. v := big.NewInt(int64(12))
  88. if err := mt.Add(k, v); err != nil {
  89. t.Fatal(err)
  90. }
  91. err := mt.Add(k, v)
  92. assert.NotNil(t, err)
  93. assert.Equal(t, err, ErrEntryIndexAlreadyExists)
  94. }
  95. func TestGet(t *testing.T) {
  96. mt := newTestingMerkle(t, 140)
  97. defer mt.db.Close()
  98. for i := 0; i < 16; i++ {
  99. k := big.NewInt(int64(i))
  100. v := big.NewInt(int64(i * 2))
  101. if err := mt.Add(k, v); err != nil {
  102. t.Fatal(err)
  103. }
  104. }
  105. v, err := mt.Get(big.NewInt(10))
  106. assert.Nil(t, err)
  107. assert.Equal(t, big.NewInt(20), v)
  108. v, err = mt.Get(big.NewInt(15))
  109. assert.Nil(t, err)
  110. assert.Equal(t, big.NewInt(30), v)
  111. v, err = mt.Get(big.NewInt(16))
  112. assert.NotNil(t, err)
  113. assert.Equal(t, ErrKeyNotFound, err)
  114. assert.Nil(t, v)
  115. }
  116. func TestUpdate(t *testing.T) {
  117. mt := newTestingMerkle(t, 140)
  118. defer mt.db.Close()
  119. for i := 0; i < 16; i++ {
  120. k := big.NewInt(int64(i))
  121. v := big.NewInt(int64(i * 2))
  122. if err := mt.Add(k, v); err != nil {
  123. t.Fatal(err)
  124. }
  125. }
  126. v, err := mt.Get(big.NewInt(10))
  127. assert.Nil(t, err)
  128. assert.Equal(t, big.NewInt(20), v)
  129. err = mt.Update(big.NewInt(10), big.NewInt(1024))
  130. assert.Nil(t, err)
  131. v, err = mt.Get(big.NewInt(10))
  132. assert.Nil(t, err)
  133. assert.Equal(t, big.NewInt(1024), v)
  134. err = mt.Update(big.NewInt(1000), big.NewInt(1024))
  135. assert.Equal(t, ErrKeyNotFound, err)
  136. }
  137. func TestUpdate2(t *testing.T) {
  138. mt1 := newTestingMerkle(t, 140)
  139. defer mt1.db.Close()
  140. mt2 := newTestingMerkle(t, 140)
  141. defer mt2.db.Close()
  142. err := mt1.Add(big.NewInt(1), big.NewInt(119))
  143. assert.Nil(t, err)
  144. err = mt1.Add(big.NewInt(2), big.NewInt(229))
  145. assert.Nil(t, err)
  146. err = mt1.Add(big.NewInt(9876), big.NewInt(6789))
  147. assert.Nil(t, err)
  148. err = mt2.Add(big.NewInt(1), big.NewInt(11))
  149. assert.Nil(t, err)
  150. err = mt2.Add(big.NewInt(2), big.NewInt(22))
  151. assert.Nil(t, err)
  152. err = mt2.Add(big.NewInt(9876), big.NewInt(10))
  153. assert.Nil(t, err)
  154. err = mt1.Update(big.NewInt(1), big.NewInt(11))
  155. assert.Nil(t, err)
  156. err = mt1.Update(big.NewInt(2), big.NewInt(22))
  157. assert.Nil(t, err)
  158. err = mt2.Update(big.NewInt(9876), big.NewInt(6789))
  159. assert.Nil(t, err)
  160. assert.Equal(t, mt1.Root(), mt2.Root())
  161. }
  162. func TestGenerateAndVerifyProof128(t *testing.T) {
  163. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 140)
  164. require.Nil(t, err)
  165. defer mt.db.Close()
  166. for i := 0; i < 128; i++ {
  167. k := big.NewInt(int64(i))
  168. v := big.NewInt(0)
  169. if err := mt.Add(k, v); err != nil {
  170. t.Fatal(err)
  171. }
  172. }
  173. proof, err := mt.GenerateProof(big.NewInt(42), nil)
  174. assert.Nil(t, err)
  175. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(42), big.NewInt(0)))
  176. }
  177. func TestTreeLimit(t *testing.T) {
  178. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 5)
  179. require.Nil(t, err)
  180. defer mt.db.Close()
  181. for i := 0; i < 16; i++ {
  182. err = mt.Add(big.NewInt(int64(i)), big.NewInt(int64(i)))
  183. assert.Nil(t, err)
  184. }
  185. // here the tree is full, should not allow to add more data as reaches the maximum number of levels
  186. err = mt.Add(big.NewInt(int64(16)), big.NewInt(int64(16)))
  187. assert.NotNil(t, err)
  188. assert.Equal(t, ErrReachedMaxLevel, err)
  189. }
  190. func TestSiblingsFromProof(t *testing.T) {
  191. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 140)
  192. require.Nil(t, err)
  193. defer mt.db.Close()
  194. for i := 0; i < 64; i++ {
  195. k := big.NewInt(int64(i))
  196. v := big.NewInt(0)
  197. if err := mt.Add(k, v); err != nil {
  198. t.Fatal(err)
  199. }
  200. }
  201. proof, err := mt.GenerateProof(big.NewInt(4), nil)
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. siblings := SiblingsFromProof(proof)
  206. assert.Equal(t, 6, len(siblings))
  207. assert.Equal(t, "23db1f6fb07af47d7715f18960548c215fc7a2e6d25cb4a7eb82c9d3cf69bc26", siblings[0].Hex())
  208. assert.Equal(t, "2156e64dedcb76719ec732414dd6a8aa4348dafb24c19351a68fbc4158bb7fba", siblings[1].Hex())
  209. assert.Equal(t, "04a8e9b34d5a8b55268ca96b0b8c7c5aaef4f606ec3437ec67e4152d9b323913", siblings[2].Hex())
  210. assert.Equal(t, "0ff484133e0d25deb4a7c0cb46d90432e00fcc280948c2fab6fed9476f1e26b2", siblings[3].Hex())
  211. assert.Equal(t, "015dff482e87eb2046b8f5323049afd05f8dd8554e2c9aa1ef28991cf205c9b6", siblings[4].Hex())
  212. assert.Equal(t, "1e4da486ad68b07acec1406bed5a60732de5ff72d63910f7afbb491f953a8769", siblings[5].Hex())
  213. }
  214. func TestVerifyProofCases(t *testing.T) {
  215. mt := newTestingMerkle(t, 140)
  216. defer mt.DB().Close()
  217. for i := 0; i < 8; i++ {
  218. if err := mt.Add(big.NewInt(int64(i)), big.NewInt(0)); err != nil {
  219. t.Fatal(err)
  220. }
  221. }
  222. // Existence proof
  223. proof, err := mt.GenerateProof(big.NewInt(4), nil)
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. assert.Equal(t, proof.Existence, true)
  228. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(4), big.NewInt(0)))
  229. assert.Equal(t, "000300000000000000000000000000000000000000000000000000000000000728ea2b267d2a9436657f20b5827285175e030f58c07375535106903b16621630b9104d995843c7cffa685009a1b28dcd371022a3b27b3a4d6987f7c8b39b0f2fffc165330710754ca0fc24451bdd5d5f82a05f42f1427fbdf17879c0b84be60f", hex.EncodeToString(proof.Bytes()))
  230. for i := 8; i < 32; i++ {
  231. proof, err = mt.GenerateProof(big.NewInt(int64(i)), nil)
  232. assert.Nil(t, err)
  233. if debug {
  234. fmt.Println(i, proof)
  235. }
  236. }
  237. // Non-existence proof, empty aux
  238. proof, err = mt.GenerateProof(big.NewInt(12), nil)
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. assert.Equal(t, proof.Existence, false)
  243. // assert.True(t, proof.nodeAux == nil)
  244. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(12), big.NewInt(0)))
  245. assert.Equal(t, "030300000000000000000000000000000000000000000000000000000000000728ea2b267d2a9436657f20b5827285175e030f58c07375535106903b16621630b9104d995843c7cffa685009a1b28dcd371022a3b27b3a4d6987f7c8b39b0f2fffc165330710754ca0fc24451bdd5d5f82a05f42f1427fbdf17879c0b84be60f04000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes()))
  246. // Non-existence proof, diff. node aux
  247. proof, err = mt.GenerateProof(big.NewInt(10), nil)
  248. if err != nil {
  249. t.Fatal(err)
  250. }
  251. assert.Equal(t, proof.Existence, false)
  252. assert.True(t, proof.NodeAux != nil)
  253. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(10), big.NewInt(0)))
  254. assert.Equal(t, "030300000000000000000000000000000000000000000000000000000000000728ea2b267d2a9436657f20b5827285175e030f58c07375535106903b1662163097fcf8f911b271df196e0a75667b8a4f3024ef39f87201ed2b7cda349ba202296b7aeba35dc19ab0d4f65e175536c9952a90b6de18c3205611c3cd4fb408f01602000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes()))
  255. }
  256. func TestVerifyProofFalse(t *testing.T) {
  257. mt := newTestingMerkle(t, 140)
  258. defer mt.DB().Close()
  259. for i := 0; i < 8; i++ {
  260. if err := mt.Add(big.NewInt(int64(i)), big.NewInt(0)); err != nil {
  261. t.Fatal(err)
  262. }
  263. }
  264. // Invalid existence proof (node used for verification doesn't
  265. // correspond to node in the proof)
  266. proof, err := mt.GenerateProof(big.NewInt(int64(4)), nil)
  267. if err != nil {
  268. t.Fatal(err)
  269. }
  270. assert.Equal(t, proof.Existence, true)
  271. assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(int64(5)), big.NewInt(int64(5))))
  272. // Invalid non-existence proof (Non-existence proof, diff. node aux)
  273. proof, err = mt.GenerateProof(big.NewInt(int64(4)), nil)
  274. if err != nil {
  275. t.Fatal(err)
  276. }
  277. assert.Equal(t, proof.Existence, true)
  278. // Now we change the proof from existence to non-existence, and add e's
  279. // data as auxiliary node.
  280. proof.Existence = false
  281. proof.NodeAux = &NodeAux{Key: NewHashFromBigInt(big.NewInt(int64(4))), Value: NewHashFromBigInt(big.NewInt(4))}
  282. assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(int64(4)), big.NewInt(0)))
  283. }
  284. func TestGraphViz(t *testing.T) {
  285. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  286. assert.Nil(t, err)
  287. mt.Add(big.NewInt(1), big.NewInt(0))
  288. mt.Add(big.NewInt(2), big.NewInt(0))
  289. mt.Add(big.NewInt(3), big.NewInt(0))
  290. mt.Add(big.NewInt(4), big.NewInt(0))
  291. mt.Add(big.NewInt(5), big.NewInt(0))
  292. mt.Add(big.NewInt(100), big.NewInt(0))
  293. // mt.PrintGraphViz(nil)
  294. expected := `digraph hierarchy {
  295. node [fontname=Monospace,fontsize=10,shape=box]
  296. "60195538..." -> {"19759736..." "18893277..."}
  297. "19759736..." -> {"16152312..." "43945008..."}
  298. "16152312..." -> {"empty0" "13952255..."}
  299. "empty0" [style=dashed,label=0];
  300. "13952255..." -> {"61769925..." "empty1"}
  301. "empty1" [style=dashed,label=0];
  302. "61769925..." -> {"92723289..." "empty2"}
  303. "empty2" [style=dashed,label=0];
  304. "92723289..." -> {"21082735..." "82784818..."}
  305. "21082735..." [style=filled];
  306. "82784818..." [style=filled];
  307. "43945008..." [style=filled];
  308. "18893277..." -> {"19855703..." "17718670..."}
  309. "19855703..." -> {"11499909..." "15828714..."}
  310. "11499909..." [style=filled];
  311. "15828714..." [style=filled];
  312. "17718670..." [style=filled];
  313. }
  314. `
  315. w := bytes.NewBufferString("")
  316. mt.GraphViz(w, nil)
  317. assert.Equal(t, []byte(expected), w.Bytes())
  318. }
  319. func TestDelete(t *testing.T) {
  320. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  321. assert.Nil(t, err)
  322. assert.Equal(t, "0", mt.Root().String())
  323. // test vectors generated using https://github.com/iden3/circomlib smt.js
  324. err = mt.Add(big.NewInt(1), big.NewInt(2))
  325. assert.Nil(t, err)
  326. assert.Equal(t, "4932297968297298434239270129193057052722409868268166443802652458940273154854", mt.Root().BigInt().String())
  327. err = mt.Add(big.NewInt(33), big.NewInt(44))
  328. assert.Nil(t, err)
  329. assert.Equal(t, "13563340744765267202993741297198970774200042973817962221376874695587906013050", mt.Root().BigInt().String())
  330. err = mt.Add(big.NewInt(1234), big.NewInt(9876))
  331. assert.Nil(t, err)
  332. assert.Equal(t, "16970503620176669663662021947486532860010370357132361783766545149750777353066", mt.Root().BigInt().String())
  333. // mt.PrintGraphViz(nil)
  334. err = mt.Delete(big.NewInt(33))
  335. // mt.PrintGraphViz(nil)
  336. assert.Nil(t, err)
  337. assert.Equal(t, "12820263606494630162816839760750120928463716794691735985748071431547370997091", mt.Root().BigInt().String())
  338. err = mt.Delete(big.NewInt(1234))
  339. assert.Nil(t, err)
  340. err = mt.Delete(big.NewInt(1))
  341. assert.Nil(t, err)
  342. assert.Equal(t, "0", mt.Root().String())
  343. }
  344. func TestDelete2(t *testing.T) {
  345. mt := newTestingMerkle(t, 140)
  346. defer mt.db.Close()
  347. for i := 0; i < 8; i++ {
  348. k := big.NewInt(int64(i))
  349. v := big.NewInt(0)
  350. if err := mt.Add(k, v); err != nil {
  351. t.Fatal(err)
  352. }
  353. }
  354. expectedRoot := mt.Root()
  355. k := big.NewInt(8)
  356. v := big.NewInt(0)
  357. err := mt.Add(k, v)
  358. require.Nil(t, err)
  359. err = mt.Delete(big.NewInt(8))
  360. assert.Nil(t, err)
  361. assert.Equal(t, expectedRoot, mt.Root())
  362. mt2 := newTestingMerkle(t, 140)
  363. defer mt2.db.Close()
  364. for i := 0; i < 8; i++ {
  365. k := big.NewInt(int64(i))
  366. v := big.NewInt(0)
  367. if err := mt2.Add(k, v); err != nil {
  368. t.Fatal(err)
  369. }
  370. }
  371. assert.Equal(t, mt2.Root(), mt.Root())
  372. }
  373. func TestDelete3(t *testing.T) {
  374. mt := newTestingMerkle(t, 140)
  375. defer mt.db.Close()
  376. err := mt.Add(big.NewInt(1), big.NewInt(1))
  377. assert.Nil(t, err)
  378. err = mt.Add(big.NewInt(2), big.NewInt(2))
  379. assert.Nil(t, err)
  380. assert.Equal(t, "2427629547967522489273866134471574861207714751136138191708011221765688788661", mt.Root().BigInt().String())
  381. err = mt.Delete(big.NewInt(1))
  382. assert.Nil(t, err)
  383. assert.Equal(t, "10822920717809411688334493481050035035708810950159417482558569847174767667301", mt.Root().BigInt().String())
  384. mt2 := newTestingMerkle(t, 140)
  385. defer mt2.db.Close()
  386. err = mt2.Add(big.NewInt(2), big.NewInt(2))
  387. assert.Nil(t, err)
  388. assert.Equal(t, mt2.Root(), mt.Root())
  389. }
  390. func TestDelete4(t *testing.T) {
  391. mt := newTestingMerkle(t, 140)
  392. defer mt.db.Close()
  393. err := mt.Add(big.NewInt(1), big.NewInt(1))
  394. assert.Nil(t, err)
  395. err = mt.Add(big.NewInt(2), big.NewInt(2))
  396. assert.Nil(t, err)
  397. err = mt.Add(big.NewInt(3), big.NewInt(3))
  398. assert.Nil(t, err)
  399. assert.Equal(t, "16614298246517994771186095530428786749320098419259206061045083278756632941513", mt.Root().BigInt().String())
  400. err = mt.Delete(big.NewInt(1))
  401. assert.Nil(t, err)
  402. assert.Equal(t, "6117330520107511783353383870014397665359816230889739699667943862706617498952", mt.Root().BigInt().String())
  403. mt2 := newTestingMerkle(t, 140)
  404. defer mt2.db.Close()
  405. err = mt2.Add(big.NewInt(2), big.NewInt(2))
  406. assert.Nil(t, err)
  407. err = mt2.Add(big.NewInt(3), big.NewInt(3))
  408. assert.Nil(t, err)
  409. assert.Equal(t, mt2.Root(), mt.Root())
  410. }
  411. func TestDelete5(t *testing.T) {
  412. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  413. assert.Nil(t, err)
  414. err = mt.Add(big.NewInt(1), big.NewInt(2))
  415. assert.Nil(t, err)
  416. err = mt.Add(big.NewInt(33), big.NewInt(44))
  417. assert.Nil(t, err)
  418. assert.Equal(t, "13563340744765267202993741297198970774200042973817962221376874695587906013050", mt.Root().BigInt().String())
  419. err = mt.Delete(big.NewInt(1))
  420. assert.Nil(t, err)
  421. assert.Equal(t, "12075524681474630909546786277734445038384732558409197537058769521806571391765", mt.Root().BigInt().String())
  422. mt2 := newTestingMerkle(t, 140)
  423. defer mt2.db.Close()
  424. err = mt2.Add(big.NewInt(33), big.NewInt(44))
  425. assert.Nil(t, err)
  426. assert.Equal(t, mt2.Root(), mt.Root())
  427. }
  428. func TestDeleteNonExistingKeys(t *testing.T) {
  429. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  430. assert.Nil(t, err)
  431. err = mt.Add(big.NewInt(1), big.NewInt(2))
  432. assert.Nil(t, err)
  433. err = mt.Add(big.NewInt(33), big.NewInt(44))
  434. assert.Nil(t, err)
  435. err = mt.Delete(big.NewInt(33))
  436. assert.Nil(t, err)
  437. err = mt.Delete(big.NewInt(33))
  438. assert.Equal(t, ErrKeyNotFound, err)
  439. err = mt.Delete(big.NewInt(1))
  440. assert.Nil(t, err)
  441. assert.Equal(t, "0", mt.Root().String())
  442. err = mt.Delete(big.NewInt(33))
  443. assert.Equal(t, ErrKeyNotFound, err)
  444. }
  445. func TestDumpLeafsImportLeafs(t *testing.T) {
  446. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 140)
  447. require.Nil(t, err)
  448. defer mt.db.Close()
  449. for i := 0; i < 10; i++ {
  450. k := big.NewInt(int64(i))
  451. v := big.NewInt(0)
  452. err = mt.Add(k, v)
  453. require.Nil(t, err)
  454. }
  455. d, err := mt.DumpLeafs(nil)
  456. assert.Nil(t, err)
  457. mt2, err := NewMerkleTree(memory.NewMemoryStorage(), 140)
  458. require.Nil(t, err)
  459. defer mt2.db.Close()
  460. err = mt2.ImportDumpedLeafs(d)
  461. assert.Nil(t, err)
  462. assert.Equal(t, mt.Root(), mt2.Root())
  463. }