mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 19:01:08 +00:00
Simplify a few chain components creation APIs related to the service (#6611)
* Simplify a few chain components creation APIs related to the service * Fix basic-authorship doc tests * Remove DefaultQueue * Update client/service/src/builder.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> * Move ExecutionExtensions comment around * Remove unused BlakeTwo256 Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Generated
+1
@@ -7686,6 +7686,7 @@ dependencies = [
|
||||
"sp-state-machine",
|
||||
"sp-std",
|
||||
"sp-test-primitives",
|
||||
"sp-trie",
|
||||
"sp-utils",
|
||||
"sp-version",
|
||||
"substrate-prometheus-endpoint",
|
||||
|
||||
@@ -49,10 +49,12 @@ macro_rules! new_full_start {
|
||||
builder.client().clone(),
|
||||
None,
|
||||
);
|
||||
Ok(sc_transaction_pool::BasicPool::new(
|
||||
Ok(sc_transaction_pool::BasicPool::new_full(
|
||||
builder.config().transaction_pool.clone(),
|
||||
std::sync::Arc::new(pool_api),
|
||||
builder.prometheus_registry(),
|
||||
builder.spawn_handle(),
|
||||
builder.client().clone(),
|
||||
))
|
||||
})?
|
||||
.with_import_queue(|
|
||||
@@ -221,12 +223,12 @@ pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
|
||||
builder.client().clone(),
|
||||
fetcher.clone(),
|
||||
);
|
||||
let pool = sc_transaction_pool::BasicPool::with_revalidation_type(
|
||||
let pool = Arc::new(sc_transaction_pool::BasicPool::new_light(
|
||||
builder.config().transaction_pool.clone(),
|
||||
Arc::new(pool_api),
|
||||
builder.prometheus_registry(),
|
||||
sc_transaction_pool::RevalidationType::Light,
|
||||
);
|
||||
builder.spawn_handle(),
|
||||
));
|
||||
Ok(pool)
|
||||
})?
|
||||
.with_import_queue_and_fprb(|
|
||||
|
||||
@@ -63,12 +63,12 @@ macro_rules! new_full_start {
|
||||
builder.client().clone(),
|
||||
builder.prometheus_registry(),
|
||||
);
|
||||
let config = builder.config();
|
||||
|
||||
Ok(sc_transaction_pool::BasicPool::new(
|
||||
config.transaction_pool.clone(),
|
||||
Ok(sc_transaction_pool::BasicPool::new_full(
|
||||
builder.config().transaction_pool.clone(),
|
||||
std::sync::Arc::new(pool_api),
|
||||
builder.prometheus_registry(),
|
||||
builder.spawn_handle(),
|
||||
builder.client().clone(),
|
||||
))
|
||||
})?
|
||||
.with_import_queue(|
|
||||
@@ -356,12 +356,12 @@ pub fn new_light_base(config: Configuration) -> Result<(
|
||||
builder.client().clone(),
|
||||
fetcher,
|
||||
);
|
||||
let pool = sc_transaction_pool::BasicPool::with_revalidation_type(
|
||||
let pool = Arc::new(sc_transaction_pool::BasicPool::new_light(
|
||||
builder.config().transaction_pool.clone(),
|
||||
Arc::new(pool_api),
|
||||
builder.prometheus_registry(),
|
||||
sc_transaction_pool::RevalidationType::Light,
|
||||
);
|
||||
builder.spawn_handle(),
|
||||
));
|
||||
Ok(pool)
|
||||
})?
|
||||
.with_import_queue_and_fprb(|
|
||||
|
||||
@@ -84,6 +84,10 @@ pub struct ExecutionExtensions<Block: traits::Block> {
|
||||
keystore: Option<BareCryptoStorePtr>,
|
||||
// FIXME: these two are only RwLock because of https://github.com/paritytech/substrate/issues/4587
|
||||
// remove when fixed.
|
||||
// To break retain cycle between `Client` and `TransactionPool` we require this
|
||||
// extension to be a `Weak` reference.
|
||||
// That's also the reason why it's being registered lazily instead of
|
||||
// during initialization.
|
||||
transaction_pool: RwLock<Option<Weak<dyn sp_transaction_pool::OffchainSubmitTransaction<Block>>>>,
|
||||
extensions_factory: RwLock<Box<dyn ExtensionsFactory>>,
|
||||
}
|
||||
@@ -121,13 +125,10 @@ impl<Block: traits::Block> ExecutionExtensions<Block> {
|
||||
}
|
||||
|
||||
/// Register transaction pool extension.
|
||||
///
|
||||
/// To break retain cycle between `Client` and `TransactionPool` we require this
|
||||
/// extension to be a `Weak` reference.
|
||||
/// That's also the reason why it's being registered lazily instead of
|
||||
/// during initialization.
|
||||
pub fn register_transaction_pool(&self, pool: Weak<dyn sp_transaction_pool::OffchainSubmitTransaction<Block>>) {
|
||||
*self.transaction_pool.write() = Some(pool);
|
||||
pub fn register_transaction_pool<T>(&self, pool: &Arc<T>)
|
||||
where T: sp_transaction_pool::OffchainSubmitTransaction<Block> + 'static
|
||||
{
|
||||
*self.transaction_pool.write() = Some(Arc::downgrade(&pool) as _);
|
||||
}
|
||||
|
||||
/// Create `ExecutionManager` and `Extensions` for given offchain call.
|
||||
|
||||
@@ -114,6 +114,12 @@ pub struct Blockchain<Block: BlockT> {
|
||||
storage: Arc<RwLock<BlockchainStorage<Block>>>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT> Default for Blockchain<Block> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT + Clone> Clone for Blockchain<Block> {
|
||||
fn clone(&self) -> Self {
|
||||
let storage = Arc::new(RwLock::new(self.storage.read().clone()));
|
||||
|
||||
@@ -358,12 +358,13 @@ mod tests {
|
||||
fn should_cease_building_block_when_deadline_is_reached() {
|
||||
// given
|
||||
let client = Arc::new(substrate_test_runtime_client::new());
|
||||
let txpool = Arc::new(
|
||||
BasicPool::new(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let txpool = BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
spawner,
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
futures::executor::block_on(
|
||||
@@ -411,12 +412,13 @@ mod tests {
|
||||
#[test]
|
||||
fn should_not_panic_when_deadline_is_reached() {
|
||||
let client = Arc::new(substrate_test_runtime_client::new());
|
||||
let txpool = Arc::new(
|
||||
BasicPool::new(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let txpool = BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
spawner,
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
let mut proposer_factory = ProposerFactory::new(client.clone(), txpool.clone(), None);
|
||||
@@ -446,12 +448,13 @@ mod tests {
|
||||
fn proposed_storage_changes_should_match_execute_block_storage_changes() {
|
||||
let (client, backend) = TestClientBuilder::new().build_with_backend();
|
||||
let client = Arc::new(client);
|
||||
let txpool = Arc::new(
|
||||
BasicPool::new(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let txpool = BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
spawner,
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
let genesis_hash = client.info().best_hash;
|
||||
@@ -508,12 +511,13 @@ mod tests {
|
||||
fn should_not_remove_invalid_transactions_when_skipping() {
|
||||
// given
|
||||
let mut client = Arc::new(substrate_test_runtime_client::new());
|
||||
let txpool = Arc::new(
|
||||
BasicPool::new(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let txpool = BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
spawner,
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
futures::executor::block_on(
|
||||
|
||||
@@ -31,10 +31,13 @@
|
||||
//! # };
|
||||
//! # use sc_transaction_pool::{BasicPool, FullChainApi};
|
||||
//! # let client = Arc::new(substrate_test_runtime_client::new());
|
||||
//! # let txpool = Arc::new(BasicPool::new(
|
||||
//! # let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
//! # let txpool = BasicPool::new_full(
|
||||
//! # Default::default(),
|
||||
//! # Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
//! # None).0,
|
||||
//! # None,
|
||||
//! # spawner,
|
||||
//! # client.clone(),
|
||||
//! # );
|
||||
//! // The first step is to create a `ProposerFactory`.
|
||||
//! let mut proposer_factory = ProposerFactory::new(client.clone(), txpool.clone(), None);
|
||||
|
||||
@@ -200,10 +200,7 @@ mod tests {
|
||||
AccountKeyring::*,
|
||||
TestClientBuilder,
|
||||
};
|
||||
use sc_transaction_pool::{
|
||||
BasicPool,
|
||||
txpool::Options,
|
||||
};
|
||||
use sc_transaction_pool::{BasicPool, RevalidationType, txpool::Options};
|
||||
use substrate_test_runtime_transaction_pool::{TestApi, uxt};
|
||||
use sp_transaction_pool::{TransactionPool, MaintainedTransactionPool, TransactionSource};
|
||||
use sp_runtime::generic::BlockId;
|
||||
@@ -223,7 +220,10 @@ mod tests {
|
||||
let (client, select_chain) = builder.build_with_longest_chain();
|
||||
let client = Arc::new(client);
|
||||
let inherent_data_providers = InherentDataProviders::new();
|
||||
let pool = Arc::new(BasicPool::new(Options::default(), api(), None).0);
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let pool = Arc::new(BasicPool::with_revalidation_type(
|
||||
Options::default(), api(), None, RevalidationType::Full, spawner,
|
||||
));
|
||||
let env = ProposerFactory::new(
|
||||
client.clone(),
|
||||
pool.clone(),
|
||||
@@ -288,7 +288,10 @@ mod tests {
|
||||
let (client, select_chain) = builder.build_with_longest_chain();
|
||||
let client = Arc::new(client);
|
||||
let inherent_data_providers = InherentDataProviders::new();
|
||||
let pool = Arc::new(BasicPool::new(Options::default(), api(), None).0);
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let pool = Arc::new(BasicPool::with_revalidation_type(
|
||||
Options::default(), api(), None, RevalidationType::Full, spawner,
|
||||
));
|
||||
let env = ProposerFactory::new(
|
||||
client.clone(),
|
||||
pool.clone(),
|
||||
@@ -357,7 +360,10 @@ mod tests {
|
||||
let client = Arc::new(client);
|
||||
let inherent_data_providers = InherentDataProviders::new();
|
||||
let pool_api = api();
|
||||
let pool = Arc::new(BasicPool::new(Options::default(), pool_api.clone(), None).0);
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let pool = Arc::new(BasicPool::with_revalidation_type(
|
||||
Options::default(), pool_api.clone(), None, RevalidationType::Full, spawner,
|
||||
));
|
||||
let env = ProposerFactory::new(
|
||||
client.clone(),
|
||||
pool.clone(),
|
||||
|
||||
@@ -84,7 +84,8 @@ use sc_telemetry::{telemetry, CONSENSUS_INFO, CONSENSUS_DEBUG};
|
||||
use parking_lot::RwLock;
|
||||
|
||||
use finality_grandpa::Error as GrandpaError;
|
||||
use finality_grandpa::{voter, BlockNumberOps, voter_set::VoterSet};
|
||||
use finality_grandpa::{voter, voter_set::VoterSet};
|
||||
pub use finality_grandpa::BlockNumberOps;
|
||||
|
||||
use std::{fmt, io};
|
||||
use std::sync::Arc;
|
||||
@@ -126,7 +127,7 @@ pub use authorities::SharedAuthoritySet;
|
||||
pub use finality_proof::{FinalityProofProvider, StorageAndProofProvider};
|
||||
pub use import::GrandpaBlockImport;
|
||||
pub use justification::GrandpaJustification;
|
||||
pub use light_import::light_block_import;
|
||||
pub use light_import::{light_block_import, GrandpaLightBlockImport};
|
||||
pub use voting_rule::{
|
||||
BeforeBestBlockBy, ThreeQuartersOfTheUnfinalizedChain, VotingRule, VotingRulesBuilder
|
||||
};
|
||||
|
||||
@@ -212,7 +212,6 @@ mod tests {
|
||||
use substrate_test_runtime_client::{TestClient, runtime::Block};
|
||||
use sc_transaction_pool::{BasicPool, FullChainApi};
|
||||
use sp_transaction_pool::{TransactionPool, InPoolTransaction};
|
||||
use sc_client_api::ExecutorProvider;
|
||||
|
||||
struct MockNetworkStateInfo();
|
||||
|
||||
@@ -227,7 +226,7 @@ mod tests {
|
||||
}
|
||||
|
||||
struct TestPool(
|
||||
BasicPool<FullChainApi<TestClient, Block>, Block>
|
||||
Arc<BasicPool<FullChainApi<TestClient, Block>, Block>>
|
||||
);
|
||||
|
||||
impl sp_transaction_pool::OffchainSubmitTransaction<Block> for TestPool {
|
||||
@@ -248,13 +247,14 @@ mod tests {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let client = Arc::new(substrate_test_runtime_client::new());
|
||||
let pool = Arc::new(TestPool(BasicPool::new(
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let pool = TestPool(BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0));
|
||||
client.execution_extensions()
|
||||
.register_transaction_pool(Arc::downgrade(&pool.clone()) as _);
|
||||
spawner,
|
||||
client.clone(),
|
||||
));
|
||||
let db = sc_client_db::offchain::LocalStorage::new_test();
|
||||
let network_state = Arc::new(MockNetworkStateInfo());
|
||||
let header = client.header(&BlockId::number(0)).unwrap().unwrap();
|
||||
|
||||
@@ -61,11 +61,14 @@ impl Default for TestSetup {
|
||||
let client_builder = substrate_test_runtime_client::TestClientBuilder::new();
|
||||
let client = Arc::new(client_builder.set_keystore(keystore.clone()).build());
|
||||
|
||||
let pool = Arc::new(BasicPool::new(
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let pool = BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0);
|
||||
spawner,
|
||||
client.clone(),
|
||||
);
|
||||
TestSetup {
|
||||
client,
|
||||
keystore,
|
||||
|
||||
@@ -66,8 +66,6 @@ use sc_client_api::{
|
||||
use sp_blockchain::{HeaderMetadata, HeaderBackend};
|
||||
use crate::{ServiceComponents, TelemetryOnConnectSinks, RpcHandlers, NetworkStatusSinks};
|
||||
|
||||
pub type BackgroundTask = Pin<Box<dyn Future<Output=()> + Send>>;
|
||||
|
||||
/// Aggregator for the components required to build a service.
|
||||
///
|
||||
/// # Usage
|
||||
@@ -518,6 +516,11 @@ impl<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, TRpc, Backend>
|
||||
self.remote_backend.clone()
|
||||
}
|
||||
|
||||
/// Returns a spawn handle created by the task manager.
|
||||
pub fn spawn_handle(&self) -> SpawnTaskHandle {
|
||||
self.task_manager.spawn_handle()
|
||||
}
|
||||
|
||||
/// Consume the builder and return the parts needed for chain operations.
|
||||
pub fn to_chain_ops_parts(self) -> (Arc<TCl>, Arc<Backend>, TImpQu, TaskManager) {
|
||||
(self.client, self.backend, self.import_queue, self.task_manager)
|
||||
@@ -728,15 +731,11 @@ impl<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, TRpc, Backend>
|
||||
self,
|
||||
transaction_pool_builder: impl FnOnce(
|
||||
&Self,
|
||||
) -> Result<(UExPool, Option<BackgroundTask>), Error>,
|
||||
) -> Result<Arc<UExPool>, Error>,
|
||||
) -> Result<ServiceBuilder<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp,
|
||||
UExPool, TRpc, Backend>, Error>
|
||||
where TSc: Clone, TFchr: Clone {
|
||||
let (transaction_pool, background_task) = transaction_pool_builder(&self)?;
|
||||
|
||||
if let Some(background_task) = background_task{
|
||||
self.task_manager.spawn_handle().spawn("txpool-background", background_task);
|
||||
}
|
||||
let transaction_pool = transaction_pool_builder(&self)?;
|
||||
|
||||
Ok(ServiceBuilder {
|
||||
config: self.config,
|
||||
@@ -749,7 +748,7 @@ impl<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, TRpc, Backend>
|
||||
import_queue: self.import_queue,
|
||||
finality_proof_request_builder: self.finality_proof_request_builder,
|
||||
finality_proof_provider: self.finality_proof_provider,
|
||||
transaction_pool: Arc::new(transaction_pool),
|
||||
transaction_pool: transaction_pool,
|
||||
rpc_extensions_builder: self.rpc_extensions_builder,
|
||||
remote_backend: self.remote_backend,
|
||||
block_announce_validator_builder: self.block_announce_validator_builder,
|
||||
@@ -978,12 +977,7 @@ ServiceBuilder<
|
||||
// Prometheus metrics.
|
||||
let metrics_service = if let Some(PrometheusConfig { port, registry }) = config.prometheus_config.clone() {
|
||||
// Set static metrics.
|
||||
let metrics = MetricsService::with_prometheus(
|
||||
®istry,
|
||||
&config.network.node_name,
|
||||
&config.impl_version,
|
||||
&config.role,
|
||||
)?;
|
||||
let metrics = MetricsService::with_prometheus(®istry, &config)?;
|
||||
spawn_handle.spawn(
|
||||
"prometheus-endpoint",
|
||||
prometheus_endpoint::init_prometheus(port, registry).map(drop)
|
||||
@@ -1122,10 +1116,6 @@ ServiceBuilder<
|
||||
|
||||
/// Builds the full service.
|
||||
pub fn build_full(self) -> Result<ServiceComponents<TBl, TBackend, TSc, TExPool, TCl>, Error> {
|
||||
// make transaction pool available for off-chain runtime calls.
|
||||
self.client.execution_extensions()
|
||||
.register_transaction_pool(Arc::downgrade(&self.transaction_pool) as _);
|
||||
|
||||
self.build_common()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,6 +181,11 @@ impl Configuration {
|
||||
pub fn display_role(&self) -> String {
|
||||
self.role.to_string()
|
||||
}
|
||||
|
||||
/// Returns the prometheus metrics registry, if available.
|
||||
pub fn prometheus_registry<'a>(&'a self) -> Option<&'a Registry> {
|
||||
self.prometheus_config.as_ref().map(|config| &config.registry)
|
||||
}
|
||||
}
|
||||
|
||||
/// Available RPC methods.
|
||||
|
||||
@@ -577,11 +577,14 @@ mod tests {
|
||||
// given
|
||||
let (client, longest_chain) = TestClientBuilder::new().build_with_longest_chain();
|
||||
let client = Arc::new(client);
|
||||
let pool = Arc::new(BasicPool::new(
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let pool = BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0);
|
||||
spawner,
|
||||
client.clone(),
|
||||
);
|
||||
let source = sp_runtime::transaction_validity::TransactionSource::External;
|
||||
let best = longest_chain.best_chain().unwrap();
|
||||
let transaction = Transfer {
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
use std::{convert::TryFrom, time::SystemTime};
|
||||
|
||||
use crate::NetworkStatus;
|
||||
use crate::{NetworkStatus, config::Configuration};
|
||||
use prometheus_endpoint::{register, Gauge, U64, F64, Registry, PrometheusError, Opts, GaugeVec};
|
||||
use sc_telemetry::{telemetry, SUBSTRATE_INFO};
|
||||
use sp_runtime::traits::{NumberFor, Block, SaturatedConversion, UniqueSaturatedInto};
|
||||
@@ -261,17 +261,17 @@ impl MetricsService {
|
||||
|
||||
|
||||
impl MetricsService {
|
||||
pub fn with_prometheus(registry: &Registry, name: &str, version: &str, role: &Role)
|
||||
pub fn with_prometheus(registry: &Registry, config: &Configuration)
|
||||
-> Result<Self, PrometheusError>
|
||||
{
|
||||
let role_bits = match role {
|
||||
let role_bits = match config.role {
|
||||
Role::Full => 1u64,
|
||||
Role::Light => 2u64,
|
||||
Role::Sentry { .. } => 3u64,
|
||||
Role::Authority { .. } => 4u64,
|
||||
};
|
||||
|
||||
PrometheusMetrics::setup(registry, name, version, role_bits).map(|p| {
|
||||
PrometheusMetrics::setup(registry, &config.network.node_name, &config.impl_version, role_bits).map(|p| {
|
||||
Self::inner_new(Some(p))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ use sp_runtime::{
|
||||
generic::BlockId,
|
||||
traits::{Block as BlockT, NumberFor, AtLeast32Bit, Extrinsic, Zero},
|
||||
};
|
||||
use sp_core::traits::SpawnNamed;
|
||||
use sp_transaction_pool::{
|
||||
TransactionPool, PoolStatus, ImportNotificationStream, TxHash, TransactionFor,
|
||||
TransactionStatusStreamFor, MaintainedTransactionPool, PoolFuture, ChainEvent,
|
||||
@@ -152,18 +153,6 @@ impl<PoolApi, Block> BasicPool<PoolApi, Block>
|
||||
Block: BlockT,
|
||||
PoolApi: ChainApi<Block=Block> + 'static,
|
||||
{
|
||||
/// Create new basic transaction pool with provided api.
|
||||
///
|
||||
/// It will also optionally return background task that might be started by the
|
||||
/// caller.
|
||||
pub fn new(
|
||||
options: sc_transaction_graph::Options,
|
||||
pool_api: Arc<PoolApi>,
|
||||
prometheus: Option<&PrometheusRegistry>,
|
||||
) -> (Self, Option<Pin<Box<dyn Future<Output=()> + Send>>>) {
|
||||
Self::with_revalidation_type(options, pool_api, prometheus, RevalidationType::Full)
|
||||
}
|
||||
|
||||
/// Create new basic transaction pool with provided api, for tests.
|
||||
#[cfg(test)]
|
||||
pub fn new_test(
|
||||
@@ -186,6 +175,18 @@ impl<PoolApi, Block> BasicPool<PoolApi, Block>
|
||||
)
|
||||
}
|
||||
|
||||
/// Create new basic transaction pool for a light node with the provided api.
|
||||
pub fn new_light(
|
||||
options: sc_transaction_graph::Options,
|
||||
pool_api: Arc<PoolApi>,
|
||||
prometheus: Option<&PrometheusRegistry>,
|
||||
spawner: impl SpawnNamed,
|
||||
) -> Self {
|
||||
Self::with_revalidation_type(
|
||||
options, pool_api, prometheus, RevalidationType::Light, spawner,
|
||||
)
|
||||
}
|
||||
|
||||
/// Create new basic transaction pool with provided api and custom
|
||||
/// revalidation type.
|
||||
pub fn with_revalidation_type(
|
||||
@@ -193,7 +194,8 @@ impl<PoolApi, Block> BasicPool<PoolApi, Block>
|
||||
pool_api: Arc<PoolApi>,
|
||||
prometheus: Option<&PrometheusRegistry>,
|
||||
revalidation_type: RevalidationType,
|
||||
) -> (Self, Option<Pin<Box<dyn Future<Output=()> + Send>>>) {
|
||||
spawner: impl SpawnNamed,
|
||||
) -> Self {
|
||||
let pool = Arc::new(sc_transaction_graph::Pool::new(options, pool_api.clone()));
|
||||
let (revalidation_queue, background_task) = match revalidation_type {
|
||||
RevalidationType::Light => (revalidation::RevalidationQueue::new(pool_api.clone(), pool.clone()), None),
|
||||
@@ -203,22 +205,23 @@ impl<PoolApi, Block> BasicPool<PoolApi, Block>
|
||||
},
|
||||
};
|
||||
|
||||
(
|
||||
BasicPool {
|
||||
api: pool_api,
|
||||
pool,
|
||||
revalidation_queue: Arc::new(revalidation_queue),
|
||||
revalidation_strategy: Arc::new(Mutex::new(
|
||||
match revalidation_type {
|
||||
RevalidationType::Light => RevalidationStrategy::Light(RevalidationStatus::NotScheduled),
|
||||
RevalidationType::Full => RevalidationStrategy::Always,
|
||||
}
|
||||
)),
|
||||
ready_poll: Default::default(),
|
||||
metrics: PrometheusMetrics::new(prometheus),
|
||||
},
|
||||
background_task,
|
||||
)
|
||||
if let Some(background_task) = background_task {
|
||||
spawner.spawn("txpool-background", background_task);
|
||||
}
|
||||
|
||||
BasicPool {
|
||||
api: pool_api,
|
||||
pool,
|
||||
revalidation_queue: Arc::new(revalidation_queue),
|
||||
revalidation_strategy: Arc::new(Mutex::new(
|
||||
match revalidation_type {
|
||||
RevalidationType::Light => RevalidationStrategy::Light(RevalidationStatus::NotScheduled),
|
||||
RevalidationType::Full => RevalidationStrategy::Always,
|
||||
}
|
||||
)),
|
||||
ready_poll: Default::default(),
|
||||
metrics: PrometheusMetrics::new(prometheus),
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets shared reference to the underlying pool.
|
||||
@@ -334,6 +337,35 @@ impl<PoolApi, Block> TransactionPool for BasicPool<PoolApi, Block>
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block, Client> BasicPool<FullChainApi<Client, Block>, Block>
|
||||
where
|
||||
Block: BlockT,
|
||||
Client: sp_api::ProvideRuntimeApi<Block>
|
||||
+ sc_client_api::BlockBackend<Block>
|
||||
+ sp_runtime::traits::BlockIdTo<Block>,
|
||||
Client: sc_client_api::ExecutorProvider<Block> + Send + Sync + 'static,
|
||||
Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,
|
||||
sp_api::ApiErrorFor<Client, Block>: Send + std::fmt::Display,
|
||||
{
|
||||
/// Create new basic transaction pool for a full node with the provided api.
|
||||
pub fn new_full(
|
||||
options: sc_transaction_graph::Options,
|
||||
pool_api: Arc<FullChainApi<Client, Block>>,
|
||||
prometheus: Option<&PrometheusRegistry>,
|
||||
spawner: impl SpawnNamed,
|
||||
client: Arc<Client>,
|
||||
) -> Arc<Self> {
|
||||
let pool = Arc::new(Self::with_revalidation_type(
|
||||
options, pool_api, prometheus, RevalidationType::Full, spawner
|
||||
));
|
||||
|
||||
// make transaction pool available for off-chain runtime calls.
|
||||
client.execution_extensions().register_transaction_pool(&pool);
|
||||
|
||||
pool
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block, Client> sp_transaction_pool::LocalTransactionPool
|
||||
for BasicPool<FullChainApi<Client, Block>, Block>
|
||||
where
|
||||
|
||||
@@ -26,6 +26,7 @@ sp-std = { version = "2.0.0-rc4", path = "../../std" }
|
||||
sp-version = { version = "2.0.0-rc4", path = "../../version" }
|
||||
sp-runtime = { version = "2.0.0-rc4", path = "../../runtime" }
|
||||
sp-utils = { version = "2.0.0-rc4", path = "../../utils" }
|
||||
sp-trie = { version = "2.0.0-rc4", path = "../../trie" }
|
||||
codec = { package = "parity-scale-codec", version = "1.3.1", features = ["derive"] }
|
||||
parking_lot = "0.10.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
@@ -54,4 +54,4 @@ impl CloneableSpawn for Executor {
|
||||
/// Create tasks executor.
|
||||
pub fn executor() -> Box<dyn CloneableSpawn> {
|
||||
Box::new(Executor::new())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,12 +298,13 @@ mod tests {
|
||||
|
||||
// given
|
||||
let client = Arc::new(substrate_test_runtime_client::new());
|
||||
let pool = Arc::new(
|
||||
BasicPool::new(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let pool = BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
spawner,
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
let source = sp_runtime::transaction_validity::TransactionSource::External;
|
||||
@@ -337,12 +338,13 @@ mod tests {
|
||||
|
||||
// given
|
||||
let client = Arc::new(substrate_test_runtime_client::new());
|
||||
let pool = Arc::new(
|
||||
BasicPool::new(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let pool = BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
spawner,
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
let accounts = FullSystem::new(client, pool, DenyUnsafe::Yes);
|
||||
@@ -360,12 +362,13 @@ mod tests {
|
||||
|
||||
// given
|
||||
let client = Arc::new(substrate_test_runtime_client::new());
|
||||
let pool = Arc::new(
|
||||
BasicPool::new(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let pool = BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
spawner,
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
let accounts = FullSystem::new(client, pool, DenyUnsafe::No);
|
||||
@@ -392,12 +395,13 @@ mod tests {
|
||||
|
||||
// given
|
||||
let client = Arc::new(substrate_test_runtime_client::new());
|
||||
let pool = Arc::new(
|
||||
BasicPool::new(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
).0
|
||||
let spawner = sp_core::testing::SpawnBlockingExecutor::new();
|
||||
let pool = BasicPool::new_full(
|
||||
Default::default(),
|
||||
Arc::new(FullChainApi::new(client.clone(), None)),
|
||||
None,
|
||||
spawner,
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
let accounts = FullSystem::new(client, pool, DenyUnsafe::No);
|
||||
|
||||
Reference in New Issue
Block a user