Skip to content

Commit 66c9403

Browse files
authored
primitives/chain: dev chain support cancun upgrade (#7033)
Signed-off-by: jsvisa <[email protected]>
1 parent 72bea5f commit 66c9403

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

crates/consensus/auto-seal/src/lib.rs

+36
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use reth_interfaces::{
2323
use reth_node_api::{ConfigureEvm, EngineTypes};
2424
use reth_primitives::{
2525
constants::{EMPTY_RECEIPTS, EMPTY_TRANSACTIONS, ETHEREUM_BLOCK_GAS_LIMIT},
26+
eip4844::calculate_excess_blob_gas,
2627
proofs, Block, BlockBody, BlockHash, BlockHashOrNumber, BlockNumber, BlockWithSenders, Bloom,
2728
ChainSpec, Header, ReceiptWithBloom, SealedBlock, SealedHeader, TransactionSigned, B256,
2829
EMPTY_OMMER_ROOT_HASH, U256,
@@ -303,6 +304,27 @@ impl StorageInner {
303304
parent_beacon_block_root: None,
304305
};
305306

307+
if chain_spec.is_cancun_active_at_timestamp(timestamp) {
308+
let parent = self.headers.get(&self.best_block);
309+
header.parent_beacon_block_root =
310+
parent.and_then(|parent| parent.parent_beacon_block_root);
311+
header.blob_gas_used = Some(0);
312+
313+
let (parent_excess_blob_gas, parent_blob_gas_used) = match parent {
314+
Some(parent_block)
315+
if chain_spec.is_cancun_active_at_timestamp(parent_block.timestamp) =>
316+
{
317+
(
318+
parent_block.excess_blob_gas.unwrap_or_default(),
319+
parent_block.blob_gas_used.unwrap_or_default(),
320+
)
321+
}
322+
_ => (0, 0),
323+
};
324+
header.excess_blob_gas =
325+
Some(calculate_excess_blob_gas(parent_excess_blob_gas, parent_blob_gas_used))
326+
}
327+
306328
header.transactions_root = if transactions.is_empty() {
307329
EMPTY_TRANSACTIONS
308330
} else {
@@ -354,6 +376,7 @@ impl StorageInner {
354376
bundle_state: &BundleStateWithReceipts,
355377
client: &S,
356378
gas_used: u64,
379+
blob_gas_used: Option<u64>,
357380
#[cfg(feature = "optimism")] chain_spec: &ChainSpec,
358381
) -> Result<Header, BlockExecutionError> {
359382
let receipts = bundle_state.receipts_by_block(header.number);
@@ -381,6 +404,7 @@ impl StorageInner {
381404
};
382405

383406
header.gas_used = gas_used;
407+
header.blob_gas_used = blob_gas_used;
384408

385409
// calculate the state root
386410
let state_root = client
@@ -425,6 +449,17 @@ impl StorageInner {
425449
let Block { header, body, .. } = block.block;
426450
let body = BlockBody { transactions: body, ommers: vec![], withdrawals: None };
427451

452+
let mut blob_gas_used = None;
453+
if chain_spec.is_cancun_active_at_timestamp(header.timestamp) {
454+
let mut sum_blob_gas_used = 0;
455+
for tx in &body.transactions {
456+
if let Some(blob_tx) = tx.transaction.as_eip4844() {
457+
sum_blob_gas_used += blob_tx.blob_gas();
458+
}
459+
}
460+
blob_gas_used = Some(sum_blob_gas_used);
461+
}
462+
428463
trace!(target: "consensus::auto", ?bundle_state, ?header, ?body, "executed block, calculating state root and completing header");
429464

430465
// fill in the rest of the fields
@@ -433,6 +468,7 @@ impl StorageInner {
433468
&bundle_state,
434469
client,
435470
gas_used,
471+
blob_gas_used,
436472
#[cfg(feature = "optimism")]
437473
chain_spec.as_ref(),
438474
)?;

crates/primitives/src/chain/spec.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub static DEV: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
216216
"2f980576711e3617a5e4d83dd539548ec0f7792007d505a3d2e9674833af2d7c"
217217
)),
218218
paris_block_and_final_difficulty: Some((0, U256::from(0))),
219-
fork_timestamps: ForkTimestamps::default().shanghai(0),
219+
fork_timestamps: ForkTimestamps::default().shanghai(0).cancun(0),
220220
hardforks: BTreeMap::from([
221221
(Hardfork::Frontier, ForkCondition::Block(0)),
222222
(Hardfork::Homestead, ForkCondition::Block(0)),
@@ -235,6 +235,7 @@ pub static DEV: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
235235
ForkCondition::TTD { fork_block: Some(0), total_difficulty: U256::from(0) },
236236
),
237237
(Hardfork::Shanghai, ForkCondition::Timestamp(0)),
238+
(Hardfork::Cancun, ForkCondition::Timestamp(0)),
238239
]),
239240
base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
240241
deposit_contract: None, // TODO: do we even have?

0 commit comments

Comments
 (0)