Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: relax some op pool validator internals #13358

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions crates/optimism/evm/src/l1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,21 @@ const L1_BLOCK_ECOTONE_SELECTOR: [u8; 4] = hex!("440a5e20");
///
/// Returns an error if the L1 info transaction is not found, if the block is empty.
pub fn extract_l1_info<B: BlockBody>(body: &B) -> Result<L1BlockInfo, OpBlockExecutionError> {
let l1_info_tx_data = body
.transactions()
.first()
.ok_or_else(|| OpBlockExecutionError::L1BlockInfoError {
let l1_info_tx =
body.transactions().first().ok_or_else(|| OpBlockExecutionError::L1BlockInfoError {
message: "could not find l1 block info tx in the L2 block".to_string(),
})
.map(|tx| tx.input())?;
})?;
extract_l1_info_from_tx(l1_info_tx)
}

/// Extracts the [`L1BlockInfo`] from the the L1 info transaction (first transaction) in the L2
/// block.
///
/// Returns an error if the calldata is shorter than 4 bytes.
pub fn extract_l1_info_from_tx<T: Transaction>(
tx: &T,
) -> Result<L1BlockInfo, OpBlockExecutionError> {
let l1_info_tx_data = tx.input();
if l1_info_tx_data.len() < 4 {
return Err(OpBlockExecutionError::L1BlockInfoError {
message: "invalid l1 block info transaction calldata in the L2 block".to_string(),
Expand All @@ -53,6 +60,12 @@ pub fn extract_l1_info<B: BlockBody>(body: &B) -> Result<L1BlockInfo, OpBlockExe
/// Parses the input of the first transaction in the L2 block, into [`L1BlockInfo`].
///
/// Returns an error if data is incorrect length.
///
/// Caution this expects that the input is the calldata of the [`L1BlockInfo`] transaction (first
/// transaction) in the L2 block.
///
/// # Panics
/// If the input is shorter than 4 bytes.
pub fn parse_l1_info(input: &[u8]) -> Result<L1BlockInfo, OpBlockExecutionError> {
// If the first 4 bytes of the calldata are the L1BlockInfoEcotone selector, then we parse the
// calldata as an Ecotone hardfork L1BlockInfo transaction. Otherwise, we parse it as a
Expand Down
38 changes: 24 additions & 14 deletions crates/optimism/node/src/txpool.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! OP transaction pool types
use alloy_consensus::{BlockHeader, Transaction};
use alloy_eips::eip2718::Encodable2718;
use parking_lot::RwLock;
use reth_chainspec::ChainSpec;
use reth_node_api::{Block, BlockBody};
use reth_optimism_evm::RethL1BlockInfo;
use reth_primitives::{
Block, GotExpected, InvalidTransactionError, SealedBlock, TransactionSigned,
};
use reth_primitives::{GotExpected, InvalidTransactionError, SealedBlock, TransactionSigned};
use reth_provider::{BlockReaderIdExt, StateProviderFactory};
use reth_revm::L1BlockInfo;
use reth_transaction_pool::{
Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct OpTransactionValidator<Client, Tx> {

impl<Client, Tx> OpTransactionValidator<Client, Tx> {
/// Returns the configured chain spec
pub fn chain_spec(&self) -> Arc<ChainSpec> {
pub fn chain_spec(&self) -> &Arc<ChainSpec> {
self.inner.chain_spec()
}

Expand Down Expand Up @@ -69,7 +69,7 @@ impl<Client, Tx> OpTransactionValidator<Client, Tx> {

impl<Client, Tx> OpTransactionValidator<Client, Tx>
where
Client: StateProviderFactory + BlockReaderIdExt<Block = reth_primitives::Block>,
Client: StateProviderFactory + BlockReaderIdExt,
Tx: EthPoolTransaction<Consensus = TransactionSigned>,
{
/// Create a new [`OpTransactionValidator`].
Expand All @@ -80,10 +80,10 @@ where
{
// genesis block has no txs, so we can't extract L1 info, we set the block info to empty
// so that we will accept txs into the pool before the first block
if block.number == 0 {
this.block_info.timestamp.store(block.timestamp, Ordering::Relaxed);
if block.header().number() == 0 {
this.block_info.timestamp.store(block.header().timestamp(), Ordering::Relaxed);
} else {
this.update_l1_block_info(&block);
this.update_l1_block_info(block.header(), block.body().transactions().first());
}
}

Expand All @@ -98,10 +98,17 @@ where
Self { inner, block_info: Arc::new(block_info), require_l1_data_gas_fee: true }
}

/// Update the L1 block info.
fn update_l1_block_info(&self, block: &Block) {
self.block_info.timestamp.store(block.timestamp, Ordering::Relaxed);
if let Ok(cost_addition) = reth_optimism_evm::extract_l1_info(&block.body) {
/// Update the L1 block info for the given header and system transaction, if any.
///
/// Note: this supports optional system transaction, in case this is used in a dev setuo
pub fn update_l1_block_info<H, T>(&self, header: &H, tx: Option<&T>)
where
H: BlockHeader,
T: Transaction,
{
self.block_info.timestamp.store(header.timestamp(), Ordering::Relaxed);

if let Some(Ok(cost_addition)) = tx.map(reth_optimism_evm::extract_l1_info_from_tx) {
*self.block_info.l1_block_info.write() = cost_addition;
}
}
Expand Down Expand Up @@ -146,7 +153,7 @@ where
tx.encode_2718(&mut encoded);

let cost_addition = match l1_block_info.l1_tx_data_fee(
&self.chain_spec(),
self.chain_spec(),
self.block_timestamp(),
&encoded,
false,
Expand Down Expand Up @@ -217,7 +224,10 @@ where

fn on_new_head_block(&self, new_tip_block: &SealedBlock) {
self.inner.on_new_head_block(new_tip_block);
self.update_l1_block_info(&new_tip_block.clone().unseal());
self.update_l1_block_info(
new_tip_block.header(),
new_tip_block.body.transactions().first(),
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/transaction-pool/src/validate/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub struct EthTransactionValidator<Client, T> {

impl<Client, Tx> EthTransactionValidator<Client, Tx> {
/// Returns the configured chain spec
pub fn chain_spec(&self) -> Arc<ChainSpec> {
self.inner.chain_spec.clone()
pub fn chain_spec(&self) -> &Arc<ChainSpec> {
&self.inner.chain_spec
}

/// Returns the configured client
Expand Down
Loading