@@ -46,6 +46,7 @@ export class BlockHeader {
46
46
public readonly nonce : Buffer
47
47
48
48
public readonly _common : Common
49
+ public _errorPostfix = ''
49
50
50
51
public static fromHeaderData ( headerData : HeaderData = { } , opts ?: BlockOptions ) {
51
52
const {
@@ -245,6 +246,10 @@ export class BlockHeader {
245
246
this . extraData = this . cliqueSealBlock ( options . cliqueSigner )
246
247
}
247
248
249
+ this . _errorPostfix = `block number=${ this . number . toNumber ( ) } hash=${ this . hash ( ) . toString (
250
+ 'hex'
251
+ ) } `
252
+
248
253
const freeze = options ?. freeze ?? true
249
254
if ( freeze ) {
250
255
Object . freeze ( this )
@@ -451,46 +456,41 @@ export class BlockHeader {
451
456
return
452
457
}
453
458
const hardfork = this . _getHardfork ( )
454
- const errorPostfix = `(block number=${ this . number . toNumber ( ) } hash=${ this . hash ( ) . toString (
455
- 'hex'
456
- ) } )`
457
459
if ( this . _common . consensusAlgorithm ( ) !== 'clique' ) {
458
460
if (
459
461
this . extraData . length > this . _common . paramByHardfork ( 'vm' , 'maxExtraDataSize' , hardfork )
460
462
) {
461
- throw new Error ( 'invalid amount of extra data' )
463
+ const msg = 'invalid amount of extra data'
464
+ throw this . _error ( msg )
462
465
}
463
466
} else {
464
467
const minLength = CLIQUE_EXTRA_VANITY + CLIQUE_EXTRA_SEAL
465
468
if ( ! this . cliqueIsEpochTransition ( ) ) {
466
469
// ExtraData length on epoch transition
467
470
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 )
471
473
}
472
474
} else {
473
475
const signerLength = this . extraData . length - minLength
474
476
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 )
478
479
}
479
480
// coinbase (beneficiary) on epoch transition
480
481
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 )
484
484
}
485
485
}
486
486
// MixHash format
487
487
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 )
491
490
}
492
491
if ( ! this . validateCliqueDifficulty ( blockchain ) ) {
493
- throw new Error ( `invalid clique difficulty ${ errorPostfix } ` )
492
+ const msg = `invalid clique difficulty`
493
+ throw this . _error ( msg )
494
494
}
495
495
}
496
496
@@ -722,6 +722,18 @@ export class BlockHeader {
722
722
}
723
723
}
724
724
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
+
725
737
private _getHardfork ( ) : string {
726
738
return this . _common . hardfork ( ) || this . _common . activeHardfork ( this . number . toNumber ( ) )
727
739
}
0 commit comments