Skip to content

Commit a738424

Browse files
committed
block -> error handling: added BlockHeader._error() helper function to annotate errors
1 parent 2ea6761 commit a738424

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

packages/block/src/block.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ export class Block {
263263
async validateData(): Promise<void> {
264264
const txErrors = this.validateTransactions(true)
265265
if (txErrors.length > 0) {
266-
throw new Error(`invalid transactions: ${txErrors.join(' ')}`)
266+
const msg = `invalid transactions: ${txErrors.join(' ')}`
267+
throw this.header._error(msg)
267268
}
268269

269270
const validateTxTrie = await this.validateTransactionsTrie()
@@ -358,6 +359,16 @@ export class Block {
358359
}
359360
}
360361

362+
/**
363+
* Internal helper function to create an annotated error message
364+
*
365+
* @param msg Base error message
366+
* @hidden
367+
*/
368+
_error(msg: string) {
369+
return this.header._error(msg)
370+
}
371+
361372
/**
362373
* The following rules are checked in this method:
363374
* Uncle Header is a valid header.

packages/block/src/header.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class BlockHeader {
4646
public readonly nonce: Buffer
4747

4848
public readonly _common: Common
49+
public _errorPostfix = ''
4950

5051
public static fromHeaderData(headerData: HeaderData = {}, opts?: BlockOptions) {
5152
const {
@@ -245,6 +246,10 @@ export class BlockHeader {
245246
this.extraData = this.cliqueSealBlock(options.cliqueSigner)
246247
}
247248

249+
this._errorPostfix = `block number=${this.number.toNumber()} hash=${this.hash().toString(
250+
'hex'
251+
)}`
252+
248253
const freeze = options?.freeze ?? true
249254
if (freeze) {
250255
Object.freeze(this)
@@ -451,46 +456,41 @@ export class BlockHeader {
451456
return
452457
}
453458
const hardfork = this._getHardfork()
454-
const errorPostfix = `(block number=${this.number.toNumber()} hash=${this.hash().toString(
455-
'hex'
456-
)})`
457459
if (this._common.consensusAlgorithm() !== 'clique') {
458460
if (
459461
this.extraData.length > this._common.paramByHardfork('vm', 'maxExtraDataSize', hardfork)
460462
) {
461-
throw new Error('invalid amount of extra data')
463+
const msg = 'invalid amount of extra data'
464+
throw this._error(msg)
462465
}
463466
} else {
464467
const minLength = CLIQUE_EXTRA_VANITY + CLIQUE_EXTRA_SEAL
465468
if (!this.cliqueIsEpochTransition()) {
466469
// ExtraData length on epoch transition
467470
if (this.extraData.length !== minLength) {
468-
throw new Error(
469-
`extraData must be ${minLength} bytes on non-epoch transition blocks, received ${this.extraData.length} bytes ${errorPostfix}`
470-
)
471+
const msg = `extraData must be ${minLength} bytes on non-epoch transition blocks, received ${this.extraData.length} bytes`
472+
throw this._error(msg)
471473
}
472474
} else {
473475
const signerLength = this.extraData.length - minLength
474476
if (signerLength % 20 !== 0) {
475-
throw new Error(
476-
`invalid signer list length in extraData, received signer length of ${signerLength} (not divisible by 20) ${errorPostfix}`
477-
)
477+
const msg = `invalid signer list length in extraData, received signer length of ${signerLength} (not divisible by 20)`
478+
throw this._error(msg)
478479
}
479480
// coinbase (beneficiary) on epoch transition
480481
if (!this.coinbase.isZero()) {
481-
throw new Error(
482-
`coinbase must be filled with zeros on epoch transition blocks, received ${this.coinbase.toString()} ${errorPostfix}`
483-
)
482+
const msg = `coinbase must be filled with zeros on epoch transition blocks, received ${this.coinbase.toString()}`
483+
throw this._error(msg)
484484
}
485485
}
486486
// MixHash format
487487
if (!this.mixHash.equals(Buffer.alloc(32))) {
488-
throw new Error(
489-
`mixHash must be filled with zeros, received ${this.mixHash} ${errorPostfix}`
490-
)
488+
const msg = `mixHash must be filled with zeros, received ${this.mixHash}`
489+
throw this._error(msg)
491490
}
492491
if (!this.validateCliqueDifficulty(blockchain)) {
493-
throw new Error(`invalid clique difficulty ${errorPostfix}`)
492+
const msg = `invalid clique difficulty`
493+
throw this._error(msg)
494494
}
495495
}
496496

@@ -722,6 +722,18 @@ export class BlockHeader {
722722
}
723723
}
724724

725+
/**
726+
* Internal helper function to create an annotated error message
727+
*
728+
* @param msg Base error message
729+
* @hidden
730+
*/
731+
_error(msg: string) {
732+
msg += ` (${this._errorPostfix})`
733+
const e = new Error(msg)
734+
return e
735+
}
736+
725737
private _getHardfork(): string {
726738
return this._common.hardfork() || this._common.activeHardfork(this.number.toNumber())
727739
}

packages/block/test/header.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ tape('[Block]: Header functions', function (t) {
139139
await header.validate(blockchain)
140140
st.fail(testCase)
141141
} catch (error) {
142-
st.equal(error.message, 'invalid amount of extra data', testCase)
142+
st.ok(error.message.includes('invalid amount of extra data'), testCase)
143143
}
144144

145145
// PoA

0 commit comments

Comments
 (0)