Skip to content
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
2 changes: 1 addition & 1 deletion crates/transaction-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ where
self.pool.set_block_info(info)
}

fn on_canonical_state_change(&self, update: CanonicalStateUpdate) {
fn on_canonical_state_change(&self, update: CanonicalStateUpdate<'_>) {
self.pool.on_canonical_state_change(update);
}

Expand Down
8 changes: 2 additions & 6 deletions crates/transaction-pool/src/maintain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,11 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(

// update the pool first
let update = CanonicalStateUpdate {
hash: new_tip.hash,
number: new_tip.number,
new_tip: &new_tip.block,
pending_block_base_fee,
changed_accounts,
// all transactions mined in the new chain need to be removed from the pool
mined_transactions: new_mined_transactions.into_iter().collect(),
timestamp: new_tip.timestamp,
};
pool.on_canonical_state_change(update);

Expand Down Expand Up @@ -348,12 +346,10 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(

// Canonical update
let update = CanonicalStateUpdate {
hash: tip.hash,
number: tip.number,
new_tip: &tip.block,
pending_block_base_fee,
changed_accounts,
mined_transactions,
timestamp: tip.timestamp,
};
pool.on_canonical_state_change(update);

Expand Down
19 changes: 5 additions & 14 deletions crates/transaction-pool/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,23 +270,14 @@ where
}

/// Updates the entire pool after a new block was executed.
pub(crate) fn on_canonical_state_change(&self, update: CanonicalStateUpdate) {
pub(crate) fn on_canonical_state_change(&self, update: CanonicalStateUpdate<'_>) {
trace!(target: "txpool", %update, "updating pool on canonical state change");

let CanonicalStateUpdate {
hash,
number,
pending_block_base_fee,
changed_accounts,
mined_transactions,
timestamp: _,
} = update;
let block_info = update.block_info();
let CanonicalStateUpdate { new_tip, changed_accounts, mined_transactions, .. } = update;
self.validator.on_new_head_block(new_tip);

let changed_senders = self.changed_senders(changed_accounts.into_iter());
let block_info = BlockInfo {
last_seen_block_hash: hash,
last_seen_block_number: number,
pending_basefee: pending_block_base_fee,
};

// update the pool
let outcome = self.pool.write().on_canonical_state_change(
Expand Down
40 changes: 31 additions & 9 deletions crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use futures_util::{ready, Stream};
use reth_primitives::{
Address, BlobTransactionSidecar, FromRecoveredPooledTransaction, FromRecoveredTransaction,
IntoRecoveredTransaction, PeerId, PooledTransactionsElement,
PooledTransactionsElementEcRecovered, Transaction, TransactionKind,
PooledTransactionsElementEcRecovered, SealedBlock, Transaction, TransactionKind,
TransactionSignedEcRecovered, TxHash, EIP1559_TX_TYPE_ID, EIP4844_TX_TYPE_ID, H256, U256,
};
use reth_rlp::Encodable;
Expand Down Expand Up @@ -295,7 +295,7 @@ pub trait TransactionPoolExt: TransactionPool {
/// Implementers need to update the pool accordingly.
/// For example the base fee of the pending block is determined after a block is mined which
/// affects the dynamic fee requirement of pending transactions in the pool.
fn on_canonical_state_change(&self, update: CanonicalStateUpdate);
fn on_canonical_state_change(&self, update: CanonicalStateUpdate<'_>);

/// Updates the accounts in the pool
fn update_accounts(&self, accounts: Vec<ChangedAccount>);
Expand Down Expand Up @@ -450,11 +450,9 @@ impl TransactionOrigin {
///
/// This is used to update the pool state accordingly.
#[derive(Debug, Clone)]
pub struct CanonicalStateUpdate {
pub struct CanonicalStateUpdate<'a> {
/// Hash of the tip block.
pub hash: H256,
/// Number of the tip block.
pub number: u64,
pub new_tip: &'a SealedBlock,
/// EIP-1559 Base fee of the _next_ (pending) block
///
/// The base fee of a block depends on the utilization of the last block and its base fee.
Expand All @@ -463,14 +461,38 @@ pub struct CanonicalStateUpdate {
pub changed_accounts: Vec<ChangedAccount>,
/// All mined transactions in the block range.
pub mined_transactions: Vec<H256>,
}

impl<'a> CanonicalStateUpdate<'a> {
/// Returns the number of the tip block.
pub fn number(&self) -> u64 {
self.new_tip.number
}

/// Returns the hash of the tip block.
pub fn hash(&self) -> H256 {
self.new_tip.hash
}

/// Timestamp of the latest chain update
pub timestamp: u64,
pub fn timestamp(&self) -> u64 {
self.new_tip.timestamp
}

/// Returns the block info for the tip block.
pub fn block_info(&self) -> BlockInfo {
BlockInfo {
last_seen_block_hash: self.hash(),
last_seen_block_number: self.number(),
pending_basefee: self.pending_block_base_fee,
}
}
}

impl fmt::Display for CanonicalStateUpdate {
impl<'a> fmt::Display for CanonicalStateUpdate<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{ hash: {}, number: {}, pending_block_base_fee: {}, changed_accounts: {}, mined_transactions: {} }}",
self.hash, self.number, self.pending_block_base_fee, self.changed_accounts.len(), self.mined_transactions.len())
self.hash(), self.number(), self.pending_block_base_fee, self.changed_accounts.len(), self.mined_transactions.len())
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/transaction-pool/src/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
traits::{PoolTransaction, TransactionOrigin},
};
use reth_primitives::{
Address, BlobTransactionSidecar, IntoRecoveredTransaction, TransactionKind,
Address, BlobTransactionSidecar, IntoRecoveredTransaction, SealedBlock, TransactionKind,
TransactionSignedEcRecovered, TxHash, H256, U256,
};
use std::{fmt, time::Instant};
Expand Down Expand Up @@ -157,6 +157,11 @@ pub trait TransactionValidator: Send + Sync {
transaction: Self::Transaction,
) -> TransactionValidationOutcome<Self::Transaction>;

/// Invoked when the head block changes.
///
/// This can be used to update fork specific values (timestamp).
fn on_new_head_block(&self, _new_tip_block: &SealedBlock) {}

/// Ensure that the code size is not greater than `max_init_code_size`.
/// `max_init_code_size` should be configurable so this will take it as an argument.
fn ensure_max_init_code_size(
Expand Down