-
Notifications
You must be signed in to change notification settings - Fork 2
/
trie_test.go
389 lines (334 loc) · 8.79 KB
/
trie_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
package mpt_test
import (
"bytes"
"github.com/begmaroman/mpt"
"math/rand"
"testing"
"github.com/begmaroman/mpt/node"
)
func TestTrie_Put(t *testing.T) {
testData := []*struct {
key, value []byte
}{
{
key: []byte("test"),
value: []byte("test value with words"),
},
{
key: []byte("test1"),
value: []byte("test value with words 1"),
},
{
key: []byte("test2"),
value: []byte("test value with words 2"),
},
}
trie := mpt.NewTrie(nil)
for _, data := range testData {
if !trie.Put(data.key, data.value) {
t.Errorf("key/value not added to tree: %s: %s", data.key, data.value)
}
}
}
func TestTrie_PutAndCheck(t *testing.T) {
testData := []*struct {
key, value, upValue []byte
}{
{
key: []byte("test"),
value: []byte("test value with words"),
upValue: []byte("test updated value"),
},
{
key: []byte("test1"),
value: []byte("test value with words 1"),
upValue: []byte("test updated value 1"),
},
{
key: []byte("test2"),
value: []byte("test value with words 2"),
upValue: []byte("test updated value 2"),
},
}
trie := mpt.NewTrie(nil)
for _, data := range testData {
if !trie.Put(data.key, data.value) {
t.Errorf("key/value not added to tree: %s: %s", data.key, data.value)
}
}
for _, data := range testData {
rVal, ok := trie.Get(data.key)
if !ok {
t.Errorf("lookup failed for key %s", data.key)
}
if !bytes.Equal(rVal, []byte(data.value)) {
t.Errorf("invalid value: expected %s got %s", data.value, rVal)
}
}
}
func TestTrie_CheckHash(t *testing.T) {
testData := []*struct {
key, value, upValue []byte
}{
{
key: []byte("test"),
value: []byte("test value with words"),
upValue: []byte("test updated value"),
},
{
key: []byte("test1"),
value: []byte("test value with words 1"),
upValue: []byte("test updated value 1"),
},
{
key: []byte("test2"),
value: []byte("test value with words 2"),
upValue: []byte("test updated value 2"),
},
}
trie := mpt.NewTrie(nil)
for _, data := range testData {
if !trie.Put(data.key, data.value) {
t.Errorf("key/value not added to tree: %s: %s", data.key, data.value)
}
}
hash, err := trie.Hash()
if err != nil {
t.Error(err)
return
}
if len(hash) != 32 {
t.Errorf("invalid length of hash: expected 32 got %d", len(hash))
}
trie.Update(testData[0].key, []byte("new value for key 0"))
newhash, err := trie.Hash()
if err != nil {
t.Error(err)
return
}
if len(hash) != 32 {
t.Errorf("invalid length of hash after update: expected 32 got %d", len(hash))
}
if newhash == hash {
t.Error("hash not rebuilding after update")
}
}
func TestTrie_PutUpdateAndCheck(t *testing.T) {
testData := []*struct {
key, value, upValue []byte
}{
{
key: []byte("test"),
value: []byte("test value with words"),
upValue: []byte("test updated value"),
},
{
key: []byte("test1"),
value: []byte("test value with words 1"),
upValue: []byte("test updated value 1"),
},
{
key: []byte("test2"),
value: []byte("test value with words 2"),
upValue: []byte("test updated value 2"),
},
}
trie := mpt.NewTrie(nil)
for _, data := range testData {
if !trie.Put(data.key, data.value) {
t.Errorf("key/value not added: %s: %s", data.key, data.value)
}
}
for _, data := range testData {
rVal, ok := trie.Get(data.key)
if !ok {
t.Errorf("lookup failed for key %s", data.key)
}
if !bytes.Equal(rVal, []byte(data.value)) {
t.Errorf("invalid value: expected %s got %s", data.value, rVal)
}
}
for _, data := range testData {
if !trie.Update(data.key, data.upValue) {
t.Errorf("value not updated: %s: %s", data.key, data.upValue)
}
}
for _, data := range testData {
rVal, ok := trie.Get(data.key)
if !ok {
t.Errorf("lookup failed for key %s", data.key)
}
if !bytes.Equal(rVal, []byte(data.upValue)) {
t.Errorf("invalid value: expected %s got %s", data.upValue, rVal)
}
}
}
func TestTrie_PutDeleteAndCheck(t *testing.T) {
testData := []*struct {
key, value []byte
}{
{
key: []byte("test"),
value: []byte("test value with words"),
},
{
key: []byte("test1"),
value: []byte("test value with words 1"),
},
{
key: []byte("test2"),
value: []byte("test value with words 2"),
},
}
trie := mpt.NewTrie(nil)
// Add data
for _, data := range testData {
if !trie.Put(data.key, data.value) {
t.Errorf("key/value not added: %s: %s", data.key, data.value)
}
}
// Check added data
for _, data := range testData {
rVal, ok := trie.Get(data.key)
if !ok {
t.Errorf("get added: lookup failed for key %s", data.key)
}
if !bytes.Equal(rVal, []byte(data.value)) {
t.Errorf("check added: invalid value: expected %s got %s", data.value, rVal)
}
}
// Delete Data
for _, data := range testData {
if !trie.Delete(data.key) {
t.Errorf("delete: value not deleted for key %s", data.key)
}
}
// Check deleted data
for _, data := range testData {
_, ok := trie.Get(data.key)
if ok {
t.Errorf("data not deleted for key %s", data.key)
}
}
}
func TestTrie_CustomStartNode_BranchNode_Empty(t *testing.T) {
key, val := []byte("test"), []byte("test value")
trie := mpt.NewTrie(node.NewBranchNode())
trie.Put(key, val)
rVal, ok := trie.Get([]byte("test"))
if !ok {
t.Errorf("lookup failed for key %s", key)
}
if !bytes.Equal(rVal, val) {
t.Errorf("check added: invalid value: expected %s got %s", val, rVal)
}
}
func TestTrie_CustomStartNode_ExtensionNode_Empty(t *testing.T) {
key, val := []byte("test"), []byte("test value")
trie := mpt.NewTrie(node.NewExtensionNode(nil, nil))
trie.Put(key, val)
rVal, ok := trie.Get([]byte("test"))
if !ok {
t.Errorf("lookup failed for key %s", key)
}
if !bytes.Equal(rVal, val) {
t.Errorf("check added: invalid value: expected %s got %s", val, rVal)
}
}
// Benchmarks
func BenchmarkTrie_Get(b *testing.B) {
key, val := []byte("tolongwseb;kjnwkjlevnoknbkomnrwobnwotrh34y6onyo"), []byte("lwebniopwjgnipwuhgpv wrhtpovm rhiopwerhgpowuihnopwrntgmopkrtmvport uiwthgipurthg puti")
fullTrie := mpt.NewTrie(nil)
fullTrie.Put(key, val)
for j := 0; j < 100000; j++ {
tokenKey, tokenValue := make([]byte, 56), make([]byte, 556)
rand.Read(tokenKey)
rand.Read(tokenValue)
fullTrie.Put(tokenKey, tokenValue)
}
b.Run("FromFullTrie100000", func(b *testing.B) {
for i := 0; i < b.N; i++ {
fullTrie.Get(key)
}
})
emptyTrie := mpt.NewTrie(nil)
emptyTrie.Put(key, val)
b.Run("FromEmptyTrie", func(b *testing.B) {
for i := 0; i < b.N; i++ {
emptyTrie.Get(key)
}
})
}
func BenchmarkTrie_Put(b *testing.B) {
key, val := []byte("tolongwseb;kjnwkjlevnoknbkomnrwobnwotrh34y6onyo"), []byte("lwebniopwjgnipwuhgpv wrhtpovm rhiopwerhgpowuihnopwrntgmopkrtmvport uiwthgipurthg puti")
fullTrie := mpt.NewTrie(nil)
for j := 0; j < 100000; j++ {
tokenKey, tokenValue := make([]byte, 56), make([]byte, 556)
rand.Read(tokenKey)
rand.Read(tokenValue)
fullTrie.Put(tokenKey, tokenValue)
}
b.Run("ToFullTrie100000", func(b *testing.B) {
for i := 0; i < b.N; i++ {
fullTrie.Put(key, val)
}
})
emptyTrie := mpt.NewTrie(nil)
emptyTrie.Put(key, val)
b.Run("ToEmptyTrie", func(b *testing.B) {
for i := 0; i < b.N; i++ {
emptyTrie.Put(key, val)
}
})
preparedBranchTrie := mpt.NewTrie(node.NewBranchNode())
preparedBranchTrie.Put(key, val)
b.Run("ToPreparedBranchNodeEmptyTrie", func(b *testing.B) {
for i := 0; i < b.N; i++ {
preparedBranchTrie.Put(key, val)
}
})
preparedExtTrie := mpt.NewTrie(node.NewExtensionNode(nil, nil))
preparedExtTrie.Put(key, val)
b.Run("ToPreparedExtensionNodeEmptyTrie", func(b *testing.B) {
for i := 0; i < b.N; i++ {
preparedExtTrie.Put(key, val)
}
})
}
func BenchmarkTrie_Update(b *testing.B) {
key, val := []byte("tolongwseb;kjnwkjlevnoknbkomnrwobnwotrh34y6onyo"), []byte("lwebniopwjgnipwuhgpv wrhtpovm rhiopwerhgpowuihnopwrntgmopkrtmvport uiwthgipurthg puti")
fullTrie := mpt.NewTrie(nil)
for j := 0; j < 100000; j++ {
tokenKey, tokenValue := make([]byte, 56), make([]byte, 556)
rand.Read(tokenKey)
rand.Read(tokenValue)
fullTrie.Put(tokenKey, tokenValue)
}
fullTrie.Put(key, val)
b.Run("FullTrie100000", func(b *testing.B) {
for i := 0; i < b.N; i++ {
fullTrie.Update(key, val)
}
})
emptyTrie := mpt.NewTrie(nil)
emptyTrie.Put(key, val)
b.Run("EmptyTrie", func(b *testing.B) {
for i := 0; i < b.N; i++ {
emptyTrie.Update(key, val)
}
})
preparedBranchTrie := mpt.NewTrie(node.NewBranchNode())
preparedBranchTrie.Put(key, val)
b.Run("PreparedBranchNodeEmptyTrie", func(b *testing.B) {
for i := 0; i < b.N; i++ {
preparedBranchTrie.Update(key, val)
}
})
preparedExtTrie := mpt.NewTrie(node.NewExtensionNode(nil, nil))
preparedExtTrie.Put(key, val)
b.Run("PreparedExtensionNodeEmptyTrie", func(b *testing.B) {
for i := 0; i < b.N; i++ {
preparedExtTrie.Update(key, val)
}
})
}