-
Notifications
You must be signed in to change notification settings - Fork 717
/
block.ts
834 lines (755 loc) · 26.1 KB
/
block.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
import { ConsensusType } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import { Trie } from '@ethereumjs/trie'
import { BlobEIP4844Transaction, Capability, TransactionFactory } from '@ethereumjs/tx'
import {
BIGINT_0,
KECCAK256_RLP,
Withdrawal,
bigIntToHex,
bytesToHex,
bytesToUtf8,
equalsBytes,
fetchFromProvider,
getProvider,
hexToBytes,
intToHex,
isHexPrefixed,
} from '@ethereumjs/util'
import { keccak256 } from 'ethereum-cryptography/keccak.js'
import { executionPayloadFromBeaconPayload } from './from-beacon-payload.js'
import { blockFromRpc } from './from-rpc.js'
import { BlockHeader } from './header.js'
import type { BeaconPayloadJson } from './from-beacon-payload.js'
import type {
BlockBytes,
BlockData,
BlockOptions,
ExecutionPayload,
HeaderData,
JsonBlock,
JsonRpcBlock,
VerkleExecutionWitness,
} from './types.js'
import type { Common } from '@ethereumjs/common'
import type {
FeeMarketEIP1559Transaction,
LegacyTransaction,
TxOptions,
TypedTransaction,
} from '@ethereumjs/tx'
import type { EthersProvider, WithdrawalBytes } from '@ethereumjs/util'
/**
* An object that represents the block.
*/
export class Block {
public readonly header: BlockHeader
public readonly transactions: TypedTransaction[] = []
public readonly uncleHeaders: BlockHeader[] = []
public readonly withdrawals?: Withdrawal[]
public readonly common: Common
protected keccakFunction: (msg: Uint8Array) => Uint8Array
/**
* EIP-6800: Verkle Proof Data (experimental)
* null implies that the non default executionWitness might exist but not available
* and will not lead to execution of the block via vm with verkle stateless manager
*/
public readonly executionWitness?: VerkleExecutionWitness | null
protected cache: {
txTrieRoot?: Uint8Array
} = {}
/**
* Returns the withdrawals trie root for array of Withdrawal.
* @param wts array of Withdrawal to compute the root of
* @param optional emptyTrie to use to generate the root
*/
public static async genWithdrawalsTrieRoot(wts: Withdrawal[], emptyTrie?: Trie) {
const trie = emptyTrie ?? new Trie()
for (const [i, wt] of wts.entries()) {
await trie.put(RLP.encode(i), RLP.encode(wt.raw()))
}
return trie.root()
}
/**
* Returns the txs trie root for array of TypedTransaction
* @param txs array of TypedTransaction to compute the root of
* @param optional emptyTrie to use to generate the root
*/
public static async genTransactionsTrieRoot(txs: TypedTransaction[], emptyTrie?: Trie) {
const trie = emptyTrie ?? new Trie()
for (const [i, tx] of txs.entries()) {
await trie.put(RLP.encode(i), tx.serialize())
}
return trie.root()
}
/**
* Static constructor to create a block from a block data dictionary
*
* @param blockData
* @param opts
*/
public static fromBlockData(blockData: BlockData = {}, opts?: BlockOptions) {
const {
header: headerData,
transactions: txsData,
uncleHeaders: uhsData,
withdrawals: withdrawalsData,
executionWitness: executionWitnessData,
} = blockData
const header = BlockHeader.fromHeaderData(headerData, opts)
// parse transactions
const transactions = []
for (const txData of txsData ?? []) {
const tx = TransactionFactory.fromTxData(txData, {
...opts,
// Use header common in case of setHardfork being activated
common: header.common,
} as TxOptions)
transactions.push(tx)
}
// parse uncle headers
const uncleHeaders = []
const uncleOpts: BlockOptions = {
...opts,
// Use header common in case of setHardfork being activated
common: header.common,
// Disable this option here (all other options carried over), since this overwrites the provided Difficulty to an incorrect value
calcDifficultyFromHeader: undefined,
}
// Uncles are obsolete post-merge, any hardfork by option implies setHardfork
if (opts?.setHardfork !== undefined) {
uncleOpts.setHardfork = true
}
for (const uhData of uhsData ?? []) {
const uh = BlockHeader.fromHeaderData(uhData, uncleOpts)
uncleHeaders.push(uh)
}
const withdrawals = withdrawalsData?.map(Withdrawal.fromWithdrawalData)
// The witness data is planned to come in rlp serialized bytes so leave this
// stub till that time
const executionWitness = executionWitnessData
return new Block(header, transactions, uncleHeaders, withdrawals, opts, executionWitness)
}
/**
* Static constructor to create a block from a RLP-serialized block
*
* @param serialized
* @param opts
*/
public static fromRLPSerializedBlock(serialized: Uint8Array, opts?: BlockOptions) {
const values = RLP.decode(Uint8Array.from(serialized)) as BlockBytes
if (!Array.isArray(values)) {
throw new Error('Invalid serialized block input. Must be array')
}
return Block.fromValuesArray(values, opts)
}
/**
* Static constructor to create a block from an array of Bytes values
*
* @param values
* @param opts
*/
public static fromValuesArray(values: BlockBytes, opts?: BlockOptions) {
if (values.length > 5) {
throw new Error(
`invalid block. More values=${values.length} than expected were received (at most 5)`
)
}
// First try to load header so that we can use its common (in case of setHardfork being activated)
// to correctly make checks on the hardforks
const [headerData, txsData, uhsData, withdrawalBytes, executionWitnessBytes] = values
const header = BlockHeader.fromValuesArray(headerData, opts)
if (
header.common.isActivatedEIP(4895) &&
(withdrawalBytes === undefined || !Array.isArray(withdrawalBytes))
) {
throw new Error(
'Invalid serialized block input: EIP-4895 is active, and no withdrawals were provided as array'
)
}
// parse transactions
const transactions = []
for (const txData of txsData ?? []) {
transactions.push(
TransactionFactory.fromBlockBodyData(txData, {
...opts,
// Use header common in case of setHardfork being activated
common: header.common,
})
)
}
// parse uncle headers
const uncleHeaders = []
const uncleOpts: BlockOptions = {
...opts,
// Use header common in case of setHardfork being activated
common: header.common,
// Disable this option here (all other options carried over), since this overwrites the provided Difficulty to an incorrect value
calcDifficultyFromHeader: undefined,
}
// Uncles are obsolete post-merge, any hardfork by option implies setHardfork
if (opts?.setHardfork !== undefined) {
uncleOpts.setHardfork = true
}
for (const uncleHeaderData of uhsData ?? []) {
uncleHeaders.push(BlockHeader.fromValuesArray(uncleHeaderData, uncleOpts))
}
const withdrawals = (withdrawalBytes as WithdrawalBytes[])
?.map(([index, validatorIndex, address, amount]) => ({
index,
validatorIndex,
address,
amount,
}))
?.map(Withdrawal.fromWithdrawalData)
// executionWitness are not part of the EL fetched blocks via eth_ bodies method
// they are currently only available via the engine api constructed blocks
let executionWitness
if (header.common.isActivatedEIP(6800) && executionWitnessBytes !== undefined) {
executionWitness = JSON.parse(bytesToUtf8(RLP.decode(executionWitnessBytes) as Uint8Array))
} else {
// don't assign default witness if eip 6800 is implemented as it leads to incorrect
// assumptions while executing the block. if not present in input implies its unavailable
executionWitness = null
}
return new Block(header, transactions, uncleHeaders, withdrawals, opts, executionWitness)
}
/**
* Creates a new block object from Ethereum JSON RPC.
*
* @param blockParams - Ethereum JSON RPC of block (eth_getBlockByNumber)
* @param uncles - Optional list of Ethereum JSON RPC of uncles (eth_getUncleByBlockHashAndIndex)
* @param opts - An object describing the blockchain
*/
public static fromRPC(blockData: JsonRpcBlock, uncles?: any[], opts?: BlockOptions) {
return blockFromRpc(blockData, uncles, opts)
}
/**
* Method to retrieve a block from a JSON-RPC provider and format as a {@link Block}
* @param provider either a url for a remote provider or an Ethers JsonRpcProvider object
* @param blockTag block hash or block number to be run
* @param opts {@link BlockOptions}
* @returns the block specified by `blockTag`
*/
public static fromJsonRpcProvider = async (
provider: string | EthersProvider,
blockTag: string | bigint,
opts: BlockOptions
) => {
let blockData
const providerUrl = getProvider(provider)
if (typeof blockTag === 'string' && blockTag.length === 66) {
blockData = await fetchFromProvider(providerUrl, {
method: 'eth_getBlockByHash',
params: [blockTag, true],
})
} else if (typeof blockTag === 'bigint') {
blockData = await fetchFromProvider(providerUrl, {
method: 'eth_getBlockByNumber',
params: [bigIntToHex(blockTag), true],
})
} else if (
isHexPrefixed(blockTag) ||
blockTag === 'latest' ||
blockTag === 'earliest' ||
blockTag === 'pending' ||
blockTag === 'finalized' ||
blockTag === 'safe'
) {
blockData = await fetchFromProvider(providerUrl, {
method: 'eth_getBlockByNumber',
params: [blockTag, true],
})
} else {
throw new Error(
`expected blockTag to be block hash, bigint, hex prefixed string, or earliest/latest/pending; got ${blockTag}`
)
}
if (blockData === null) {
throw new Error('No block data returned from provider')
}
const uncleHeaders = []
if (blockData.uncles.length > 0) {
for (let x = 0; x < blockData.uncles.length; x++) {
const headerData = await fetchFromProvider(providerUrl, {
method: 'eth_getUncleByBlockHashAndIndex',
params: [blockData.hash, intToHex(x)],
})
uncleHeaders.push(headerData)
}
}
return blockFromRpc(blockData, uncleHeaders, opts)
}
/**
* Method to retrieve a block from an execution payload
* @param execution payload constructed from beacon payload
* @param opts {@link BlockOptions}
* @returns the block constructed block
*/
public static async fromExecutionPayload(
payload: ExecutionPayload,
opts?: BlockOptions
): Promise<Block> {
const {
blockNumber: number,
receiptsRoot: receiptTrie,
prevRandao: mixHash,
feeRecipient: coinbase,
transactions,
withdrawals: withdrawalsData,
executionWitness,
} = payload
const txs = []
for (const [index, serializedTx] of transactions.entries()) {
try {
const tx = TransactionFactory.fromSerializedData(hexToBytes(serializedTx), {
common: opts?.common,
})
txs.push(tx)
} catch (error) {
const validationError = `Invalid tx at index ${index}: ${error}`
throw validationError
}
}
const transactionsTrie = await Block.genTransactionsTrieRoot(
txs,
new Trie({ common: opts?.common })
)
const withdrawals = withdrawalsData?.map((wData) => Withdrawal.fromWithdrawalData(wData))
const withdrawalsRoot = withdrawals
? await Block.genWithdrawalsTrieRoot(withdrawals, new Trie({ common: opts?.common }))
: undefined
const header: HeaderData = {
...payload,
number,
receiptTrie,
transactionsTrie,
withdrawalsRoot,
mixHash,
coinbase,
}
// we are not setting setHardfork as common is already set to the correct hf
const block = Block.fromBlockData(
{ header, transactions: txs, withdrawals, executionWitness },
opts
)
if (
block.common.isActivatedEIP(6800) &&
(executionWitness === undefined || executionWitness === null)
) {
throw Error('Missing executionWitness for EIP-6800 activated executionPayload')
}
// Verify blockHash matches payload
if (!equalsBytes(block.hash(), hexToBytes(payload.blockHash))) {
const validationError = `Invalid blockHash, expected: ${
payload.blockHash
}, received: ${bytesToHex(block.hash())}`
throw Error(validationError)
}
return block
}
/**
* Method to retrieve a block from a beacon payload json
* @param payload json of a beacon beacon fetched from beacon apis
* @param opts {@link BlockOptions}
* @returns the block constructed block
*/
public static async fromBeaconPayloadJson(
payload: BeaconPayloadJson,
opts?: BlockOptions
): Promise<Block> {
const executionPayload = executionPayloadFromBeaconPayload(payload)
return Block.fromExecutionPayload(executionPayload, opts)
}
/**
* This constructor takes the values, validates them, assigns them and freezes the object.
* Use the static factory methods to assist in creating a Block object from varying data types and options.
*/
constructor(
header?: BlockHeader,
transactions: TypedTransaction[] = [],
uncleHeaders: BlockHeader[] = [],
withdrawals?: Withdrawal[],
opts: BlockOptions = {},
executionWitness?: VerkleExecutionWitness | null
) {
this.header = header ?? BlockHeader.fromHeaderData({}, opts)
this.common = this.header.common
this.keccakFunction = this.common.customCrypto.keccak256 ?? keccak256
this.transactions = transactions
this.withdrawals = withdrawals ?? (this.common.isActivatedEIP(4895) ? [] : undefined)
this.executionWitness = executionWitness
// null indicates an intentional absence of value or unavailability
// undefined indicates that the executionWitness should be initialized with the default state
if (this.common.isActivatedEIP(6800) && this.executionWitness === undefined) {
this.executionWitness = {
stateDiff: [],
verkleProof: {
commitmentsByPath: [],
d: '0x',
depthExtensionPresent: '0x',
ipaProof: {
cl: [],
cr: [],
finalEvaluation: '0x',
},
otherStems: [],
},
}
}
this.uncleHeaders = uncleHeaders
if (uncleHeaders.length > 0) {
this.validateUncles()
if (this.common.consensusType() === ConsensusType.ProofOfAuthority) {
const msg = this._errorMsg(
'Block initialization with uncleHeaders on a PoA network is not allowed'
)
throw new Error(msg)
}
if (this.common.consensusType() === ConsensusType.ProofOfStake) {
const msg = this._errorMsg(
'Block initialization with uncleHeaders on a PoS network is not allowed'
)
throw new Error(msg)
}
}
if (!this.common.isActivatedEIP(4895) && withdrawals !== undefined) {
throw new Error('Cannot have a withdrawals field if EIP 4895 is not active')
}
if (
!this.common.isActivatedEIP(6800) &&
executionWitness !== undefined &&
executionWitness !== null
) {
throw new Error(`Cannot have executionWitness field if EIP 6800 is not active `)
}
const freeze = opts?.freeze ?? true
if (freeze) {
Object.freeze(this)
}
}
/**
* Returns a Array of the raw Bytes Arrays of this block, in order.
*/
raw(): BlockBytes {
const bytesArray = <BlockBytes>[
this.header.raw(),
this.transactions.map((tx) =>
tx.supports(Capability.EIP2718TypedTransaction) ? tx.serialize() : tx.raw()
) as Uint8Array[],
this.uncleHeaders.map((uh) => uh.raw()),
]
const withdrawalsRaw = this.withdrawals?.map((wt) => wt.raw())
if (withdrawalsRaw) {
bytesArray.push(withdrawalsRaw)
}
if (this.executionWitness !== undefined && this.executionWitness !== null) {
const executionWitnessBytes = RLP.encode(JSON.stringify(this.executionWitness))
bytesArray.push(executionWitnessBytes as any)
}
return bytesArray
}
/**
* Returns the hash of the block.
*/
hash(): Uint8Array {
return this.header.hash()
}
/**
* Determines if this block is the genesis block.
*/
isGenesis(): boolean {
return this.header.isGenesis()
}
/**
* Returns the rlp encoding of the block.
*/
serialize(): Uint8Array {
return RLP.encode(this.raw())
}
/**
* Generates transaction trie for validation.
*/
async genTxTrie(): Promise<Uint8Array> {
return Block.genTransactionsTrieRoot(this.transactions, new Trie({ common: this.common }))
}
/**
* Validates the transaction trie by generating a trie
* and do a check on the root hash.
* @returns True if the transaction trie is valid, false otherwise
*/
async transactionsTrieIsValid(): Promise<boolean> {
let result
if (this.transactions.length === 0) {
result = equalsBytes(this.header.transactionsTrie, KECCAK256_RLP)
return result
}
if (this.cache.txTrieRoot === undefined) {
this.cache.txTrieRoot = await this.genTxTrie()
}
result = equalsBytes(this.cache.txTrieRoot, this.header.transactionsTrie)
return result
}
/**
* Validates transaction signatures and minimum gas requirements.
* @returns {string[]} an array of error strings
*/
getTransactionsValidationErrors(): string[] {
const errors: string[] = []
let blobGasUsed = BIGINT_0
const blobGasLimit = this.common.param('gasConfig', 'maxblobGasPerBlock')
const blobGasPerBlob = this.common.param('gasConfig', 'blobGasPerBlob')
// eslint-disable-next-line prefer-const
for (let [i, tx] of this.transactions.entries()) {
const errs = tx.getValidationErrors()
if (this.common.isActivatedEIP(1559) === true) {
if (tx.supports(Capability.EIP1559FeeMarket)) {
tx = tx as FeeMarketEIP1559Transaction
if (tx.maxFeePerGas < this.header.baseFeePerGas!) {
errs.push('tx unable to pay base fee (EIP-1559 tx)')
}
} else {
tx = tx as LegacyTransaction
if (tx.gasPrice < this.header.baseFeePerGas!) {
errs.push('tx unable to pay base fee (non EIP-1559 tx)')
}
}
}
if (this.common.isActivatedEIP(4844) === true) {
if (tx instanceof BlobEIP4844Transaction) {
blobGasUsed += BigInt(tx.numBlobs()) * blobGasPerBlob
if (blobGasUsed > blobGasLimit) {
errs.push(
`tx causes total blob gas of ${blobGasUsed} to exceed maximum blob gas per block of ${blobGasLimit}`
)
}
}
}
if (errs.length > 0) {
errors.push(`errors at tx ${i}: ${errs.join(', ')}`)
}
}
if (this.common.isActivatedEIP(4844) === true) {
if (blobGasUsed !== this.header.blobGasUsed) {
errors.push(`invalid blobGasUsed expected=${this.header.blobGasUsed} actual=${blobGasUsed}`)
}
}
return errors
}
/**
* Validates transaction signatures and minimum gas requirements.
* @returns True if all transactions are valid, false otherwise
*/
transactionsAreValid(): boolean {
const errors = this.getTransactionsValidationErrors()
return errors.length === 0
}
/**
* Validates the block data, throwing if invalid.
* This can be checked on the Block itself without needing access to any parent block
* It checks:
* - All transactions are valid
* - The transactions trie is valid
* - The uncle hash is valid
* @param onlyHeader if only passed the header, skip validating txTrie and unclesHash (default: false)
* @param verifyTxs if set to `false`, will not check for transaction validation errors (default: true)
*/
async validateData(onlyHeader: boolean = false, verifyTxs: boolean = true): Promise<void> {
if (verifyTxs) {
const txErrors = this.getTransactionsValidationErrors()
if (txErrors.length > 0) {
const msg = this._errorMsg(`invalid transactions: ${txErrors.join(' ')}`)
throw new Error(msg)
}
}
if (onlyHeader) {
return
}
if (verifyTxs) {
for (const [index, tx] of this.transactions.entries()) {
if (!tx.isSigned()) {
const msg = this._errorMsg(
`invalid transactions: transaction at index ${index} is unsigned`
)
throw new Error(msg)
}
}
}
if (!(await this.transactionsTrieIsValid())) {
const msg = this._errorMsg('invalid transaction trie')
throw new Error(msg)
}
if (!this.uncleHashIsValid()) {
const msg = this._errorMsg('invalid uncle hash')
throw new Error(msg)
}
if (this.common.isActivatedEIP(4895) && !(await this.withdrawalsTrieIsValid())) {
const msg = this._errorMsg('invalid withdrawals trie')
throw new Error(msg)
}
// Validation for Verkle blocks
// Unnecessary in this implementation since we're providing defaults if those fields are undefined
if (this.common.isActivatedEIP(6800)) {
if (this.executionWitness === undefined) {
throw new Error(`Invalid block: missing executionWitness`)
}
if (this.executionWitness === null) {
throw new Error(`Invalid block: ethereumjs stateless client needs executionWitness`)
}
}
}
/**
* Validates that blob gas fee for each transaction is greater than or equal to the
* blobGasPrice for the block and that total blob gas in block is less than maximum
* blob gas per block
* @param parentHeader header of parent block
*/
validateBlobTransactions(parentHeader: BlockHeader) {
if (this.common.isActivatedEIP(4844)) {
const blobGasLimit = this.common.param('gasConfig', 'maxblobGasPerBlock')
const blobGasPerBlob = this.common.param('gasConfig', 'blobGasPerBlob')
let blobGasUsed = BIGINT_0
const expectedExcessBlobGas = parentHeader.calcNextExcessBlobGas()
if (this.header.excessBlobGas !== expectedExcessBlobGas) {
throw new Error(
`block excessBlobGas mismatch: have ${this.header.excessBlobGas}, want ${expectedExcessBlobGas}`
)
}
let blobGasPrice
for (const tx of this.transactions) {
if (tx instanceof BlobEIP4844Transaction) {
blobGasPrice = blobGasPrice ?? this.header.getBlobGasPrice()
if (tx.maxFeePerBlobGas < blobGasPrice) {
throw new Error(
`blob transaction maxFeePerBlobGas ${
tx.maxFeePerBlobGas
} < than block blob gas price ${blobGasPrice} - ${this.errorStr()}`
)
}
blobGasUsed += BigInt(tx.blobVersionedHashes.length) * blobGasPerBlob
if (blobGasUsed > blobGasLimit) {
throw new Error(
`tx causes total blob gas of ${blobGasUsed} to exceed maximum blob gas per block of ${blobGasLimit}`
)
}
}
}
if (this.header.blobGasUsed !== blobGasUsed) {
throw new Error(
`block blobGasUsed mismatch: have ${this.header.blobGasUsed}, want ${blobGasUsed}`
)
}
}
}
/**
* Validates the uncle's hash.
* @returns true if the uncle's hash is valid, false otherwise.
*/
uncleHashIsValid(): boolean {
const uncles = this.uncleHeaders.map((uh) => uh.raw())
const raw = RLP.encode(uncles)
return equalsBytes(this.keccakFunction(raw), this.header.uncleHash)
}
/**
* Validates the withdrawal root
* @returns true if the withdrawals trie root is valid, false otherwise
*/
async withdrawalsTrieIsValid(): Promise<boolean> {
if (!this.common.isActivatedEIP(4895)) {
throw new Error('EIP 4895 is not activated')
}
const withdrawalsRoot = await Block.genWithdrawalsTrieRoot(
this.withdrawals!,
new Trie({ common: this.common })
)
return equalsBytes(withdrawalsRoot, this.header.withdrawalsRoot!)
}
/**
* Consistency checks for uncles included in the block, if any.
*
* Throws if invalid.
*
* The rules for uncles checked are the following:
* Header has at most 2 uncles.
* Header does not count an uncle twice.
*/
validateUncles() {
if (this.isGenesis()) {
return
}
// Header has at most 2 uncles
if (this.uncleHeaders.length > 2) {
const msg = this._errorMsg('too many uncle headers')
throw new Error(msg)
}
// Header does not count an uncle twice.
const uncleHashes = this.uncleHeaders.map((header) => bytesToHex(header.hash()))
if (!(new Set(uncleHashes).size === uncleHashes.length)) {
const msg = this._errorMsg('duplicate uncles')
throw new Error(msg)
}
}
/**
* Returns the canonical difficulty for this block.
*
* @param parentBlock - the parent of this `Block`
*/
ethashCanonicalDifficulty(parentBlock: Block): bigint {
return this.header.ethashCanonicalDifficulty(parentBlock.header)
}
/**
* Validates if the block gasLimit remains in the boundaries set by the protocol.
* Throws if invalid
*
* @param parentBlock - the parent of this `Block`
*/
validateGasLimit(parentBlock: Block) {
return this.header.validateGasLimit(parentBlock.header)
}
/**
* Returns the block in JSON format.
*/
toJSON(): JsonBlock {
const withdrawalsAttr = this.withdrawals
? {
withdrawals: this.withdrawals.map((wt) => wt.toJSON()),
}
: {}
return {
header: this.header.toJSON(),
transactions: this.transactions.map((tx) => tx.toJSON()),
uncleHeaders: this.uncleHeaders.map((uh) => uh.toJSON()),
...withdrawalsAttr,
}
}
/**
* Return a compact error string representation of the object
*/
public errorStr() {
let hash = ''
try {
hash = bytesToHex(this.hash())
} catch (e: any) {
hash = 'error'
}
let hf = ''
try {
hf = this.common.hardfork()
} catch (e: any) {
hf = 'error'
}
let errorStr = `block number=${this.header.number} hash=${hash} `
errorStr += `hf=${hf} baseFeePerGas=${this.header.baseFeePerGas ?? 'none'} `
errorStr += `txs=${this.transactions.length} uncles=${this.uncleHeaders.length}`
return errorStr
}
/**
* Internal helper function to create an annotated error message
*
* @param msg Base error message
* @hidden
*/
protected _errorMsg(msg: string) {
return `${msg} (${this.errorStr()})`
}
}