Skip to content

Commit

Permalink
txpool: internal crate reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkucharczyk committed May 23, 2024
1 parent a823d18 commit 4965180
Show file tree
Hide file tree
Showing 13 changed files with 1,036 additions and 768 deletions.
179 changes: 179 additions & 0 deletions substrate/client/transaction-pool/src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
use sp_runtime::traits::Block as BlockT;
use std::{marker::PhantomData, sync::Arc};

use crate::{
graph::IsValidator, single_state_txpool::single_state_txpool::FullPool as SingleStateFullPool,
};
use prometheus_endpoint::Registry as PrometheusRegistry;
use sc_transaction_pool_api::{LocalTransactionPool, MaintainedTransactionPool};
use sp_core::traits::SpawnEssentialNamed;

/// The type of transaction pool.
#[derive(Debug, Clone)]
pub enum TransactionPoolType {
/// Single-state transaction pool
SingleState,
}

/// Transaction pool options.
#[derive(Debug, Clone)]
pub struct TransactionPoolOptions {
txpool_type: TransactionPoolType,
options: crate::graph::Options,
}

impl Default for TransactionPoolOptions {
fn default() -> Self {
Self { txpool_type: TransactionPoolType::SingleState, options: Default::default() }
}
}

impl TransactionPoolOptions {
/// Creates the options for the transaction pool using given parameters.
pub fn new_with_params(
pool_limit: usize,
pool_kbytes: usize,
tx_ban_seconds: Option<u64>,
txpool_type: TransactionPoolType,
is_dev: bool,
) -> TransactionPoolOptions {
let mut options = crate::graph::Options::default();

// ready queue
options.ready.count = pool_limit;
options.ready.total_bytes = pool_kbytes * 1024;

// future queue
let factor = 10;
options.future.count = pool_limit / factor;
options.future.total_bytes = pool_kbytes * 1024 / factor;

options.ban_time = if let Some(ban_seconds) = tx_ban_seconds {
std::time::Duration::from_secs(ban_seconds)
} else if is_dev {
std::time::Duration::from_secs(0)
} else {
std::time::Duration::from_secs(30 * 60)
};

TransactionPoolOptions { options, txpool_type }
}
}

use crate::{common::api::FullChainApi, graph::ChainApi};

/// `FullClientTransactionPool` is a trait that combines the functionality of
/// `MaintainedTransactionPool` and `LocalTransactionPool` for a given `Client` and `Block`.
///
/// This trait defines the requirements for a full client transaction pool, ensuring
/// that it can handle transactions submission and maintenance.
pub trait FullClientTransactionPool<Client, Block>:
MaintainedTransactionPool<
Block = Block,
Hash = crate::graph::ExtrinsicHash<FullChainApi<Client, Block>>,
InPoolTransaction = crate::graph::base_pool::Transaction<
crate::graph::ExtrinsicHash<FullChainApi<Client, Block>>,
<Block as BlockT>::Extrinsic,
>,
Error = <FullChainApi<Client, Block> as ChainApi>::Error,
> + LocalTransactionPool<
Block = Block,
Hash = crate::graph::ExtrinsicHash<FullChainApi<Client, Block>>,
Error = <FullChainApi<Client, Block> as ChainApi>::Error,
>
where
Block: BlockT,
Client: sp_api::ProvideRuntimeApi<Block>
+ sc_client_api::BlockBackend<Block>
+ sc_client_api::blockchain::HeaderBackend<Block>
+ sp_runtime::traits::BlockIdTo<Block>
+ sp_blockchain::HeaderMetadata<Block, Error = sp_blockchain::Error>
+ 'static,
Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,
{
}

impl<Client, Block, P> FullClientTransactionPool<Client, Block> for P
where
Block: BlockT,
Client: sp_api::ProvideRuntimeApi<Block>
+ sc_client_api::BlockBackend<Block>
+ sc_client_api::blockchain::HeaderBackend<Block>
+ sp_runtime::traits::BlockIdTo<Block>
+ sp_blockchain::HeaderMetadata<Block, Error = sp_blockchain::Error>
+ 'static,
Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,
P: MaintainedTransactionPool<
Block = Block,
Hash = crate::graph::ExtrinsicHash<FullChainApi<Client, Block>>,
InPoolTransaction = crate::graph::base_pool::Transaction<
crate::graph::ExtrinsicHash<FullChainApi<Client, Block>>,
<Block as BlockT>::Extrinsic,
>,
Error = <FullChainApi<Client, Block> as ChainApi>::Error,
> + LocalTransactionPool<
Block = Block,
Hash = crate::graph::ExtrinsicHash<FullChainApi<Client, Block>>,
Error = <FullChainApi<Client, Block> as ChainApi>::Error,
>,
{
}

