arbo/addbatch_test.go
addbatch_test.goRaw
1package arbo
2
3import (
4 "fmt"
5 "math/big"
6 "testing"
7 "time"
8
9 qt "github.com/frankban/quicktest"
10 "github.com/iden3/go-merkletree/db/memory"
11)
12
13func TestAddBatchCaseA(t *testing.T) {
14 c := qt.New(t)
15
16 nLeafs := 1024
17
18 tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
19 c.Assert(err, qt.IsNil)
20 defer tree.db.Close()
21
22 start := time.Now()
23 for i := 0; i < nLeafs; i++ {
24 k := BigIntToBytes(big.NewInt(int64(i)))
25 v := BigIntToBytes(big.NewInt(int64(i * 2)))
26 if err := tree.Add(k, v); err != nil {
27 t.Fatal(err)
28 }
29 }
30 fmt.Println(time.Since(start))
31
32 tree2, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
33 c.Assert(err, qt.IsNil)
34 defer tree2.db.Close()
35
36 var keys, values [][]byte
37 for i := 0; i < nLeafs; i++ {
38 k := BigIntToBytes(big.NewInt(int64(i)))
39 v := BigIntToBytes(big.NewInt(int64(i * 2)))
40 keys = append(keys, k)
41 values = append(values, v)
42 }
43 start = time.Now()
44 indexes, err := tree2.AddBatchOpt(keys, values)
45 c.Assert(err, qt.IsNil)
46 fmt.Println(time.Since(start))
47 c.Check(len(indexes), qt.Equals, 0)
48
49 // check that both trees roots are equal
50 c.Check(tree2.Root(), qt.DeepEquals, tree.Root())
51}
52
53func TestAddBatchCaseB(t *testing.T) {
54 c := qt.New(t)
55
56 nLeafs := 1024
57
58 tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
59 c.Assert(err, qt.IsNil)
60 defer tree.db.Close()
61
62 start := time.Now()
63 for i := 0; i < nLeafs; i++ {
64 k := BigIntToBytes(big.NewInt(int64(i)))
65 v := BigIntToBytes(big.NewInt(int64(i * 2)))
66 if err := tree.Add(k, v); err != nil {
67 t.Fatal(err)
68 }
69 }
70 fmt.Println(time.Since(start))
71
72 tree2, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
73 c.Assert(err, qt.IsNil)
74 defer tree2.db.Close()
75
76 // add the initial leafs to fill a bit the tree before calling the
77 // AddBatch method
78 for i := 0; i < 99; i++ { // TMP TODO use const minLeafsThreshold-1 once ready
79 k := BigIntToBytes(big.NewInt(int64(i)))
80 v := BigIntToBytes(big.NewInt(int64(i * 2)))
81 if err := tree2.Add(k, v); err != nil {
82 t.Fatal(err)
83 }
84 }
85
86 var keys, values [][]byte
87 for i := 99; i < nLeafs; i++ {
88 k := BigIntToBytes(big.NewInt(int64(i)))
89 v := BigIntToBytes(big.NewInt(int64(i * 2)))
90 keys = append(keys, k)
91 values = append(values, v)
92 }
93 start = time.Now()
94 indexes, err := tree2.AddBatchOpt(keys, values)
95 c.Assert(err, qt.IsNil)
96 fmt.Println(time.Since(start))
97 c.Check(len(indexes), qt.Equals, 0)
98
99 // check that both trees roots are equal
100 c.Check(tree2.Root(), qt.DeepEquals, tree.Root())
101}