feat: use system call to update blockhashes (#10535)

This commit is contained in:
Oliver
2024-08-26 13:45:08 +02:00
committed by GitHub
parent 655495d8c3
commit 20756d672c
11 changed files with 181 additions and 176 deletions

1
Cargo.lock generated
View File

@ -8281,7 +8281,6 @@ dependencies = [
name = "reth-revm"
version = "1.0.5"
dependencies = [
"alloy-eips",
"reth-chainspec",
"reth-consensus-common",
"reth-ethereum-forks",

View File

@ -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,
};
@ -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},
@ -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
@ -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},
};
@ -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
}
@ -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)
@ -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)
@ -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)

View File

@ -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,
};
@ -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,
@ -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);
@ -123,8 +123,6 @@ where
&chain_spec,
&initialized_cfg,
&initialized_block_env,
block_number,
attributes.timestamp,
attributes.parent_beacon_block_root,
)
.map_err(|err| {
@ -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())
})?;
@ -302,8 +302,6 @@ where
&chain_spec,
&initialized_cfg,
&initialized_block_env,
block_number,
attributes.timestamp,
attributes.parent_beacon_block_root,
)
.map_err(|err| {
@ -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() {

View File

@ -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

View File

@ -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},
@ -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
@ -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,
)

View File

@ -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);
@ -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| {
@ -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| {

View File

@ -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

View File

@ -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.
///
@ -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.
///

View File

@ -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},
@ -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;
@ -253,8 +253,6 @@ pub trait LoadPendingBlock: EthApiTypes {
chain_spec.as_ref(),
&cfg,
&block_env,
block_number,
block_env.timestamp.to::<u64>(),
origin.header().parent_beacon_block_root,
)
.map_err(|err| EthApiError::Internal(err.into()))?;
@ -262,13 +260,15 @@ 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,
)?;
origin.header().hash(),
)
.map_err(|err| EthApiError::Internal(err.into()))?;
let mut receipts = Vec::new();

View File

@ -2,19 +2,11 @@
//!
//! Types used in block building.
use std::{fmt, time::Instant};
use std::time::Instant;
use derive_more::Constructor;
use reth_chainspec::ChainSpec;
use reth_primitives::{BlockId, BlockNumberOrTag, SealedBlockWithSenders, SealedHeader, B256};
use reth_revm::state_change::apply_blockhashes_update;
use reth_storage_api::errors::provider::ProviderError;
use revm_primitives::{
db::{Database, DatabaseCommit},
BlockEnv, CfgEnvWithHandlerCfg,
};
use super::{EthApiError, EthResult};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg};
/// Configured [`BlockEnv`] and [`CfgEnvWithHandlerCfg`] for a pending block
#[derive(Debug, Clone, Constructor)]
@ -27,32 +19,6 @@ pub struct PendingBlockEnv {
pub origin: PendingBlockEnvOrigin,
}
/// Apply the [EIP-2935](https://eips.ethereum.org/EIPS/eip-2935) pre block state transitions.
///
/// This constructs a new [Evm](revm::Evm) with the given DB, and environment
/// [`CfgEnvWithHandlerCfg`] and [`BlockEnv`].
///
/// This uses [`apply_blockhashes_update`].
pub fn pre_block_blockhashes_update<DB: Database<Error = ProviderError> + DatabaseCommit>(
db: &mut DB,
chain_spec: &ChainSpec,
initialized_block_env: &BlockEnv,
block_number: u64,
parent_block_hash: B256,
) -> EthResult<()>
where
DB::Error: fmt::Display,
{
apply_blockhashes_update(
db,
chain_spec,
initialized_block_env.timestamp.to::<u64>(),
block_number,
parent_block_hash,
)
.map_err(|err| EthApiError::Internal(err.into()))
}
/// The origin for a configured [`PendingBlockEnv`]
#[derive(Clone, Debug)]
pub enum PendingBlockEnvOrigin {

View File

@ -2,7 +2,10 @@ use alloy_rlp::{Decodable, Encodable};
use async_trait::async_trait;
use jsonrpsee::core::RpcResult;
use reth_chainspec::{ChainSpec, EthereumHardforks};
use reth_evm::{system_calls::pre_block_beacon_root_contract_call, ConfigureEvmEnv};
use reth_evm::{
system_calls::{pre_block_beacon_root_contract_call, pre_block_blockhashes_contract_call},
ConfigureEvmEnv,
};
use reth_primitives::{
Address, Block, BlockId, BlockNumberOrTag, Bytes, TransactionSignedEcRecovered, B256, U256,
};
@ -10,7 +13,7 @@ use reth_provider::{
BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider, HeaderProvider, StateProofProvider,
StateProviderFactory, TransactionVariant,
};
use reth_revm::{database::StateProviderDatabase, state_change::apply_blockhashes_update};
use reth_revm::database::StateProviderDatabase;
use reth_rpc_api::DebugApiServer;
use reth_rpc_eth_api::{
helpers::{Call, EthApiSpec, EthTransactions, TraceExt},
@ -588,18 +591,17 @@ where
&this.inner.provider.chain_spec(),
&cfg,
&block_env,
block.timestamp,
block.number,
block.parent_beacon_block_root,
)
.map_err(|err| EthApiError::Internal(err.into()))?;
// apply eip-2935 blockhashes update
apply_blockhashes_update(
pre_block_blockhashes_contract_call(
&mut db,
&evm_config,
&this.inner.provider.chain_spec(),
block.timestamp,
block.number,
&cfg,
&block_env,
block.parent_hash,
)
.map_err(|err| EthApiError::Internal(err.into()))?;