Skip to content

Commit

Permalink
feat: use system call to update blockhashes
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg committed Aug 26, 2024
1 parent 29058ad commit 6b86b5e
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 146 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.

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
25 changes: 18 additions & 7 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 @@ -137,13 +138,17 @@ 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>(),
&initialized_cfg,
&initialized_block_env,
block_number,
attributes.timestamp,
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 @@ -316,14 +321,20 @@ 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>(),
&initialized_cfg,
&initialized_block_env,
block_number,
attributes.timestamp,
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
107 changes: 106 additions & 1 deletion 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,9 +24,113 @@ 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.
#[allow(clippy::too_many_arguments)]
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,
block_number: u64,
block_timestamp: u64,
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,
block_timestamp,
block_number,
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
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
78 changes: 1 addition & 77 deletions crates/revm/src/state_change.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use crate::precompile::HashMap;
use alloy_eips::eip2935::{HISTORY_STORAGE_ADDRESS, HISTORY_STORAGE_CODE};
use reth_chainspec::{ChainSpec, EthereumHardforks};
use reth_consensus_common::calc;
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_primitives::{Address, Block, Withdrawal, Withdrawals, B256, U256};
use reth_storage_errors::provider::ProviderError;
use revm::{
primitives::{Account, AccountInfo, Bytecode, EvmStorageSlot, BLOCKHASH_SERVE_WINDOW},
Database, DatabaseCommit,
};
use reth_primitives::{Address, Block, Withdrawal, Withdrawals, U256};

/// Collect all balance changes at the end of the block.
///
Expand Down Expand Up @@ -48,75 +41,6 @@ pub fn post_block_balance_increments(
balance_increments
}

/// Applies the pre-block state change outlined in [EIP-2935] to store historical blockhashes in a
/// system contract.
///
/// If Prague is not activated, or the block is the genesis block, then this is a no-op, and no
/// state changes are made.
///
/// If the provided block is after Prague has been activated, the parent hash will be inserted.
///
/// [EIP-2935]: https://eips.ethereum.org/EIPS/eip-2935
#[inline]
pub fn apply_blockhashes_update<DB: Database<Error: Into<ProviderError>> + DatabaseCommit>(
db: &mut DB,
chain_spec: &ChainSpec,
block_timestamp: u64,
block_number: u64,
parent_block_hash: B256,
) -> Result<(), BlockExecutionError>
where
DB::Error: core::fmt::Display,
{
// If Prague is not activated or this is the genesis block, no hashes are added.
if !chain_spec.is_prague_active_at_timestamp(block_timestamp) || block_number == 0 {
return Ok(())
}
assert!(block_number > 0);

// Account is expected to exist either in genesis (for tests) or deployed on mainnet or
// testnets.
// If the account for any reason does not exist, we create it with the EIP-2935 bytecode and a
// nonce of 1, so it does not get deleted.
let mut account: Account = db
.basic(HISTORY_STORAGE_ADDRESS)
.map_err(|err| BlockValidationError::BlockHashAccountLoadingFailed(err.into()))?
.unwrap_or_else(|| AccountInfo {
nonce: 1,
code: Some(Bytecode::new_raw(HISTORY_STORAGE_CODE.clone())),
..Default::default()
})
.into();

// Insert the state change for the slot
let (slot, value) = eip2935_block_hash_slot(db, block_number - 1, parent_block_hash)?;
account.storage.insert(slot, value);

// Mark the account as touched and commit the state change
account.mark_touch();
db.commit(HashMap::from([(HISTORY_STORAGE_ADDRESS, account)]));

Ok(())
}

/// Helper function to create a [`EvmStorageSlot`] for [EIP-2935] state transitions for a given
/// block number.
///
/// This calculates the correct storage slot in the `BLOCKHASH` history storage address, fetches the
/// blockhash and creates a [`EvmStorageSlot`] with appropriate previous and new values.
fn eip2935_block_hash_slot<DB: Database<Error: Into<ProviderError>>>(
db: &mut DB,
block_number: u64,
block_hash: B256,
) -> Result<(U256, EvmStorageSlot), BlockValidationError> {
let slot = U256::from(block_number % BLOCKHASH_SERVE_WINDOW as u64);
let current_hash = db
.storage(HISTORY_STORAGE_ADDRESS, slot)
.map_err(|err| BlockValidationError::BlockHashAccountLoadingFailed(err.into()))?;

Ok((slot, EvmStorageSlot::new_changed(current_hash, block_hash.into())))
}

/// Returns a map of addresses to their balance increments if the Shanghai hardfork is active at the
/// given timestamp.
///
Expand Down
20 changes: 12 additions & 8 deletions crates/rpc/rpc-eth-api/src/helpers/pending_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::time::{Duration, Instant};

use futures::Future;
use reth_chainspec::{ChainSpec, EthereumHardforks};
use reth_evm::{system_calls::pre_block_beacon_root_contract_call, ConfigureEvm, ConfigureEvmEnv};
use reth_evm::{
system_calls::{pre_block_beacon_root_contract_call, pre_block_blockhashes_contract_call},
ConfigureEvm, ConfigureEvmEnv,
};
use reth_execution_types::ExecutionOutcome;
use reth_primitives::{
constants::{eip4844::MAX_DATA_GAS_PER_BLOCK, BEACON_NONCE, EMPTY_ROOT_HASH},
Expand All @@ -25,10 +28,7 @@ use reth_provider::{
use reth_revm::{
database::StateProviderDatabase, state_change::post_block_withdrawals_balance_increments,
};
use reth_rpc_eth_types::{
pending_block::pre_block_blockhashes_update, EthApiError, PendingBlock, PendingBlockEnv,
PendingBlockEnvOrigin,
};
use reth_rpc_eth_types::{EthApiError, PendingBlock, PendingBlockEnv, PendingBlockEnvOrigin};
use reth_transaction_pool::{BestTransactionsAttributes, TransactionPool};
use revm::{db::states::bundle_state::BundleRetention, DatabaseCommit, State};
use tokio::sync::Mutex;
Expand Down Expand Up @@ -262,13 +262,17 @@ pub trait LoadPendingBlock: EthApiTypes {
} else {
None
};
pre_block_blockhashes_update(
pre_block_blockhashes_contract_call(
&mut db,
self.evm_config(),
chain_spec.as_ref(),
&cfg,
&block_env,
block_number,
parent_hash,
)?;
block_env.timestamp.to::<u64>(),
origin.header().hash(),
)
.map_err(|err| EthApiError::Internal(err.into()))?;

let mut receipts = Vec::new();

Expand Down
Loading

0 comments on commit 6b86b5e

Please sign in to comment.