node_rpc.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // This file is part of Substrate.
  2. // Copyright (C) 2019-2020 Parity Technologies (UK) Ltd.
  3. // SPDX-License-Identifier: Apache-2.0
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //! A collection of node-specific RPC methods.
  16. //!
  17. //! Since `substrate` core functionality makes no assumptions
  18. //! about the modules used inside the runtime, so do
  19. //! RPC methods defined in `sc-rpc` crate.
  20. //! It means that `client/rpc` can't have any methods that
  21. //! need some strong assumptions about the particular runtime.
  22. //!
  23. //! The RPCs available in this crate however can make some assumptions
  24. //! about how the runtime is constructed and what FRAME pallets
  25. //! are part of it. Therefore all node-runtime-specific RPCs can
  26. //! be placed here or imported from corresponding FRAME RPC definitions.
  27. #![warn(missing_docs)]
  28. use node_runtime::{opaque::Block, AccountId, Balance, BlockNumber, Hash, Index};
  29. use sc_consensus_babe::{Config, Epoch};
  30. use sc_consensus_babe_rpc::BabeRpcHandler;
  31. use sc_consensus_epochs::SharedEpochChanges;
  32. use sc_finality_grandpa::{
  33. FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,
  34. };
  35. use sc_finality_grandpa_rpc::GrandpaRpcHandler;
  36. use sc_keystore::KeyStorePtr;
  37. use sc_rpc::SubscriptionTaskExecutor;
  38. pub use sc_rpc_api::DenyUnsafe;
  39. use sp_api::ProvideRuntimeApi;
  40. use sp_block_builder::BlockBuilder;
  41. use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
  42. use sp_consensus::SelectChain;
  43. use sp_consensus_babe::BabeApi;
  44. use sp_transaction_pool::TransactionPool;
  45. use std::sync::Arc;
  46. /// Light client extra dependencies.
  47. pub struct LightDeps<C, F, P> {
  48. /// The client instance to use.
  49. pub client: Arc<C>,
  50. /// Transaction pool instance.
  51. pub pool: Arc<P>,
  52. /// Remote access to the blockchain (async).
  53. pub remote_blockchain: Arc<dyn sc_client_api::light::RemoteBlockchain<Block>>,
  54. /// Fetcher instance.
  55. pub fetcher: Arc<F>,
  56. }
  57. /// Extra dependencies for BABE.
  58. pub struct BabeDeps {
  59. /// BABE protocol config.
  60. pub babe_config: Config,
  61. /// BABE pending epoch changes.
  62. pub shared_epoch_changes: SharedEpochChanges<Block, Epoch>,
  63. /// The keystore that manages the keys of the node.
  64. pub keystore: KeyStorePtr,
  65. }
  66. /// Extra dependencies for GRANDPA
  67. pub struct GrandpaDeps<B> {
  68. /// Voting round info.
  69. pub shared_voter_state: SharedVoterState,
  70. /// Authority set info.
  71. pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,
  72. /// Receives notifications about justification events from Grandpa.
  73. pub justification_stream: GrandpaJustificationStream<Block>,
  74. /// Executor to drive the subscription manager in the Grandpa RPC handler.
  75. pub subscription_executor: SubscriptionTaskExecutor,
  76. /// Finality proof provider.
  77. pub finality_provider: Arc<FinalityProofProvider<B, Block>>,
  78. }
  79. /// Full client dependencies.
  80. pub struct FullDeps<C, P, SC, B> {
  81. /// The client instance to use.
  82. pub client: Arc<C>,
  83. /// Transaction pool instance.
  84. pub pool: Arc<P>,
  85. /// The SelectChain Strategy
  86. pub select_chain: SC,
  87. /// Whether to deny unsafe calls
  88. pub deny_unsafe: DenyUnsafe,
  89. /// BABE specific dependencies.
  90. pub babe: BabeDeps,
  91. /// GRANDPA specific dependencies.
  92. pub grandpa: GrandpaDeps<B>,
  93. }
  94. /// A IO handler that uses all Full RPC extensions.
  95. pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;
  96. /// Instantiate all Full RPC extensions.
  97. pub fn create_full<C, P, SC, B>(
  98. deps: FullDeps<C, P, SC, B>,
  99. ) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>
  100. where
  101. C: ProvideRuntimeApi<Block>,
  102. C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,
  103. C: Send + Sync + 'static,
  104. C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,
  105. C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
  106. C::Api: BabeApi<Block>,
  107. C::Api: BlockBuilder<Block>,
  108. P: TransactionPool + 'static,
  109. SC: SelectChain<Block> + 'static,
  110. B: sc_client_api::Backend<Block> + Send + Sync + 'static,
  111. B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,
  112. {
  113. use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
  114. use substrate_frame_rpc_system::{FullSystem, SystemApi};
  115. let mut io = jsonrpc_core::IoHandler::default();
  116. let FullDeps {
  117. client,
  118. pool,
  119. select_chain,
  120. deny_unsafe,
  121. babe,
  122. grandpa,
  123. } = deps;
  124. let BabeDeps {
  125. keystore,
  126. babe_config,
  127. shared_epoch_changes,
  128. } = babe;
  129. let GrandpaDeps {
  130. shared_voter_state,
  131. shared_authority_set,
  132. justification_stream,
  133. subscription_executor,
  134. finality_provider,
  135. } = grandpa;
  136. io.extend_with(SystemApi::to_delegate(FullSystem::new(
  137. client.clone(),
  138. pool,
  139. deny_unsafe,
  140. )));
  141. io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(
  142. client.clone(),
  143. )));
  144. io.extend_with(sc_consensus_babe_rpc::BabeApi::to_delegate(
  145. BabeRpcHandler::new(
  146. client,
  147. shared_epoch_changes,
  148. keystore,
  149. babe_config,
  150. select_chain,
  151. deny_unsafe,
  152. ),
  153. ));
  154. io.extend_with(sc_finality_grandpa_rpc::GrandpaApi::to_delegate(
  155. GrandpaRpcHandler::new(
  156. shared_authority_set,
  157. shared_voter_state,
  158. justification_stream,
  159. subscription_executor,
  160. finality_provider,
  161. ),
  162. ));
  163. io
  164. }
  165. /// Instantiate all Light RPC extensions.
  166. pub fn create_light<C, P, M, F>(deps: LightDeps<C, F, P>) -> jsonrpc_core::IoHandler<M>
  167. where
  168. C: sp_blockchain::HeaderBackend<Block>,
  169. C: Send + Sync + 'static,
  170. F: sc_client_api::light::Fetcher<Block> + 'static,
  171. P: TransactionPool + 'static,
  172. M: jsonrpc_core::Metadata + Default,
  173. {
  174. use substrate_frame_rpc_system::{LightSystem, SystemApi};
  175. let LightDeps {
  176. client,
  177. pool,
  178. remote_blockchain,
  179. fetcher,
  180. } = deps;
  181. let mut io = jsonrpc_core::IoHandler::default();
  182. io.extend_with(SystemApi::<Hash, AccountId, Index>::to_delegate(
  183. LightSystem::new(client, remote_blockchain, fetcher, pool),
  184. ));
  185. io
  186. }