From 8b574712216fb4ac1a5d608060a95fec146e45d7 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Thu, 22 Dec 2022 23:33:01 +0100 Subject: [PATCH] txpool: don't validate block transactions if the pool is empty (#12973) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * txpool: don't validate block transactions if the pool is empty Fix shall prevent from wasting the CPU during the major sync. Block transaction don't need to be re-validated when the txpool is empty. Fixes: #12903 * Apply suggestions from code review Co-authored-by: Bastian Köcher --- .../client/transaction-pool/src/graph/pool.rs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/substrate/client/transaction-pool/src/graph/pool.rs b/substrate/client/transaction-pool/src/graph/pool.rs index 96cbeb9b80..8e3570d1db 100644 --- a/substrate/client/transaction-pool/src/graph/pool.rs +++ b/substrate/client/transaction-pool/src/graph/pool.rs @@ -271,14 +271,23 @@ impl Pool { // if it's not found in the pool query the runtime at parent block // to get validity info and tags that the extrinsic provides. None => { - let validity = self - .validated_pool - .api() - .validate_transaction(parent, TransactionSource::InBlock, extrinsic.clone()) - .await; + // Avoid validating block txs if the pool is empty + if !self.validated_pool.status().is_empty() { + let validity = self + .validated_pool + .api() + .validate_transaction( + parent, + TransactionSource::InBlock, + extrinsic.clone(), + ) + .await; - if let Ok(Ok(validity)) = validity { - future_tags.extend(validity.provides); + if let Ok(Ok(validity)) = validity { + future_tags.extend(validity.provides); + } + } else { + log::trace!(target: "txpool", "txpool is empty, skipping validation for block {at:?}"); } }, }