Skip to content

Commit

Permalink
Limit transaction pool size (paritytech#1676)
Browse files Browse the repository at this point in the history
* Avoid excessive hashing. Store extrinsic len.

* Implement pool limits.

* Fix issues.

* Make sure we return error in case it doesn't make into the pool.

* Pass parameters from CLI.

* Remove redundant todo.

* Fix tests.
  • Loading branch information
tomusdrw authored and MTDK1 committed Apr 12, 2019
1 parent 807e644 commit b15efa8
Show file tree
Hide file tree
Showing 14 changed files with 398 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions core/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use structopt::{StructOpt, clap::AppSettings};
pub use structopt::clap::App;
use params::{
RunCmd, PurgeChainCmd, RevertCmd, ImportBlocksCmd, ExportBlocksCmd, BuildSpecCmd,
NetworkConfigurationParams, SharedParams, MergeParameters
NetworkConfigurationParams, SharedParams, MergeParameters, TransactionPoolParams,
};
pub use params::{NoCustom, CoreParams};
pub use traits::{GetLogFilter, AugmentClap};
Expand Down Expand Up @@ -103,7 +103,7 @@ fn generate_node_name() -> String {
break node_name
}
};

result
}

Expand Down Expand Up @@ -237,6 +237,23 @@ fn parse_node_key(key: Option<String>) -> error::Result<Option<Secret>> {
}
}

/// Fill the given `PoolConfiguration` by looking at the cli parameters.
fn fill_transaction_pool_configuration<F: ServiceFactory>(
options: &mut FactoryFullConfiguration<F>,
params: TransactionPoolParams,
) -> error::Result<()> {
// ready queue
options.transaction_pool.ready.count = params.pool_limit;
options.transaction_pool.ready.total_bytes = params.pool_kbytes * 1024;

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

Ok(())
}

/// Fill the given `NetworkConfiguration` by looking at the cli parameters.
fn fill_network_configuration(
cli: NetworkConfigurationParams,
Expand Down Expand Up @@ -356,6 +373,11 @@ where
client_id,
)?;

fill_transaction_pool_configuration::<F>(
&mut config,
cli.pool_config,
)?;

if let Some(key) = cli.key {
config.keys.push(key);
}
Expand Down
15 changes: 15 additions & 0 deletions core/cli/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ pub struct NetworkConfigurationParams {
pub in_peers: u32,
}

/// Parameters used to create the pool configuration.
#[derive(Debug, StructOpt, Clone)]
pub struct TransactionPoolParams {
/// Maximum number of transactions in the transaction pool.
#[structopt(long = "pool-limit", value_name = "COUNT", default_value = "512")]
pub pool_limit: usize,
/// Maximum number of kilobytes of all transactions stored in the pool.
#[structopt(long = "pool-kbytes", value_name = "COUNT", default_value="10240")]
pub pool_kbytes: usize,
}

/// The `run` command used to run a node.
#[derive(Debug, StructOpt, Clone)]
pub struct RunCmd {
Expand Down Expand Up @@ -183,6 +194,10 @@ pub struct RunCmd {
#[allow(missing_docs)]
#[structopt(flatten)]
pub network_config: NetworkConfigurationParams,

#[allow(missing_docs)]
#[structopt(flatten)]
pub pool_config: TransactionPoolParams,
}

impl_augment_clap!(RunCmd);
Expand Down
14 changes: 7 additions & 7 deletions core/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,16 +452,16 @@ impl<C: Components> network::TransactionPool<ComponentExHash<C>, ComponentBlock<
let encoded = transaction.encode();
if let Some(uxt) = Decode::decode(&mut &encoded[..]) {
let best_block_id = self.best_block_id()?;
let hash = self.pool.hash_of(&uxt);
match self.pool.submit_one(&best_block_id, uxt) {
Ok(hash) => Some(hash),
Err(e) => match e.into_pool_error() {
Ok(e) => match e.kind() {
txpool::error::ErrorKind::AlreadyImported => Some(hash),
_ => {
debug!("Error adding transaction to the pool: {:?}", e);
None
},
Ok(txpool::error::Error(txpool::error::ErrorKind::AlreadyImported(hash), _)) => {
hash.downcast::<ComponentExHash<C>>().ok()
.map(|x| x.as_ref().clone())
},
Ok(e) => {
debug!("Error adding transaction to the pool: {:?}", e);
None
},
Err(e) => {
debug!("Error converting pool error: {:?}", e);
Expand Down
2 changes: 1 addition & 1 deletion core/transaction-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ substrate-primitives = { path = "../primitives" }
txpool = { package = "substrate-transaction-graph", path = "./graph" }

[dev-dependencies]
test_client = { package = "substrate-test-client", path = "../../core/test-client" }
keyring = { package = "substrate-keyring", path = "../../core/keyring" }
test_client = { package = "substrate-test-client", path = "../../core/test-client" }
1 change: 1 addition & 0 deletions core/transaction-pool/graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ sr-primitives = { path = "../../sr-primitives" }

[dev-dependencies]
assert_matches = "1.1"
parity-codec = "3.0"
test_runtime = { package = "substrate-test-runtime", path = "../../test-runtime" }
Loading

0 comments on commit b15efa8

Please sign in to comment.