lib.rs 6.83 KB
Newer Older
Shawn Tabrizi's avatar
Shawn Tabrizi committed
1
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! Polkadot-specific RPCs implementation.

#![warn(missing_docs)]

use std::sync::Arc;

Shawn Tabrizi's avatar
Shawn Tabrizi committed
23
24
25
26
27
28
29
30
31
use polkadot_primitives::v0::{AccountId, Balance, Block, BlockNumber, Hash, Nonce};
use sc_client_api::{
	light::{Fetcher, RemoteBlockchain},
	AuxStore,
};
use sc_consensus_babe::Epoch;
use sc_finality_grandpa::FinalityProofProvider;
pub use sc_rpc::{DenyUnsafe, SubscriptionTaskExecutor};
use sc_sync_state_rpc::{SyncStateRpcApi, SyncStateRpcHandler};
32
use sp_api::ProvideRuntimeApi;
Jon Häggblad's avatar
Jon Häggblad committed
33
use sp_block_builder::BlockBuilder;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
34
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
35
use sp_consensus::SelectChain;
36
use sp_consensus_babe::BabeApi;
37
use sp_keystore::SyncCryptoStorePtr;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
38
use txpool_api::TransactionPool;
39
40

/// A type representing all RPC extensions.
41
pub type RpcExtension = jsonrpc_core::IoHandler<sc_rpc::Metadata>;
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/// Light client extra dependencies.
pub struct LightDeps<C, F, P> {
	/// The client instance to use.
	pub client: Arc<C>,
	/// Transaction pool instance.
	pub pool: Arc<P>,
	/// Remote access to the blockchain (async).
	pub remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
	/// Fetcher instance.
	pub fetcher: Arc<F>,
}

/// Extra dependencies for BABE.
pub struct BabeDeps {
	/// BABE protocol config.
	pub babe_config: sc_consensus_babe::Config,
	/// BABE pending epoch changes.
	pub shared_epoch_changes: sc_consensus_epochs::SharedEpochChanges<Block, Epoch>,
	/// The keystore that manages the keys of the node.
62
	pub keystore: SyncCryptoStorePtr,
63
64
}

65
/// Dependencies for GRANDPA
Jon Häggblad's avatar
Jon Häggblad committed
66
pub struct GrandpaDeps<B> {
67
68
69
70
	/// Voting round info.
	pub shared_voter_state: sc_finality_grandpa::SharedVoterState,
	/// Authority set info.
	pub shared_authority_set: sc_finality_grandpa::SharedAuthoritySet<Hash, BlockNumber>,
71
72
	/// Receives notifications about justification events from Grandpa.
	pub justification_stream: sc_finality_grandpa::GrandpaJustificationStream<Block>,
Jon Häggblad's avatar
Jon Häggblad committed
73
	/// Executor to drive the subscription manager in the Grandpa RPC handler.
74
	pub subscription_executor: sc_rpc::SubscriptionTaskExecutor,
Jon Häggblad's avatar
Jon Häggblad committed
75
76
	/// Finality proof provider.
	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,
77
78
}

79
/// Dependencies for BEEFY
Andreas Doerr's avatar
Andreas Doerr committed
80
pub struct BeefyDeps {
81
	/// Receives notifications about signed commitment events from BEEFY.
Andreas Doerr's avatar
Andreas Doerr committed
82
	pub beefy_commitment_stream: beefy_gadget::notification::BeefySignedCommitmentStream<Block>,
83
84
85
86
	/// Executor to drive the subscription manager in the BEEFY RPC handler.
	pub subscription_executor: sc_rpc::SubscriptionTaskExecutor,
}

87
/// Full client dependencies
Andreas Doerr's avatar
Andreas Doerr committed
88
pub struct FullDeps<C, P, SC, B> {
89
90
91
92
	/// The client instance to use.
	pub client: Arc<C>,
	/// Transaction pool instance.
	pub pool: Arc<P>,
93
	/// The [`SelectChain`] Strategy
94
	pub select_chain: SC,
95
96
	/// A copy of the chain spec.
	pub chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
97
98
	/// Whether to deny unsafe calls
	pub deny_unsafe: DenyUnsafe,
99
100
101
	/// BABE specific dependencies.
	pub babe: BabeDeps,
	/// GRANDPA specific dependencies.
Jon Häggblad's avatar
Jon Häggblad committed
102
	pub grandpa: GrandpaDeps<B>,
103
	/// BEEFY specific dependencies.
Andreas Doerr's avatar
Andreas Doerr committed
104
	pub beefy: BeefyDeps,
105
106
}

