@@ -23,40 +23,35 @@ func getNodeValue(t ReadOnlyTrie, key string) ([]byte, error) {
23
23
}
24
24
25
25
func getNodeValueWithBranchFactor (t ReadOnlyTrie , key string , bf BranchFactor ) ([]byte , error ) {
26
+ var view * trieView
26
27
if asTrieView , ok := t .(* trieView ); ok {
27
28
if err := asTrieView .calculateNodeIDs (context .Background ()); err != nil {
28
29
return nil , err
29
30
}
30
- path := ToKey ([]byte (key ), bf )
31
- nodePath , err := asTrieView .getPathTo (path )
32
- if err != nil {
33
- return nil , err
34
- }
35
- closestNode := nodePath [len (nodePath )- 1 ]
36
- if closestNode .key != path || closestNode == nil {
37
- return nil , database .ErrNotFound
38
- }
39
-
40
- return closestNode .value .Value (), nil
31
+ view = asTrieView
41
32
}
42
33
if asDatabases , ok := t .(* merkleDB ); ok {
43
- view , err := asDatabases .NewView (context .Background (), ViewChanges {})
34
+ dbView , err := asDatabases .NewView (context .Background (), ViewChanges {})
44
35
if err != nil {
45
36
return nil , err
46
37
}
47
- path := ToKey ([]byte (key ), bf )
48
- nodePath , err := view .(* trieView ).getPathTo (path )
49
- if err != nil {
50
- return nil , err
51
- }
52
- closestNode := nodePath [len (nodePath )- 1 ]
53
- if closestNode .key != path || closestNode == nil {
54
- return nil , database .ErrNotFound
55
- }
38
+ view = dbView .(* trieView )
39
+ }
56
40
57
- return closestNode .value .Value (), nil
41
+ path := ToKey ([]byte (key ), bf )
42
+ var result * node
43
+ err := view .visitPathToKey (path , func (n * node ) error {
44
+ result = n
45
+ return nil
46
+ })
47
+ if err != nil {
48
+ return nil , err
58
49
}
59
- return nil , nil
50
+ if result .key != path || result == nil {
51
+ return nil , database .ErrNotFound
52
+ }
53
+
54
+ return result .value .Value (), nil
60
55
}
61
56
62
57
func Test_GetValue_Safety (t * testing.T ) {
@@ -116,7 +111,7 @@ func Test_GetValues_Safety(t *testing.T) {
116
111
require .Equal ([]byte {0 }, trieVals [0 ])
117
112
}
118
113
119
- func TestTrieViewGetPathTo (t * testing.T ) {
114
+ func TestTrieViewVisitPathToKey (t * testing.T ) {
120
115
require := require .New (t )
121
116
122
117
db , err := getBasicDB ()
@@ -127,8 +122,11 @@ func TestTrieViewGetPathTo(t *testing.T) {
127
122
require .IsType (& trieView {}, trieIntf )
128
123
trie := trieIntf .(* trieView )
129
124
130
- nodePath , err := trie .getPathTo (ToKey (nil , BranchFactor16 ))
131
- require .NoError (err )
125
+ var nodePath []* node
126
+ require .NoError (trie .visitPathToKey (ToKey (nil , BranchFactor16 ), func (n * node ) error {
127
+ nodePath = append (nodePath , n )
128
+ return nil
129
+ }))
132
130
133
131
// Just the root
134
132
require .Len (nodePath , 1 )
@@ -149,8 +147,11 @@ func TestTrieViewGetPathTo(t *testing.T) {
149
147
trie = trieIntf .(* trieView )
150
148
require .NoError (trie .calculateNodeIDs (context .Background ()))
151
149
152
- nodePath , err = trie .getPathTo (ToKey (key1 , BranchFactor16 ))
153
- require .NoError (err )
150
+ nodePath = make ([]* node , 0 , 2 )
151
+ require .NoError (trie .visitPathToKey (ToKey (key1 , BranchFactor16 ), func (n * node ) error {
152
+ nodePath = append (nodePath , n )
153
+ return nil
154
+ }))
154
155
155
156
// Root and 1 value
156
157
require .Len (nodePath , 2 )
@@ -172,8 +173,11 @@ func TestTrieViewGetPathTo(t *testing.T) {
172
173
trie = trieIntf .(* trieView )
173
174
require .NoError (trie .calculateNodeIDs (context .Background ()))
174
175
175
- nodePath , err = trie .getPathTo (ToKey (key2 , BranchFactor16 ))
176
- require .NoError (err )
176
+ nodePath = make ([]* node , 0 , 3 )
177
+ require .NoError (trie .visitPathToKey (ToKey (key2 , BranchFactor16 ), func (n * node ) error {
178
+ nodePath = append (nodePath , n )
179
+ return nil
180
+ }))
177
181
require .Len (nodePath , 3 )
178
182
require .Equal (trie .root , nodePath [0 ])
179
183
require .Equal (ToKey (key1 , BranchFactor16 ), nodePath [1 ].key )
@@ -194,33 +198,45 @@ func TestTrieViewGetPathTo(t *testing.T) {
194
198
trie = trieIntf .(* trieView )
195
199
require .NoError (trie .calculateNodeIDs (context .Background ()))
196
200
197
- nodePath , err = trie .getPathTo (ToKey (key3 , BranchFactor16 ))
198
- require .NoError (err )
201
+ nodePath = make ([]* node , 0 , 2 )
202
+ require .NoError (trie .visitPathToKey (ToKey (key3 , BranchFactor16 ), func (n * node ) error {
203
+ nodePath = append (nodePath , n )
204
+ return nil
205
+ }))
199
206
require .Len (nodePath , 2 )
200
207
require .Equal (trie .root , nodePath [0 ])
201
208
require .Equal (ToKey (key3 , BranchFactor16 ), nodePath [1 ].key )
202
209
203
210
// Other key path not affected
204
- nodePath , err = trie .getPathTo (ToKey (key2 , BranchFactor16 ))
205
- require .NoError (err )
211
+ nodePath = make ([]* node , 0 , 3 )
212
+ require .NoError (trie .visitPathToKey (ToKey (key2 , BranchFactor16 ), func (n * node ) error {
213
+ nodePath = append (nodePath , n )
214
+ return nil
215
+ }))
206
216
require .Len (nodePath , 3 )
207
217
require .Equal (trie .root , nodePath [0 ])
208
218
require .Equal (ToKey (key1 , BranchFactor16 ), nodePath [1 ].key )
209
219
require .Equal (ToKey (key2 , BranchFactor16 ), nodePath [2 ].key )
210
220
211
221
// Gets closest node when key doesn't exist
212
222
key4 := []byte {0 , 1 , 2 }
213
- nodePath , err = trie .getPathTo (ToKey (key4 , BranchFactor16 ))
214
- require .NoError (err )
223
+ nodePath = make ([]* node , 0 , 3 )
224
+ require .NoError (trie .visitPathToKey (ToKey (key4 , BranchFactor16 ), func (n * node ) error {
225
+ nodePath = append (nodePath , n )
226
+ return nil
227
+ }))
215
228
require .Len (nodePath , 3 )
216
229
require .Equal (trie .root , nodePath [0 ])
217
230
require .Equal (ToKey (key1 , BranchFactor16 ), nodePath [1 ].key )
218
231
require .Equal (ToKey (key2 , BranchFactor16 ), nodePath [2 ].key )
219
232
220
233
// Gets just root when key doesn't exist and no key shares a prefix
221
234
key5 := []byte {128 }
222
- nodePath , err = trie .getPathTo (ToKey (key5 , BranchFactor16 ))
223
- require .NoError (err )
235
+ nodePath = make ([]* node , 0 , 1 )
236
+ require .NoError (trie .visitPathToKey (ToKey (key5 , BranchFactor16 ), func (n * node ) error {
237
+ nodePath = append (nodePath , n )
238
+ return nil
239
+ }))
224
240
require .Len (nodePath , 1 )
225
241
require .Equal (trie .root , nodePath [0 ])
226
242
}
0 commit comments