1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use crate::{default_config, ChainInfo};
use futures::channel::mpsc;
use jsonrpc_core::MetaIoHandler;
use manual_seal::{
consensus::babe::{BabeConsensusDataProvider, SlotTimestampProvider},
import_queue,
rpc::{ManualSeal, ManualSealApi},
run_manual_seal, EngineCommand, ManualSealParams,
};
use sc_client_api::backend::Backend;
use sc_service::{
build_network, new_full_parts, spawn_tasks, BuildNetworkParams, ChainSpec, Configuration,
SpawnTasksParams, TFullBackend, TFullClient, TaskExecutor, TaskManager,
};
use sc_transaction_pool::BasicPool;
use sc_transaction_pool_api::TransactionPool;
use sp_api::{ApiExt, ConstructRuntimeApi, Core, Metadata};
use sp_block_builder::BlockBuilder;
use sp_consensus_babe::BabeApi;
use sp_finality_grandpa::GrandpaApi;
use sp_keyring::sr25519::Keyring::Alice;
use sp_offchain::OffchainWorkerApi;
use sp_runtime::traits::{Block as BlockT, Header};
use sp_session::SessionKeys;
use sp_transaction_pool::runtime_api::TaggedTransactionQueue;
use std::{str::FromStr, sync::Arc};
type ClientParts<T> = (
Arc<MetaIoHandler<sc_rpc::Metadata, sc_rpc_server::RpcMiddleware>>,
TaskManager,
Arc<
TFullClient<
<T as ChainInfo>::Block,
<T as ChainInfo>::RuntimeApi,
<T as ChainInfo>::Executor,
>,
>,
Arc<
dyn TransactionPool<
Block = <T as ChainInfo>::Block,
Hash = <<T as ChainInfo>::Block as BlockT>::Hash,
Error = sc_transaction_pool::error::Error,
InPoolTransaction = sc_transaction_pool::Transaction<
<<T as ChainInfo>::Block as BlockT>::Hash,
<<T as ChainInfo>::Block as BlockT>::Extrinsic,
>,
>,
>,
mpsc::Sender<EngineCommand<<<T as ChainInfo>::Block as BlockT>::Hash>>,
Arc<TFullBackend<<T as ChainInfo>::Block>>,
);
pub enum ConfigOrChainSpec {
Config(Configuration),
ChainSpec(Box<dyn ChainSpec>, TaskExecutor),
}
pub fn client_parts<T>(
config_or_chain_spec: ConfigOrChainSpec,
) -> Result<ClientParts<T>, sc_service::Error>
where
T: ChainInfo + 'static,
<T::RuntimeApi as ConstructRuntimeApi<
T::Block,
TFullClient<T::Block, T::RuntimeApi, T::Executor>,
>>::RuntimeApi: Core<T::Block>
+ Metadata<T::Block>
+ OffchainWorkerApi<T::Block>
+ SessionKeys<T::Block>
+ TaggedTransactionQueue<T::Block>
+ BlockBuilder<T::Block>
+ BabeApi<T::Block>
+ ApiExt<T::Block, StateBackend = <TFullBackend<T::Block> as Backend<T::Block>>::State>
+ GrandpaApi<T::Block>,
<T::Runtime as frame_system::Config>::Call: From<frame_system::Call<T::Runtime>>,
<<T as ChainInfo>::Block as BlockT>::Hash: FromStr,
<<<T as ChainInfo>::Block as BlockT>::Header as Header>::Number:
num_traits::cast::AsPrimitive<usize>,
{
use sp_consensus_babe::AuthorityId;
let config = match config_or_chain_spec {
ConfigOrChainSpec::Config(config) => config,
ConfigOrChainSpec::ChainSpec(chain_spec, task_executor) =>
default_config(task_executor, chain_spec),
};
let (client, backend, keystore, mut task_manager) =
new_full_parts::<T::Block, T::RuntimeApi, T::Executor>(&config, None)?;
let client = Arc::new(client);
let select_chain = sc_consensus::LongestChain::new(backend.clone());
let (grandpa_block_import, ..) = grandpa::block_import(
client.clone(),
&(client.clone() as Arc<_>),
select_chain.clone(),
None,
)?;
let slot_duration = sc_consensus_babe::Config::get_or_compute(&*client)?;
let (block_import, babe_link) = sc_consensus_babe::block_import(
slot_duration.clone(),
grandpa_block_import,
client.clone(),
)?;
let consensus_data_provider = BabeConsensusDataProvider::new(
client.clone(),
keystore.sync_keystore(),
babe_link.epoch_changes().clone(),
vec![(AuthorityId::from(Alice.public()), 1000)],
)
.expect("failed to create ConsensusDataProvider");
let import_queue =
import_queue(Box::new(block_import.clone()), &task_manager.spawn_essential_handle(), None);
let transaction_pool = BasicPool::new_full(
config.transaction_pool.clone(),
true.into(),
config.prometheus_registry(),
task_manager.spawn_essential_handle(),
client.clone(),
);
let (network, system_rpc_tx, network_starter) = {
let params = 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,
warp_sync: None,
};
build_network(params)?
};
sc_service::build_offchain_workers(
&config,
task_manager.spawn_handle(),
client.clone(),
network.clone(),
);
let env = sc_basic_authorship::ProposerFactory::new(
task_manager.spawn_handle(),
client.clone(),
transaction_pool.clone(),
config.prometheus_registry(),
None,
);
let (command_sink, commands_stream) = mpsc::channel(10);
let rpc_sink = command_sink.clone();
let rpc_handlers = {
let params = SpawnTasksParams {
config,
client: client.clone(),
backend: backend.clone(),
task_manager: &mut task_manager,
keystore: keystore.sync_keystore(),
on_demand: None,
transaction_pool: transaction_pool.clone(),
rpc_extensions_builder: Box::new(move |_, _| {
let mut io = jsonrpc_core::IoHandler::default();
io.extend_with(ManualSealApi::to_delegate(ManualSeal::new(rpc_sink.clone())));
Ok(io)
}),
remote_blockchain: None,
network,
system_rpc_tx,
telemetry: None,
};
spawn_tasks(params)?
};
let cloned_client = client.clone();
let create_inherent_data_providers = Box::new(move |_, _| {
let client = cloned_client.clone();
async move {
let timestamp =
SlotTimestampProvider::new(client.clone()).map_err(|err| format!("{:?}", err))?;
let babe =
sp_consensus_babe::inherents::InherentDataProvider::new(timestamp.slot().into());
Ok((timestamp, babe))
}
});
let authorship_future = run_manual_seal(ManualSealParams {
block_import,
env,
client: client.clone(),
pool: transaction_pool.clone(),
commands_stream,
select_chain,
consensus_data_provider: Some(Box::new(consensus_data_provider)),
create_inherent_data_providers,
});
task_manager.spawn_essential_handle().spawn("manual-seal", authorship_future);
network_starter.start_network();
let rpc_handler = rpc_handlers.io_handler();
Ok((rpc_handler, task_manager, client, transaction_pool, command_sink, backend))
}