referrerpolicy=no-referrer-when-downgrade

node_rpc/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! A collection of node-specific RPC methods.
19//!
20//! Since `substrate` core functionality makes no assumptions
21//! about the modules used inside the runtime, so do
22//! RPC methods defined in `sc-rpc` crate.
23//! It means that `client/rpc` can't have any methods that
24//! need some strong assumptions about the particular runtime.
25//!
26//! The RPCs available in this crate however can make some assumptions
27//! about how the runtime is constructed and what FRAME pallets
28//! are part of it. Therefore all node-runtime-specific RPCs can
29//! be placed here or imported from corresponding FRAME RPC definitions.
30
31#![warn(missing_docs)]
32#![warn(unused_crate_dependencies)]
33
34use std::sync::Arc;
35
36use jsonrpsee::RpcModule;
37use node_primitives::{AccountId, Balance, Block, BlockNumber, Hash, Nonce};
38use sc_client_api::AuxStore;
39use sc_consensus_babe::BabeWorkerHandle;
40use sc_consensus_beefy::communication::notification::{
41	BeefyBestBlockStream, BeefyVersionedFinalityProofStream,
42};
43use sc_consensus_grandpa::{
44	FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,
45};
46pub use sc_rpc::SubscriptionTaskExecutor;
47use sc_transaction_pool_api::TransactionPool;
48use sp_api::ProvideRuntimeApi;
49use sp_application_crypto::RuntimeAppPublic;
50use sp_block_builder::BlockBuilder;
51use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
52use sp_consensus::SelectChain;
53use sp_consensus_babe::BabeApi;
54use sp_consensus_beefy::AuthorityIdBound;
55use sp_keystore::KeystorePtr;
56
57/// Extra dependencies for BABE.
58pub struct BabeDeps {
59	/// A handle to the BABE worker for issuing requests.
60	pub babe_worker_handle: BabeWorkerHandle<Block>,
61	/// The keystore that manages the keys of the node.
62	pub keystore: KeystorePtr,
63}
64
65/// Extra dependencies for GRANDPA
66pub struct GrandpaDeps<B> {
67	/// Voting round info.
68	pub shared_voter_state: SharedVoterState,
69	/// Authority set info.
70	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,
71	/// Receives notifications about justification events from Grandpa.
72	pub justification_stream: GrandpaJustificationStream<Block>,
73	/// Executor to drive the subscription manager in the Grandpa RPC handler.
74	pub subscription_executor: SubscriptionTaskExecutor,
75	/// Finality proof provider.
76	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,
77}
78
79/// Dependencies for BEEFY
80pub struct BeefyDeps<AuthorityId: AuthorityIdBound> {
81	/// Receives notifications about finality proof events from BEEFY.
82	pub beefy_finality_proof_stream: BeefyVersionedFinalityProofStream<Block, AuthorityId>,
83	/// Receives notifications about best block events from BEEFY.
84	pub beefy_best_block_stream: BeefyBestBlockStream<Block>,
85	/// Executor to drive the subscription manager in the BEEFY RPC handler.
86	pub subscription_executor: SubscriptionTaskExecutor,
87}
88
89/// Full client dependencies.
90pub struct FullDeps<C, P, SC, B, AuthorityId: AuthorityIdBound> {
91	/// The client instance to use.
92	pub client: Arc<C>,
93	/// Transaction pool instance.
94	pub pool: Arc<P>,
95	/// The SelectChain Strategy
96	pub select_chain: SC,
97	/// A copy of the chain spec.
98	pub chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
99	/// BABE specific dependencies.
100	pub babe: BabeDeps,
101	/// GRANDPA specific dependencies.
102	pub grandpa: GrandpaDeps<B>,
103	/// BEEFY specific dependencies.
104	pub beefy: BeefyDeps<AuthorityId>,
105	/// Shared statement store reference.
106	pub statement_store: Arc<dyn sp_statement_store::StatementStore>,
107	/// The backend used by the node.
108	pub backend: Arc<B>,
109	/// Mixnet API.
110	pub mixnet_api: Option<sc_mixnet::Api>,
111}
112
113/// Instantiate all Full RPC extensions.
114pub fn create_full<C, P, SC, B, AuthorityId>(
115	FullDeps {
116		client,
117		pool,
118		select_chain,
119		chain_spec,
120		babe,
121		grandpa,
122		beefy,
123		statement_store,
124		backend,
125		mixnet_api,
126	}: FullDeps<C, P, SC, B, AuthorityId>,
127) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
128where
129	C: ProvideRuntimeApi<Block>
130		+ sc_client_api::BlockBackend<Block>
131		+ HeaderBackend<Block>
132		+ AuxStore
133		+ HeaderMetadata<Block, Error = BlockChainError>
134		+ Sync
135		+ Send
136		+ 'static,
137	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
138	C::Api: mmr_rpc::MmrRuntimeApi<Block, <Block as sp_runtime::traits::Block>::Hash, BlockNumber>,
139	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
140	C::Api: BabeApi<Block>,
141	C::Api: BlockBuilder<Block>,
142	P: TransactionPool + 'static,
143	SC: SelectChain<Block> + 'static,
144	B: sc_client_api::Backend<Block> + Send + Sync + 'static,
145	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashingFor<Block>>,
146	AuthorityId: AuthorityIdBound,
147	<AuthorityId as RuntimeAppPublic>::Signature: Send + Sync,
148{
149	use mmr_rpc::{Mmr, MmrApiServer};
150	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
151	use sc_consensus_babe_rpc::{Babe, BabeApiServer};
152	use sc_consensus_beefy_rpc::{Beefy, BeefyApiServer};
153	use sc_consensus_grandpa_rpc::{Grandpa, GrandpaApiServer};
154	use sc_rpc::{
155		dev::{Dev, DevApiServer},
156		mixnet::MixnetApiServer,
157		statement::StatementApiServer,
158	};
159	use sc_sync_state_rpc::{SyncState, SyncStateApiServer};
160	use substrate_frame_rpc_system::{System, SystemApiServer};
161	use substrate_state_trie_migration_rpc::{StateMigration, StateMigrationApiServer};
162
163	let mut io = RpcModule::new(());
164
165	let BabeDeps { keystore, babe_worker_handle } = babe;
166	let GrandpaDeps {
167		shared_voter_state,
168		shared_authority_set,
169		justification_stream,
170		subscription_executor,
171		finality_provider,
172	} = grandpa;
173
174	io.merge(System::new(client.clone(), pool).into_rpc())?;
175	// Making synchronous calls in light client freezes the browser currently,
176	// more context: https://github.com/paritytech/substrate/pull/3480
177	// These RPCs should use an asynchronous caller instead.
178	io.merge(
179		Mmr::new(
180			client.clone(),
181			backend
182				.offchain_storage()
183				.ok_or_else(|| "Backend doesn't provide an offchain storage")?,
184		)
185		.into_rpc(),
186	)?;
187	io.merge(TransactionPayment::new(client.clone()).into_rpc())?;
188	io.merge(
189		Babe::new(client.clone(), babe_worker_handle.clone(), keystore, select_chain).into_rpc(),
190	)?;
191	io.merge(
192		Grandpa::new(
193			subscription_executor,
194			shared_authority_set.clone(),
195			shared_voter_state,
196			justification_stream,
197			finality_provider,
198		)
199		.into_rpc(),
200	)?;
201
202	io.merge(
203		SyncState::new(chain_spec, client.clone(), shared_authority_set, babe_worker_handle)?
204			.into_rpc(),
205	)?;
206
207	io.merge(StateMigration::new(client.clone(), backend).into_rpc())?;
208	io.merge(Dev::new(client).into_rpc())?;
209	let statement_store = sc_rpc::statement::StatementStore::new(statement_store).into_rpc();
210	io.merge(statement_store)?;
211
212	if let Some(mixnet_api) = mixnet_api {
213		let mixnet = sc_rpc::mixnet::Mixnet::new(mixnet_api).into_rpc();
214		io.merge(mixnet)?;
215	}
216
217	io.merge(
218		Beefy::<Block, AuthorityId>::new(
219			beefy.beefy_finality_proof_stream,
220			beefy.beefy_best_block_stream,
221			beefy.subscription_executor,
222		)?
223		.into_rpc(),
224	)?;
225
226	Ok(io)
227}