anvil/eth/pool/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
//! # Transaction Pool implementation
//!
//! The transaction pool is responsible for managing a set of transactions that can be included in
//! upcoming blocks.
//!
//! The main task of the pool is to prepare an ordered list of transactions that are ready to be
//! included in a new block.
//!
//! Each imported block can affect the validity of transactions already in the pool.
//! The miner expects the most up-to-date transactions when attempting to create a new block.
//! After being included in a block, a transaction should be removed from the pool, this process is
//! called _pruning_ and due to separation of concerns is triggered externally.
//! The pool essentially performs following services:
//! * import transactions
//! * order transactions
//! * provide ordered set of transactions that are ready for inclusion
//! * prune transactions
//!
//! Each transaction in the pool contains markers that it _provides_ or _requires_. This property is
//! used to determine whether it can be included in a block (transaction is ready) or whether it
//! still _requires_ other transactions to be mined first (transaction is pending).
//! A transaction is associated with the nonce of the account it's sent from. A unique identifying
//! marker for a transaction is therefore the pair `(nonce + account)`. An incoming transaction with
//! a `nonce > nonce on chain` will _require_ `(nonce -1, account)` first, before it is ready to be
//! included in a block.
//!
//! This implementation is adapted from <https://github.com/paritytech/substrate/tree/master/client/transaction-pool>
use crate::{
eth::{
error::PoolError,
pool::transactions::{
PendingPoolTransaction, PendingTransactions, PoolTransaction, ReadyTransactions,
TransactionsIterator, TxMarker,
},
},
mem::storage::MinedBlockOutcome,
};
use alloy_primitives::{Address, TxHash, U64};
use alloy_rpc_types::txpool::TxpoolStatus;
use anvil_core::eth::transaction::PendingTransaction;
use futures::channel::mpsc::{channel, Receiver, Sender};
use parking_lot::{Mutex, RwLock};
use std::{collections::VecDeque, fmt, sync::Arc};
pub mod transactions;
/// Transaction pool that performs validation.
#[derive(Default)]
pub struct Pool {
/// processes all pending transactions
inner: RwLock<PoolInner>,
/// listeners for new ready transactions
transaction_listener: Mutex<Vec<Sender<TxHash>>>,
}
// == impl Pool ==
impl Pool {
/// Returns an iterator that yields all transactions that are currently ready
pub fn ready_transactions(&self) -> TransactionsIterator {
self.inner.read().ready_transactions()
}
/// Returns all transactions that are not ready to be included in a block yet
pub fn pending_transactions(&self) -> Vec<Arc<PoolTransaction>> {
self.inner.read().pending_transactions.transactions().collect()
}
/// Returns the _pending_ transaction for that `hash` if it exists in the mempool
pub fn get_transaction(&self, hash: TxHash) -> Option<PendingTransaction> {
self.inner.read().get_transaction(hash)
}
/// Returns the number of tx that are ready and queued for further execution
pub fn txpool_status(&self) -> TxpoolStatus {
// Note: naming differs here compared to geth's `TxpoolStatus`
let pending: u64 = self.ready_transactions().count().try_into().unwrap_or(0);
let queued: u64 = self.inner.read().pending_transactions.len().try_into().unwrap_or(0);
TxpoolStatus { pending, queued }
}
/// Invoked when a set of transactions ([Self::ready_transactions()]) was executed.
///
/// This will remove the transactions from the pool.
pub fn on_mined_block(&self, outcome: MinedBlockOutcome) -> PruneResult {
let MinedBlockOutcome { block_number, included, invalid } = outcome;
// remove invalid transactions from the pool
self.remove_invalid(invalid.into_iter().map(|tx| tx.hash()).collect());
// prune all the markers the mined transactions provide
let res = self
.prune_markers(block_number, included.into_iter().flat_map(|tx| tx.provides.clone()));
trace!(target: "txpool", "pruned transaction markers {:?}", res);
res
}
/// Removes ready transactions for the given iterator of identifying markers.
///
/// For each marker we can remove transactions in the pool that either provide the marker
/// directly or are a dependency of the transaction associated with that marker.
pub fn prune_markers(
&self,
block_number: U64,
markers: impl IntoIterator<Item = TxMarker>,
) -> PruneResult {
debug!(target: "txpool", ?block_number, "pruning transactions");
self.inner.write().prune_markers(markers)
}
/// Adds a new transaction to the pool
pub fn add_transaction(&self, tx: PoolTransaction) -> Result<AddedTransaction, PoolError> {
let added = self.inner.write().add_transaction(tx)?;
if let AddedTransaction::Ready(ref ready) = added {
self.notify_listener(ready.hash);
// also notify promoted transactions
for promoted in ready.promoted.iter().copied() {
self.notify_listener(promoted);
}
}
Ok(added)
}
/// Adds a new transaction listener to the pool that gets notified about every new ready
/// transaction
pub fn add_ready_listener(&self) -> Receiver<TxHash> {
const TX_LISTENER_BUFFER_SIZE: usize = 2048;
let (tx, rx) = channel(TX_LISTENER_BUFFER_SIZE);
self.transaction_listener.lock().push(tx);
rx
}
/// Returns true if this pool already contains the transaction
pub fn contains(&self, tx_hash: &TxHash) -> bool {
self.inner.read().contains(tx_hash)
}
/// Remove the given transactions from the pool
pub fn remove_invalid(&self, tx_hashes: Vec<TxHash>) -> Vec<Arc<PoolTransaction>> {
self.inner.write().remove_invalid(tx_hashes)
}
/// Remove transactions by sender
pub fn remove_transactions_by_address(&self, sender: Address) -> Vec<Arc<PoolTransaction>> {
self.inner.write().remove_transactions_by_address(sender)
}
/// Removes a single transaction from the pool
///
/// This is similar to `[Pool::remove_invalid()]` but for a single transaction.
///
/// **Note**: this will also drop any transaction that depend on the `tx`
pub fn drop_transaction(&self, tx: TxHash) -> Option<Arc<PoolTransaction>> {
trace!(target: "txpool", "Dropping transaction: [{:?}]", tx);
let removed = {
let mut pool = self.inner.write();
pool.ready_transactions.remove_with_markers(vec![tx], None)
};
trace!(target: "txpool", "Dropped transactions: {:?}", removed);
let mut dropped = None;
if !removed.is_empty() {
dropped = removed.into_iter().find(|t| *t.pending_transaction.hash() == tx);
}
dropped
}
/// Removes all transactions from the pool
pub fn clear(&self) {
let mut pool = self.inner.write();
pool.clear();
}
/// notifies all listeners about the transaction
fn notify_listener(&self, hash: TxHash) {
let mut listener = self.transaction_listener.lock();
// this is basically a retain but with mut reference
for n in (0..listener.len()).rev() {
let mut listener_tx = listener.swap_remove(n);
let retain = match listener_tx.try_send(hash) {
Ok(()) => true,
Err(e) => {
if e.is_full() {
warn!(
target: "txpool",
"[{:?}] Failed to send tx notification because channel is full",
hash,
);
true
} else {
false
}
}
};
if retain {
listener.push(listener_tx)
}
}
}
}
/// A Transaction Pool
///
/// Contains all transactions that are ready to be executed
#[derive(Debug, Default)]
struct PoolInner {
ready_transactions: ReadyTransactions,
pending_transactions: PendingTransactions,
}
// == impl PoolInner ==
impl PoolInner {
/// Returns an iterator over transactions that are ready.
fn ready_transactions(&self) -> TransactionsIterator {
self.ready_transactions.get_transactions()
}
/// Clears
fn clear(&mut self) {
self.ready_transactions.clear();
self.pending_transactions.clear();
}
/// checks both pools for the matching transaction
///
/// Returns `None` if the transaction does not exist in the pool
fn get_transaction(&self, hash: TxHash) -> Option<PendingTransaction> {
if let Some(pending) = self.pending_transactions.get(&hash) {
return Some(pending.transaction.pending_transaction.clone())
}
Some(
self.ready_transactions.get(&hash)?.transaction.transaction.pending_transaction.clone(),
)
}
/// Returns an iterator over all transactions in the pool filtered by the sender
pub fn transactions_by_sender(
&self,
sender: Address,
) -> impl Iterator<Item = Arc<PoolTransaction>> + '_ {
let pending_txs = self
.pending_transactions
.transactions()
.filter(move |tx| tx.pending_transaction.sender().eq(&sender));
let ready_txs = self
.ready_transactions
.get_transactions()
.filter(move |tx| tx.pending_transaction.sender().eq(&sender));
pending_txs.chain(ready_txs)
}
/// Returns true if this pool already contains the transaction
fn contains(&self, tx_hash: &TxHash) -> bool {
self.pending_transactions.contains(tx_hash) || self.ready_transactions.contains(tx_hash)
}
fn add_transaction(&mut self, tx: PoolTransaction) -> Result<AddedTransaction, PoolError> {
if self.contains(&tx.hash()) {
warn!(target: "txpool", "[{:?}] Already imported", tx.hash());
return Err(PoolError::AlreadyImported(Box::new(tx)))
}
let tx = PendingPoolTransaction::new(tx, self.ready_transactions.provided_markers());
trace!(target: "txpool", "[{:?}] {:?}", tx.transaction.hash(), tx);
// If all markers are not satisfied import to future
if !tx.is_ready() {
let hash = tx.transaction.hash();
self.pending_transactions.add_transaction(tx)?;
return Ok(AddedTransaction::Pending { hash })
}
self.add_ready_transaction(tx)
}
/// Adds the transaction to the ready queue
fn add_ready_transaction(
&mut self,
tx: PendingPoolTransaction,
) -> Result<AddedTransaction, PoolError> {
let hash = tx.transaction.hash();
trace!(target: "txpool", "adding ready transaction [{:?}]", hash);
let mut ready = ReadyTransaction::new(hash);
let mut tx_queue = VecDeque::from([tx]);
// tracks whether we're processing the given `tx`
let mut is_new_tx = true;
// take first transaction from the list
while let Some(current_tx) = tx_queue.pop_front() {
// also add the transaction that the current transaction unlocks
tx_queue.extend(
self.pending_transactions.mark_and_unlock(¤t_tx.transaction.provides),
);
let current_hash = current_tx.transaction.hash();
// try to add the transaction to the ready pool
match self.ready_transactions.add_transaction(current_tx) {
Ok(replaced_transactions) => {
if !is_new_tx {
ready.promoted.push(current_hash);
}
// tx removed from ready pool
ready.removed.extend(replaced_transactions);
}
Err(err) => {
// failed to add transaction
if is_new_tx {
debug!(target: "txpool", "[{:?}] Failed to add tx: {:?}", current_hash,
err);
return Err(err)
} else {
ready.discarded.push(current_hash);
}
}
}
is_new_tx = false;
}
// check for a cycle where importing a transaction resulted in pending transactions to be
// added while removing current transaction. in which case we move this transaction back to
// the pending queue
if ready.removed.iter().any(|tx| *tx.hash() == hash) {
self.ready_transactions.clear_transactions(&ready.promoted);
return Err(PoolError::CyclicTransaction)
}
Ok(AddedTransaction::Ready(ready))
}
/// Prunes the transactions that provide the given markers
///
/// This will effectively remove those transactions that satisfy the markers and transactions
/// from the pending queue might get promoted to if the markers unlock them.
pub fn prune_markers(&mut self, markers: impl IntoIterator<Item = TxMarker>) -> PruneResult {
let mut imports = vec![];
let mut pruned = vec![];
for marker in markers {
// mark as satisfied and store the transactions that got unlocked
imports.extend(self.pending_transactions.mark_and_unlock(Some(&marker)));
// prune transactions
pruned.extend(self.ready_transactions.prune_tags(marker.clone()));
}
let mut promoted = vec![];
let mut failed = vec![];
for tx in imports {
let hash = tx.transaction.hash();
match self.add_ready_transaction(tx) {
Ok(res) => promoted.push(res),
Err(e) => {
warn!(target: "txpool", "Failed to promote tx [{:?}] : {:?}", hash, e);
failed.push(hash)
}
}
}
PruneResult { pruned, failed, promoted }
}
/// Remove the given transactions from the pool
pub fn remove_invalid(&mut self, tx_hashes: Vec<TxHash>) -> Vec<Arc<PoolTransaction>> {
// early exit in case there is no invalid transactions.
if tx_hashes.is_empty() {
return vec![]
}
trace!(target: "txpool", "Removing invalid transactions: {:?}", tx_hashes);
let mut removed = self.ready_transactions.remove_with_markers(tx_hashes.clone(), None);
removed.extend(self.pending_transactions.remove(tx_hashes));
trace!(target: "txpool", "Removed invalid transactions: {:?}", removed);
removed
}
/// Remove transactions by sender address
pub fn remove_transactions_by_address(&mut self, sender: Address) -> Vec<Arc<PoolTransaction>> {
let tx_hashes =
self.transactions_by_sender(sender).map(move |tx| tx.hash()).collect::<Vec<TxHash>>();
if tx_hashes.is_empty() {
return vec![]
}
trace!(target: "txpool", "Removing transactions: {:?}", tx_hashes);
let mut removed = self.ready_transactions.remove_with_markers(tx_hashes.clone(), None);
removed.extend(self.pending_transactions.remove(tx_hashes));
trace!(target: "txpool", "Removed transactions: {:?}", removed);
removed
}
}
/// Represents the outcome of a prune
pub struct PruneResult {
/// a list of added transactions that a pruned marker satisfied
pub promoted: Vec<AddedTransaction>,
/// all transactions that failed to be promoted and now are discarded
pub failed: Vec<TxHash>,
/// all transactions that were pruned from the ready pool
pub pruned: Vec<Arc<PoolTransaction>>,
}
impl fmt::Debug for PruneResult {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "PruneResult {{ ")?;
write!(
fmt,
"promoted: {:?}, ",
self.promoted.iter().map(|tx| *tx.hash()).collect::<Vec<_>>()
)?;
write!(fmt, "failed: {:?}, ", self.failed)?;
write!(
fmt,
"pruned: {:?}, ",
self.pruned.iter().map(|tx| *tx.pending_transaction.hash()).collect::<Vec<_>>()
)?;
write!(fmt, "}}")?;
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct ReadyTransaction {
/// the hash of the submitted transaction
hash: TxHash,
/// transactions promoted to the ready queue
promoted: Vec<TxHash>,
/// transaction that failed and became discarded
discarded: Vec<TxHash>,
/// Transactions removed from the Ready pool
removed: Vec<Arc<PoolTransaction>>,
}
impl ReadyTransaction {
pub fn new(hash: TxHash) -> Self {
Self {
hash,
promoted: Default::default(),
discarded: Default::default(),
removed: Default::default(),
}
}
}
#[derive(Clone, Debug)]
pub enum AddedTransaction {
/// transaction was successfully added and being processed
Ready(ReadyTransaction),
/// Transaction was successfully added but not yet queued for processing
Pending {
/// the hash of the submitted transaction
hash: TxHash,
},
}
impl AddedTransaction {
pub fn hash(&self) -> &TxHash {
match self {
Self::Ready(tx) => &tx.hash,
Self::Pending { hash } => hash,
}
}
}