Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use system call to update blockhashes #10535

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

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

15 changes: 7 additions & 8 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use reth_evm::{
BlockExecutorProvider, BlockValidationError, Executor, ProviderError,
},
system_calls::{
apply_beacon_root_contract_call, apply_consolidation_requests_contract_call,
apply_withdrawal_requests_contract_call,
apply_beacon_root_contract_call, apply_blockhashes_contract_call,
apply_consolidation_requests_contract_call, apply_withdrawal_requests_contract_call,
},
ConfigureEvm,
};
Expand All @@ -24,10 +24,8 @@ use reth_primitives::{
};
use reth_prune_types::PruneModes;
use reth_revm::{
batch::BlockBatchRecord,
db::states::bundle_state::BundleRetention,
state_change::{apply_blockhashes_update, post_block_balance_increments},
Evm, State,
batch::BlockBatchRecord, db::states::bundle_state::BundleRetention,
state_change::post_block_balance_increments, Evm, State,
};
use revm_primitives::{
db::{Database, DatabaseCommit},
Expand Down Expand Up @@ -156,12 +154,13 @@ where
block.parent_beacon_block_root,
&mut evm,
)?;
apply_blockhashes_update(
evm.db_mut(),
apply_blockhashes_contract_call(
&self.evm_config,
&self.chain_spec,
block.timestamp,
block.number,
block.parent_hash,
&mut evm,
)?;

// execute transactions
Expand Down
30 changes: 16 additions & 14 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use reth_errors::RethError;
use reth_evm::{
system_calls::{
post_block_withdrawal_requests_contract_call, pre_block_beacon_root_contract_call,
pre_block_blockhashes_contract_call,
},
ConfigureEvm,
};
Expand All @@ -35,7 +36,7 @@ use reth_primitives::{
U256,
};
use reth_provider::StateProviderFactory;
use reth_revm::{database::StateProviderDatabase, state_change::apply_blockhashes_update};
use reth_revm::database::StateProviderDatabase;
use reth_transaction_pool::{BestTransactionsAttributes, TransactionPool};
use revm::{
db::states::bundle_state::BundleRetention,
Expand Down Expand Up @@ -112,7 +113,6 @@ where
.build();

let base_fee = initialized_block_env.basefee.to::<u64>();
let block_number = initialized_block_env.number.to::<u64>();
let block_gas_limit =
initialized_block_env.gas_limit.try_into().unwrap_or(chain_spec.max_gas_limit);

Expand All @@ -123,8 +123,6 @@ where
&chain_spec,
&initialized_cfg,
&initialized_block_env,
block_number,
attributes.timestamp,
attributes.parent_beacon_block_root,
)
.map_err(|err| {
Expand All @@ -137,13 +135,15 @@ where
})?;

// apply eip-2935 blockhashes update
apply_blockhashes_update(
pre_block_blockhashes_contract_call(
&mut db,
&self.evm_config,
&chain_spec,
initialized_block_env.timestamp.to::<u64>(),
block_number,
&initialized_cfg,
&initialized_block_env,
parent_block.hash(),
).map_err(|err| {
)
.map_err(|err| {
warn!(target: "payload_builder", parent_hash=%parent_block.hash(), %err, "failed to update blockhashes for empty payload");
PayloadBuilderError::Internal(err.into())
})?;
Expand Down Expand Up @@ -302,8 +302,6 @@ where
&chain_spec,
&initialized_cfg,
&initialized_block_env,
block_number,
attributes.timestamp,
attributes.parent_beacon_block_root,
)
.map_err(|err| {
Expand All @@ -316,14 +314,18 @@ where
})?;

// apply eip-2935 blockhashes update
apply_blockhashes_update(
pre_block_blockhashes_contract_call(
&mut db,
&evm_config,
&chain_spec,
initialized_block_env.timestamp.to::<u64>(),
block_number,
&initialized_cfg,
&initialized_block_env,
parent_block.hash(),
)
.map_err(|err| PayloadBuilderError::Internal(err.into()))?;
.map_err(|err| {
warn!(target: "payload_builder", parent_hash=%parent_block.hash(), %err, "failed to update blockhashes for empty payload");
PayloadBuilderError::Internal(err.into())
})?;

let mut receipts = Vec::new();
while let Some(pool_tx) = best_txs.next() {
Expand Down
8 changes: 6 additions & 2 deletions crates/evm/execution-errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ pub enum BlockValidationError {
/// The error message.
message: String,
},
/// Provider error during the [EIP-2935] block hash account loading.
/// EVM error during [EIP-2935] blockhash contract call.
///
/// [EIP-2935]: https://eips.ethereum.org/EIPS/eip-2935
BlockHashAccountLoadingFailed(ProviderError),
#[display("failed to apply blockhash contract call: {message}")]
BlockHashContractCall {
/// The error message.
message: String,
},
/// EVM error during withdrawal requests contract call [EIP-7002]
///
/// [EIP-7002]: https://eips.ethereum.org/EIPS/eip-7002
Expand Down
111 changes: 105 additions & 6 deletions crates/evm/src/system_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use {

use crate::ConfigureEvm;
use alloy_eips::{
eip2935::HISTORY_STORAGE_ADDRESS,
eip4788::BEACON_ROOTS_ADDRESS,
eip7002::{WithdrawalRequest, WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS},
eip7251::{ConsolidationRequest, CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS},
Expand All @@ -23,22 +24,120 @@ use revm_primitives::{
ResultAndState, B256,
};

/// Apply the [EIP-2935](https://eips.ethereum.org/EIPS/eip-2935) pre block contract call.
///
/// This constructs a new [`Evm`] with the given database and environment ([`CfgEnvWithHandlerCfg`]
/// and [`BlockEnv`]) to execute the pre block contract call.
///
/// This uses [`apply_blockhashes_contract_call`] to ultimately apply the blockhash contract state
/// change.
pub fn pre_block_blockhashes_contract_call<EvmConfig, DB>(
db: &mut DB,
evm_config: &EvmConfig,
chain_spec: &ChainSpec,
initialized_cfg: &CfgEnvWithHandlerCfg,
initialized_block_env: &BlockEnv,
parent_block_hash: B256,
) -> Result<(), BlockExecutionError>
where
DB: Database + DatabaseCommit,
DB::Error: Display,
EvmConfig: ConfigureEvm,
{
// Apply the pre-block EIP-2935 contract call
let mut evm_pre_block = Evm::builder()
.with_db(db)
.with_env_with_handler_cfg(EnvWithHandlerCfg::new_with_cfg_env(
initialized_cfg.clone(),
initialized_block_env.clone(),
Default::default(),
))
.build();

apply_blockhashes_contract_call(
evm_config,
chain_spec,
initialized_block_env.timestamp.to(),
initialized_block_env.number.to(),
parent_block_hash,
&mut evm_pre_block,
)
}

/// Applies the pre-block call to the [EIP-2935] blockhashes contract, using the given block,
/// [`ChainSpec`], and EVM.
///
/// If Prague is not activated, or the block is the genesis block, then this is a no-op, and no
/// state changes are made.
///
/// [EIP-2935]: https://eips.ethereum.org/EIPS/eip-2935
#[inline]
pub fn apply_blockhashes_contract_call<EvmConfig, EXT, DB>(
evm_config: &EvmConfig,
chain_spec: &ChainSpec,
block_timestamp: u64,
block_number: u64,
parent_block_hash: B256,
evm: &mut Evm<'_, EXT, DB>,
) -> Result<(), BlockExecutionError>
where
DB: Database + DatabaseCommit,
DB::Error: core::fmt::Display,
EvmConfig: ConfigureEvm,
{
if !chain_spec.is_prague_active_at_timestamp(block_timestamp) {
shekhirin marked this conversation as resolved.
Show resolved Hide resolved
return Ok(())
}

// if the block number is zero (genesis block) then no system transaction may occur as per
// EIP-2935
if block_number == 0 {
return Ok(())
}

// get previous env
let previous_env = Box::new(evm.context.env().clone());

// modify env for pre block call
evm_config.fill_tx_env_system_contract_call(
&mut evm.context.evm.env,
alloy_eips::eip4788::SYSTEM_ADDRESS,
shekhirin marked this conversation as resolved.
Show resolved Hide resolved
HISTORY_STORAGE_ADDRESS,
parent_block_hash.0.into(),
);

let mut state = match evm.transact() {
Ok(res) => res.state,
Err(e) => {
evm.context.evm.env = previous_env;
return Err(BlockValidationError::BlockHashContractCall { message: e.to_string() }.into())
}
};

state.remove(&alloy_eips::eip4788::SYSTEM_ADDRESS);
shekhirin marked this conversation as resolved.
Show resolved Hide resolved
state.remove(&evm.block().coinbase);

evm.context.evm.db.commit(state);

// re-set the previous env
evm.context.evm.env = previous_env;

Ok(())
}

/// Apply the [EIP-4788](https://eips.ethereum.org/EIPS/eip-4788) pre block contract call.
///
/// This constructs a new [Evm] with the given DB, and environment
/// This constructs a new [`Evm`] with the given DB, and environment
/// ([`CfgEnvWithHandlerCfg`] and [`BlockEnv`]) to execute the pre block contract call.
///
/// This uses [`apply_beacon_root_contract_call`] to ultimately apply the beacon root contract state
/// change.
#[allow(clippy::too_many_arguments)]
pub fn pre_block_beacon_root_contract_call<EvmConfig, DB>(
db: &mut DB,
evm_config: &EvmConfig,
chain_spec: &ChainSpec,
initialized_cfg: &CfgEnvWithHandlerCfg,
initialized_block_env: &BlockEnv,
block_number: u64,
block_timestamp: u64,
parent_beacon_block_root: Option<B256>,
) -> Result<(), BlockExecutionError>
where
Expand All @@ -60,8 +159,8 @@ where
apply_beacon_root_contract_call(
evm_config,
chain_spec,
block_timestamp,
block_number,
initialized_block_env.timestamp.to(),
initialized_block_env.number.to(),
parent_beacon_block_root,
&mut evm_pre_block,
)
Expand Down
5 changes: 0 additions & 5 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ where
.build();

let base_fee = initialized_block_env.basefee.to::<u64>();
let block_number = initialized_block_env.number.to::<u64>();
let block_gas_limit: u64 =
initialized_block_env.gas_limit.try_into().unwrap_or(chain_spec.max_gas_limit);

Expand All @@ -124,8 +123,6 @@ where
&chain_spec,
&initialized_cfg,
&initialized_block_env,
block_number,
attributes.payload_attributes.timestamp,
attributes.payload_attributes.parent_beacon_block_root,
)
.map_err(|err| {
Expand Down Expand Up @@ -289,8 +286,6 @@ where
&chain_spec,
&initialized_cfg,
&initialized_block_env,
block_number,
attributes.payload_attributes.timestamp,
attributes.payload_attributes.parent_beacon_block_root,
)
.map_err(|err| {
Expand Down
3 changes: 0 additions & 3 deletions crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ reth-trie = { workspace = true, optional = true }
# revm
revm.workspace = true

# alloy
alloy-eips.workspace = true

[dev-dependencies]
reth-trie.workspace = true
reth-ethereum-forks.workspace = true
Expand Down
Loading
Loading