partner_chains_demo_node/
rpc.rs

1//! A collection of node-specific RPC methods.
2//! Substrate provides the `sc-rpc` crate, which defines the core RPC layer
3//! used by Substrate nodes. This file extends those RPC definitions with
4//! capabilities that are specific to this project's runtime configuration.
5
6#![warn(missing_docs)]
7
8use crate::data_sources::DataSources;
9use authority_selection_inherents::filter_invalid_candidates::CandidateValidationApi;
10use authority_selection_inherents::{
11	CommitteeMember, authority_selection_inputs::AuthoritySelectionInputs,
12};
13use jsonrpsee::RpcModule;
14use pallet_block_producer_fees_rpc::*;
15use pallet_block_producer_metadata_rpc::*;
16use pallet_session_validator_management_rpc::*;
17use pallet_sidechain_rpc::*;
18use partner_chains_demo_runtime::{
19	AccountId, Balance, Nonce,
20	opaque::{Block, SessionKeys},
21};
22use partner_chains_demo_runtime::{BlockNumber, BlockProducerMetadataType, CrossChainPublic, Hash};
23use sc_consensus_grandpa::{
24	FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,
25};
26use sc_consensus_grandpa_rpc::{Grandpa, GrandpaApiServer};
27use sc_rpc::SubscriptionTaskExecutor;
28use sc_transaction_pool_api::TransactionPool;
29use sidechain_domain::ScEpochNumber;
30use sidechain_domain::mainchain_epoch::MainchainEpochConfig;
31use sp_api::ProvideRuntimeApi;
32use sp_block_builder::BlockBuilder;
33use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
34use sp_session_validator_management_query::SessionValidatorManagementQuery;
35use std::sync::Arc;
36use time_source::TimeSource;
37
38/// Extra dependencies for GRANDPA
39pub struct GrandpaDeps<B> {
40	/// Voting round info.
41	pub shared_voter_state: SharedVoterState,
42	/// Authority set info.
43	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,
44	/// Receives notifications about justification events from Grandpa.
45	pub justification_stream: GrandpaJustificationStream<Block>,
46	/// Executor to drive the subscription manager in the Grandpa RPC handler.
47	pub subscription_executor: SubscriptionTaskExecutor,
48	/// Finality proof provider.
49	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,
50}
51
52/// Full client dependencies.
53pub struct FullDeps<C, P, B, T> {
54	/// The client instance to use.
55	pub client: Arc<C>,
56	/// Transaction pool instance.
57	pub pool: Arc<P>,
58	/// GRANDPA specific dependencies.
59	pub grandpa: GrandpaDeps<B>,
60	/// Data sources.
61	pub data_sources: DataSources,
62	/// Source of system time
63	pub time_source: Arc<T>,
64}
65
66/// Instantiate all full RPC extensions.
67pub fn create_full<C, P, B, T>(
68	deps: FullDeps<C, P, B, T>,
69) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
70where
71	C: ProvideRuntimeApi<Block>,
72	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,
73	C: Send + Sync + 'static,
74	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
75	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
76	C::Api: BlockBuilder<Block>,
77	C::Api: sp_consensus_aura::AuraApi<Block, sp_consensus_aura::sr25519::AuthorityId>,
78	C::Api: sidechain_slots::SlotApi<Block>,
79	C::Api: sp_sidechain::GetGenesisUtxo<Block>,
80	C::Api: sp_sidechain::GetSidechainStatus<Block>,
81	C::Api: sp_block_producer_fees::BlockProducerFeesApi<Block, AccountId>,
82	C::Api: sp_block_producer_metadata::BlockProducerMetadataApi<Block, BlockProducerMetadataType>,
83	C::Api: sp_session_validator_management::SessionValidatorManagementApi<
84			Block,
85			CommitteeMember<CrossChainPublic, SessionKeys>,
86			AuthoritySelectionInputs,
87			ScEpochNumber,
88		>,
89	C::Api: CandidateValidationApi<Block>,
90	P: TransactionPool + 'static,
91	B: sc_client_api::Backend<Block> + Send + Sync + 'static,
92	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashingFor<Block>>,
93	T: TimeSource + Send + Sync + 'static,
94{
95	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
96	use substrate_frame_rpc_system::{System, SystemApiServer};
97
98	let mut module = RpcModule::new(());
99	let FullDeps { client, pool, grandpa, data_sources, time_source } = deps;
100
101	module.merge(System::new(client.clone(), pool.clone()).into_rpc())?;
102	module.merge(TransactionPayment::new(client.clone()).into_rpc())?;
103	module.merge(
104		SidechainRpc::new(
105			client.clone(),
106			MainchainEpochConfig::read_from_env().unwrap(),
107			data_sources.sidechain_rpc.clone(),
108			time_source.clone(),
109		)
110		.into_rpc(),
111	)?;
112	module.merge(BlockProducerFeesRpc::new(client.clone()).into_rpc())?;
113	module.merge(BlockProducerMetadataRpc::new(client.clone()).into_rpc())?;
114
115	let GrandpaDeps {
116		shared_voter_state,
117		shared_authority_set,
118		justification_stream,
119		subscription_executor,
120		finality_provider,
121	} = grandpa;
122	module.merge(
123		Grandpa::new(
124			subscription_executor,
125			shared_authority_set.clone(),
126			shared_voter_state,
127			justification_stream,
128			finality_provider,
129		)
130		.into_rpc(),
131	)?;
132	module.merge(
133		SessionValidatorManagementRpc::new(Arc::new(SessionValidatorManagementQuery::new(
134			client.clone(),
135			data_sources.authority_selection.clone(),
136		)))
137		.into_rpc(),
138	)?;
139
140	// Extend this RPC with a custom API by using the following syntax.
141	// `YourRpcStruct` should have a reference to a client, which is needed
142	// to call into the runtime.
143	// `module.merge(YourRpcTrait::into_rpc(YourRpcStruct::new(ReferenceToClient, ...)))?;`
144
145	Ok(module)
146}