-
Notifications
You must be signed in to change notification settings - Fork 4
/
bulk_test.go
148 lines (129 loc) · 3.6 KB
/
bulk_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package smt
import (
"bytes"
crand "crypto/rand"
"crypto/sha256"
"math/rand"
"testing"
"github.com/stretchr/testify/require"
"github.com/pokt-network/smt/kvstore/simplemap"
)
type (
opCounts struct{ ops, inserts, updates, deletes int }
bulkop struct{ key, val []byte }
)
// Test all trie operations in bulk.
func TestBulkOperations(t *testing.T) {
cases := []opCounts{
// Test more inserts/updates than deletions.
{200, 100, 100, 50},
{1000, 100, 100, 50},
// Test extreme deletions.
{200, 100, 100, 500},
{1000, 100, 100, 500},
}
for _, tc := range cases {
bulkOperations(t, tc.ops, tc.inserts, tc.updates, tc.deletes)
}
}
// Test all trie operations in bulk, with specified ratio probabilities of insert, update and delete.
func bulkOperations(t *testing.T, operations int, insert int, update int, delete int) {
smn := simplemap.NewSimpleMap()
smv := simplemap.NewSimpleMap()
smt := NewSMTWithStorage(smn, smv, sha256.New())
max := insert + update + delete
var kv []bulkop
r := rand.New(rand.NewSource(1))
for i := 0; i < operations; i++ {
n := rand.Intn(max)
if n < insert { // Insert
keyLen := 16 + r.Intn(32)
key := make([]byte, keyLen)
_, err := crand.Read(key)
require.NoError(t, err)
valLen := 1 + r.Intn(64)
val := make([]byte, valLen)
_, err = crand.Read(val)
require.NoError(t, err)
err = smt.Update(key, val)
if err != nil {
t.Fatalf("error: %v", err)
}
kv = append(kv, bulkop{key, val})
} else if n > insert && n < insert+update { // Update
if len(kv) == 0 {
continue
}
ki := rand.Intn(len(kv))
valLen := 1 + r.Intn(64)
val := make([]byte, valLen)
_, err := crand.Read(val)
require.NoError(t, err)
err = smt.Update(kv[ki].key, val)
if err != nil {
t.Fatalf("error: %v", err)
}
kv[ki].val = val
} else { // Delete
if len(kv) == 0 {
continue
}
ki := r.Intn(len(kv))
err := smt.Delete(kv[ki].key)
if err != nil && err != ErrKeyNotFound {
t.Fatalf("error: %v", err)
}
kv[ki].val = defaultEmptyValue
}
}
bulkCheckAll(t, smt, kv)
}
func bulkCheckAll(t *testing.T, smt *SMTWithStorage, kv []bulkop) {
for ki := range kv {
k, v := kv[ki].key, kv[ki].val
value, err := smt.GetValue([]byte(k))
if err != nil {
t.Errorf("error: %v", err)
}
if !bytes.Equal([]byte(v), value) {
t.Errorf("Incorrect value (i=%d)", ki)
}
// Generate and verify a Merkle proof for this key.
proof, err := smt.Prove([]byte(k))
if err != nil {
t.Errorf("error: %v", err)
}
valid, err := VerifyProof(proof, smt.Root(), []byte(k), []byte(v), smt.Spec())
if !valid || err != nil {
t.Fatalf("Merkle proof failed to verify (i=%d): %v", ki, []byte(k))
}
compactProof, err := ProveCompact([]byte(k), smt)
if err != nil {
t.Errorf("error: %v", err)
}
valid, err = VerifyCompactProof(compactProof, smt.Root(), []byte(k), []byte(v), smt.Spec())
if !valid || err != nil {
t.Fatalf("Compact Merkle proof failed to verify (i=%d): %v", ki, []byte(k))
}
if v == nil {
continue
}
// Check that the key is at the correct height in the trie.
largestCommonPrefix := 0
for ki2 := range kv {
k2, v2 := kv[ki2].key, kv[ki2].val
if v2 == nil {
continue
}
ph := smt.Spec().ph
commonPrefix := countCommonPrefixBits(ph.Path([]byte(k)), ph.Path([]byte(k2)), 0)
if commonPrefix != smt.Spec().depth() && commonPrefix > largestCommonPrefix {
largestCommonPrefix = commonPrefix
}
}
if len(proof.SideNodes) != largestCommonPrefix+1 &&
(len(proof.SideNodes) != 0 && largestCommonPrefix != 0) {
t.Errorf("leaf is at unexpected height (ki=%d)", ki)
}
}
}