Transaction pool: Adds benchmark and improves performance (#9958)

* Yep

* Make it compile

* Make the benchmark work

* Some stuff

* Optimize transaction pool `BestIterator`

* Some docs

* Fix more warnings

* Fix compilation

* FMT
This commit is contained in:
Bastian Köcher
2021-10-08 10:02:25 +02:00
committed by GitHub
parent b965d49a6a
commit 451413f47e
9 changed files with 415 additions and 65 deletions
+1 -1
View File
@@ -33,7 +33,7 @@
pub mod chain_spec;
#[macro_use]
mod service;
pub mod service;
#[cfg(feature = "cli")]
mod cli;
#[cfg(feature = "cli")]
+88 -4
View File
@@ -20,20 +20,25 @@
//! Service implementation. Specialized wrapper over substrate service.
use codec::Encode;
use frame_system_rpc_runtime_api::AccountNonceApi;
use futures::prelude::*;
use node_executor::ExecutorDispatch;
use node_primitives::Block;
use node_runtime::RuntimeApi;
use sc_client_api::{ExecutorProvider, RemoteBackend};
use sc_client_api::{BlockBackend, ExecutorProvider, RemoteBackend};
use sc_consensus_babe::{self, SlotProportion};
use sc_executor::NativeElseWasmExecutor;
use sc_network::{Event, NetworkService};
use sc_service::{config::Configuration, error::Error as ServiceError, RpcHandlers, TaskManager};
use sc_telemetry::{Telemetry, TelemetryWorker};
use sp_runtime::traits::Block as BlockT;
use sp_api::ProvideRuntimeApi;
use sp_core::crypto::Pair;
use sp_runtime::{generic, traits::Block as BlockT, SaturatedConversion};
use std::sync::Arc;
type FullClient =
/// The full client type definition.
pub type FullClient =
sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
@@ -41,7 +46,80 @@ type FullGrandpaBlockImport =
grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>;
type LightClient =
sc_service::TLightClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
/// The transaction pool type defintion.
pub type TransactionPool = sc_transaction_pool::FullPool<Block, FullClient>;
/// Fetch the nonce of the given `account` from the chain state.
///
/// Note: Should only be used for tests.
pub fn fetch_nonce(client: &FullClient, account: sp_core::sr25519::Pair) -> u32 {
let best_hash = client.chain_info().best_hash;
client
.runtime_api()
.account_nonce(&generic::BlockId::Hash(best_hash), account.public().into())
.expect("Fetching account nonce works; qed")
}
/// Create a transaction using the given `call`.
///
/// The transaction will be signed by `sender`. If `nonce` is `None` it will be fetched from the
/// state of the best block.
///
/// Note: Should only be used for tests.
pub fn create_extrinsic(
client: &FullClient,
sender: sp_core::sr25519::Pair,
function: impl Into<node_runtime::Call>,
nonce: Option<u32>,
) -> node_runtime::UncheckedExtrinsic {
let function = function.into();
let genesis_hash = client.block_hash(0).ok().flatten().expect("Genesis block exists; qed");
let best_hash = client.chain_info().best_hash;
let best_block = client.chain_info().best_number;
let nonce = nonce.unwrap_or_else(|| fetch_nonce(client, sender.clone()));
let period = node_runtime::BlockHashCount::get()
.checked_next_power_of_two()
.map(|c| c / 2)
.unwrap_or(2) as u64;
let tip = 0;
let extra: node_runtime::SignedExtra = (
frame_system::CheckSpecVersion::<node_runtime::Runtime>::new(),
frame_system::CheckTxVersion::<node_runtime::Runtime>::new(),
frame_system::CheckGenesis::<node_runtime::Runtime>::new(),
frame_system::CheckEra::<node_runtime::Runtime>::from(generic::Era::mortal(
period,
best_block.saturated_into(),
)),
frame_system::CheckNonce::<node_runtime::Runtime>::from(nonce),
frame_system::CheckWeight::<node_runtime::Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<node_runtime::Runtime>::from(tip),
);
let raw_payload = node_runtime::SignedPayload::from_raw(
function.clone(),
extra.clone(),
(
node_runtime::VERSION.spec_version,
node_runtime::VERSION.transaction_version,
genesis_hash,
best_hash,
(),
(),
(),
),
);
let signature = raw_payload.using_encoded(|e| sender.sign(e));
node_runtime::UncheckedExtrinsic::new_signed(
function.clone(),
sp_runtime::AccountId32::from(sender.public()).into(),
node_runtime::Signature::Sr25519(signature.clone()),
extra.clone(),
)
}
/// Creates a new partial node.
pub fn new_partial(
config: &Configuration,
) -> Result<
@@ -211,11 +289,16 @@ pub fn new_partial(
})
}
/// Result of [`new_full_base`].
pub struct NewFullBase {
/// The task manager of the node.
pub task_manager: TaskManager,
/// The client instance of the node.
pub client: Arc<FullClient>,
/// The networking service of the node.
pub network: Arc<NetworkService<Block, <Block as BlockT>::Hash>>,
pub transaction_pool: Arc<sc_transaction_pool::FullPool<Block, FullClient>>,
/// The transaction pool of the node.
pub transaction_pool: Arc<TransactionPool>,
}
/// Creates a full service from the configuration.
@@ -433,6 +516,7 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
new_full_base(config, |_, _| ()).map(|NewFullBase { task_manager, .. }| task_manager)
}
/// Creates a light service from the configuration.
pub fn new_light_base(
mut config: Configuration,
) -> Result<