107
/// Instantiate all RPC extensions.
108
109
110
pub fn create_full<C, P, SC, B>(
	deps: FullDeps<C, P, SC, B>,
) -> Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>>
Shawn Tabrizi's avatar
Shawn Tabrizi committed
111
112
113
114
115
116
117
118
where
	C: ProvideRuntimeApi<Block>
		+ HeaderBackend<Block>
		+ AuxStore
		+ HeaderMetadata<Block, Error = BlockChainError>
		+ Send
		+ Sync
		+ 'static,
Gavin Wood's avatar
Gavin Wood committed
119
	C::Api: frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
120
	C::Api: pallet_mmr_rpc::MmrRuntimeApi<Block, <Block as sp_runtime::traits::Block>::Hash>,
121
	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
122
	C::Api: BabeApi<Block>,
Xiliang Chen's avatar
Xiliang Chen committed
123
	C::Api: BlockBuilder<Block>,
Gavin Wood's avatar
Gavin Wood committed
124
	P: TransactionPool + Sync + Send + 'static,
125
	SC: SelectChain<Block> + 'static,
Jon Häggblad's avatar
Jon Häggblad committed
126
127
	B: sc_client_api::Backend<Block> + Send + Sync + 'static,
	B::State: sc_client_api::StateBackend<sp_runtime::traits::HashFor<Block>>,
128
{
Gavin Wood's avatar
Gavin Wood committed
129
	use frame_rpc_system::{FullSystem, SystemApi};
Shawn Tabrizi's avatar
Shawn Tabrizi committed
130
	use pallet_mmr_rpc::{Mmr, MmrApi};
131
	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
132
	use sc_consensus_babe_rpc::BabeRpcHandler;
133
	use sc_finality_grandpa_rpc::{GrandpaApi, GrandpaRpcHandler};
134
135

	let mut io = jsonrpc_core::IoHandler::default();
Shawn Tabrizi's avatar
Shawn Tabrizi committed
136
137
138
	let FullDeps { client, pool, select_chain, chain_spec, deny_unsafe, babe, grandpa, beefy } =
		deps;
	let BabeDeps { keystore, babe_config, shared_epoch_changes } = babe;
139
140
141
	let GrandpaDeps {
		shared_voter_state,
		shared_authority_set,
142
		justification_stream,
143
		subscription_executor,
Jon Häggblad's avatar
Jon Häggblad committed
144
		finality_provider,
145
	} = grandpa;
146

Shawn Tabrizi's avatar
Shawn Tabrizi committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
	io.extend_with(SystemApi::to_delegate(FullSystem::new(client.clone(), pool, deny_unsafe)));
	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone())));
	io.extend_with(MmrApi::to_delegate(Mmr::new(client.clone())));
	io.extend_with(sc_consensus_babe_rpc::BabeApi::to_delegate(BabeRpcHandler::new(
		client.clone(),
		shared_epoch_changes.clone(),
		keystore,
		babe_config,
		select_chain,
		deny_unsafe,
	)));
	io.extend_with(GrandpaApi::to_delegate(GrandpaRpcHandler::new(
		shared_authority_set.clone(),
		shared_voter_state,
		justification_stream,
		subscription_executor,
		finality_provider,
	)));
	io.extend_with(SyncStateRpcApi::to_delegate(SyncStateRpcHandler::new(
		chain_spec,
		client,
		shared_authority_set,
		shared_epoch_changes,
		deny_unsafe,
171
	)?));
172
173
174
175
176
177
178
179

	io.extend_with(beefy_gadget_rpc::BeefyApi::to_delegate(
		beefy_gadget_rpc::BeefyRpcHandler::new(
			beefy.beefy_commitment_stream,
			beefy.subscription_executor,
		),
	));

180
	Ok(io)
181
}
Gavin Wood's avatar
Gavin Wood committed
182
183

/// Instantiate all RPC extensions for light node.
184
pub fn create_light<C, P, F>(deps: LightDeps<C, F, P>) -> RpcExtension
Shawn Tabrizi's avatar
Shawn Tabrizi committed
185
186
187
188
189
190
191
192
where
	C: ProvideRuntimeApi<Block>,
	C: HeaderBackend<Block>,
	C: Send + Sync + 'static,
	C::Api: frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
	P: TransactionPool + Sync + Send + 'static,
	F: Fetcher<Block> + 'static,
Gavin Wood's avatar
Gavin Wood committed
193
194
195
{
	use frame_rpc_system::{LightSystem, SystemApi};

Shawn Tabrizi's avatar
Shawn Tabrizi committed
196
197
198
	let LightDeps { client, pool, remote_blockchain, fetcher } = deps;
	let mut io = jsonrpc_core::IoHandler::default();
	io.extend_with(SystemApi::<Hash, AccountId, Nonce>::to_delegate(LightSystem::new(
199
200
201
		client,
		remote_blockchain,
		fetcher,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
202
203
		pool,
	)));
Gavin Wood's avatar
Gavin Wood committed
204
205
	io
}