Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
bad814f
txpool: don't maintain the pool during major sync
michalkucharczyk Dec 22, 2022
536df11
passing sync_oracle to maintain method
michalkucharczyk Dec 22, 2022
368653d
fixed: builder, txpool tests
michalkucharczyk Dec 22, 2022
b462bfc
Merge remote-tracking branch 'origin/master' into mku-txpool-avoid-cp…
michalkucharczyk Jan 10, 2023
f13c244
do not maintain tx-pool if node gone out of sync
michalkucharczyk Jan 10, 2023
8032569
EnactmentAction: all logic moved to EnactmentState
michalkucharczyk Jan 12, 2023
6149606
maintain guard logic moved directly to MaintainedTransactionPool
michalkucharczyk Jan 12, 2023
3b5ddc4
minor fixes
michalkucharczyk Jan 12, 2023
6fd64ff
EnactmentAction: all logic moved to EnactmentState (again)
michalkucharczyk Jan 12, 2023
f3487f0
SyncOracle fixes here and there
michalkucharczyk Jan 12, 2023
18e6aad
Merge remote-tracking branch 'origin/master' into mku-txpool-avoid-cp…
Jan 12, 2023
47f9fce
Update client/transaction-pool/src/enactment_state.rs
michalkucharczyk Jan 12, 2023
54cf4c0
Update client/transaction-pool/src/enactment_state.rs
michalkucharczyk Jan 12, 2023
d5261cc
sync_oracle removed
michalkucharczyk Jan 12, 2023
ed7fdf6
spelling + fmt + doc
michalkucharczyk Jan 12, 2023
c0f8d0f
Review suggestions applied
michalkucharczyk Jan 12, 2023
b12aee1
log::info -> debug
michalkucharczyk Jan 13, 2023
68e3192
Update client/transaction-pool/src/enactment_state.rs
michalkucharczyk Jan 13, 2023
05332a2
".git/.scripts/commands/fmt/fmt.sh"
Jan 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
txpool: don't maintain the pool during major sync
Fix shall prevent from wasting the CPU during the major sync. No actions
are actually required in transaction pool during the major sync.

Fixes: #12903
  • Loading branch information
michalkucharczyk committed Dec 22, 2022
commit bad814f18af0e983a15de69f7077918f471b24c1
1 change: 1 addition & 0 deletions client/transaction-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sc-transaction-pool-api = { version = "4.0.0-dev", path = "./api" }
sc-utils = { version = "4.0.0-dev", path = "../utils" }
sp-api = { version = "4.0.0-dev", path = "../../primitives/api" }
sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" }
sp-consensus = { version = "0.10.0-dev", path = "../../primitives/consensus/common" }
sp-core = { version = "7.0.0", path = "../../primitives/core" }
sp-runtime = { version = "7.0.0", path = "../../primitives/runtime" }
sp-tracing = { version = "6.0.0", path = "../../primitives/tracing" }
Expand Down
5 changes: 4 additions & 1 deletion client/transaction-pool/src/enactment_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ where
pub fn force_update(&mut self, event: &ChainEvent<Block>) {
match event {
ChainEvent::NewBestBlock { hash, .. } => self.recent_best_block = *hash,
ChainEvent::Finalized { hash, .. } => self.recent_finalized_block = *hash,
ChainEvent::Finalized { hash, .. } => {
self.recent_best_block = *hash;
self.recent_finalized_block = *hash;
},
};
log::debug!(target: "txpool", "forced update: {:?}, {:?}", self.recent_best_block, self.recent_finalized_block);
}
Expand Down
39 changes: 30 additions & 9 deletions client/transaction-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use crate::metrics::MetricsLink as PrometheusMetrics;
use prometheus_endpoint::Registry as PrometheusRegistry;

use sp_blockchain::{HashAndNumber, TreeRoute};
use sp_consensus::SyncOracle as SyncOracleT;

type BoxedReadyIterator<Hash, Data> =
Box<dyn ReadyTransactions<Item = Arc<graph::base_pool::Transaction<Hash, Data>>> + Send>;
Expand All @@ -76,13 +77,15 @@ type ReadyIteratorFor<PoolApi> =
type PolledIterator<PoolApi> = Pin<Box<dyn Future<Output = ReadyIteratorFor<PoolApi>> + Send>>;

/// A transaction pool for a full node.
pub type FullPool<Block, Client> = BasicPool<FullChainApi<Client, Block>, Block>;
pub type FullPool<Block, Client, SyncOracle> =
BasicPool<FullChainApi<Client, Block>, Block, SyncOracle>;

/// Basic implementation of transaction pool that can be customized by providing PoolApi.
pub struct BasicPool<PoolApi, Block>
pub struct BasicPool<PoolApi, Block, SyncOracle>
where
Block: BlockT,
PoolApi: graph::ChainApi<Block = Block>,
SyncOracle: SyncOracleT,
{
pool: Arc<graph::Pool<PoolApi>>,
api: Arc<PoolApi>,
Expand All @@ -91,6 +94,7 @@ where
ready_poll: Arc<Mutex<ReadyPoll<ReadyIteratorFor<PoolApi>, Block>>>,
metrics: PrometheusMetrics,
enactment_state: Arc<Mutex<EnactmentState<Block>>>,
sync_oracle: Arc<SyncOracle>,
}

