Skip to content

Commit 0cc06f2

Browse files
committed
add ssz blockheader type and update the blockhash to use when ssz activated
1 parent 838f25d commit 0cc06f2

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

packages/block/src/header/header.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
createZeroAddress,
1717
equalsBytes,
1818
hexToBytes,
19+
ssz,
1920
toType,
2021
zeros,
2122
} from '@ethereumjs/util'
@@ -30,6 +31,9 @@ import { fakeExponential } from '../helpers.js'
3031
import { paramsBlock } from '../params.js'
3132

3233
import type { BlockHeaderBytes, BlockOptions, HeaderData, JSONHeader } from '../types.js'
34+
import type { ValueOf } from '@chainsafe/ssz'
35+
36+
export type SSZHeaderType = ValueOf<typeof ssz.BlockHeader>
3337

3438
interface HeaderCache {
3539
hash: Uint8Array | undefined
@@ -633,17 +637,55 @@ export class BlockHeader {
633637
return rawItems
634638
}
635639

640+
sszRaw(): SSZHeaderType {
641+
const header = {
642+
parentHash: this.parentHash,
643+
uncleHash: this.uncleHash,
644+
coinbase: this.coinbase.bytes,
645+
stateRoot: this.stateRoot,
646+
transactionsTrie: this.transactionsTrie,
647+
receiptTrie: this.receiptTrie,
648+
number: this.number,
649+
gasLimits: {
650+
regular: this.gasLimit,
651+
blob: this.common.isActivatedEIP(4844) ? this.common.param('maxblobGasPerBlock') : null,
652+
},
653+
gasUsed: { regular: this.gasUsed, blob: this.blobGasUsed ?? null },
654+
timestamp: this.timestamp,
655+
extraData: this.extraData,
656+
mixHash: this.mixHash,
657+
baseFeePerGas: {
658+
regular: this.baseFeePerGas ?? null,
659+
blob: this.common.isActivatedEIP(4844) ? this.getBlobGasPrice() : null,
660+
},
661+
withdrawalsRoot: this.withdrawalsRoot ?? null,
662+
excessBlobGas: this.excessBlobGas ?? null,
663+
parentBeaconBlockRoot: this.parentBeaconBlockRoot ?? null,
664+
requestsRoot: this.requestsRoot ?? null,
665+
}
666+
667+
return header
668+
}
669+
670+
calcHash(): Uint8Array {
671+
if (this.common.isActivatedEIP(6493)) {
672+
return ssz.BlockHeader.hashTreeRoot(this.sszRaw())
673+
} else {
674+
return this.keccakFunction(RLP.encode(this.raw()))
675+
}
676+
}
677+
636678
/**
637679
* Returns the hash of the block header.
638680
*/
639681
hash(): Uint8Array {
640682
if (Object.isFrozen(this)) {
641683
if (!this.cache.hash) {
642-
this.cache.hash = this.keccakFunction(RLP.encode(this.raw())) as Uint8Array
684+
this.cache.hash = this.calcHash()
643685
}
644686
return this.cache.hash
645687
}
646-
return this.keccakFunction(RLP.encode(this.raw()))
688+
return this.calcHash()
647689
}
648690

649691
/**

packages/block/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ export { type BeaconPayloadJSON, executionPayloadFromBeaconPayload } from './fro
55
export * from './header/index.js'
66
export {
77
genRequestsTrieRoot,
8-
genTransactionsTrieRoot,
98
genTransactionsSszRoot,
10-
genWithdrawalsTrieRoot,
9+
genTransactionsTrieRoot,
1110
genWithdrawalsSszRoot,
11+
genWithdrawalsTrieRoot,
1212
getDifficulty,
1313
valuesArrayToHeaderData,
1414
} from './helpers.js'

packages/util/src/ssz.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const Uint256 = new UintBigintType(32)
3131

3232
export const Bytes20 = new ByteVectorType(20)
3333
export const Bytes32 = new ByteVectorType(32)
34+
export const Bytes256 = new ByteVectorType(256)
3435

3536
export const FeePerGas = Uint256
3637
export const ChainId = Uint64
@@ -358,3 +359,30 @@ export type TransactionV1 = {
358359
payload: TransactionPayloadV1
359360
signature: TransactionSignatureV1
360361
}
362+
363+
export const MAX_BLOCKHEADER_FIELDS = 64
364+
const MAX_EXTRA_DATA_BYTES = 32
365+
366+
export const BlockHeader = new StableContainerType(
367+
{
368+
parentHash: new OptionalType(Bytes32),
369+
uncleHash: new OptionalType(Bytes32),
370+
coinbase: new OptionalType(Bytes20),
371+
stateRoot: new OptionalType(Bytes32),
372+
transactionsTrie: new OptionalType(Bytes32),
373+
receiptTrie: new OptionalType(Bytes32),
374+
number: new OptionalType(Uint64),
375+
gasLimits: new OptionalType(FeesPerGas),
376+
gasUsed: new OptionalType(FeesPerGas),
377+
timestamp: new OptionalType(Uint64),
378+
extraData: new OptionalType(new ByteListType(MAX_EXTRA_DATA_BYTES)),
379+
mixHash: new OptionalType(Bytes32),
380+
baseFeePerGas: new OptionalType(FeesPerGas),
381+
withdrawalsRoot: new OptionalType(Bytes32),
382+
excessBlobGas: new OptionalType(FeePerGas),
383+
parentBeaconBlockRoot: new OptionalType(Bytes32),
384+
requestsRoot: new OptionalType(Bytes32),
385+
},
386+
MAX_BLOCKHEADER_FIELDS,
387+
{ typeName: 'BlockHeader', jsonCase: 'eth2' },
388+
)

0 commit comments

Comments
 (0)