Skip to content

Commit

Permalink
tx-pool: add PoolUpdateKind for CanonicalStateUpdate (#12525)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Nov 14, 2024
1 parent 457ac5f commit 68a6ada
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
4 changes: 3 additions & 1 deletion crates/transaction-pool/src/maintain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
error::PoolError,
metrics::MaintainPoolMetrics,
traits::{CanonicalStateUpdate, TransactionPool, TransactionPoolExt},
BlockInfo, PoolTransaction,
BlockInfo, PoolTransaction, PoolUpdateKind,
};
use alloy_eips::BlockNumberOrTag;
use alloy_primitives::{Address, BlockHash, BlockNumber};
Expand Down Expand Up @@ -352,6 +352,7 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(
changed_accounts,
// all transactions mined in the new chain need to be removed from the pool
mined_transactions: new_blocks.transaction_hashes().collect(),
update_kind: PoolUpdateKind::Reorg,
};
pool.on_canonical_state_change(update);

Expand Down Expand Up @@ -434,6 +435,7 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(
pending_block_blob_fee,
changed_accounts,
mined_transactions,
update_kind: PoolUpdateKind::Commit,
};
pool.on_canonical_state_change(update);

Expand Down
5 changes: 4 additions & 1 deletion crates/transaction-pool/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ where
trace!(target: "txpool", ?update, "updating pool on canonical state change");

let block_info = update.block_info();
let CanonicalStateUpdate { new_tip, changed_accounts, mined_transactions, .. } = update;
let CanonicalStateUpdate {
new_tip, changed_accounts, mined_transactions, update_kind, ..
} = update;
self.validator.on_new_head_block(new_tip);

let changed_senders = self.changed_senders(changed_accounts.into_iter());
Expand All @@ -404,6 +406,7 @@ where
block_info,
mined_transactions,
changed_senders,
update_kind,
);

// This will discard outdated transactions based on the account's nonce
Expand Down
9 changes: 8 additions & 1 deletion crates/transaction-pool/src/pool/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
AddedPendingTransaction, AddedTransaction, OnNewCanonicalStateOutcome,
},
traits::{BestTransactionsAttributes, BlockInfo, PoolSize},
PoolConfig, PoolResult, PoolTransaction, PriceBumpConfig, TransactionOrdering,
PoolConfig, PoolResult, PoolTransaction, PoolUpdateKind, PriceBumpConfig, TransactionOrdering,
ValidPoolTransaction, U256,
};
use alloy_consensus::constants::{
Expand Down Expand Up @@ -76,6 +76,8 @@ pub struct TxPool<T: TransactionOrdering> {
all_transactions: AllTransactions<T::Transaction>,
/// Transaction pool metrics
metrics: TxPoolMetrics,
/// The last update kind that was applied to the pool.
latest_update_kind: Option<PoolUpdateKind>,
}

// === impl TxPool ===
Expand All @@ -92,6 +94,7 @@ impl<T: TransactionOrdering> TxPool<T> {
all_transactions: AllTransactions::new(&config),
config,
metrics: Default::default(),
latest_update_kind: None,
}
}

Expand Down Expand Up @@ -479,6 +482,7 @@ impl<T: TransactionOrdering> TxPool<T> {
block_info: BlockInfo,
mined_transactions: Vec<TxHash>,
changed_senders: HashMap<SenderId, SenderInfo>,
update_kind: PoolUpdateKind,
) -> OnNewCanonicalStateOutcome<T::Transaction> {
// update block info
let block_hash = block_info.last_seen_block_hash;
Expand All @@ -497,6 +501,9 @@ impl<T: TransactionOrdering> TxPool<T> {
self.update_transaction_type_metrics();
self.metrics.performed_state_updates.increment(1);

// Update the latest update kind
self.latest_update_kind = Some(update_kind);

OnNewCanonicalStateOutcome { block_hash, mined: mined_transactions, promoted, discarded }
}

Expand Down
13 changes: 12 additions & 1 deletion crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ pub trait TransactionPoolExt: TransactionPool {
///
/// ## Fee changes
///
/// The [CanonicalStateUpdate] includes the base and blob fee of the pending block, which
/// The [`CanonicalStateUpdate`] includes the base and blob fee of the pending block, which
/// affects the dynamic fee requirement of pending transactions in the pool.
///
/// ## EIP-4844 Blob transactions
Expand Down Expand Up @@ -669,6 +669,15 @@ impl TransactionOrigin {
}
}

/// Represents the kind of update to the canonical state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PoolUpdateKind {
/// The update was due to a block commit.
Commit,
/// The update was due to a reorganization.
Reorg,
}

/// Represents changes after a new canonical block or range of canonical blocks was added to the
/// chain.
///
Expand All @@ -693,6 +702,8 @@ pub struct CanonicalStateUpdate<'a> {
pub changed_accounts: Vec<ChangedAccount>,
/// All mined transactions in the block range.
pub mined_transactions: Vec<B256>,
/// The kind of update to the canonical state.
pub update_kind: PoolUpdateKind,
}

impl CanonicalStateUpdate<'_> {
Expand Down

0 comments on commit 68a6ada

Please sign in to comment.