@@ -6,9 +6,12 @@ import {
66 BIGINT_1 ,
77 BIGINT_2 ,
88 BIGINT_7 ,
9+ ErrorCode ,
910 KECCAK256_RLP ,
1011 KECCAK256_RLP_ARRAY ,
1112 TypeOutput ,
13+ UsageError ,
14+ ValueError ,
1215 bigIntToHex ,
1316 bigIntToUnpaddedBytes ,
1417 bytesToHex ,
@@ -26,7 +29,6 @@ import {
2629 CLIQUE_EXTRA_VANITY ,
2730 cliqueIsEpochTransition ,
2831} from '../consensus/clique.js'
29- import { HeaderValidationError , ValidationErrorCode } from '../errors.js'
3032import { fakeExponential } from '../helpers.js'
3133import { paramsBlock } from '../params.js'
3234
@@ -77,10 +79,13 @@ export class BlockHeader {
7779 */
7880 get prevRandao ( ) {
7981 if ( ! this . common . isActivatedEIP ( 4399 ) ) {
80- const msg = this . _errorMsg (
82+ throw new UsageError (
8183 'The prevRandao parameter can only be accessed when EIP-4399 is activated' ,
84+ ErrorCode . EIP_NOT_ACTIVATED ,
85+ {
86+ objectContext : this . errorStr ( ) ,
87+ } ,
8288 )
83- throw new Error ( msg )
8489 }
8590 return this . mixHash
8691 }
@@ -181,7 +186,10 @@ export class BlockHeader {
181186 toType ( headerData . requestsRoot , TypeOutput . Uint8Array ) ?? hardforkDefaults . requestsRoot
182187
183188 if ( ! this . common . isActivatedEIP ( 1559 ) && baseFeePerGas !== undefined ) {
184- throw new Error ( 'A base fee for a block can only be set with EIP1559 being activated' )
189+ throw new UsageError (
190+ 'A base fee for a block can only be set with EIP1559 being activated' ,
191+ ErrorCode . EIP_NOT_ACTIVATED ,
192+ )
185193 }
186194
187195 if ( ! this . common . isActivatedEIP ( 4895 ) && withdrawalsRoot !== undefined ) {
@@ -260,38 +268,41 @@ export class BlockHeader {
260268 const { parentHash, stateRoot, transactionsTrie, receiptTrie, mixHash, nonce } = this
261269
262270 if ( parentHash . length !== 32 ) {
263- const msg = this . _errorMsg ( `parentHash must be 32 bytes, received ${ parentHash . length } bytes` )
264- throw new Error ( msg )
271+ throw new ValueError ( `parentHash must be 32 bytes` , ErrorCode . INVALID_VALUE_LENGTH , {
272+ objectContext : this . errorStr ( ) ,
273+ received : `${ parentHash . length } bytes` ,
274+ } )
265275 }
266276 if ( stateRoot . length !== 32 ) {
267- const msg = this . _errorMsg ( `stateRoot must be 32 bytes, received ${ stateRoot . length } bytes` )
268- throw new Error ( msg )
277+ throw new ValueError ( `stateRoot must be 32 bytes` , ErrorCode . INVALID_VALUE_LENGTH , {
278+ objectContext : this . errorStr ( ) ,
279+ received : `${ stateRoot . length } bytes` ,
280+ } )
269281 }
270282 if ( transactionsTrie . length !== 32 ) {
271- const e = new HeaderValidationError (
272- 'transactionsTrie must be 32 bytes' ,
273- ValidationErrorCode . WRONG_TX_TRIE_LENGTH ,
274- {
275- block : this . errorStr ( ) ,
276- received : `${ bytesToHex ( transactionsTrie ) } (${ transactionsTrie . length } bytes)` ,
277- } ,
278- )
279- throw e
283+ throw new ValueError ( 'transactionsTrie must be 32 bytes' , ErrorCode . INVALID_VALUE_LENGTH , {
284+ objectContext : this . errorStr ( ) ,
285+ received : `${ bytesToHex ( transactionsTrie ) } (${ transactionsTrie . length } bytes)` ,
286+ } )
280287 }
281288 if ( receiptTrie . length !== 32 ) {
282- const msg = this . _errorMsg (
283- `receiptTrie must be 32 bytes, received ${ receiptTrie . length } bytes` ,
284- )
285- throw new Error ( msg )
289+ throw new ValueError ( 'receiptTrie must be 32 bytes' , ErrorCode . INVALID_VALUE_LENGTH , {
290+ objectContext : this . errorStr ( ) ,
291+ received : ` ${ bytesToHex ( receiptTrie ) } ( ${ receiptTrie . length } bytes)` ,
292+ } )
286293 }
287294 if ( mixHash . length !== 32 ) {
288- const msg = this . _errorMsg ( `mixHash must be 32 bytes, received ${ mixHash . length } bytes` )
289- throw new Error ( msg )
295+ throw new ValueError ( 'mixHash must be 32 bytes' , ErrorCode . INVALID_VALUE_LENGTH , {
296+ objectContext : this . errorStr ( ) ,
297+ received : `${ bytesToHex ( mixHash ) } (${ mixHash . length } bytes)` ,
298+ } )
290299 }
291300
292301 if ( nonce . length !== 8 ) {
293- const msg = this . _errorMsg ( `nonce must be 8 bytes, received ${ nonce . length } bytes` )
294- throw new Error ( msg )
302+ throw new ValueError ( 'nonce must be 8 bytes' , ErrorCode . INVALID_VALUE_LENGTH , {
303+ objectContext : this . errorStr ( ) ,
304+ received : `${ bytesToHex ( nonce ) } (${ nonce . length } bytes)` ,
305+ } )
295306 }
296307
297308 // check if the block used too much gas
@@ -436,8 +447,9 @@ export class BlockHeader {
436447 }
437448 }
438449 if ( error ) {
439- const msg = this . _errorMsg ( `Invalid PoS block: ${ errorMsg } ` )
440- throw new Error ( msg )
450+ throw new ValueError ( `Invalid PoS block${ errorMsg } ` , ErrorCode . INVALID_OBJECT , {
451+ objectContext : this . errorStr ( ) ,
452+ } )
441453 }
442454 }
443455 }
@@ -666,14 +678,22 @@ export class BlockHeader {
666678 */
667679 ethashCanonicalDifficulty ( parentBlockHeader : BlockHeader ) : bigint {
668680 if ( this . common . consensusType ( ) !== ConsensusType . ProofOfWork ) {
669- const msg = this . _errorMsg ( 'difficulty calculation is only supported on PoW chains' )
670- throw new Error ( msg )
681+ throw new UsageError (
682+ 'difficulty calculation is only supported on PoW chains' ,
683+ ErrorCode . INVALID_METHOD_CALL ,
684+ {
685+ objectContext : this . errorStr ( ) ,
686+ } ,
687+ )
671688 }
672689 if ( this . common . consensusAlgorithm ( ) !== ConsensusAlgorithm . Ethash ) {
673- const msg = this . _errorMsg (
690+ throw new UsageError (
674691 'difficulty calculation currently only supports the ethash algorithm' ,
692+ ErrorCode . INVALID_METHOD_CALL ,
693+ {
694+ objectContext : this . errorStr ( ) ,
695+ } ,
675696 )
676- throw new Error ( msg )
677697 }
678698 const blockTs = this . timestamp
679699 const { timestamp : parentTs , difficulty : parentDif } = parentBlockHeader
0 commit comments