From 66c9403ea1d3d03938eafc099e64e789aadc3616 Mon Sep 17 00:00:00 2001 From: Delweng Date: Mon, 11 Mar 2024 21:21:26 +0800 Subject: [PATCH] primitives/chain: dev chain support cancun upgrade (#7033) Signed-off-by: jsvisa --- crates/consensus/auto-seal/src/lib.rs | 36 +++++++++++++++++++++++++++ crates/primitives/src/chain/spec.rs | 3 ++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/consensus/auto-seal/src/lib.rs b/crates/consensus/auto-seal/src/lib.rs index 67261af4ad15..deabbb1cfeb5 100644 --- a/crates/consensus/auto-seal/src/lib.rs +++ b/crates/consensus/auto-seal/src/lib.rs @@ -23,6 +23,7 @@ use reth_interfaces::{ use reth_node_api::{ConfigureEvm, EngineTypes}; use reth_primitives::{ constants::{EMPTY_RECEIPTS, EMPTY_TRANSACTIONS, ETHEREUM_BLOCK_GAS_LIMIT}, + eip4844::calculate_excess_blob_gas, proofs, Block, BlockBody, BlockHash, BlockHashOrNumber, BlockNumber, BlockWithSenders, Bloom, ChainSpec, Header, ReceiptWithBloom, SealedBlock, SealedHeader, TransactionSigned, B256, EMPTY_OMMER_ROOT_HASH, U256, @@ -303,6 +304,27 @@ impl StorageInner { parent_beacon_block_root: None, }; + if chain_spec.is_cancun_active_at_timestamp(timestamp) { + let parent = self.headers.get(&self.best_block); + header.parent_beacon_block_root = + parent.and_then(|parent| parent.parent_beacon_block_root); + header.blob_gas_used = Some(0); + + let (parent_excess_blob_gas, parent_blob_gas_used) = match parent { + Some(parent_block) + if chain_spec.is_cancun_active_at_timestamp(parent_block.timestamp) => + { + ( + parent_block.excess_blob_gas.unwrap_or_default(), + parent_block.blob_gas_used.unwrap_or_default(), + ) + } + _ => (0, 0), + }; + header.excess_blob_gas = + Some(calculate_excess_blob_gas(parent_excess_blob_gas, parent_blob_gas_used)) + } + header.transactions_root = if transactions.is_empty() { EMPTY_TRANSACTIONS } else { @@ -354,6 +376,7 @@ impl StorageInner { bundle_state: &BundleStateWithReceipts, client: &S, gas_used: u64, + blob_gas_used: Option, #[cfg(feature = "optimism")] chain_spec: &ChainSpec, ) -> Result { let receipts = bundle_state.receipts_by_block(header.number); @@ -381,6 +404,7 @@ impl StorageInner { }; header.gas_used = gas_used; + header.blob_gas_used = blob_gas_used; // calculate the state root let state_root = client @@ -425,6 +449,17 @@ impl StorageInner { let Block { header, body, .. } = block.block; let body = BlockBody { transactions: body, ommers: vec![], withdrawals: None }; + let mut blob_gas_used = None; + if chain_spec.is_cancun_active_at_timestamp(header.timestamp) { + let mut sum_blob_gas_used = 0; + for tx in &body.transactions { + if let Some(blob_tx) = tx.transaction.as_eip4844() { + sum_blob_gas_used += blob_tx.blob_gas(); + } + } + blob_gas_used = Some(sum_blob_gas_used); + } + trace!(target: "consensus::auto", ?bundle_state, ?header, ?body, "executed block, calculating state root and completing header"); // fill in the rest of the fields @@ -433,6 +468,7 @@ impl StorageInner { &bundle_state, client, gas_used, + blob_gas_used, #[cfg(feature = "optimism")] chain_spec.as_ref(), )?; diff --git a/crates/primitives/src/chain/spec.rs b/crates/primitives/src/chain/spec.rs index 5c28e8fd7c37..da1e3a2621f4 100644 --- a/crates/primitives/src/chain/spec.rs +++ b/crates/primitives/src/chain/spec.rs @@ -216,7 +216,7 @@ pub static DEV: Lazy> = Lazy::new(|| { "2f980576711e3617a5e4d83dd539548ec0f7792007d505a3d2e9674833af2d7c" )), paris_block_and_final_difficulty: Some((0, U256::from(0))), - fork_timestamps: ForkTimestamps::default().shanghai(0), + fork_timestamps: ForkTimestamps::default().shanghai(0).cancun(0), hardforks: BTreeMap::from([ (Hardfork::Frontier, ForkCondition::Block(0)), (Hardfork::Homestead, ForkCondition::Block(0)), @@ -235,6 +235,7 @@ pub static DEV: Lazy> = Lazy::new(|| { ForkCondition::TTD { fork_block: Some(0), total_difficulty: U256::from(0) }, ), (Hardfork::Shanghai, ForkCondition::Timestamp(0)), + (Hardfork::Cancun, ForkCondition::Timestamp(0)), ]), base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()), deposit_contract: None, // TODO: do we even have?