Skip to content

Commit

Permalink
primitives/chain: dev chain support cancun upgrade (#7033)
Browse files Browse the repository at this point in the history
      Signed-off-by: jsvisa <[email protected]>
  • Loading branch information
jsvisa authored Mar 11, 2024
1 parent 72bea5f commit 66c9403
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
36 changes: 36 additions & 0 deletions crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -354,6 +376,7 @@ impl StorageInner {
bundle_state: &BundleStateWithReceipts,
client: &S,
gas_used: u64,
blob_gas_used: Option<u64>,
#[cfg(feature = "optimism")] chain_spec: &ChainSpec,
) -> Result<Header, BlockExecutionError> {
let receipts = bundle_state.receipts_by_block(header.number);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -433,6 +468,7 @@ impl StorageInner {
&bundle_state,
client,
gas_used,
blob_gas_used,
#[cfg(feature = "optimism")]
chain_spec.as_ref(),
)?;
Expand Down
3 changes: 2 additions & 1 deletion crates/primitives/src/chain/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub static DEV: Lazy<Arc<ChainSpec>> = 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)),
Expand All @@ -235,6 +235,7 @@ pub static DEV: Lazy<Arc<ChainSpec>> = 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?
Expand Down

0 comments on commit 66c9403

Please sign in to comment.