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