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

23
use polkadot_primitives::{Block, BlockNumber, AccountId, Nonce, Balance, Hash};
24
use sp_api::ProvideRuntimeApi;
Gavin Wood's avatar
Gavin Wood committed
25
use txpool_api::TransactionPool;
26
27
use sp_blockchain::{HeaderBackend, HeaderMetadata, Error as BlockChainError};
use sp_consensus::SelectChain;
28
use sp_consensus_babe::BabeApi;
Seun Lanlege's avatar
Seun Lanlege committed
29
use sc_client_api::light::{Fetcher, RemoteBlockchain};
30
use sc_consensus_babe::Epoch;
31
use sc_rpc::DenyUnsafe;
32
33

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

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/// 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.
	pub keystore: sc_keystore::KeyStorePtr,
}

58
59
60
61
62
63
64
65
/// Dependencies for GRANDPA
pub struct GrandpaDeps {
	/// Voting round info.
	pub shared_voter_state: sc_finality_grandpa::SharedVoterState,
	/// Authority set info.
	pub shared_authority_set: sc_finality_grandpa::SharedAuthoritySet<Hash, BlockNumber>,
}

66
67
68
69
70
71
72
73
/// Full client dependencies
pub struct FullDeps<C, P, SC> {
	/// The client instance to use.
	pub client: Arc<C>,
	/// Transaction pool instance.
	pub pool: Arc<P>,
	/// The SelectChain Strategy
	pub select_chain: SC,
74
75
	/// Whether to deny unsafe calls
	pub deny_unsafe: DenyUnsafe,
76
77
78
79
80
81
	/// BABE specific dependencies.
	pub babe: BabeDeps,
	/// GRANDPA specific dependencies.
	pub grandpa: GrandpaDeps,
}

82
/// Instantiate all RPC extensions.
83
pub fn create_full<C, P, UE, SC>(deps: FullDeps<C, P, SC>) -> RpcExtension where
84
	C: ProvideRuntimeApi<Block>,
85
	C: HeaderBackend<Block> + HeaderMetadata<Block, Error=BlockChainError>,
86
	C: Send + Sync + 'static,
Gavin Wood's avatar
Gavin Wood committed
87
	C::Api: frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
88
	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance, UE>,
89
	C::Api: BabeApi<Block>,
Gavin Wood's avatar
Gavin Wood committed
90
	P: TransactionPool + Sync + Send + 'static,
91
	UE: codec::Codec + Send + Sync + 'static,
92
	SC: SelectChain<Block> + 'static,
93
{
Gavin Wood's avatar
Gavin Wood committed
94
	use frame_rpc_system::{FullSystem, SystemApi};
95
	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
96
	use sc_finality_grandpa_rpc::{GrandpaApi, GrandpaRpcHandler};
97
	use sc_consensus_babe_rpc::BabeRpcHandler;
98
99

	let mut io = jsonrpc_core::IoHandler::default();
100
101
102
103
	let FullDeps {
		client,
		pool,
		select_chain,
104
		deny_unsafe,
105
106
107
108
109
110
111
112
		babe,
		grandpa,
	} = deps;
	let BabeDeps {
		keystore,
		babe_config,
		shared_epoch_changes,
	} = babe;
113
114
115
	let GrandpaDeps {
		shared_voter_state,
		shared_authority_set,
116
	} = grandpa;
117

118
	io.extend_with(
Gavin Wood's avatar
Gavin Wood committed
119
		SystemApi::to_delegate(FullSystem::new(client.clone(), pool))
120
	);
Kian Paimani's avatar
Kian Paimani committed
121
	io.extend_with(
122
123
124
125
		TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone()))
	);
	io.extend_with(
		sc_consensus_babe_rpc::BabeApi::to_delegate(
126
127
128
129
130
131
132
133
			BabeRpcHandler::new(
				client,
				shared_epoch_changes,
				keystore,
				babe_config,
				select_chain,
				deny_unsafe,
			)
134
		)
Kian Paimani's avatar
Kian Paimani committed
135
	);
136
137
138
139
140
141
	io.extend_with(
		GrandpaApi::to_delegate(GrandpaRpcHandler::new(
			shared_authority_set,
			shared_voter_state,
		))
	);
142
143
	io
}
Gavin Wood's avatar
Gavin Wood committed
144
145

/// Instantiate all RPC extensions for light node.
146
pub fn create_light<C, P, F, UE>(deps: LightDeps<C, F, P>) -> RpcExtension
Gavin Wood's avatar
Gavin Wood committed
147
	where
148
		C: ProvideRuntimeApi<Block>,
Seun Lanlege's avatar
Seun Lanlege committed
149
		C: HeaderBackend<Block>,
Gavin Wood's avatar
Gavin Wood committed
150
151
		C: Send + Sync + 'static,
		C::Api: frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
152
		C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance, UE>,
Gavin Wood's avatar
Gavin Wood committed
153
		P: TransactionPool + Sync + Send + 'static,
Seun Lanlege's avatar
Seun Lanlege committed
154
		F: Fetcher<Block> + 'static,
155
		UE: codec::Codec + Send + Sync + 'static,
Gavin Wood's avatar
Gavin Wood committed
156
157
158
{
	use frame_rpc_system::{LightSystem, SystemApi};

159
160
161
162
163
164
	let LightDeps {
		client,
		pool,
		remote_blockchain,
		fetcher,
	} = deps;
Gavin Wood's avatar
Gavin Wood committed
165
166
167
168
169
170
	let mut io = jsonrpc_core::IoHandler::default();
	io.extend_with(
		SystemApi::<AccountId, Nonce>::to_delegate(LightSystem::new(client, remote_blockchain, fetcher, pool))
	);
	io
}