Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 689da49

Browse files
authored
sc-transaction-pool: Always use best block to check if we should skip enactment (#14285)
We will calculate the tree route always against the best block and thus, we also should use this one to check if we should skip the checks.
1 parent 28f56b6 commit 689da49

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/transaction-pool/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ futures = "0.3.21"
1919
futures-timer = "3.0.2"
2020
linked-hash-map = "0.5.4"
2121
log = "0.4.17"
22-
num-traits = "0.2.8"
2322
parking_lot = "0.12.1"
2423
serde = { version = "1.0.163", features = ["derive"] }
2524
thiserror = "1.0.30"

client/transaction-pool/api/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,20 @@ pub enum ChainEvent<B: BlockT> {
311311
},
312312
}
313313

314+
impl<B: BlockT> ChainEvent<B> {
315+
/// Returns the block hash associated to the event.
316+
pub fn hash(&self) -> B::Hash {
317+
match self {
318+
Self::NewBestBlock { hash, .. } | Self::Finalized { hash, .. } => *hash,
319+
}
320+
}
321+
322+
/// Is `self == Self::Finalized`?
323+
pub fn is_finalized(&self) -> bool {
324+
matches!(self, Self::Finalized { .. })
325+
}
326+
}
327+
314328
/// Trait for transaction pool maintenance.
315329
#[async_trait]
316330
pub trait MaintainedTransactionPool: TransactionPool {

client/transaction-pool/src/enactment_state.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
//! Substrate transaction pool implementation.
2020
2121
use crate::LOG_TARGET;
22-
use num_traits::CheckedSub;
2322
use sc_transaction_pool_api::ChainEvent;
2423
use sp_blockchain::TreeRoute;
25-
use sp_runtime::traits::{Block as BlockT, NumberFor};
24+
use sp_runtime::traits::{Block as BlockT, NumberFor, Saturating};
2625

2726
/// The threshold since the last update where we will skip any maintenance for blocks.
2827
///
@@ -101,17 +100,16 @@ where
101100
TreeRouteF: Fn(Block::Hash, Block::Hash) -> Result<TreeRoute<Block>, String>,
102101
BlockNumberF: Fn(Block::Hash) -> Result<Option<NumberFor<Block>>, String>,
103102
{
104-
let (new_hash, current_hash, finalized) = match event {
105-
ChainEvent::NewBestBlock { hash, .. } => (*hash, self.recent_best_block, false),
106-
ChainEvent::Finalized { hash, .. } => (*hash, self.recent_finalized_block, true),
107-
};
103+
let new_hash = event.hash();
104+
let finalized = event.is_finalized();
108105

109106
// do not proceed with txpool maintain if block distance is to high
110-
let skip_maintenance = match (hash_to_number(new_hash), hash_to_number(current_hash)) {
111-
(Ok(Some(new)), Ok(Some(current))) =>
112-
new.checked_sub(&current) > Some(SKIP_MAINTENANCE_THRESHOLD.into()),
113-
_ => true,
114-
};
107+
let skip_maintenance =
108+
match (hash_to_number(new_hash), hash_to_number(self.recent_best_block)) {
109+
(Ok(Some(new)), Ok(Some(current))) =>
110+
new.saturating_sub(current) > SKIP_MAINTENANCE_THRESHOLD.into(),
111+
_ => true,
112+
};
115113

116114
if skip_maintenance {
117115
log::debug!(target: LOG_TARGET, "skip maintain: tree_route would be too long");
@@ -131,10 +129,10 @@ where
131129

132130
log::debug!(
133131
target: LOG_TARGET,
134-
"resolve hash:{:?} finalized:{:?} tree_route:{:?} best_block:{:?} finalized_block:{:?}",
135-
new_hash,
136-
finalized,
137-
tree_route,
132+
"resolve hash: {new_hash:?} finalized: {finalized:?} \
133+
tree_route: (common {:?}, last {:?}) best_block: {:?} finalized_block:{:?}",
134+
tree_route.common_block(),
135+
tree_route.last(),
138136
self.recent_best_block,
139137
self.recent_finalized_block
140138
);

0 commit comments

Comments
 (0)