Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions packages/mpt/src/mpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
concatBytes,
equalsBytes,
} from '@ethereumjs/util'
import debug from 'debug'
import { keccak256 } from 'ethereum-cryptography/keccak.js'

import { CheckpointDB } from './db/checkpointDB.ts'
Expand All @@ -29,14 +28,15 @@
decodeRawMPTNode,
isRawMPTNode,
} from './node/index.ts'
import { ROOT_DB_KEY } from './types.ts'
import { ConsoleLogger, ROOT_DB_KEY } from './types.ts'
import { _walkTrie } from './util/asyncWalk.ts'
import { bytesToNibbles, matchingNibbleLength, nibblesTypeToPackedBytes } from './util/nibbles.ts'
import { WalkController } from './util/walkController.ts'

import type {
BranchMPTNodeBranchValue,
FoundNodeFunction,
Logger,
MPTNode,
MPTOpts,
MPTOptsWithDefaults,
Expand All @@ -47,7 +47,6 @@
} from './types.ts'
import type { OnFound } from './util/asyncWalk.ts'
import type { BatchDBOp, DB } from '@ethereumjs/util'
import type { Debugger } from 'debug'

/**
* The basic trie interface, use with `import { MerklePatriciaTrie } from '@ethereumjs/mpt'`.
Expand All @@ -74,8 +73,8 @@

/** Debug logging */
protected DEBUG: boolean
protected _debug: Debugger = debug('mpt:#')
protected debug: (...args: any) => void
logger: Logger

/**
* Creates a new trie.
Expand Down Expand Up @@ -103,15 +102,20 @@
valueEncoding = ValueEncoding.Bytes
}

if (opts?.logger) {
// console.log('dbg100')
this.logger = opts.logger

Check warning on line 107 in packages/mpt/src/mpt.ts

View check run for this annotation

Codecov / codecov/patch

packages/mpt/src/mpt.ts#L107

Added line #L107 was not covered by tests
} else {
// console.log('dbg101')
this.logger = new ConsoleLogger()
}

this.DEBUG =
typeof window === 'undefined' ? (process?.env?.DEBUG?.includes('ethjs') ?? false) : false
this.debug = this.DEBUG
? (message: string, namespaces: string[] = []) => {
let log = this._debug
for (const name of namespaces) {
log = log.extend(name)
}
log(message)
//@ts-ignore
this.logger.debug(message + namespaces) // not sure what's wrong with typing here
}
: (..._: any) => {}

Expand Down
35 changes: 35 additions & 0 deletions packages/mpt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,42 @@

export type HashKeysFunction = (msg: Uint8Array) => Uint8Array

export interface Logger {
info(message: string, ...meta: any[]): void
warn(message: string, ...meta: any[]): void
error(message: string, ...meta: any[]): void
debug?(message: string, ...meta: any[]): void // Optional for production
trace?(message: string, ...meta: any[]): void // Optional deep debugging
}

export class ConsoleLogger implements Logger {
info(message: string, ...meta: any[]) {
console.info(`[INFO] ${message}`, ...meta)
}

Check warning on line 58 in packages/mpt/src/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/mpt/src/types.ts#L57-L58

Added lines #L57 - L58 were not covered by tests

warn(message: string, ...meta: any[]) {
console.warn(`[WARN] ${message}`, ...meta)
}

Check warning on line 62 in packages/mpt/src/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/mpt/src/types.ts#L61-L62

Added lines #L61 - L62 were not covered by tests

error(message: string, ...meta: any[]) {
console.error(`[ERROR] ${message}`, ...meta)
}

Check warning on line 66 in packages/mpt/src/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/mpt/src/types.ts#L65-L66

Added lines #L65 - L66 were not covered by tests

debug?(message: string, ...meta: any[]) {
if (process.env.DEBUG !== undefined) console.debug(`[DEBUG] ${message}`, ...meta)
}

trace?(message: string, ...meta: any[]) {
if (process.env.TRACE !== undefined) console.trace(`[TRACE] ${message}`, ...meta)
}

Check warning on line 74 in packages/mpt/src/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/mpt/src/types.ts#L73-L74

Added lines #L73 - L74 were not covered by tests
}

export interface MPTOpts {
/**
*
*/
logger?: Logger

/**
* A database instance.
*/
Expand Down
Loading