Skip to content

Commit 58e68b9

Browse files
am1r021holgerd77acolytec3
authored
Debug logger namespace standardization (#3692)
* trie: Make debug logger tags use lower case naming * verkle: Make debug logger tags use lower case naming * trie: Update debug namespaces in README * trie: Update debug examples to use lower case naming * verkle: Update debug namespaces in README * verkle: Update debug examples in README to use lower case naming * trie: Print hex instead of bytes to console * Change root debug namespaces to include a collon-hash to be included in wild-card namespaces * Make base namespace end with colon-hash in docs and READMEs * Do not calculate canoncial header if DEBUG is not set * Fix spelling issue * Fix spelling mistakes * Make fetcher namespace use collon-pound * Remove unnecessary check * Add verkle core namespace option to README * Update debug option description * Fix spelling mistake in example * Update trie README debug namespaces * Add proof symbols to debug options list * Make proof-related debug tags lowercase * Put branch index in debug message and out of namespace * Update verkle README debug namespaces * Convert bytes before logging them * Expand debug string logic and add branch index to it * Do not convert bytes to hex --------- Co-authored-by: Holger Drewes <[email protected]> Co-authored-by: acolytec3 <[email protected]>
1 parent 15f8ff2 commit 58e68b9

File tree

11 files changed

+107
-82
lines changed

11 files changed

+107
-82
lines changed

packages/blockchain/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ The following initial logger is currently available:
247247

248248
| Logger | Description |
249249
| ------------------- | ------------------------------------------------------------------------ |
250-
| `blockchain` | Core blockchain operations like when a block or header is put or deleted |
250+
| `blockchain:#` | Core blockchain operations like when a block or header is put or deleted |
251251
| `blockchain:clique` | Clique consensus operations like updating the vote and/or signer list |
252252
| `blockchain:ethash` | Ethash consensus operations like PoW block or header validation |
253253

packages/blockchain/src/blockchain.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class Blockchain implements BlockchainInterface {
107107
constructor(opts: BlockchainOptions = {}) {
108108
this.DEBUG =
109109
typeof window === 'undefined' ? (process?.env?.DEBUG?.includes('ethjs') ?? false) : false
110-
this._debug = debugDefault('blockchain')
110+
this._debug = debugDefault('blockchain:#')
111111

112112
if (opts.common) {
113113
this.common = opts.common
@@ -327,7 +327,10 @@ export class Blockchain implements BlockchainInterface {
327327

328328
async resetCanonicalHead(canonicalHead: bigint) {
329329
let hash: Uint8Array | undefined
330-
const canonicalHeadHash = (await this.getCanonicalHeadHeader()).hash()
330+
let canonicalHeadHash: Uint8Array | undefined
331+
if (this.DEBUG) {
332+
canonicalHeadHash = (await this.getCanonicalHeadHeader()).hash()
333+
}
331334
await this.runWithLock<void>(async () => {
332335
hash = await this.dbManager.numberToHash(canonicalHead)
333336
if (hash === undefined) {
@@ -352,7 +355,7 @@ export class Blockchain implements BlockchainInterface {
352355

353356
this.DEBUG &&
354357
this._debug(
355-
`Canonical head set from ${bytesToHex(canonicalHeadHash)} to ${bytesToHex(hash!)} (number ${bigIntToHex(canonicalHead)})`,
358+
`Canonical head set from ${bytesToHex(canonicalHeadHash!)} to ${bytesToHex(hash!)} (number ${bigIntToHex(canonicalHead)})`,
356359
)
357360

358361
this._deletedBlocks = []

packages/blockchain/src/constructors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { Chain } from '@ethereumjs/common'
1717

1818
const DEBUG =
1919
typeof window === 'undefined' ? (process?.env?.DEBUG?.includes('ethjs') ?? false) : false
20-
const debug = debugDefault('blockchain')
20+
const debug = debugDefault('blockchain:#')
2121

2222
export async function createBlockchain(opts: BlockchainOptions = {}) {
2323
const blockchain = new Blockchain(opts)

packages/client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ The following options are available:
466466
467467
| Logger | Description |
468468
| ------------------- | ------------------------------------------------ |
469-
| `client:fetcher` | This option enables logging for all fetchers |
469+
| `client:fetcher:#` | This option enables logging for core fetcher operations such as job scheduling |
470470
| `client:fetcher:bytecode` | This option enables logging for the snap sync bytecode fetcher |
471471
| `client:fetcher:storage` | This option enables logging for the snap sync storage fetcher |
472472
| `client:fetcher:trienode` | This option enables logging for the snap sync trienode fetcher |

packages/client/src/sync/fetcher/fetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
8282
typeof window === 'undefined' ? (process?.env?.DEBUG?.includes('ethjs') ?? false) : false
8383

8484
this.config = options.config
85-
this.debug = debug('client:fetcher')
85+
this.debug = debug('client:fetcher:#')
8686

8787
this.pool = options.pool
8888
this.timeout = options.timeout ?? 8000

packages/devp2p/src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { secp256k1 } from 'ethereum-cryptography/secp256k1.js'
77
import type { ETH } from './protocol/eth.js'
88
import type { LES } from './protocol/les.js'
99

10-
export const devp2pDebug = debug('devp2p')
10+
export const devp2pDebug = debug('devp2p:#')
1111

1212
export function genPrivateKey(): Uint8Array {
1313
const privateKey = secp256k1.utils.randomPrivateKey()

packages/trie/README.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,26 @@ The `Trie` class features optional debug logging. Individual debug selections ca
341341

342342
The following options are available:
343343

344-
| Logger | Description |
345-
| ----------------- | ---------------------------------------------- |
346-
| `trie` | minimal info logging for all trie methods |
347-
| `trie:<METHOD>` | debug logging for specific trie method |
348-
| `trie:<METHOD>:*` | verbose debug logging for specific trie method |
349-
| `trie:*` | verbose debug logging for all trie methods |
344+
| Logger | Description |
345+
| --------------------------------- | -------------------------------------------------------------------- |
346+
| `trie:#` | minimal info logging for all trie methods |
347+
| `trie:#:put` | a trie put operation has occurred |
348+
| `trie:#:get` | a trie get operation has occurred |
349+
| `trie:#:del` | a trie del operation has occurred |
350+
| `trie:#:find_path` | a node is being searched for |
351+
| `trie:#:find_path:branch_node` | a branch node has been found during a node search |
352+
| `trie:#:find_path:extension_node` | an extension node has been found during a node search |
353+
| `trie:#:lookup_node` | node lookup operations |
354+
| `trie:#:lookup_node:raw_node` | node lookup operations that have hit a raw node |
355+
| `trie:#:lookup_node:by_hash` | node lookup operations that have hit a node hash |
356+
| `trie:#:persist_root` | operations writing the state root to the disk |
357+
| `trie:#:checkpoint` | checkpoint operations |
358+
| `trie:#:commit` | operations committing checkpoints to the disk |
359+
| `trie:#:revert:before` | the stateRoot before reverting committed checkpoints |
360+
| `trie:#:revert:after` | the stateRoot after reverting committed checkpoints |
361+
| `trie:#:flush_checkpoints` | checkpoints are being flushed |
362+
| `trie:#:from_proof` | a trie has been updated from a proof using updateTrieFromMerkleProof |
363+
| `trie:#:create_proof` | a merkle proof has been created using updateTrieFromMerkleProof |
350364

351365
To observe the logging in action at different levels:
352366

@@ -359,25 +373,25 @@ DEBUG=ethjs,trie npx vitest test/util/log.spec.ts
359373
Run with **put** method logging:
360374

361375
```shell
362-
DEBUG=ethjs,trie:PUT npx vitest test/util/log.spec.ts
376+
DEBUG=ethjs,trie:put npx vitest test/util/log.spec.ts
363377
```
364378

365379
Run with **trie** + **put**/**get**/**del** logging:
366380

367381
```shell
368-
DEBUG=ethjs,trie,trie:PUT,trie:GET,trie:DEL npx vitest test/util/log.spec.ts
382+
DEBUG=ethjs,trie,trie:put,trie:get,trie:del npx vitest test/util/log.spec.ts
369383
```
370384

371385
Run with **findPath** debug logging:
372386

373387
```shell
374-
DEBUG=ethjs,trie:FIND_PATH npx vitest test/util/log.spec.ts
388+
DEBUG=ethjs,trie:find_path npx vitest test/util/log.spec.ts
375389
```
376390

377391
Run with **findPath** verbose logging:
378392

379393
```shell
380-
DEBUG=ethjs,trie:FIND_PATH:* npx vitest test/util/log.spec.ts
394+
DEBUG=ethjs,trie:find_path:* npx vitest test/util/log.spec.ts
381395
```
382396

383397
Run with max logging:
@@ -389,7 +403,7 @@ DEBUG=ethjs,trie:* npx vitest test/util/log.spec.ts
389403
`ethjs` **must** be included in the `DEBUG` environment variables to enable **any** logs.
390404
Additional log selections can be added with a comma separated list (no spaces). Logs with extensions can be enabled with a colon `:`, and `*` can be used to include all extensions.
391405

392-
`DEBUG=ethjs,tie:PUT,trie:FIND_PATH:* npx vitest test/proof.spec.ts`
406+
`DEBUG=ethjs,trie:put,trie:find_path:* npx vitest test/proof.spec.ts`
393407

394408
## References
395409

packages/trie/src/proof/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ export function verifyTrieRangeProof(
7272
* @param key key to create a proof for
7373
*/
7474
export async function createMerkleProof(trie: Trie, key: Uint8Array): Promise<Proof> {
75-
trie['DEBUG'] && trie['debug'](`Creating Proof for Key: ${bytesToHex(key)}`, ['CREATE_PROOF'])
75+
trie['DEBUG'] && trie['debug'](`Creating Proof for Key: ${bytesToHex(key)}`, ['create_proof'])
7676
const { stack } = await trie.findPath(trie['appliedKey'](key))
7777
const p = stack.map((stackElem) => {
7878
return stackElem.serialize()
7979
})
80-
trie['DEBUG'] && trie['debug'](`Proof created with (${stack.length}) nodes`, ['CREATE_PROOF'])
80+
trie['DEBUG'] && trie['debug'](`Proof created with (${stack.length}) nodes`, ['create_proof'])
8181
return p
8282
}
8383

@@ -94,7 +94,7 @@ export async function updateTrieFromMerkleProof(
9494
proof: Proof,
9595
shouldVerifyRoot: boolean = false,
9696
) {
97-
trie['DEBUG'] && trie['debug'](`Saving (${proof.length}) proof nodes in DB`, ['FROM_PROOF'])
97+
trie['DEBUG'] && trie['debug'](`Saving (${proof.length}) proof nodes in DB`, ['from_proof'])
9898
const opStack = proof.map((nodeValue) => {
9999
let key = Uint8Array.from(trie['hash'](nodeValue))
100100
key = trie['_opts'].keyPrefix ? concatBytes(trie['_opts'].keyPrefix, key) : key

packages/trie/src/trie.ts

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class Trie {
7272

7373
/** Debug logging */
7474
protected DEBUG: boolean
75-
protected _debug: Debugger = debug('trie')
75+
protected _debug: Debugger = debug('trie:#')
7676
protected debug: (...args: any) => void
7777

7878
/**
@@ -187,13 +187,13 @@ export class Trie {
187187
* @returns A Promise that resolves to `Uint8Array` if a value was found or `null` if no value was found.
188188
*/
189189
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'])
191191
const { node, remaining } = await this.findPath(this.appliedKey(key), throwIfMissing)
192192
let value: Uint8Array | null = null
193193
if (node && remaining.length === 0) {
194194
value = node.value()
195195
}
196-
this.DEBUG && this.debug(`Value: ${value === null ? 'null' : bytesToHex(value)}`, ['GET'])
196+
this.DEBUG && this.debug(`Value: ${value === null ? 'null' : bytesToHex(value)}`, ['get'])
197197
return value
198198
}
199199

@@ -209,8 +209,8 @@ export class Trie {
209209
value: Uint8Array | null,
210210
skipKeyTransform: boolean = false,
211211
): 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'])
214214
if (this._opts.useRootPersistence && equalsBytes(key, ROOT_DB_KEY) === true) {
215215
throw new Error(`Attempted to set '${bytesToUtf8(ROOT_DB_KEY)}' key but it is not allowed.`)
216216
}
@@ -272,7 +272,7 @@ export class Trie {
272272
* @returns A Promise that resolves once value is deleted.
273273
*/
274274
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'])
276276
await this._lock.acquire()
277277
const appliedKey = skipKeyTransform ? key : this.appliedKey(key)
278278
const { node, stack } = await this.findPath(appliedKey)
@@ -331,7 +331,7 @@ export class Trie {
331331
stack[i] = partialPath.stack[i]
332332
progress += stack[i] instanceof BranchNode ? 1 : (<ExtensionNode>stack[i]).keyLength()
333333
}
334-
this.DEBUG && this.debug(`Target (${targetKey.length}): [${targetKey}]`, ['FIND_PATH'])
334+
this.DEBUG && this.debug(`Target (${targetKey.length}): [${targetKey}]`, ['find_path'])
335335
let result: Path | null = null
336336

337337
const onFound: FoundNodeFunction = async (_, node, keyProgress, walkController) => {
@@ -343,19 +343,25 @@ export class Trie {
343343
const branchIndex = targetKey[progress]
344344
this.DEBUG &&
345345
this.debug(`Looking for node on branch index: [${branchIndex}]`, [
346-
'FIND_PATH',
347-
'BranchNode',
346+
'find_path',
347+
'branch_node',
348348
])
349349
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
355359
? `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+
}
359365
if (!branchNode) {
360366
result = { node: null, remaining: targetKey.slice(progress), stack }
361367
} else {
@@ -388,11 +394,11 @@ export class Trie {
388394
node.key().toString()
389395
}]
390396
`,
391-
['FIND_PATH', 'ExtensionNode'],
397+
['find_path', 'extension_node'],
392398
)
393399
const _progress = progress
394400
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'])
396402
if (k !== targetKey[progress]) {
397403
result = { node: null, remaining: targetKey.slice(_progress), stack }
398404
return
@@ -408,7 +414,7 @@ export class Trie {
408414
this.DEBUG &&
409415
this.debug(
410416
`Walking trie from ${startingNode === undefined ? 'ROOT' : 'NODE'}: ${bytesToHex(start)}`,
411-
['FIND_PATH'],
417+
['find_path'],
412418
)
413419
await this.walkTrie(start, onFound)
414420
} catch (error: any) {
@@ -425,7 +431,7 @@ export class Trie {
425431
result.node !== null
426432
? `Target Node FOUND for ${bytesToNibbles(key)}`
427433
: `Target Node NOT FOUND`,
428-
['FIND_PATH'],
434+
['find_path'],
429435
)
430436

431437
result.stack = result.stack.filter((e) => e !== undefined)
@@ -436,7 +442,7 @@ export class Trie {
436442
|| Remaining: [${result.remaining}]\n|| Stack: ${result.stack
437443
.map((e) => e.constructor.name)
438444
.join(', ')}`,
439-
['FIND_PATH'],
445+
['find_path'],
440446
)
441447
return result
442448
}
@@ -503,10 +509,10 @@ export class Trie {
503509
async lookupNode(node: Uint8Array | Uint8Array[]): Promise<TrieNode> {
504510
if (isRawNode(node)) {
505511
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'])
507513
return decoded
508514
}
509-
this.DEBUG && this.debug(`${`${bytesToHex(node)}`}`, ['LOOKUP_NODE', 'BY_HASH'])
515+
this.DEBUG && this.debug(`${`${bytesToHex(node)}`}`, ['lookup_node', 'by_hash'])
510516
const key = this._opts.keyPrefix ? concatBytes(this._opts.keyPrefix, node) : node
511517
const value = (await this._db.get(key)) ?? null
512518

@@ -516,7 +522,7 @@ export class Trie {
516522
}
517523

518524
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'])
520526
return decoded
521527
}
522528

@@ -965,7 +971,7 @@ export class Trie {
965971
`Persisting root: \n|| RootHash: ${bytesToHex(this.root())}\n|| RootKey: ${bytesToHex(
966972
this.appliedKey(ROOT_DB_KEY),
967973
)}`,
968-
['PERSIST_ROOT'],
974+
['persist_root'],
969975
)
970976
let key = this.appliedKey(ROOT_DB_KEY)
971977
key = this._opts.keyPrefix ? concatBytes(this._opts.keyPrefix, key) : key
@@ -1020,7 +1026,7 @@ export class Trie {
10201026
* After this is called, all changes can be reverted until `commit` is called.
10211027
*/
10221028
checkpoint() {
1023-
this.DEBUG && this.debug(`${bytesToHex(this.root())}`, ['CHECKPOINT'])
1029+
this.DEBUG && this.debug(`${bytesToHex(this.root())}`, ['checkpoint'])
10241030
this._db.checkpoint(this.root())
10251031
}
10261032

@@ -1033,7 +1039,7 @@ export class Trie {
10331039
if (!this.hasCheckpoints()) {
10341040
throw new Error('trying to commit when not checkpointed')
10351041
}
1036-
this.DEBUG && this.debug(`${bytesToHex(this.root())}`, ['COMMIT'])
1042+
this.DEBUG && this.debug(`${bytesToHex(this.root())}`, ['commit'])
10371043
await this._lock.acquire()
10381044
await this._db.commit()
10391045
await this.persistRoot()
@@ -1050,20 +1056,20 @@ export class Trie {
10501056
throw new Error('trying to revert when not checkpointed')
10511057
}
10521058

1053-
this.DEBUG && this.debug(`${bytesToHex(this.root())}`, ['REVERT', 'BEFORE'])
1059+
this.DEBUG && this.debug(`${bytesToHex(this.root())}`, ['revert', 'before'])
10541060
await this._lock.acquire()
10551061
this.root(await this._db.revert())
10561062
await this.persistRoot()
10571063
this._lock.release()
1058-
this.DEBUG && this.debug(`${bytesToHex(this.root())}`, ['REVERT', 'AFTER'])
1064+
this.DEBUG && this.debug(`${bytesToHex(this.root())}`, ['revert', 'after'])
10591065
}
10601066

10611067
/**
10621068
* Flushes all checkpoints, restoring the initial checkpoint state.
10631069
*/
10641070
flushCheckpoints() {
10651071
this.DEBUG &&
1066-
this.debug(`Deleting ${this._db.checkpoints.length} checkpoints.`, ['FLUSH_CHECKPOINTS'])
1072+
this.debug(`Deleting ${this._db.checkpoints.length} checkpoints.`, ['flush_checkpoints'])
10671073
this._db.checkpoints = []
10681074
}
10691075

0 commit comments

Comments
 (0)