feat: add Validator::on_new_head_block (#4303)

This commit is contained in:
Matthias Seitz
2023-08-22 01:12:03 +02:00
committed by GitHub
parent 34b68deedf
commit 07a1af85ea
5 changed files with 46 additions and 32 deletions

View File

@@ -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);
}

View File

@@ -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);
@@ -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);

View File

@@ -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(

View File

@@ -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<ChangedAccount>);
@@ -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<ChangedAccount>,
/// All mined transactions in the block range.
pub mined_transactions: Vec<H256>,
/// 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())
}
}

View File

@@ -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<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(