Skip to content

Commit

Permalink
integrate blob_params_at_timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo committed Jan 31, 2025
1 parent 62edaf1 commit 4077ec0
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 53 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/engine/util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ workspace = true
reth-primitives.workspace = true
reth-primitives-traits.workspace = true
reth-errors.workspace = true
reth-chainspec.workspace = true
reth-consensus-common.workspace = true
reth-fs-util.workspace = true
reth-rpc-types-compat.workspace = true
Expand Down
13 changes: 5 additions & 8 deletions crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Stream wrapper that simulates reorgs.
use alloy_consensus::{Header, Transaction};
use alloy_eips::eip7840::BlobParams;
use alloy_rpc_types_engine::{
CancunPayloadFields, ExecutionPayload, ExecutionPayloadSidecar, ForkchoiceState, PayloadStatus,
};
use futures::{stream::FuturesUnordered, Stream, StreamExt, TryFutureExt};
use itertools::Either;
use reth_chainspec::EthChainSpec;
use reth_engine_primitives::{
BeaconEngineMessage, BeaconOnNewPayloadError, EngineTypes, OnForkChoiceUpdated,
};
Expand Down Expand Up @@ -109,7 +109,7 @@ where
Engine: EngineTypes,
Provider: BlockReader<Block = reth_primitives::Block> + StateProviderFactory,
Evm: ConfigureEvm<Header = Header, Transaction = reth_primitives::TransactionSigned>,
Spec: EthereumHardforks,
Spec: EthChainSpec + EthereumHardforks,
{
type Item = S::Item;

Expand Down Expand Up @@ -256,7 +256,7 @@ fn create_reorg_head<Provider, Evm, Spec>(
where
Provider: BlockReader<Block = reth_primitives::Block> + StateProviderFactory,
Evm: ConfigureEvm<Header = Header, Transaction = reth_primitives::TransactionSigned>,
Spec: EthereumHardforks,
Spec: EthChainSpec + EthereumHardforks,
{
let chain_spec = payload_validator.chain_spec();

Expand Down Expand Up @@ -380,11 +380,8 @@ where
let hashed_state = state_provider.hashed_post_state(outcome.state());

let (blob_gas_used, excess_blob_gas) =
if chain_spec.is_cancun_active_at_timestamp(reorg_target.timestamp) {
(
Some(sum_blob_gas_used),
reorg_target_parent.next_block_excess_blob_gas(BlobParams::cancun()),
)
if let Some(blob_params) = chain_spec.blob_params_at_timestamp(reorg_target.timestamp) {
(Some(sum_blob_gas_used), reorg_target_parent.next_block_excess_blob_gas(blob_params))
} else {
(None, None)
};
Expand Down
10 changes: 2 additions & 8 deletions crates/ethereum/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use alloy_consensus::EMPTY_OMMER_ROOT_HASH;
use alloy_eips::{eip7840::BlobParams, merge::ALLOWED_FUTURE_BLOCK_TIME_SECONDS};
use alloy_eips::merge::ALLOWED_FUTURE_BLOCK_TIME_SECONDS;
use alloy_primitives::U256;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_consensus::{
Expand Down Expand Up @@ -192,13 +192,7 @@ where
)?;

// ensure that the blob gas fields for this block
if self.chain_spec.is_cancun_active_at_timestamp(header.timestamp()) {
let blob_params = if self.chain_spec.is_prague_active_at_timestamp(header.timestamp()) {
BlobParams::prague()
} else {
BlobParams::cancun()
};

if let Some(blob_params) = self.chain_spec.blob_params_at_timestamp(header.timestamp()) {
validate_against_parent_4844(header.header(), parent.header(), blob_params)?;
}

Expand Down
11 changes: 5 additions & 6 deletions crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use alloc::{sync::Arc, vec::Vec};
use alloy_consensus::{BlockHeader, Header};
use alloy_primitives::{Address, U256};
use core::{convert::Infallible, fmt::Debug};
use reth_chainspec::ChainSpec;
use reth_chainspec::{ChainSpec, EthChainSpec};
use reth_evm::{env::EvmEnv, ConfigureEvm, ConfigureEvmEnv, Database, Evm, NextBlockEnvAttributes};
use reth_primitives::TransactionSigned;
use reth_primitives_traits::transaction::execute::FillTxEnv;
Expand All @@ -32,7 +32,7 @@ use revm_primitives::{
};

mod config;
use alloy_eips::{eip1559::INITIAL_BASE_FEE, eip7840::BlobParams};
use alloy_eips::eip1559::INITIAL_BASE_FEE;
pub use config::{revm_spec, revm_spec_by_timestamp_and_block_number};
use reth_ethereum_forks::EthereumHardfork;

Expand Down Expand Up @@ -188,13 +188,12 @@ impl ConfigureEvmEnv for EthEvmConfig {
parent.number() + 1,
);

let blob_params =
if spec_id >= SpecId::PRAGUE { BlobParams::prague() } else { BlobParams::cancun() };

// if the parent block did not have excess blob gas (i.e. it was pre-cancun), but it is
// cancun now, we need to set the excess blob gas to the default value(0)
let blob_excess_gas_and_price = parent
.next_block_excess_blob_gas(blob_params)
.maybe_next_block_excess_blob_gas(
self.chain_spec.blob_params_at_timestamp(attributes.timestamp),
)
.or_else(|| (spec_id == SpecId::CANCUN).then_some(0))
.map(|gas| BlobExcessGasAndPrice::new(gas, spec_id >= SpecId::PRAGUE));

Expand Down
29 changes: 12 additions & 17 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![allow(clippy::useless_let_if_seq)]

use alloy_consensus::{Header, Transaction, Typed2718, EMPTY_OMMER_ROOT_HASH};
use alloy_consensus::{BlockHeader, Header, Transaction, Typed2718, EMPTY_OMMER_ROOT_HASH};
use alloy_eips::{
eip4844::MAX_DATA_GAS_PER_BLOCK, eip6110, eip7685::Requests, eip7840::BlobParams,
merge::BEACON_NONCE,
eip4844::MAX_DATA_GAS_PER_BLOCK, eip6110, eip7685::Requests, merge::BEACON_NONCE,
};
use alloy_primitives::U256;
use reth_basic_payload_builder::{
commit_withdrawals, is_better_payload, BuildArguments, BuildOutcome, PayloadBuilder,
PayloadConfig,
};
use reth_chainspec::{ChainSpec, ChainSpecProvider};
use reth_chainspec::{ChainSpec, ChainSpecProvider, EthChainSpec};
use reth_errors::RethError;
use reth_evm::{
env::EvmEnv, system_calls::SystemCaller, ConfigureEvm, Evm, EvmError, InvalidTxError,
Expand Down Expand Up @@ -418,19 +417,15 @@ where
)
.map_err(PayloadBuilderError::other)?;

excess_blob_gas = if chain_spec.is_cancun_active_at_timestamp(parent_header.timestamp) {
let blob_params = if chain_spec.is_prague_active_at_timestamp(attributes.timestamp) {
BlobParams::prague()
} else {
// cancun
BlobParams::cancun()
};
parent_header.next_block_excess_blob_gas(blob_params)
} else {
// for the first post-fork block, both parent.blob_gas_used and
// parent.excess_blob_gas are evaluated as 0
Some(alloy_eips::eip4844::calc_excess_blob_gas(0, 0))
};
excess_blob_gas = parent_header
.maybe_next_block_excess_blob_gas(
chain_spec.blob_params_at_timestamp(attributes.timestamp),
)
.or_else(|| {
// for the first post-fork block, both parent.blob_gas_used and
// parent.excess_blob_gas are evaluated as 0
Some(alloy_eips::eip4844::calc_excess_blob_gas(0, 0))
});

blob_gas_used = Some(sum_blob_gas_used);
}
Expand Down
7 changes: 3 additions & 4 deletions crates/optimism/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ extern crate alloc;

use alloc::sync::Arc;
use alloy_consensus::{BlockHeader as _, EMPTY_OMMER_ROOT_HASH};
use alloy_eips::eip7840::BlobParams;
use alloy_primitives::{B64, U256};
use reth_chainspec::EthereumHardforks;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_consensus::{
Consensus, ConsensusError, FullConsensus, HeaderValidator, PostExecutionInput,
};
Expand Down Expand Up @@ -149,8 +148,8 @@ impl<H: BlockHeader> HeaderValidator<H> for OpBeaconConsensus {
}

// ensure that the blob gas fields for this block
if self.chain_spec.is_cancun_active_at_timestamp(header.timestamp()) {
validate_against_parent_4844(header.header(), parent.header(), BlobParams::cancun())?;
if let Some(blob_params) = self.chain_spec.blob_params_at_timestamp(header.timestamp()) {
validate_against_parent_4844(header.header(), parent.header(), blob_params)?;
}

Ok(())
Expand Down
6 changes: 4 additions & 2 deletions crates/optimism/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ extern crate alloc;

use alloc::sync::Arc;
use alloy_consensus::{BlockHeader, Header};
use alloy_eips::eip7840::BlobParams;
use alloy_primitives::{Address, U256};
use core::fmt::Debug;
use op_alloy_consensus::EIP1559ParamError;
use reth_chainspec::EthChainSpec;
use reth_evm::{env::EvmEnv, ConfigureEvm, ConfigureEvmEnv, Database, Evm, NextBlockEnvAttributes};
use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_primitives::OpTransactionSigned;
Expand Down Expand Up @@ -194,7 +194,9 @@ impl ConfigureEvmEnv for OpEvmConfig {
// if the parent block did not have excess blob gas (i.e. it was pre-cancun), but it is
// cancun now, we need to set the excess blob gas to the default value(0)
let blob_excess_gas_and_price = parent
.next_block_excess_blob_gas(BlobParams::cancun())
.maybe_next_block_excess_blob_gas(
self.chain_spec().blob_params_at_timestamp(attributes.timestamp),
)
.or_else(|| (spec_id.is_enabled_in(SpecId::CANCUN)).then_some(0))
.map(|gas| BlobExcessGasAndPrice::new(gas, false));

Expand Down
21 changes: 18 additions & 3 deletions crates/rpc/rpc-eth-api/src/helpers/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ pub trait EthFees: LoadFee {
for header in &headers {
base_fee_per_gas.push(header.base_fee_per_gas().unwrap_or_default() as u128);
gas_used_ratio.push(header.gas_used() as f64 / header.gas_limit() as f64);
base_fee_per_blob_gas.push(header.blob_fee(BlobParams::cancun()).unwrap_or_default());

let blob_params = self.provider()
.chain_spec()
.blob_params_at_timestamp(header.timestamp())
.unwrap_or_else(BlobParams::cancun);

base_fee_per_blob_gas.push(header.blob_fee(blob_params).unwrap_or_default());
blob_gas_used_ratio.push(
header.blob_gas_used().unwrap_or_default() as f64
/ alloy_eips::eip4844::MAX_DATA_GAS_PER_BLOCK as f64,
Expand Down Expand Up @@ -206,7 +212,12 @@ pub trait EthFees: LoadFee {

// Same goes for the `base_fee_per_blob_gas`:
// > "[..] includes the next block after the newest of the returned range, because this value can be derived from the newest block.
base_fee_per_blob_gas.push(last_header.next_block_blob_fee(BlobParams::cancun()).unwrap_or_default());
base_fee_per_blob_gas.push(
last_header
.maybe_next_block_blob_fee(
self.provider().chain_spec().blob_params_at_timestamp(last_header.timestamp())
).unwrap_or_default()
);
};

Ok(FeeHistory {
Expand Down Expand Up @@ -331,7 +342,11 @@ pub trait LoadFee: LoadBlock {
async move {
self.block_with_senders(BlockNumberOrTag::Latest.into())
.await?
.and_then(|h| h.next_block_blob_fee(BlobParams::cancun()))
.and_then(|h| {
h.maybe_next_block_blob_fee(
self.provider().chain_spec().blob_params_at_timestamp(h.timestamp()),
)
})
.ok_or(EthApiError::ExcessBlobGasNotSet.into())
.map(U256::from)
}
Expand Down
15 changes: 10 additions & 5 deletions crates/transaction-pool/src/maintain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
BlockInfo, PoolTransaction, PoolUpdateKind,
};
use alloy_consensus::{BlockHeader, Typed2718};
use alloy_eips::{eip7840::BlobParams, BlockNumberOrTag};
use alloy_eips::BlockNumberOrTag;
use alloy_primitives::{Address, BlockHash, BlockNumber};
use alloy_rlp::Encodable;
use futures_util::{
Expand Down Expand Up @@ -119,7 +119,9 @@ pub async fn maintain_transaction_pool<N, Client, P, St, Tasks>(
chain_spec.base_fee_params_at_timestamp(latest.timestamp() + 12),
)
.unwrap_or_default(),
pending_blob_fee: latest.next_block_blob_fee(BlobParams::cancun()),
pending_blob_fee: latest.maybe_next_block_blob_fee(
chain_spec.blob_params_at_timestamp(latest.timestamp() + 12),
),
};
pool.set_block_info(info);
}
Expand Down Expand Up @@ -277,8 +279,9 @@ pub async fn maintain_transaction_pool<N, Client, P, St, Tasks>(
chain_spec.base_fee_params_at_timestamp(new_tip.timestamp() + 12),
)
.unwrap_or_default();
let pending_block_blob_fee =
new_tip.header().next_block_blob_fee(BlobParams::cancun());
let pending_block_blob_fee = new_tip.header().maybe_next_block_blob_fee(
chain_spec.blob_params_at_timestamp(new_tip.timestamp() + 12),
);

// we know all changed account in the new chain
let new_changed_accounts: HashSet<_> =
Expand Down Expand Up @@ -382,7 +385,9 @@ pub async fn maintain_transaction_pool<N, Client, P, St, Tasks>(
chain_spec.base_fee_params_at_timestamp(tip.timestamp() + 12),
)
.unwrap_or_default();
let pending_block_blob_fee = tip.header().next_block_blob_fee(BlobParams::cancun());
let pending_block_blob_fee = tip.header().maybe_next_block_blob_fee(
chain_spec.blob_params_at_timestamp(tip.timestamp() + 12),
);

let first_block = blocks.first();
trace!(
Expand Down

0 comments on commit 4077ec0

Please sign in to comment.