From 07a1af85ea797df30ef3a82ab51453ddf6411563 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 22 Aug 2023 01:12:03 +0200 Subject: [PATCH] feat: add Validator::on_new_head_block (#4303) --- crates/transaction-pool/src/lib.rs | 2 +- crates/transaction-pool/src/maintain.rs | 8 +--- crates/transaction-pool/src/pool/mod.rs | 19 +++------- crates/transaction-pool/src/traits.rs | 42 ++++++++++++++++----- crates/transaction-pool/src/validate/mod.rs | 7 +++- 5 files changed, 46 insertions(+), 32 deletions(-) diff --git a/crates/transaction-pool/src/lib.rs b/crates/transaction-pool/src/lib.rs index 60f56cace2..d9669943db 100644 --- a/crates/transaction-pool/src/lib.rs +++ b/crates/transaction-pool/src/lib.rs @@ -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); } diff --git a/crates/transaction-pool/src/maintain.rs b/crates/transaction-pool/src/maintain.rs index c7c55a6196..2e9936fe95 100644 --- a/crates/transaction-pool/src/maintain.rs +++ b/crates/transaction-pool/src/maintain.rs @@ -271,13 +271,11 @@ pub async fn maintain_transaction_pool( // 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); @@ -348,12 +346,10 @@ pub async fn maintain_transaction_pool( // 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); diff --git a/crates/transaction-pool/src/pool/mod.rs b/crates/transaction-pool/src/pool/mod.rs index 718fb40704..211f488ae5 100644 --- a/crates/transaction-pool/src/pool/mod.rs +++ b/crates/transaction-pool/src/pool/mod.rs @@ -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( diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index 9d3c5026dc..eef461587c 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -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; @@ -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); @@ -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. @@ -463,14 +461,38 @@ pub struct CanonicalStateUpdate { pub changed_accounts: Vec, /// All mined transactions in the block range. pub mined_transactions: Vec, - /// Timestamp of the latest chain update - pub timestamp: u64, } -impl fmt::Display for CanonicalStateUpdate { +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 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<'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()) } } diff --git a/crates/transaction-pool/src/validate/mod.rs b/crates/transaction-pool/src/validate/mod.rs index 1ec2bd07f4..1e5cc78f89 100644 --- a/crates/transaction-pool/src/validate/mod.rs +++ b/crates/transaction-pool/src/validate/mod.rs @@ -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}; @@ -157,6 +157,11 @@ pub trait TransactionValidator: Send + Sync { transaction: Self::Transaction, ) -> TransactionValidationOutcome; + /// 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(