@@ -5,7 +5,7 @@ import { assert, beforeAll, describe, it } from 'vitest'
5
5
import {
6
6
InternalNode ,
7
7
LeafNode ,
8
- ROOT_DB_KEY ,
8
+ VerkleNodeType ,
9
9
decodeNode ,
10
10
matchingBytesLength ,
11
11
} from '../src/index.js'
@@ -163,16 +163,16 @@ describe('findPath validation', () => {
163
163
leafNode1 . setValue ( hexToBytes ( keys [ 0 ] ) [ 31 ] , hexToBytes ( values [ 0 ] ) )
164
164
leafNode1 . setValue ( hexToBytes ( keys [ 1 ] ) [ 31 ] , hexToBytes ( values [ 1 ] ) )
165
165
166
- putStack . push ( [ stem1 , leafNode1 ] )
166
+ putStack . push ( [ leafNode1 . hash ( ) , leafNode1 ] )
167
167
168
168
// Pull root node from DB
169
- const rawNode = await trie [ '_db' ] . get ( ROOT_DB_KEY )
169
+ const rawNode = await trie [ '_db' ] . get ( trie . root ( ) )
170
170
const rootNode = decodeNode ( rawNode ! , verkleCrypto ) as InternalNode
171
171
// Update root node with commitment from leaf node
172
172
rootNode . setChild ( stem1 [ 0 ] , { commitment : leafNode1 . commitment , path : stem1 } )
173
- putStack . push ( [ ROOT_DB_KEY , rootNode ] )
174
- await trie . saveStack ( putStack )
175
173
trie . root ( verkleCrypto . serializeCommitment ( rootNode . commitment ) )
174
+ putStack . push ( [ trie . root ( ) , rootNode ] )
175
+ await trie . saveStack ( putStack )
176
176
177
177
// Verify that path to leaf node can be found from stem
178
178
const res = await trie . findPath ( stem1 )
@@ -185,41 +185,58 @@ describe('findPath validation', () => {
185
185
// Put a second leaf node in the tree with a partially matching stem
186
186
putStack = [ ]
187
187
const stem2 = hexToBytes ( keys [ 2 ] ) . slice ( 0 , 31 )
188
+
189
+ // Find path to closest node in tree
190
+ const foundPath = await trie . findPath ( stem2 )
191
+
192
+ // Confirm node with stem2 doesn't exist in trie
193
+ assert . equal ( foundPath . node , null )
194
+
195
+ // Create new leaf node
188
196
const leafNode2 = await LeafNode . create (
189
197
stem2 ,
190
198
new Array ( 256 ) . fill ( new Uint8Array ( 32 ) ) ,
191
199
verkleCrypto
192
200
)
193
201
leafNode2 . setValue ( hexToBytes ( keys [ 2 ] ) [ 31 ] , hexToBytes ( values [ 2 ] ) )
194
- putStack . push ( [ stem2 , leafNode2 ] )
202
+ putStack . push ( [ leafNode2 . hash ( ) , leafNode2 ] )
195
203
196
- // Create new internal node
197
- const internalNode1 = InternalNode . create ( verkleCrypto )
204
+ const nearestNode = foundPath . stack . pop ( ) ! [ 0 ]
205
+ // Verify that another leaf node is "nearest" node
206
+ assert . equal ( nearestNode . type , VerkleNodeType . Leaf )
207
+ assert . deepEqual ( nearestNode , leafNode1 )
198
208
199
- // Compute the portion of stem1 and stem2 that match
209
+ // Compute the portion of stem1 and stem2 that match (i.e. the partial path closest to stem2)
200
210
// Note: We subtract 1 since we are using 0-indexed arrays
201
211
const partialMatchingStemIndex = matchingBytesLength ( stem1 , stem2 ) - 1
202
212
// Find the path to the new internal node (the matching portion of stem1 and stem2)
203
213
const internalNode1Path = stem1 . slice ( 0 , partialMatchingStemIndex )
214
+ // Create new internal node
215
+ const internalNode1 = InternalNode . create ( verkleCrypto )
216
+
204
217
// Update the child references for leafNode1 and leafNode 2
205
218
internalNode1 . setChild ( stem1 [ partialMatchingStemIndex ] , {
206
- commitment : leafNode1 . commitment ,
207
- path : stem1 ,
219
+ commitment : nearestNode . commitment ,
220
+ path : ( nearestNode as LeafNode ) . stem ,
208
221
} )
209
222
internalNode1 . setChild ( stem2 [ partialMatchingStemIndex ] , {
210
223
commitment : leafNode2 . commitment ,
211
224
path : stem2 ,
212
225
} )
213
226
214
- putStack . push ( [ internalNode1Path , internalNode1 ] )
227
+ putStack . push ( [ internalNode1 . hash ( ) , internalNode1 ] )
215
228
// Update rootNode child reference for internal node 1
216
- rootNode . setChild ( internalNode1Path [ 0 ] , {
229
+
230
+ const rootNodeFromPath = foundPath . stack . pop ( ) ! [ 0 ] as InternalNode
231
+ // Confirm node from findPath matches root
232
+ assert . deepEqual ( rootNodeFromPath , rootNode )
233
+ rootNodeFromPath . setChild ( internalNode1Path [ 0 ] , {
217
234
commitment : internalNode1 . commitment ,
218
235
path : internalNode1Path ,
219
236
} )
220
- putStack . push ( [ ROOT_DB_KEY , rootNode ] )
237
+ trie . root ( verkleCrypto . serializeCommitment ( rootNodeFromPath . commitment ) )
238
+ putStack . push ( [ trie . root ( ) , rootNodeFromPath ] )
221
239
await trie . saveStack ( putStack )
222
- trie . root ( verkleCrypto . serializeCommitment ( rootNode . commitment ) )
223
240
let res2 = await trie . findPath ( stem1 )
224
241
225
242
assert . equal ( res2 . remaining . length , 0 , 'confirm full path was found' )
0 commit comments