Skip to content

Instantly share code, notes, and snippets.

@yrong
Created July 31, 2021 17:06
Show Gist options
  • Save yrong/b7d141aef69a41b817be4fbce0f1140c to your computer and use it in GitHub Desktop.
Save yrong/b7d141aef69a41b817be4fbce0f1140c to your computer and use it in GitHub Desktop.
consensus-manual-seal
use std::sync::Arc;
use asgard_runtime::Header;
use polkadot_service::{FullBackend, LongestChain};
use sc_client_api::ExecutorProvider;
pub use sc_consensus_aura::{ImportQueueParams, StartAuraParams};
use sc_consensus_manual_seal::InstantSealParams;
use sc_consensus_slots::SlotProportion;
use sc_service::{error::Error as ServiceError, Configuration, PartialComponents, TaskManager};
use sc_telemetry::{Telemetry, TelemetryWorker, TelemetryWorkerHandle};
use sp_api::TransactionFor;
use sp_consensus::{import_queue::BasicQueue, SlotData};
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
use sp_runtime::generic;
use crate::AsgardExecutor;
type Block = generic::Block<Header, sp_runtime::OpaqueExtrinsic>;
type FullSelectChain = LongestChain<FullBackend, Block>;
type FullClientDev = sc_service::TFullClient<Block, asgard_runtime::RuntimeApi, AsgardExecutor>;
#[allow(clippy::type_complexity)]
pub fn new_partial(
config: &Configuration,
) -> Result<
PartialComponents<
FullClientDev,
FullBackend,
FullSelectChain,
BasicQueue<Block, TransactionFor<FullClientDev, Block>>,
sc_transaction_pool::FullPool<Block, FullClientDev>,
(),
>,
ServiceError,
> {
let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, asgard_runtime::RuntimeApi, AsgardExecutor>(
&config, None,
)?;
let client = Arc::new(client);
let select_chain = sc_consensus::LongestChain::new(backend.clone());
let transaction_pool = sc_transaction_pool::BasicPool::new_full(
config.transaction_pool.clone(),
config.role.is_authority().into(),
config.prometheus_registry(),
task_manager.spawn_essential_handle(),
client.clone(),
);
let import_queue = sc_consensus_manual_seal::import_queue(
Box::new(client.clone()),
&task_manager.spawn_handle(),
config.prometheus_registry(),
);
Ok(PartialComponents {
client,
backend,
import_queue,
keystore_container,
task_manager,
transaction_pool,
select_chain,
other: (),
})
}
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
let sc_service::PartialComponents {
client,
backend,
mut task_manager,
import_queue,
keystore_container,
select_chain,
transaction_pool,
..
} = new_partial(&config)?;
let (network, system_rpc_tx, network_starter) =
sc_service::build_network(sc_service::BuildNetworkParams {
config: &config,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
spawn_handle: task_manager.spawn_handle(),
import_queue,
on_demand: None,
block_announce_validator_builder: None,
})?;
let keystore = keystore_container.sync_keystore();
if config.offchain_worker.enabled {
// Initialize seed for signing transaction using off-chain workers. This is a convenience
// so learners can see the transactions submitted simply running the node.
// Typically these keys should be inserted with RPC calls to `author_insertKey`.
#[cfg(feature = "ocw")]
{
sp_keystore::SyncCryptoStore::sr25519_generate_new(
&*keystore,
runtime::ocw_demo::KEY_TYPE,
Some("//Alice"),
)
.expect("Creating key with account Alice should succeed.");
}
sc_service::build_offchain_workers(
&config,
task_manager.spawn_handle(),
client.clone(),
network.clone(),
);
}
let is_authority = config.role.is_authority();
let prometheus_registry = config.prometheus_registry().cloned();
sc_service::spawn_tasks(sc_service::SpawnTasksParams {
network,
client: client.clone(),
keystore,
task_manager: &mut task_manager,
transaction_pool: transaction_pool.clone(),
rpc_extensions_builder: Box::new(|_, _| ()),
on_demand: None,
remote_blockchain: None,
backend,
system_rpc_tx,
config,
telemetry: None,
})?;
if is_authority {
let proposer = sc_basic_authorship::ProposerFactory::new(
task_manager.spawn_handle(),
client.clone(),
transaction_pool.clone(),
prometheus_registry.as_ref(),
None,
);
let authorship_future = sc_consensus_manual_seal::run_instant_seal(InstantSealParams {
block_import: client.clone(),
env: proposer,
client,
pool: transaction_pool.pool().clone(),
select_chain,
consensus_data_provider: None,
create_inherent_data_providers: None,
});
task_manager
.spawn_essential_handle()
.spawn_blocking("instant-seal", authorship_future);
};
network_starter.start_network();
Ok(task_manager)
}
@yrong
Copy link
Author

yrong commented Jul 31, 2021

build error log as following:

error[E0277]: the trait bound `SpawnTaskHandle: SpawnEssentialNamed` is not satisfied
  --> node/service/src/manual.rs:71:21
   |
71 |     let import_queue = sc_consensus_manual_seal::import_queue(
   |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SpawnEssentialNamed` is not implemented for `SpawnTaskHandle`
   | 
  ::: /Users/yangrong/.cargo/git/checkouts/substrate-7e08433d4c370a21/1b758b2/client/consensus/manual-seal/src/lib.rs:80:20
   |
80 |     spawner: &impl sp_core::traits::SpawnEssentialNamed,
   |                    ------------------------------------ required by this bound in `sc_consensus_manual_seal::import_queue`


error[E0277]: expected a `Fn<(H256, ())>` closure, found `std::option::Option<_>`
   --> node/service/src/manual.rs:162:70
    |
162 |           let authorship_future = sc_consensus_manual_seal::run_instant_seal(InstantSealParams {
    |  ____________________________________________________________________________^
163 | |             block_import: client.clone(),
164 | |             env: proposer,
165 | |             client,
...   |
169 | |             create_inherent_data_providers: None,
170 | |         });
    | |_________^ expected an `Fn<(H256, ())>` closure, found `std::option::Option<_>`
    | 
   ::: /Users/yangrong/.cargo/git/checkouts/substrate-7e08433d4c370a21/1b758b2/client/consensus/manual-seal/src/lib.rs:240:15
    |
240 |           CIDP: CreateInherentDataProviders<B, ()>,
    |                 ---------------------------------- required by this bound in `run_instant_seal`
    |
    = help: the trait `Fn<(H256, ())>` is not implemented for `std::option::Option<_>`
    = note: required because of the requirements on the impl of `CreateInherentDataProviders<sp_runtime::generic::Block<sp_runtime::generic::Header<u32, BlakeTwo256>, OpaqueExtrinsic>, ()>` for `std::option::Option<_>`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment