refactor: simplify withdrawals outcome (#12780)

This commit is contained in:
Hai | RISE
2024-11-22 21:15:43 +07:00
committed by GitHub
parent 87ecb43413
commit 64728e0856
3 changed files with 23 additions and 56 deletions

View File

@@ -14,7 +14,7 @@ use alloy_eips::{eip4844::MAX_DATA_GAS_PER_BLOCK, eip7685::Requests, merge::BEAC
use alloy_primitives::U256;
use reth_basic_payload_builder::{
commit_withdrawals, is_better_payload, BuildArguments, BuildOutcome, PayloadBuilder,
PayloadConfig, WithdrawalsOutcome,
PayloadConfig,
};
use reth_chain_state::ExecutedBlock;
use reth_chainspec::ChainSpec;
@@ -356,8 +356,8 @@ where
None
};
let WithdrawalsOutcome { withdrawals_root, withdrawals } =
commit_withdrawals(&mut db, &chain_spec, attributes.timestamp, attributes.withdrawals)?;
let withdrawals_root =
commit_withdrawals(&mut db, &chain_spec, attributes.timestamp, &attributes.withdrawals)?;
// merge all transitions into bundle state, this would apply the withdrawal balance changes
// and 4788 contract call
@@ -443,7 +443,11 @@ where
// seal the block
let block = Block {
header,
body: BlockBody { transactions: executed_txs, ommers: vec![], withdrawals },
body: BlockBody {
transactions: executed_txs,
ommers: vec![],
withdrawals: Some(attributes.withdrawals.clone()),
},
};
let sealed_block = Arc::new(block.seal_slow());

View File

@@ -4,7 +4,7 @@ use std::{fmt::Display, sync::Arc};
use alloy_consensus::{Header, Transaction, EMPTY_OMMER_ROOT_HASH};
use alloy_eips::merge::BEACON_NONCE;
use alloy_primitives::{Address, Bytes, U256};
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_rpc_types_debug::ExecutionWitness;
use alloy_rpc_types_engine::PayloadId;
use reth_basic_payload_builder::*;
@@ -318,13 +318,13 @@ where
}
}
let withdrawals_outcome = ctx.commit_withdrawals(state)?;
let withdrawals_root = ctx.commit_withdrawals(state)?;
// merge all transitions into bundle state, this would apply the withdrawal balance changes
// and 4788 contract call
state.merge_transitions(BundleRetention::Reverts);
Ok(BuildOutcomeKind::Better { payload: ExecutedPayload { info, withdrawals_outcome } })
Ok(BuildOutcomeKind::Better { payload: ExecutedPayload { info, withdrawals_root } })
}
/// Builds the payload on top of the state.
@@ -338,10 +338,7 @@ where
DB: Database<Error = ProviderError> + AsRef<P>,
P: StateRootProvider,
{
let ExecutedPayload {
info,
withdrawals_outcome: WithdrawalsOutcome { withdrawals, withdrawals_root },
} = match self.execute(&mut state, &ctx)? {
let ExecutedPayload { info, withdrawals_root } = match self.execute(&mut state, &ctx)? {
BuildOutcomeKind::Better { payload } | BuildOutcomeKind::Freeze(payload) => payload,
BuildOutcomeKind::Cancelled => return Ok(BuildOutcomeKind::Cancelled),
BuildOutcomeKind::Aborted { fees } => return Ok(BuildOutcomeKind::Aborted { fees }),
@@ -419,7 +416,7 @@ where
body: BlockBody {
transactions: info.executed_transactions,
ommers: vec![],
withdrawals,
withdrawals: Some(ctx.attributes().payload_attributes.withdrawals.clone()),
},
};
@@ -501,8 +498,8 @@ impl OpPayloadTransactions for () {
pub struct ExecutedPayload {
/// Tracked execution info
pub info: ExecutionInfo,
/// Outcome after committing withdrawals.
pub withdrawals_outcome: WithdrawalsOutcome,
/// Withdrawal hash.
pub withdrawals_root: Option<B256>,
}
/// This acts as the container for executed transactions and its byproducts (receipts, gas used)
@@ -652,10 +649,7 @@ impl<EvmConfig> OpPayloadBuilderCtx<EvmConfig> {
}
/// Commits the withdrawals from the payload attributes to the state.
pub fn commit_withdrawals<DB>(
&self,
db: &mut State<DB>,
) -> Result<WithdrawalsOutcome, ProviderError>
pub fn commit_withdrawals<DB>(&self, db: &mut State<DB>) -> Result<Option<B256>, ProviderError>
where
DB: Database<Error = ProviderError>,
{
@@ -663,7 +657,7 @@ impl<EvmConfig> OpPayloadBuilderCtx<EvmConfig> {
db,
&self.chain_spec,
self.attributes().payload_attributes.timestamp,
self.attributes().payload_attributes.withdrawals.clone(),
&self.attributes().payload_attributes.withdrawals,
)
}

View File

@@ -979,31 +979,6 @@ impl<Payload> Default for MissingPayloadBehaviour<Payload> {
}
}
/// Represents the outcome of committing withdrawals to the runtime database and post state.
/// Pre-shanghai these are `None` values.
#[derive(Default, Debug)]
pub struct WithdrawalsOutcome {
/// committed withdrawals, if any.
pub withdrawals: Option<Withdrawals>,
/// withdrawals root if any.
pub withdrawals_root: Option<B256>,
}
impl WithdrawalsOutcome {
/// No withdrawals pre shanghai
pub const fn pre_shanghai() -> Self {
Self { withdrawals: None, withdrawals_root: None }
}
/// No withdrawals
pub fn empty() -> Self {
Self {
withdrawals: Some(Withdrawals::default()),
withdrawals_root: Some(EMPTY_WITHDRAWALS),
}
}
}
/// Executes the withdrawals and commits them to the _runtime_ Database and `BundleState`.
///
/// Returns the withdrawals root.
@@ -1013,32 +988,26 @@ pub fn commit_withdrawals<DB, ChainSpec>(
db: &mut State<DB>,
chain_spec: &ChainSpec,
timestamp: u64,
withdrawals: Withdrawals,
) -> Result<WithdrawalsOutcome, DB::Error>
withdrawals: &Withdrawals,
) -> Result<Option<B256>, DB::Error>
where
DB: Database,
ChainSpec: EthereumHardforks,
{
if !chain_spec.is_shanghai_active_at_timestamp(timestamp) {
return Ok(WithdrawalsOutcome::pre_shanghai())
return Ok(None)
}
if withdrawals.is_empty() {
return Ok(WithdrawalsOutcome::empty())
return Ok(Some(EMPTY_WITHDRAWALS))
}
let balance_increments =
post_block_withdrawals_balance_increments(chain_spec, timestamp, &withdrawals);
post_block_withdrawals_balance_increments(chain_spec, timestamp, withdrawals);
db.increment_balances(balance_increments)?;
let withdrawals_root = proofs::calculate_withdrawals_root(&withdrawals);
// calculate withdrawals root
Ok(WithdrawalsOutcome {
withdrawals: Some(withdrawals),
withdrawals_root: Some(withdrawals_root),
})
Ok(Some(proofs::calculate_withdrawals_root(withdrawals)))
}
/// Checks if the new payload is better than the current best.