Skip to content

Commit

Permalink
Util, Block: added Util error module, first exemplary BlockHeader err…
Browse files Browse the repository at this point in the history
…or implementation
  • Loading branch information
holgerd77 authored and jochem-brouwer committed Sep 30, 2024
1 parent 58e68b9 commit 95b6b8b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/block/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { EthereumJSError } from '@ethereumjs/util'

export enum HeaderValidationErrorCode {
WRONG_TX_TRIE_LENGTH = 'WRONG_TX_TRIE_LENGTH',
}

export type HeaderValidationErrorType = {
block: string
received: string
}

export class HeaderValidationError extends EthereumJSError<HeaderValidationErrorType> {}
12 changes: 9 additions & 3 deletions packages/block/src/header/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
CLIQUE_EXTRA_VANITY,
cliqueIsEpochTransition,
} from '../consensus/clique.js'
import { HeaderValidationError, HeaderValidationErrorCode } from '../errors.js'
import { fakeExponential } from '../helpers.js'
import { paramsBlock } from '../params.js'

Expand Down Expand Up @@ -267,10 +268,15 @@ export class BlockHeader {
throw new Error(msg)
}
if (transactionsTrie.length !== 32) {
const msg = this._errorMsg(
`transactionsTrie must be 32 bytes, received ${transactionsTrie.length} bytes`,
const e = new HeaderValidationError(
'transactionsTrie must be 32 bytes',
HeaderValidationErrorCode.WRONG_TX_TRIE_LENGTH,
{
block: this.errorStr(),
received: `${bytesToHex(transactionsTrie)} (${transactionsTrie.length} bytes)`,
},
)
throw new Error(msg)
throw e
}
if (receiptTrie.length !== 32) {
const msg = this._errorMsg(
Expand Down
30 changes: 30 additions & 0 deletions packages/util/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Generic EthereumJS error with metadata attached
*
* Kudos to https://github.com/ChainSafe/lodestar monorepo
* for the inspiration :-)
*/
export class EthereumJSError<T extends {}> extends Error {
code: string
errorContext: T

constructor(msg: string, code: string, errorContext: T) {
super(msg)
this.code = code
this.errorContext = errorContext
}

getErrorContext(): Record<string, string | number | null> {
return { code: this.code, ...this.errorContext }
}

/**
* Get the metadata and the stacktrace for the error.
*/
toObject(): Record<string, string | number | null> {
return {
...this.getErrorContext(),
stack: this.stack ?? '',
}
}
}
5 changes: 5 additions & 0 deletions packages/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export * from './db.js'
*/
export * from './withdrawal.js'

/**
* EthereumJS Extended Errors
*/
export * from './errors.js'

/**
* ECDSA signature
*/
Expand Down

0 comments on commit 95b6b8b

Please sign in to comment.