-
Notifications
You must be signed in to change notification settings - Fork 4
/
smt_test.go
401 lines (326 loc) · 11.4 KB
/
smt_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package smt
import (
"crypto/rand"
"crypto/sha256"
"hash"
"testing"
"github.com/stretchr/testify/require"
"github.com/pokt-network/smt/kvstore"
"github.com/pokt-network/smt/kvstore/simplemap"
)
func NewSMTWithStorage(
nodes, preimages kvstore.MapStore,
hasher hash.Hash,
options ...TrieSpecOption,
) *SMTWithStorage {
return &SMTWithStorage{
SMT: NewSparseMerkleTrie(nodes, hasher, options...),
preimages: preimages,
}
}
func TestSMT_TrieUpdateBasic(t *testing.T) {
smn := simplemap.NewSimpleMap()
smv := simplemap.NewSimpleMap()
lazy := NewSparseMerkleTrie(smn, sha256.New())
smt := &SMTWithStorage{SMT: lazy, preimages: smv}
var value []byte
var has bool
// Test getting an empty key.
value, err := smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, defaultEmptyValue, value)
has, err = smt.Has([]byte("testKey"))
require.NoError(t, err)
require.False(t, has)
// Test updating the empty key.
err = smt.Update([]byte("testKey"), []byte("testValue"))
require.NoError(t, err)
value, err = smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, []byte("testValue"), value)
has, err = smt.Has([]byte("testKey"))
require.NoError(t, err)
require.True(t, has)
// Test updating the non-empty key.
err = smt.Update([]byte("testKey"), []byte("testValue2"))
require.NoError(t, err)
value, err = smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, []byte("testValue2"), value)
// Test updating a second empty key where the path for both keys share the
// first 2 bits (when using SHA256).
err = smt.Update([]byte("foo"), []byte("testValue"))
require.NoError(t, err)
value, err = smt.GetValue([]byte("foo"))
require.NoError(t, err)
require.Equal(t, []byte("testValue"), value)
// Test updating a third empty key.
err = smt.Update([]byte("testKey2"), []byte("testValue"))
require.NoError(t, err)
value, err = smt.GetValue([]byte("testKey2"))
require.NoError(t, err)
require.Equal(t, []byte("testValue"), value)
value, err = smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, []byte("testValue2"), value)
require.NoError(t, lazy.Commit())
// Test that a trie can be imported from a KVStore
lazy = ImportSparseMerkleTrie(smn, sha256.New(), smt.Root())
require.NoError(t, err)
smt = &SMTWithStorage{SMT: lazy, preimages: smv}
value, err = smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, []byte("testValue2"), value)
value, err = smt.GetValue([]byte("foo"))
require.NoError(t, err)
require.Equal(t, []byte("testValue"), value)
value, err = smt.GetValue([]byte("testKey2"))
require.NoError(t, err)
require.Equal(t, []byte("testValue"), value)
}
// Test base case trie delete operations with a few keys.
func TestSMT_TrieDeleteBasic(t *testing.T) {
smn := simplemap.NewSimpleMap()
smv := simplemap.NewSimpleMap()
lazy := NewSparseMerkleTrie(smn, sha256.New())
smt := &SMTWithStorage{SMT: lazy, preimages: smv}
rootEmpty := smt.Root()
// Testing inserting, deleting a key, and inserting it again.
err := smt.Update([]byte("testKey"), []byte("testValue"))
require.NoError(t, err)
root1 := smt.Root()
err = smt.Delete([]byte("testKey"))
require.NoError(t, err)
value, err := smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, defaultEmptyValue, value, "getting deleted key")
has, err := smt.Has([]byte("testKey"))
require.NoError(t, err)
require.False(t, has, "checking existence of deleted key")
err = smt.Update([]byte("testKey"), []byte("testValue"))
require.NoError(t, err)
value, err = smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, []byte("testValue"), value)
require.Equal(t, root1, smt.Root(), "re-inserting key after deletion")
// Test inserting and deleting a second key.
err = smt.Update([]byte("testKey2"), []byte("testValue"))
require.NoError(t, err)
err = smt.Delete([]byte("testKey2"))
require.NoError(t, err)
value, err = smt.GetValue([]byte("testKey2"))
require.NoError(t, err)
require.Equal(t, defaultEmptyValue, value, "getting deleted key")
value, err = smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, []byte("testValue"), value)
require.Equal(t, root1, smt.Root(), "after deleting second key")
// Test inserting and deleting a different second key, when the the first 2
// bits of the path for the two keys in the trie are the same (when using SHA256).
err = smt.Update([]byte("foo"), []byte("testValue"))
require.NoError(t, err)
_, err = smt.GetValue([]byte("foo"))
require.NoError(t, err)
err = smt.Delete([]byte("foo"))
require.NoError(t, err)
value, err = smt.GetValue([]byte("foo"))
require.NoError(t, err)
require.Equal(t, defaultEmptyValue, value, "getting deleted key")
value, err = smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, []byte("testValue"), value)
require.Equal(t, root1, smt.Root(), "after deleting second key")
// Testing inserting, deleting a key, and inserting it again
err = smt.Update([]byte("testKey"), []byte("testValue"))
require.NoError(t, err)
root1 = smt.Root()
err = smt.Delete([]byte("testKey"))
require.NoError(t, err)
// Fail to delete an absent key, but leave trie in a valid state
err = smt.Delete([]byte("testKey"))
require.Error(t, err)
value, err = smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, defaultEmptyValue, value, "getting deleted key")
has, err = smt.Has([]byte("testKey"))
require.NoError(t, err)
require.False(t, has, "checking existence of deleted key")
require.Equal(t, rootEmpty, smt.Root())
err = smt.Update([]byte("testKey"), []byte("testValue"))
require.NoError(t, err)
value, err = smt.GetValue([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, []byte("testValue"), value)
require.Equal(t, root1, smt.Root(), "re-inserting key after deletion")
}
// Test trie ops with known paths
func TestSMT_TrieKnownPath(t *testing.T) {
ph := dummyPathHasher{32}
smn := simplemap.NewSimpleMap()
smv := simplemap.NewSimpleMap()
smt := NewSMTWithStorage(smn, smv, sha256.New(), WithPathHasher(ph))
var value []byte
baseKey := make([]byte, ph.PathSize())
keys := make([][]byte, 7)
for i := range keys {
keys[i] = make([]byte, ph.PathSize())
copy(keys[i], baseKey)
}
keys[0][0] = byte(0b00000000)
keys[1][0] = byte(0b00100000)
keys[2][0] = byte(0b10000000)
keys[3][0] = byte(0b11000000)
keys[4][0] = byte(0b11010000)
keys[5][0] = byte(0b11100000)
keys[6][0] = byte(0b11110000)
err := smt.Update(keys[0], []byte("testValue1"))
require.NoError(t, err)
err = smt.Update(keys[1], []byte("testValue2"))
require.NoError(t, err)
err = smt.Update(keys[2], []byte("testValue3"))
require.NoError(t, err)
err = smt.Update(keys[3], []byte("testValue4"))
require.NoError(t, err)
err = smt.Update(keys[4], []byte("testValue5"))
require.NoError(t, err)
err = smt.Update(keys[5], []byte("testValue6"))
require.NoError(t, err)
value, err = smt.GetValue(keys[0])
require.NoError(t, err)
require.Equal(t, []byte("testValue1"), value)
value, err = smt.GetValue(keys[1])
require.NoError(t, err)
require.Equal(t, []byte("testValue2"), value)
value, err = smt.GetValue(keys[2])
require.NoError(t, err)
require.Equal(t, []byte("testValue3"), value)
value, err = smt.GetValue(keys[3])
require.NoError(t, err)
require.Equal(t, []byte("testValue4"), value)
err = smt.Delete(keys[3])
require.NoError(t, err)
value, err = smt.GetValue(keys[4])
require.NoError(t, err)
require.Equal(t, []byte("testValue5"), value)
value, err = smt.GetValue(keys[5])
require.NoError(t, err)
require.Equal(t, []byte("testValue6"), value)
// Fail to delete an absent key with a leaf where it would be
err = smt.Delete(keys[6])
require.Error(t, err)
// Key at would-be position is still accessible
value, err = smt.GetValue(keys[5])
require.NoError(t, err)
require.Equal(t, []byte("testValue6"), value)
}
// Test trie operations when two leafs are immediate neighbors.
func TestSMT_TrieMaxHeightCase(t *testing.T) {
ph := dummyPathHasher{32}
smn := simplemap.NewSimpleMap()
smv := simplemap.NewSimpleMap()
smt := NewSMTWithStorage(smn, smv, sha256.New(), WithPathHasher(ph))
var value []byte
// Make two neighboring keys.
// The dummy hash function will return the preimage itself as the digest.
key1 := make([]byte, ph.PathSize())
key2 := make([]byte, ph.PathSize())
_, err := rand.Read(key1)
require.NoError(t, err)
copy(key2, key1)
// We make key2's least significant bit different than key1's
key1[ph.PathSize()-1] = byte(0)
key2[ph.PathSize()-1] = byte(1)
err = smt.Update(key1, []byte("testValue1"))
require.NoError(t, err)
err = smt.Update(key2, []byte("testValue2"))
require.NoError(t, err)
value, err = smt.GetValue(key1)
require.NoError(t, err)
require.Equal(t, []byte("testValue1"), value)
value, err = smt.GetValue(key2)
require.NoError(t, err)
require.Equal(t, []byte("testValue2"), value)
proof, err := smt.Prove(key1)
require.NoError(t, err)
require.Equal(t, 256, len(proof.SideNodes), "unexpected proof size")
}
func TestSMT_OrphanRemoval(t *testing.T) {
var smn, smv kvstore.MapStore
var impl *SMT
var smt *SMTWithStorage
var err error
nodeCount := func(t *testing.T) int {
require.NoError(t, impl.Commit())
len, err := smn.Len()
require.NoError(t, err)
return len
}
setup := func() {
smn = simplemap.NewSimpleMap()
smv = simplemap.NewSimpleMap()
require.NoError(t, err)
impl = NewSparseMerkleTrie(smn, sha256.New())
smt = &SMTWithStorage{SMT: impl, preimages: smv}
err = smt.Update([]byte("testKey"), []byte("testValue"))
require.NoError(t, err)
require.Equal(t, 1, nodeCount(t)) // only root node
}
t.Run("delete 1", func(t *testing.T) {
setup()
err = smt.Delete([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, 0, nodeCount(t))
})
t.Run("overwrite 1", func(t *testing.T) {
setup()
err = smt.Update([]byte("testKey"), []byte("testValue2"))
require.NoError(t, err)
require.Equal(t, 1, nodeCount(t))
})
type testCase struct {
keys []string
count int
}
// sha256(testKey) = 0001...
// sha256(testKey2) = 1000... common prefix len 0; 3 nodes (root + 2 leaf)
// sha256(foo) = 0010... common prefix len 2; 5 nodes (3 inner + 2 leaf)
cases := []testCase{
{[]string{"testKey2"}, 3},
{[]string{"foo"}, 4},
{[]string{"testKey2", "foo"}, 6},
{[]string{"a", "b", "c", "d", "e"}, 14},
}
t.Run("overwrite and delete", func(t *testing.T) {
setup()
err = smt.Update([]byte("testKey"), []byte("testValue2"))
require.NoError(t, err)
require.Equal(t, 1, nodeCount(t))
err = smt.Delete([]byte("testKey"))
require.NoError(t, err)
require.Equal(t, 0, nodeCount(t))
for tci, tc := range cases {
setup()
for _, key := range tc.keys {
err = smt.Update([]byte(key), []byte("testValue2"))
require.NoError(t, err, tci)
}
require.Equal(t, tc.count, nodeCount(t), tci)
// Overwrite doesn't change count
for _, key := range tc.keys {
err = smt.Update([]byte(key), []byte("testValue3"))
require.NoError(t, err, tci)
}
require.Equal(t, tc.count, nodeCount(t), tci)
// Deletion removes all nodes except root
for _, key := range tc.keys {
err = smt.Delete([]byte(key))
require.NoError(t, err, tci)
}
require.Equal(t, 1, nodeCount(t), tci)
// Deleting and re-inserting a persisted node doesn't change count
require.NoError(t, smt.Delete([]byte("testKey")))
require.NoError(t, smt.Update([]byte("testKey"), []byte("testValue")))
require.Equal(t, 1, nodeCount(t), tci)
}
})
}