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::CandidateValidationApi;
10use jsonrpsee::RpcModule;
11use pallet_block_producer_fees_rpc::*;
12use pallet_block_producer_metadata_rpc::*;
13use pallet_session_validator_management_rpc::*;
14use pallet_sidechain_rpc::*;
15use partner_chains_demo_runtime::{
16	AccountId, Balance, Nonce,
17	opaque::{Block, SessionKeys},
18};
19use partner_chains_demo_runtime::{BlockNumber, BlockProducerMetadataType, CrossChainPublic, Hash};
20use sc_consensus_grandpa::{
21	FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,
22};
23use sc_consensus_grandpa_rpc::{Grandpa, GrandpaApiServer};
24use sc_rpc::SubscriptionTaskExecutor;
25use sc_transaction_pool_api::TransactionPool;
26use sidechain_domain::ScEpochNumber;
27use sidechain_domain::mainchain_epoch::MainchainEpochConfig;
28use sp_api::{CallApiAt, ProvideRuntimeApi};
29use sp_block_builder::BlockBuilder;
30use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
31use sp_session_validator_management_query::SessionValidatorManagementQuery;
32use sp_sidechain::GetEpochDurationApi;
33use std::sync::Arc;
34use time_source::TimeSource;
35
36/// Extra dependencies for GRANDPA
37pub struct GrandpaDeps<B> {
38	/// Voting round info.
39	pub shared_voter_state: SharedVoterState,
40	/// Authority set info.
41	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,
42	/// Receives notifications about justification events from Grandpa.
43	pub justification_stream: GrandpaJustificationStream<Block>,
44	/// Executor to drive the subscription manager in the Grandpa RPC handler.
45	pub subscription_executor: SubscriptionTaskExecutor,
46	/// Finality proof provider.
47	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,
48}
49
50/// Full client dependencies.
51pub struct FullDeps<C, P, B, T> {
52	/// The client instance to use.
53	pub client: Arc<C>,
54	/// Transaction pool instance.
55	pub pool: Arc<P>,
56	/// GRANDPA specific dependencies.
57	pub grandpa: GrandpaDeps<B>,
58	/// Data sources.
59	pub data_sources: DataSources,
60	/// Source of system time
61	pub time_source: Arc<T>,
62}
63
64/// Instantiate all full RPC extensions.
65pub fn create_full<C, P, B, T>(
66	deps: FullDeps<C, P, B, T>,
67) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
68where
69	C: ProvideRuntimeApi<Block>,
70	C: CallApiAt<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			CrossChainPublic,
85			SessionKeys,
86			ScEpochNumber,
87		>,
88	C::Api: CandidateValidationApi<Block>,
89	C::Api: GetEpochDurationApi<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}