struct ReadyPoll<T, Block: BlockT> {
Expand Down Expand Up @@ -152,16 +156,18 @@ pub enum RevalidationType {
Full,
}

impl<PoolApi, Block> BasicPool<PoolApi, Block>
impl<PoolApi, Block, SyncOracle> BasicPool<PoolApi, Block, SyncOracle>
where
Block: BlockT,
PoolApi: graph::ChainApi<Block = Block> + 'static,
SyncOracle: SyncOracleT,
{
/// Create new basic transaction pool with provided api, for tests.
pub fn new_test(
pool_api: Arc<PoolApi>,
best_block_hash: Block::Hash,
finalized_hash: Block::Hash,
sync_oracle: Arc<SyncOracle>,
) -> (Self, Pin<Box<dyn Future<Output = ()> + Send>>) {
let pool = Arc::new(graph::Pool::new(Default::default(), true.into(), pool_api.clone()));
let (revalidation_queue, background_task) =
Expand All @@ -178,6 +184,7 @@ where
best_block_hash,
finalized_hash,
))),
sync_oracle,
},
background_task,
)
Expand All @@ -195,6 +202,7 @@ where
best_block_number: NumberFor<Block>,
best_block_hash: Block::Hash,
finalized_hash: Block::Hash,
sync_oracle: Arc<SyncOracle>,
) -> Self {
let pool = Arc::new(graph::Pool::new(options, is_validator, pool_api.clone()));
let (revalidation_queue, background_task) = match revalidation_type {
Expand Down Expand Up @@ -226,6 +234,7 @@ where
best_block_hash,
finalized_hash,
))),
sync_oracle,
}
}

Expand All @@ -240,10 +249,11 @@ where
}
}

impl<PoolApi, Block> TransactionPool for BasicPool<PoolApi, Block>
impl<PoolApi, Block, SyncOracle> TransactionPool for BasicPool<PoolApi, Block, SyncOracle>
where
Block: BlockT,
PoolApi: 'static + graph::ChainApi<Block = Block>,
SyncOracle: SyncOracleT + std::marker::Send + std::marker::Sync,
{
type Block = PoolApi::Block;
type Hash = graph::ExtrinsicHash<PoolApi>;
Expand Down Expand Up @@ -358,7 +368,7 @@ where
}
}

impl<Block, Client> FullPool<Block, Client>
impl<Block, Client, SyncOracle> FullPool<Block, Client, SyncOracle>
where
Block: BlockT,
Client: sp_api::ProvideRuntimeApi<Block>
Expand All @@ -372,6 +382,7 @@ where
+ Sync
+ 'static,
Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,
SyncOracle: SyncOracleT + std::marker::Send + std::marker::Sync + 'static,
{
/// Create new basic transaction pool for a full node with the provided api.
pub fn new_full(
Expand All @@ -380,6 +391,7 @@ where
prometheus: Option<&PrometheusRegistry>,
spawner: impl SpawnEssentialNamed,
client: Arc<Client>,
sync_oracle: Arc<SyncOracle>,
) -> Arc<Self> {
let pool_api = Arc::new(FullChainApi::new(client.clone(), prometheus, &spawner));
let pool = Arc::new(Self::with_revalidation_type(
Expand All @@ -392,6 +404,7 @@ where
client.usage_info().chain.best_number,
client.usage_info().chain.best_hash,
client.usage_info().chain.finalized_hash,
sync_oracle,
));

// make transaction pool available for off-chain runtime calls.
Expand All @@ -401,8 +414,8 @@ where
}
}

impl<Block, Client> sc_transaction_pool_api::LocalTransactionPool
for BasicPool<FullChainApi<Client, Block>, Block>
impl<Block, Client, SyncOracle> sc_transaction_pool_api::LocalTransactionPool
for BasicPool<FullChainApi<Client, Block>, Block, SyncOracle>
where
Block: BlockT,
Client: sp_api::ProvideRuntimeApi<Block>
Expand All @@ -412,6 +425,7 @@ where
+ sp_blockchain::HeaderMetadata<Block, Error = sp_blockchain::Error>,
Client: Send + Sync + 'static,
Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,
SyncOracle: SyncOracleT + std::marker::Send + std::marker::Sync,
{
type Block = Block;
type Hash = graph::ExtrinsicHash<FullChainApi<Client, Block>>;
Expand Down Expand Up @@ -578,10 +592,11 @@ async fn prune_known_txs_for_block<Block: BlockT, Api: graph::ChainApi<Block = B
hashes
}

impl<PoolApi, Block> BasicPool<PoolApi, Block>
impl<PoolApi, Block, SyncOracle> BasicPool<PoolApi, Block, SyncOracle>
where
Block: BlockT,
PoolApi: 'static + graph::ChainApi<Block = Block>,
SyncOracle: SyncOracleT,
{
/// Handles enactment and retraction of blocks, prunes stale transactions
/// (that have already been enacted) and resubmits transactions that were
Expand Down Expand Up @@ -716,12 +731,18 @@ where
}

#[async_trait]
impl<PoolApi, Block> MaintainedTransactionPool for BasicPool<PoolApi, Block>
impl<PoolApi, Block, SyncOracle> MaintainedTransactionPool for BasicPool<PoolApi, Block, SyncOracle>
where
Block: BlockT,
PoolApi: 'static + graph::ChainApi<Block = Block>,
SyncOracle: SyncOracleT + std::marker::Send + std::marker::Sync,
{
async fn maintain(&self, event: ChainEvent<Self::Block>) {
if self.sync_oracle.is_major_syncing() {
self.enactment_state.lock().force_update(&event);
return
}

let prev_finalized_block = self.enactment_state.lock().recent_finalized_block();
let compute_tree_route = |from, to| -> Result<TreeRoute<Block>, String> {
match self.api.tree_route(from, to) {
Expand Down