Skip to content

Commit

Permalink
feat(payload): include EIP-7002 withdrawal requests (#8227)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed May 29, 2024
1 parent 202215d commit b9101bc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 25 deletions.
6 changes: 3 additions & 3 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use reth_revm::{
batch::{BlockBatchRecord, BlockExecutorStats},
db::states::bundle_state::BundleRetention,
state_change::{
apply_beacon_root_contract_call, post_block_balance_increments,
post_block_withdrawal_requests,
apply_beacon_root_contract_call, apply_withdrawal_requests_contract_call,
post_block_balance_increments,
},
Evm, State,
};
Expand Down Expand Up @@ -197,7 +197,7 @@ where

// Collect all EIP-7685 requests
let withdrawal_requests =
post_block_withdrawal_requests(&self.chain_spec, block.timestamp, &mut evm)?;
apply_withdrawal_requests_contract_call(&self.chain_spec, block.timestamp, &mut evm)?;
let requests = withdrawal_requests;

Ok(EthExecuteOutput { receipts, requests, gas_used: cumulative_gas_used })
Expand Down
38 changes: 36 additions & 2 deletions crates/payload/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ use reth_payload_builder::{
};
use reth_primitives::{
constants::{EMPTY_WITHDRAWALS, RETH_CLIENT_VERSION, SLOT_DURATION},
proofs, BlockNumberOrTag, Bytes, ChainSpec, SealedBlock, Withdrawals, B256, U256,
proofs, BlockNumberOrTag, Bytes, ChainSpec, Request, SealedBlock, Withdrawals, B256, U256,
};
use reth_provider::{
BlockReaderIdExt, BlockSource, CanonStateNotification, ProviderError, StateProviderFactory,
};
use reth_revm::state_change::{
apply_beacon_root_contract_call, post_block_withdrawals_balance_increments,
apply_beacon_root_contract_call, apply_withdrawal_requests_contract_call,
post_block_withdrawals_balance_increments,
};
use reth_tasks::TaskSpawner;
use reth_transaction_pool::TransactionPool;
Expand Down Expand Up @@ -888,6 +889,39 @@ where
.map_err(|err| PayloadBuilderError::Internal(err.into()))
}

/// Apply the [EIP-7002](https://eips.ethereum.org/EIPS/eip-7002) post block contract call.
///
/// This constructs a new [Evm] with the given DB, and environment
/// ([CfgEnvWithHandlerCfg] and [BlockEnv]) to execute the post block contract call.
///
/// This uses [apply_withdrawal_requests_contract_call] to ultimately calculate the
/// [requests](Request).
pub fn post_block_withdrawal_requests_contract_call<DB: Database + DatabaseCommit, Attributes>(
db: &mut DB,
chain_spec: &ChainSpec,
initialized_cfg: &CfgEnvWithHandlerCfg,
initialized_block_env: &BlockEnv,
attributes: &Attributes,
) -> Result<Vec<Request>, PayloadBuilderError>
where
DB::Error: std::fmt::Display,
Attributes: PayloadBuilderAttributes,
{
// apply post-block EIP-7002 contract call
let mut evm_post_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();

// initialize a block from the env, because the post block call needs the block itself
apply_withdrawal_requests_contract_call(chain_spec, attributes.timestamp(), &mut evm_post_block)
.map_err(|err| PayloadBuilderError::Internal(err.into()))
}

/// Checks if the new payload is better than the current best.
///
/// This compares the total fees of the blocks, higher is better.
Expand Down
42 changes: 23 additions & 19 deletions crates/payload/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#![allow(clippy::useless_let_if_seq)]

use reth_basic_payload_builder::{
commit_withdrawals, is_better_payload, pre_block_beacon_root_contract_call, BuildArguments,
BuildOutcome, PayloadBuilder, PayloadConfig, WithdrawalsOutcome,
commit_withdrawals, is_better_payload, post_block_withdrawal_requests_contract_call,
pre_block_beacon_root_contract_call, BuildArguments, BuildOutcome, PayloadBuilder,
PayloadConfig, WithdrawalsOutcome,
};
use reth_evm::ConfigureEvm;
use reth_evm_ethereum::EthEvmConfig;
Expand All @@ -24,7 +25,7 @@ use reth_primitives::{
EMPTY_TRANSACTIONS,
},
eip4844::calculate_excess_blob_gas,
proofs,
proofs::{self, calculate_requests_root},
revm::env::tx_env_with_recovered,
Block, Header, IntoRecoveredTransaction, Receipt, Receipts, Requests, EMPTY_OMMER_ROOT_HASH,
U256,
Expand Down Expand Up @@ -142,14 +143,6 @@ where
err
})?;

// Calculate the requests and the requests root.
let (requests, requests_root) =
if chain_spec.is_prague_active_at_timestamp(attributes.timestamp) {
(Some(Requests::default()), Some(EMPTY_ROOT_HASH))
} else {
(None, None)
};

// merge all transitions into bundle state, this would apply the withdrawal balance
// changes and 4788 contract call
db.merge_transitions(BundleRetention::PlainState);
Expand Down Expand Up @@ -385,6 +378,25 @@ where
return Ok(BuildOutcome::Aborted { fees: total_fees, cached_reads })
}

// calculate the requests and the requests root
let (requests, requests_root) =
if chain_spec.is_prague_active_at_timestamp(attributes.timestamp) {
let withdrawal_requests = post_block_withdrawal_requests_contract_call(
&mut db,
&chain_spec,
&initialized_cfg,
&initialized_block_env,
&attributes,
)?;
// TODO: add deposit requests (https://github.com/paradigmxyz/reth/pull/8204)

let requests = withdrawal_requests;
let requests_root = calculate_requests_root(&requests);
(Some(requests.into()), Some(requests_root))
} else {
(None, None)
};

let WithdrawalsOutcome { withdrawals_root, withdrawals } =
commit_withdrawals(&mut db, &chain_spec, attributes.timestamp, attributes.withdrawals)?;

Expand Down Expand Up @@ -434,14 +446,6 @@ where
blob_gas_used = Some(sum_blob_gas_used);
}

// todo: compute requests and requests root
let (requests, requests_root) =
if chain_spec.is_prague_active_at_timestamp(attributes.timestamp) {
(Some(Requests::default()), Some(EMPTY_ROOT_HASH))
} else {
(None, None)
};

let header = Header {
parent_hash: parent_block.hash(),
ommers_hash: EMPTY_OMMER_ROOT_HASH,
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/state_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub fn insert_post_block_withdrawals_balance_increments(
/// If Prague is not active at the given timestamp, then this is a no-op, and an empty vector is
/// returned. Otherwise, the withdrawal requests are returned.
#[inline]
pub fn post_block_withdrawal_requests<EXT, DB: Database + DatabaseCommit>(
pub fn apply_withdrawal_requests_contract_call<EXT, DB: Database + DatabaseCommit>(
chain_spec: &ChainSpec,
block_timestamp: u64,
evm: &mut Evm<'_, EXT, DB>,
Expand Down

0 comments on commit b9101bc

Please sign in to comment.