Skip to content

Commit

Permalink
feat: introduce iterator for default_ethereum_payload function (#11978)
Browse files Browse the repository at this point in the history
  • Loading branch information
edisontim authored Oct 25, 2024
1 parent 2ae7ee5 commit a87d654
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ use reth_primitives::{
use reth_provider::{ChainSpecProvider, StateProviderFactory};
use reth_revm::database::StateProviderDatabase;
use reth_transaction_pool::{
noop::NoopTransactionPool, BestTransactionsAttributes, TransactionPool,
noop::NoopTransactionPool, BestTransactions, BestTransactionsAttributes, TransactionPool,
ValidPoolTransaction,
};
use reth_trie::HashedPostState;
use revm::{
Expand All @@ -45,6 +46,10 @@ use revm_primitives::calc_excess_blob_gas;
use std::sync::Arc;
use tracing::{debug, trace, warn};

type BestTransactionsIter<Pool> = Box<
dyn BestTransactions<Item = Arc<ValidPoolTransaction<<Pool as TransactionPool>::Transaction>>>,
>;

/// Ethereum payload builder
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EthereumPayloadBuilder<EvmConfig = EthEvmConfig> {
Expand Down Expand Up @@ -94,27 +99,37 @@ where
args: BuildArguments<Pool, Client, EthPayloadBuilderAttributes, EthBuiltPayload>,
) -> Result<BuildOutcome<EthBuiltPayload>, PayloadBuilderError> {
let (cfg_env, block_env) = self.cfg_and_block_env(&args.config, &args.config.parent_block);
default_ethereum_payload(self.evm_config.clone(), args, cfg_env, block_env)

let pool = args.pool.clone();
default_ethereum_payload(self.evm_config.clone(), args, cfg_env, block_env, |attributes| {
pool.best_transactions_with_attributes(attributes)
})
}

fn build_empty_payload(
&self,
client: &Client,
config: PayloadConfig<Self::Attributes>,
) -> Result<EthBuiltPayload, PayloadBuilderError> {
let args = BuildArguments {
let args = BuildArguments::new(
client,
config,
// we use defaults here because for the empty payload we don't need to execute anything
pool: NoopTransactionPool::default(),
cached_reads: Default::default(),
cancel: Default::default(),
best_payload: None,
};
NoopTransactionPool::default(),
Default::default(),
config,
Default::default(),
None,
);

let (cfg_env, block_env) = self.cfg_and_block_env(&args.config, &args.config.parent_block);
default_ethereum_payload(self.evm_config.clone(), args, cfg_env, block_env)?
.into_payload()
.ok_or_else(|| PayloadBuilderError::MissingPayload)

let pool = args.pool.clone();

default_ethereum_payload(self.evm_config.clone(), args, cfg_env, block_env, |attributes| {
pool.best_transactions_with_attributes(attributes)
})?
.into_payload()
.ok_or_else(|| PayloadBuilderError::MissingPayload)
}
}

Expand All @@ -124,16 +139,18 @@ where
/// and configuration, this function creates a transaction payload. Returns
/// a result indicating success with the payload or an error in case of failure.
#[inline]
pub fn default_ethereum_payload<EvmConfig, Pool, Client>(
pub fn default_ethereum_payload<EvmConfig, Pool, Client, F>(
evm_config: EvmConfig,
args: BuildArguments<Pool, Client, EthPayloadBuilderAttributes, EthBuiltPayload>,
initialized_cfg: CfgEnvWithHandlerCfg,
initialized_block_env: BlockEnv,
best_txs: F,
) -> Result<BuildOutcome<EthBuiltPayload>, PayloadBuilderError>
where
EvmConfig: ConfigureEvm<Header = Header>,
Client: StateProviderFactory + ChainSpecProvider<ChainSpec = ChainSpec>,
Pool: TransactionPool,
F: FnOnce(BestTransactionsAttributes) -> BestTransactionsIter<Pool>,
{
let BuildArguments { client, pool, mut cached_reads, config, cancel, best_payload } = args;

Expand All @@ -153,11 +170,10 @@ where
let mut executed_txs = Vec::new();
let mut executed_senders = Vec::new();

let mut best_txs = pool.best_transactions_with_attributes(BestTransactionsAttributes::new(
let mut best_txs = best_txs(BestTransactionsAttributes::new(
base_fee,
initialized_block_env.get_blob_gasprice().map(|gasprice| gasprice as u64),
));

let mut total_fees = U256::ZERO;

let block_number = initialized_block_env.number.to::<u64>();
Expand Down

0 comments on commit a87d654

Please sign in to comment.