/// The type alias for a dynamic trait object implementing
/// `FullClientTransactionPool` with the given `Client` and `Block` types.
///
/// This trait object abstracts away the specific implementations of the transaction pool.
pub type TransactionPoolImpl<Client, Block> = dyn FullClientTransactionPool<Client, Block>;

/// Builder allowing to create specific instance of transaction pool.
pub struct Builder<Client, Block> {
options: TransactionPoolOptions,
_phantom: PhantomData<(Client, Block)>,
}

impl<Client, Block> Builder<Client, Block>
where
Block: BlockT,
Client: sp_api::ProvideRuntimeApi<Block>
+ sc_client_api::BlockBackend<Block>
+ sc_client_api::blockchain::HeaderBackend<Block>
+ sp_runtime::traits::BlockIdTo<Block>
+ sc_client_api::ExecutorProvider<Block>
+ sc_client_api::UsageProvider<Block>
+ sp_blockchain::HeaderMetadata<Block, Error = sp_blockchain::Error>
+ Send
+ Sync
+ 'static,
<Block as BlockT>::Hash: std::marker::Unpin,
Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,
{
/// Creates new instance of `Builder`
pub fn new() -> Builder<Client, Block> {
Builder { options: Default::default(), _phantom: Default::default() }
}

/// Sets the options used for creating a transaction pool instance.
pub fn with_options(mut self, options: TransactionPoolOptions) -> Self {
self.options = options;
self
}

/// Creates an instance of transaction pool.
pub fn build(
self,
is_validator: IsValidator,
prometheus: Option<&PrometheusRegistry>,
spawner: impl SpawnEssentialNamed,
client: Arc<Client>,
) -> Arc<TransactionPoolImpl<Client, Block>> {
match self.options.txpool_type {
TransactionPoolType::SingleState => SingleStateFullPool::new_full(
self.options.options,
is_validator,
prometheus,
spawner,
client,
),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ use sp_runtime::{
};
use sp_transaction_pool::runtime_api::TaggedTransactionQueue;

use crate::{
use super::{
error::{self, Error},
graph,
metrics::{ApiMetrics, ApiMetricsExt},
};
use crate::graph;

/// The transaction pool logic for full client.
pub struct FullChainApi<Client, Block> {
Expand Down
26 changes: 26 additions & 0 deletions substrate/client/transaction-pool/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Common components re-used across different txpool implementations.

pub(crate) mod api;
pub(crate) mod enactment_state;
pub(crate) mod error;
pub(crate) mod metrics;
#[cfg(test)]
pub(crate) mod tests;
5 changes: 1 addition & 4 deletions substrate/client/transaction-pool/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,5 @@ mod validated_pool;
pub mod base_pool;
pub mod watcher;

pub use self::{
base_pool::Transaction,
pool::{BlockHash, ChainApi, ExtrinsicFor, ExtrinsicHash, NumberFor, Options, Pool},
};
pub use self::pool::{BlockHash, ChainApi, ExtrinsicFor, ExtrinsicHash, NumberFor, Options, Pool};
pub use validated_pool::{IsValidator, ValidatedTransaction};
2 changes: 1 addition & 1 deletion substrate/client/transaction-pool/src/graph/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ impl<B: ChainApi> Clone for Pool<B> {
#[cfg(test)]
mod tests {
use super::{super::base_pool::Limit, *};
use crate::tests::{pool, uxt, TestApi, INVALID_NONCE};
use crate::common::tests::{pool, uxt, TestApi, INVALID_NONCE};
use assert_matches::assert_matches;
use codec::Encode;
use futures::executor::block_on;
Expand Down
Loading

0 comments on commit 4965180

Please sign in to comment.