forked from zhangchiqing/merkle-patricia-trie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie_test.go
309 lines (243 loc) · 10.5 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
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func hexEqual(t *testing.T, hex string, bytes []byte) {
require.Equal(t, hex, fmt.Sprintf("%x", bytes))
}
// check basic key-value mapping
func TestGetPut(t *testing.T) {
t.Run("should get nothing if key does not exist", func(t *testing.T) {
trie := NewTrie()
_, found := trie.Get([]byte("notexist"))
require.Equal(t, false, found)
})
t.Run("should get value if key exist", func(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello"))
val, found := trie.Get([]byte{1, 2, 3, 4})
require.Equal(t, true, found)
require.Equal(t, val, []byte("hello"))
})
t.Run("should get updated value", func(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello"))
trie.Put([]byte{1, 2, 3, 4}, []byte("world"))
val, found := trie.Get([]byte{1, 2, 3, 4})
require.Equal(t, true, found)
require.Equal(t, val, []byte("world"))
})
}
// verify data integrity
func TestDataIntegrity(t *testing.T) {
t.Run("should get a different hash if a new key-value pair was added or updated", func(t *testing.T) {
trie := NewTrie()
hash0 := trie.Hash()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello"))
hash1 := trie.Hash()
trie.Put([]byte{1, 2}, []byte("world"))
hash2 := trie.Hash()
trie.Put([]byte{1, 2}, []byte("trie"))
hash3 := trie.Hash()
require.NotEqual(t, hash0, hash1)
require.NotEqual(t, hash1, hash2)
require.NotEqual(t, hash2, hash3)
})
t.Run("should get the same hash if two tries have the identicial key-value pairs", func(t *testing.T) {
trie1 := NewTrie()
trie1.Put([]byte{1, 2, 3, 4}, []byte("hello"))
trie1.Put([]byte{1, 2}, []byte("world"))
trie2 := NewTrie()
trie2.Put([]byte{1, 2, 3, 4}, []byte("hello"))
trie2.Put([]byte{1, 2}, []byte("world"))
require.Equal(t, trie1.Hash(), trie2.Hash())
})
}
func TestPut2Pairs(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("verb"))
trie.Put([]byte{1, 2, 3, 4, 5, 6}, []byte("coin"))
verb, ok := trie.Get([]byte{1, 2, 3, 4})
require.True(t, ok)
require.Equal(t, []byte("verb"), verb)
coin, ok := trie.Get([]byte{1, 2, 3, 4, 5, 6})
require.True(t, ok)
require.Equal(t, []byte("coin"), coin)
fmt.Printf("%T\n", trie.root)
ext, ok := trie.root.(*ExtensionNode)
require.True(t, ok)
branch, ok := ext.Next.(*BranchNode)
require.True(t, ok)
leaf, ok := branch.Branches[0].(*LeafNode)
require.True(t, ok)
hexEqual(t, "c37ec985b7a88c2c62beb268750efe657c36a585beb435eb9f43b839846682ce", leaf.Hash())
hexEqual(t, "ddc882350684636f696e8080808080808080808080808080808476657262", branch.Serialize())
hexEqual(t, "d757709f08f7a81da64a969200e59ff7e6cd6b06674c3f668ce151e84298aa79", branch.Hash())
hexEqual(t, "64d67c5318a714d08de6958c0e63a05522642f3f1087c6fd68a97837f203d359", ext.Hash())
}
func TestPut(t *testing.T) {
trie := NewTrie()
require.Equal(t, EmptyNodeHash, trie.Hash())
trie.Put([]byte{1, 2, 3, 4}, []byte("hello"))
ns := NewLeafNodeFromBytes([]byte{1, 2, 3, 4}, []byte("hello"))
require.Equal(t, ns.Hash(), trie.Hash())
}
func TestPutLeafShorter(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello"))
trie.Put([]byte{1, 2, 3}, []byte("world"))
leaf := NewLeafNodeFromNibbles([]Nibble{4}, []byte("hello"))
branch := NewBranchNode()
branch.SetBranch(Nibble(0), leaf)
branch.SetValue([]byte("world"))
ext := NewExtensionNode([]Nibble{0, 1, 0, 2, 0, 3}, branch)
require.Equal(t, ext.Hash(), trie.Hash())
}
func TestPutLeafAllMatched(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello"))
trie.Put([]byte{1, 2, 3, 4}, []byte("world"))
ns := NewLeafNodeFromBytes([]byte{1, 2, 3, 4}, []byte("world"))
require.Equal(t, ns.Hash(), trie.Hash())
}
func TestPutLeafMore(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello"))
trie.Put([]byte{1, 2, 3, 4, 5, 6}, []byte("world"))
leaf := NewLeafNodeFromNibbles([]Nibble{5, 0, 6}, []byte("world"))
branch := NewBranchNode()
branch.SetValue([]byte("hello"))
branch.SetBranch(Nibble(0), leaf)
ext := NewExtensionNode([]Nibble{0, 1, 0, 2, 0, 3, 0, 4}, branch)
require.Equal(t, ext.Hash(), trie.Hash())
}
func TestPutOrder(t *testing.T) {
trie1, trie2 := NewTrie(), NewTrie()
trie1.Put([]byte{1, 2, 3, 4, 5, 6}, []byte("world"))
trie1.Put([]byte{1, 2, 3, 4}, []byte("hello"))
trie2.Put([]byte{1, 2, 3, 4}, []byte("hello"))
trie2.Put([]byte{1, 2, 3, 4, 5, 6}, []byte("world"))
require.Equal(t, trie1.Hash(), trie2.Hash())
}
// Before put:
//
// ┌───────────────────────────┐
// │ Extension Node │
// │ Path: [0, 1, 0, 2, 0, 3] │
// └────────────┬──────────────┘
// │
// ┌───────────────────────┴──────────────────┐
// │ Branch Node │
// │ [0] ... [5] │
// └────┼────────────────────────┼────────────┘
// │ │
// │ │
// │ │
// │ │
// ┌───────┴──────────┐ ┌─────────┴─────────┐
// │ Leaf Node │ │ Leaf Node │
// │ Path: [4] │ │ Path: [0] │
// │ Value: "hello1" │ │ Value: "hello2" │
// └──────────────────┘ └───────────────────┘
//
// After put([]byte{[1, 2, 3]}, "world"):
// ┌───────────────────────────┐
// │ Extension Node │
// │ Path: [0, 1, 0, 2, 0, 3] │
// └────────────┬──────────────┘
// │
// ┌───────────────────────┴────────────────────────┐
// │ Branch Node │
// │ [0] ... [5] value: "world" │
// └────┼────────────────────────┼──────────────────┘
// │ │
// │ │
// │ │
// │ │
// ┌───────┴──────────┐ ┌─────────┴─────────┐
// │ Leaf Node │ │ Leaf Node │
// │ Path: [4] │ │ Path: [0] │
// │ Value: "hello1" │ │ Value: "hello2" │
// └──────────────────┘ └───────────────────┘
func TestPutExtensionShorterAllMatched(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello1"))
trie.Put([]byte{1, 2, 3, 5}, []byte("hello2"))
trie.Put([]byte{1, 2, 3}, []byte("world"))
leaf1 := NewLeafNodeFromNibbles([]Nibble{}, []byte("hello1"))
leaf2 := NewLeafNodeFromNibbles([]Nibble{}, []byte("hello2"))
branch1 := NewBranchNode()
branch1.SetBranch(Nibble(4), leaf1)
branch1.SetBranch(Nibble(5), leaf2)
branch2 := NewBranchNode()
branch2.SetValue([]byte("world"))
branch2.SetBranch(Nibble(0), branch1)
ext := NewExtensionNode([]Nibble{0, 1, 0, 2, 0, 3}, branch2)
require.Equal(t, ext.Hash(), trie.Hash())
}
func TestPutExtensionShorterPartialMatched(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello1"))
trie.Put([]byte{1, 2, 3, 5}, []byte("hello2"))
trie.Put([]byte{1, 2, 5}, []byte("world"))
leaf1 := NewLeafNodeFromNibbles([]Nibble{}, []byte("hello1"))
leaf2 := NewLeafNodeFromNibbles([]Nibble{}, []byte("hello2"))
branch1 := NewBranchNode()
branch1.SetBranch(Nibble(4), leaf1)
branch1.SetBranch(Nibble(5), leaf2)
ext1 := NewExtensionNode([]Nibble{0}, branch1)
branch2 := NewBranchNode()
branch2.SetBranch(Nibble(3), ext1)
leaf3 := NewLeafNodeFromNibbles([]Nibble{}, []byte("world"))
branch2.SetBranch(Nibble(5), leaf3)
ext2 := NewExtensionNode([]Nibble{0, 1, 0, 2, 0}, branch2)
require.Equal(t, ext2.Hash(), trie.Hash())
}
func TestPutExtensionShorterZeroMatched(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello1"))
trie.Put([]byte{1, 2, 3, 5}, []byte("hello2"))
trie.Put([]byte{1 << 4, 2, 5}, []byte("world"))
leaf1 := NewLeafNodeFromNibbles([]Nibble{}, []byte("hello1"))
leaf2 := NewLeafNodeFromNibbles([]Nibble{}, []byte("hello2"))
branch1 := NewBranchNode()
branch1.SetBranch(Nibble(4), leaf1)
branch1.SetBranch(Nibble(5), leaf2)
ext1 := NewExtensionNode([]Nibble{1, 0, 2, 0, 3, 0}, branch1)
branch2 := NewBranchNode()
branch2.SetBranch(Nibble(0), ext1)
leaf3 := NewLeafNodeFromNibbles([]Nibble{0, 0, 2, 0, 5}, []byte("world"))
branch2.SetBranch(Nibble(1), leaf3)
require.Equal(t, branch2.Hash(), trie.Hash())
}
func TestPutExtensionAllMatched(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello1"))
trie.Put([]byte{1, 2, 3, 5 << 4}, []byte("hello2"))
trie.Put([]byte{1, 2, 3}, []byte("world"))
leaf1 := NewLeafNodeFromNibbles([]Nibble{4}, []byte("hello1"))
leaf2 := NewLeafNodeFromNibbles([]Nibble{0}, []byte("hello2"))
branch := NewBranchNode()
branch.SetBranch(Nibble(0), leaf1)
branch.SetBranch(Nibble(5), leaf2)
branch.SetValue([]byte("world"))
ext := NewExtensionNode([]Nibble{0, 1, 0, 2, 0, 3}, branch)
require.Equal(t, ext.Hash(), trie.Hash())
}
func TestPutExtensionMore(t *testing.T) {
trie := NewTrie()
trie.Put([]byte{1, 2, 3, 4}, []byte("hello1"))
trie.Put([]byte{1, 2, 3, 5}, []byte("hello2"))
trie.Put([]byte{1, 2, 3, 6}, []byte("world"))
leaf1 := NewLeafNodeFromNibbles([]Nibble{}, []byte("hello1"))
leaf2 := NewLeafNodeFromNibbles([]Nibble{}, []byte("hello2"))
leaf3 := NewLeafNodeFromNibbles([]Nibble{}, []byte("world"))
branch := NewBranchNode()
branch.SetBranch(Nibble(4), leaf1)
branch.SetBranch(Nibble(5), leaf2)
branch.SetBranch(Nibble(6), leaf3)
ext := NewExtensionNode([]Nibble{0, 1, 0, 2, 0, 3, 0}, branch)
require.Equal(t, ext.Hash(), trie.Hash())
}