Skip to content

Commit

Permalink
feat: use system call to update blockhashes (#10535)
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg committed Aug 26, 2024
1 parent 655495d commit 20756d6
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 176 deletions.
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.

45 changes: 31 additions & 14 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 Expand Up @@ -467,7 +466,7 @@ where
mod tests {
use super::*;
use alloy_eips::{
eip2935::HISTORY_STORAGE_ADDRESS,
eip2935::{HISTORY_STORAGE_ADDRESS, HISTORY_STORAGE_CODE},
eip4788::{BEACON_ROOTS_ADDRESS, BEACON_ROOTS_CODE, SYSTEM_ADDRESS},
eip7002::{WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS, WITHDRAWAL_REQUEST_PREDEPLOY_CODE},
};
Expand Down Expand Up @@ -868,11 +867,26 @@ mod tests {
assert_eq!(parent_beacon_block_root_storage, U256::from(0x69));
}

/// Create a state provider with blockhashes and the EIP-2935 system contract.
fn create_state_provider_with_block_hashes(latest_block: u64) -> StateProviderTest {
let mut db = StateProviderTest::default();
for block_number in 0..=latest_block {
db.insert_block_hash(block_number, keccak256(block_number.to_string()));
}

let blockhashes_contract_account = Account {
balance: U256::ZERO,
bytecode_hash: Some(keccak256(HISTORY_STORAGE_CODE.clone())),
nonce: 1,
};

db.insert_account(
HISTORY_STORAGE_ADDRESS,
blockhashes_contract_account,
Some(HISTORY_STORAGE_CODE.clone()),
HashMap::new(),
);

db
}

Expand Down Expand Up @@ -918,9 +932,9 @@ mod tests {
// ensure that the block hash was *not* written to storage, since this is before the fork
// was activated
//
// we load the account first, which should also not exist, because revm expects it to be
// we load the account first, because revm expects it to be
// loaded
assert!(executor.state_mut().basic(HISTORY_STORAGE_ADDRESS).unwrap().is_none());
executor.state_mut().basic(HISTORY_STORAGE_ADDRESS).unwrap();
assert!(executor
.state_mut()
.storage(HISTORY_STORAGE_ADDRESS, U256::ZERO)
Expand Down Expand Up @@ -968,9 +982,9 @@ mod tests {
// ensure that the block hash was *not* written to storage, since there are no blocks
// preceding genesis
//
// we load the account first, which should also not exist, because revm expects it to be
// we load the account first, because revm expects it to be
// loaded
assert!(executor.state_mut().basic(HISTORY_STORAGE_ADDRESS).unwrap().is_none());
executor.state_mut().basic(HISTORY_STORAGE_ADDRESS).unwrap();
assert!(executor
.state_mut()
.storage(HISTORY_STORAGE_ADDRESS, U256::ZERO)
Expand Down Expand Up @@ -1140,7 +1154,10 @@ mod tests {
);

// nothing should be written as the genesis has no ancestors
assert!(executor.state_mut().basic(HISTORY_STORAGE_ADDRESS).unwrap().is_none());
//
// we load the account first, because revm expects it to be
// loaded
executor.state_mut().basic(HISTORY_STORAGE_ADDRESS).unwrap();
assert!(executor
.state_mut()
.storage(HISTORY_STORAGE_ADDRESS, U256::ZERO)
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) {
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,
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);
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
Loading

0 comments on commit 20756d6

Please sign in to comment.