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: sp_sidechain::GetGenesisUtxo<Block>,
78	C::Api: sp_sidechain::GetSidechainStatus<Block>,
79	C::Api: sp_block_producer_fees::BlockProducerFeesApi<Block, AccountId>,
80	C::Api: sp_block_producer_metadata::BlockProducerMetadataApi<Block, BlockProducerMetadataType>,
81	C::Api: sp_session_validator_management::SessionValidatorManagementApi<
82			Block,
83			CrossChainPublic,
84			SessionKeys,
85			ScEpochNumber,
86		>,
87	C::Api: CandidateValidationApi<Block>,
88	C::Api: GetEpochDurationApi<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}