@@ -72,7 +72,7 @@ export class Trie {
72
72
73
73
/** Debug logging */
74
74
protected DEBUG : boolean
75
- protected _debug : Debugger = debug ( 'trie' )
75
+ protected _debug : Debugger = debug ( 'trie:# ' )
76
76
protected debug : ( ...args : any ) => void
77
77
78
78
/**
@@ -187,13 +187,13 @@ export class Trie {
187
187
* @returns A Promise that resolves to `Uint8Array` if a value was found or `null` if no value was found.
188
188
*/
189
189
async get ( key : Uint8Array , throwIfMissing = false ) : Promise < Uint8Array | null > {
190
- this . DEBUG && this . debug ( `Key: ${ bytesToHex ( key ) } ` , [ 'GET ' ] )
190
+ this . DEBUG && this . debug ( `Key: ${ bytesToHex ( key ) } ` , [ 'get ' ] )
191
191
const { node, remaining } = await this . findPath ( this . appliedKey ( key ) , throwIfMissing )
192
192
let value : Uint8Array | null = null
193
193
if ( node && remaining . length === 0 ) {
194
194
value = node . value ( )
195
195
}
196
- this . DEBUG && this . debug ( `Value: ${ value === null ? 'null' : bytesToHex ( value ) } ` , [ 'GET ' ] )
196
+ this . DEBUG && this . debug ( `Value: ${ value === null ? 'null' : bytesToHex ( value ) } ` , [ 'get ' ] )
197
197
return value
198
198
}
199
199
@@ -209,8 +209,8 @@ export class Trie {
209
209
value : Uint8Array | null ,
210
210
skipKeyTransform : boolean = false ,
211
211
) : Promise < void > {
212
- this . DEBUG && this . debug ( `Key: ${ bytesToHex ( key ) } ` , [ 'PUT ' ] )
213
- this . DEBUG && this . debug ( `Value: ${ value === null ? 'null' : bytesToHex ( key ) } ` , [ 'PUT ' ] )
212
+ this . DEBUG && this . debug ( `Key: ${ bytesToHex ( key ) } ` , [ 'put ' ] )
213
+ this . DEBUG && this . debug ( `Value: ${ value === null ? 'null' : bytesToHex ( key ) } ` , [ 'put ' ] )
214
214
if ( this . _opts . useRootPersistence && equalsBytes ( key , ROOT_DB_KEY ) === true ) {
215
215
throw new Error ( `Attempted to set '${ bytesToUtf8 ( ROOT_DB_KEY ) } ' key but it is not allowed.` )
216
216
}
@@ -272,7 +272,7 @@ export class Trie {
272
272
* @returns A Promise that resolves once value is deleted.
273
273
*/
274
274
async del ( key : Uint8Array , skipKeyTransform : boolean = false ) : Promise < void > {
275
- this . DEBUG && this . debug ( `Key: ${ bytesToHex ( key ) } ` , [ 'DEL ' ] )
275
+ this . DEBUG && this . debug ( `Key: ${ bytesToHex ( key ) } ` , [ 'del ' ] )
276
276
await this . _lock . acquire ( )
277
277
const appliedKey = skipKeyTransform ? key : this . appliedKey ( key )
278
278
const { node, stack } = await this . findPath ( appliedKey )
@@ -331,7 +331,7 @@ export class Trie {
331
331
stack [ i ] = partialPath . stack [ i ]
332
332
progress += stack [ i ] instanceof BranchNode ? 1 : ( < ExtensionNode > stack [ i ] ) . keyLength ( )
333
333
}
334
- this . DEBUG && this . debug ( `Target (${ targetKey . length } ): [${ targetKey } ]` , [ 'FIND_PATH ' ] )
334
+ this . DEBUG && this . debug ( `Target (${ targetKey . length } ): [${ targetKey } ]` , [ 'find_path ' ] )
335
335
let result : Path | null = null
336
336
337
337
const onFound : FoundNodeFunction = async ( _ , node , keyProgress , walkController ) => {
@@ -343,19 +343,25 @@ export class Trie {
343
343
const branchIndex = targetKey [ progress ]
344
344
this . DEBUG &&
345
345
this . debug ( `Looking for node on branch index: [${ branchIndex } ]` , [
346
- 'FIND_PATH ' ,
347
- 'BranchNode ' ,
346
+ 'find_path ' ,
347
+ 'branch_node ' ,
348
348
] )
349
349
const branchNode = node . getBranch ( branchIndex )
350
- this . DEBUG &&
351
- this . debug (
352
- branchNode === null
353
- ? 'NULL'
354
- : branchNode instanceof Uint8Array
350
+
351
+ if ( this . DEBUG ) {
352
+ let debugString : string
353
+ if ( branchNode === null ) {
354
+ debugString = 'NULL'
355
+ } else {
356
+ debugString = `Branch index: ${ branchIndex . toString ( ) } - `
357
+ debugString +=
358
+ branchNode instanceof Uint8Array
355
359
? `NodeHash: ${ bytesToHex ( branchNode ) } `
356
- : `Raw_Node: ${ branchNode . toString ( ) } ` ,
357
- [ 'FIND_PATH' , 'BranchNode' , branchIndex . toString ( ) ] ,
358
- )
360
+ : `Raw_Node: ${ branchNode ! . toString ( ) } `
361
+ }
362
+
363
+ this . debug ( debugString , [ 'find_path' , 'branch_node' ] )
364
+ }
359
365
if ( ! branchNode ) {
360
366
result = { node : null , remaining : targetKey . slice ( progress ) , stack }
361
367
} else {
@@ -388,11 +394,11 @@ export class Trie {
388
394
node . key ( ) . toString ( )
389
395
} ]
390
396
` ,
391
- [ 'FIND_PATH ' , 'ExtensionNode ' ] ,
397
+ [ 'find_path ' , 'extension_node ' ] ,
392
398
)
393
399
const _progress = progress
394
400
for ( const k of node . key ( ) ) {
395
- this . DEBUG && this . debug ( `NextNode: ${ node . value ( ) } ` , [ 'FIND_PATH ' , 'ExtensionNode ' ] )
401
+ this . DEBUG && this . debug ( `NextNode: ${ node . value ( ) } ` , [ 'find_path ' , 'extension_node ' ] )
396
402
if ( k !== targetKey [ progress ] ) {
397
403
result = { node : null , remaining : targetKey . slice ( _progress ) , stack }
398
404
return
@@ -408,7 +414,7 @@ export class Trie {
408
414
this . DEBUG &&
409
415
this . debug (
410
416
`Walking trie from ${ startingNode === undefined ? 'ROOT' : 'NODE' } : ${ bytesToHex ( start ) } ` ,
411
- [ 'FIND_PATH ' ] ,
417
+ [ 'find_path ' ] ,
412
418
)
413
419
await this . walkTrie ( start , onFound )
414
420
} catch ( error : any ) {
@@ -425,7 +431,7 @@ export class Trie {
425
431
result . node !== null
426
432
? `Target Node FOUND for ${ bytesToNibbles ( key ) } `
427
433
: `Target Node NOT FOUND` ,
428
- [ 'FIND_PATH ' ] ,
434
+ [ 'find_path ' ] ,
429
435
)
430
436
431
437
result . stack = result . stack . filter ( ( e ) => e !== undefined )
@@ -436,7 +442,7 @@ export class Trie {
436
442
|| Remaining: [${ result . remaining } ]\n|| Stack: ${ result . stack
437
443
. map ( ( e ) => e . constructor . name )
438
444
. join ( ', ' ) } `,
439
- [ 'FIND_PATH ' ] ,
445
+ [ 'find_path ' ] ,
440
446
)
441
447
return result
442
448
}
@@ -503,10 +509,10 @@ export class Trie {
503
509
async lookupNode ( node : Uint8Array | Uint8Array [ ] ) : Promise < TrieNode > {
504
510
if ( isRawNode ( node ) ) {
505
511
const decoded = decodeRawNode ( node )
506
- this . DEBUG && this . debug ( `${ decoded . constructor . name } ` , [ 'LOOKUP_NODE ' , 'RAW_NODE ' ] )
512
+ this . DEBUG && this . debug ( `${ decoded . constructor . name } ` , [ 'lookup_node ' , 'raw_node ' ] )
507
513
return decoded
508
514
}
509
- this . DEBUG && this . debug ( `${ `${ bytesToHex ( node ) } ` } ` , [ 'LOOKUP_NODE ' , 'BY_HASH ' ] )
515
+ this . DEBUG && this . debug ( `${ `${ bytesToHex ( node ) } ` } ` , [ 'lookup_node ' , 'by_hash ' ] )
510
516
const key = this . _opts . keyPrefix ? concatBytes ( this . _opts . keyPrefix , node ) : node
511
517
const value = ( await this . _db . get ( key ) ) ?? null
512
518
@@ -516,7 +522,7 @@ export class Trie {
516
522
}
517
523
518
524
const decoded = decodeNode ( value )
519
- this . DEBUG && this . debug ( `${ decoded . constructor . name } found in DB` , [ 'LOOKUP_NODE ' , 'BY_HASH ' ] )
525
+ this . DEBUG && this . debug ( `${ decoded . constructor . name } found in DB` , [ 'lookup_node ' , 'by_hash ' ] )
520
526
return decoded
521
527
}
522
528
@@ -965,7 +971,7 @@ export class Trie {
965
971
`Persisting root: \n|| RootHash: ${ bytesToHex ( this . root ( ) ) } \n|| RootKey: ${ bytesToHex (
966
972
this . appliedKey ( ROOT_DB_KEY ) ,
967
973
) } `,
968
- [ 'PERSIST_ROOT ' ] ,
974
+ [ 'persist_root ' ] ,
969
975
)
970
976
let key = this . appliedKey ( ROOT_DB_KEY )
971
977
key = this . _opts . keyPrefix ? concatBytes ( this . _opts . keyPrefix , key ) : key
@@ -1020,7 +1026,7 @@ export class Trie {
1020
1026
* After this is called, all changes can be reverted until `commit` is called.
1021
1027
*/
1022
1028
checkpoint ( ) {
1023
- this . DEBUG && this . debug ( `${ bytesToHex ( this . root ( ) ) } ` , [ 'CHECKPOINT ' ] )
1029
+ this . DEBUG && this . debug ( `${ bytesToHex ( this . root ( ) ) } ` , [ 'checkpoint ' ] )
1024
1030
this . _db . checkpoint ( this . root ( ) )
1025
1031
}
1026
1032
@@ -1033,7 +1039,7 @@ export class Trie {
1033
1039
if ( ! this . hasCheckpoints ( ) ) {
1034
1040
throw new Error ( 'trying to commit when not checkpointed' )
1035
1041
}
1036
- this . DEBUG && this . debug ( `${ bytesToHex ( this . root ( ) ) } ` , [ 'COMMIT ' ] )
1042
+ this . DEBUG && this . debug ( `${ bytesToHex ( this . root ( ) ) } ` , [ 'commit ' ] )
1037
1043
await this . _lock . acquire ( )
1038
1044
await this . _db . commit ( )
1039
1045
await this . persistRoot ( )
@@ -1050,20 +1056,20 @@ export class Trie {
1050
1056
throw new Error ( 'trying to revert when not checkpointed' )
1051
1057
}
1052
1058
1053
- this . DEBUG && this . debug ( `${ bytesToHex ( this . root ( ) ) } ` , [ 'REVERT ' , 'BEFORE ' ] )
1059
+ this . DEBUG && this . debug ( `${ bytesToHex ( this . root ( ) ) } ` , [ 'revert ' , 'before ' ] )
1054
1060
await this . _lock . acquire ( )
1055
1061
this . root ( await this . _db . revert ( ) )
1056
1062
await this . persistRoot ( )
1057
1063
this . _lock . release ( )
1058
- this . DEBUG && this . debug ( `${ bytesToHex ( this . root ( ) ) } ` , [ 'REVERT ' , 'AFTER ' ] )
1064
+ this . DEBUG && this . debug ( `${ bytesToHex ( this . root ( ) ) } ` , [ 'revert ' , 'after ' ] )
1059
1065
}
1060
1066
1061
1067
/**
1062
1068
* Flushes all checkpoints, restoring the initial checkpoint state.
1063
1069
*/
1064
1070
flushCheckpoints ( ) {
1065
1071
this . DEBUG &&
1066
- this . debug ( `Deleting ${ this . _db . checkpoints . length } checkpoints.` , [ 'FLUSH_CHECKPOINTS ' ] )
1072
+ this . debug ( `Deleting ${ this . _db . checkpoints . length } checkpoints.` , [ 'flush_checkpoints ' ] )
1067
1073
this . _db . checkpoints = [ ]
1068
1074
}
1069
1075
0 commit comments