lib.rs 6.97 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;

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

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

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// 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.
60
	pub keystore: SyncCryptoStorePtr,
61
62
}

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

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

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

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

	let mut io = jsonrpc_core::IoHandler::default();
127
128
129
130
	let FullDeps {
		client,
		pool,
		select_chain,
131
		chain_spec,
132
		deny_unsafe,
133
134
		babe,
		grandpa,
135
		beefy,
136
137
138
139
140
141
	} = deps;
	let BabeDeps {
		keystore,
		babe_config,
		shared_epoch_changes,
	} = babe;
142
143
144
	let GrandpaDeps {
		shared_voter_state,
		shared_authority_set,
145
		justification_stream,
146
		subscription_executor,
Jon Häggblad's avatar
Jon Häggblad committed
147
		finality_provider,
148
	} = grandpa;
149

150
	io.extend_with(
Xiliang Chen's avatar
Xiliang Chen committed
151
		SystemApi::to_delegate(FullSystem::new(client.clone(), pool, deny_unsafe))
152
	);
Kian Paimani's avatar
Kian Paimani committed
153
	io.extend_with(
154
155
		TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone()))
	);
156
157
158
	io.extend_with(
		MmrApi::to_delegate(Mmr::new(client.clone()))
	);
159
160
	io.extend_with(
		sc_consensus_babe_rpc::BabeApi::to_delegate(
161
			BabeRpcHandler::new(
162
163
				client.clone(),
				shared_epoch_changes.clone(),
164
165
166
167
168
				keystore,
				babe_config,
				select_chain,
				deny_unsafe,
			)
169
		)
Kian Paimani's avatar
Kian Paimani committed
170
	);
171
172
	io.extend_with(
		GrandpaApi::to_delegate(GrandpaRpcHandler::new(
173
			shared_authority_set.clone(),
174
			shared_voter_state,
175
			justification_stream,
176
			subscription_executor,
Jon Häggblad's avatar
Jon Häggblad committed
177
			finality_provider,
178
179
		))
	);
180
181
182
183
184
185
186
187
188
	io.extend_with(
		SyncStateRpcApi::to_delegate(SyncStateRpcHandler::new(
			chain_spec,
			client,
			shared_authority_set,
			shared_epoch_changes,
			deny_unsafe,
		))
	);
189
190
191
192
193
194
195
196

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

197
198
	io
}
Gavin Wood's avatar
Gavin Wood committed
199
200

/// Instantiate all RPC extensions for light node.
201
pub fn create_light<C, P, F>(deps: LightDeps<C, F, P>) -> RpcExtension
Gavin Wood's avatar
Gavin Wood committed
202
	where
203
		C: ProvideRuntimeApi<Block>,
Seun Lanlege's avatar
Seun Lanlege committed
204
		C: HeaderBackend<Block>,
Gavin Wood's avatar
Gavin Wood committed
205
206
		C: Send + Sync + 'static,
		C::Api: frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
207
		C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
Gavin Wood's avatar
Gavin Wood committed
208
		P: TransactionPool + Sync + Send + 'static,
Seun Lanlege's avatar
Seun Lanlege committed
209
		F: Fetcher<Block> + 'static,
Gavin Wood's avatar
Gavin Wood committed
210
211
212
{
	use frame_rpc_system::{LightSystem, SystemApi};

213
214
215
216
217
218
	let LightDeps {
		client,
		pool,
		remote_blockchain,
		fetcher,
	} = deps;
Gavin Wood's avatar
Gavin Wood committed
219
220
	let mut io = jsonrpc_core::IoHandler::default();
	io.extend_with(
Xiliang Chen's avatar
Xiliang Chen committed
221
		SystemApi::<Hash, AccountId, Nonce>::to_delegate(LightSystem::new(client, remote_blockchain, fetcher, pool))
Gavin Wood's avatar
Gavin Wood committed
222
223
224
	);
	io
}