node_rpc.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 std::sync::Arc;
  29. use node_runtime::UncheckedExtrinsic;
  30. use node_runtime::{opaque::Block, AccountId, Balance, BlockNumber, Hash, Index};
  31. use sc_consensus_babe::{Config, Epoch};
  32. use sc_consensus_babe_rpc::BabeRpcHandler;
  33. use sc_consensus_epochs::SharedEpochChanges;
  34. use sc_finality_grandpa::{SharedAuthoritySet, SharedVoterState};
  35. use sc_finality_grandpa_rpc::GrandpaRpcHandler;
  36. use sc_keystore::KeyStorePtr;
  37. use sc_rpc_api::DenyUnsafe;
  38. use sp_api::ProvideRuntimeApi;
  39. use sp_block_builder::BlockBuilder;
  40. use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
  41. use sp_consensus::SelectChain;
  42. use sp_consensus_babe::BabeApi;
  43. use sp_transaction_pool::TransactionPool;
  44. /// Light client extra dependencies.
  45. pub struct LightDeps<C, F, P> {
  46. /// The client instance to use.
  47. pub client: Arc<C>,
  48. /// Transaction pool instance.
  49. pub pool: Arc<P>,
  50. /// Remote access to the blockchain (async).
  51. pub remote_blockchain: Arc<dyn sc_client_api::light::RemoteBlockchain<Block>>,
  52. /// Fetcher instance.
  53. pub fetcher: Arc<F>,
  54. }
  55. /// Extra dependencies for BABE.
  56. pub struct BabeDeps {
  57. /// BABE protocol config.
  58. pub babe_config: Config,
  59. /// BABE pending epoch changes.
  60. pub shared_epoch_changes: SharedEpochChanges<Block, Epoch>,
  61. /// The keystore that manages the keys of the node.
  62. pub keystore: KeyStorePtr,
  63. }
  64. /// Extra dependencies for GRANDPA
  65. pub struct GrandpaDeps {
  66. /// Voting round info.
  67. pub shared_voter_state: SharedVoterState,
  68. /// Authority set info.
  69. pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,
  70. }
  71. /// Full client dependencies.
  72. pub struct FullDeps<C, P, SC> {
  73. /// The client instance to use.
  74. pub client: Arc<C>,
  75. /// Transaction pool instance.
  76. pub pool: Arc<P>,
  77. /// The SelectChain Strategy
  78. pub select_chain: SC,
  79. /// Whether to deny unsafe calls
  80. pub deny_unsafe: DenyUnsafe,
  81. /// BABE specific dependencies.
  82. pub babe: BabeDeps,
  83. /// GRANDPA specific dependencies.
  84. pub grandpa: GrandpaDeps,
  85. }
  86. /// Instantiate all Full RPC extensions.
  87. pub fn create_full<C, P, M, SC>(deps: FullDeps<C, P, SC>) -> jsonrpc_core::IoHandler<M>
  88. where
  89. C: ProvideRuntimeApi<Block>,
  90. C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,
  91. C: Send + Sync + 'static,
  92. C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,
  93. C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<
  94. Block,
  95. Balance,
  96. UncheckedExtrinsic,
  97. >,
  98. C::Api: BabeApi<Block>,
  99. C::Api: BlockBuilder<Block>,
  100. P: TransactionPool + 'static,
  101. M: jsonrpc_core::Metadata + Default,
  102. SC: SelectChain<Block> + 'static,
  103. {
  104. use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
  105. use substrate_frame_rpc_system::{FullSystem, SystemApi};
  106. let mut io = jsonrpc_core::IoHandler::default();
  107. let FullDeps {
  108. client,
  109. pool,
  110. select_chain,
  111. deny_unsafe,
  112. babe,
  113. grandpa,
  114. } = deps;
  115. let BabeDeps {
  116. keystore,
  117. babe_config,
  118. shared_epoch_changes,
  119. } = babe;
  120. let GrandpaDeps {
  121. shared_voter_state,
  122. shared_authority_set,
  123. } = grandpa;
  124. io.extend_with(SystemApi::to_delegate(FullSystem::new(
  125. client.clone(),
  126. pool,
  127. deny_unsafe,
  128. )));
  129. // Making synchronous calls in light client freezes the browser currently,
  130. // more context: https://github.com/paritytech/substrate/pull/3480
  131. // These RPCs should use an asynchronous caller instead.
  132. io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(
  133. client.clone(),
  134. )));
  135. io.extend_with(sc_consensus_babe_rpc::BabeApi::to_delegate(
  136. BabeRpcHandler::new(
  137. client,
  138. shared_epoch_changes,
  139. keystore,
  140. babe_config,
  141. select_chain,
  142. deny_unsafe,
  143. ),
  144. ));
  145. io.extend_with(sc_finality_grandpa_rpc::GrandpaApi::to_delegate(
  146. GrandpaRpcHandler::new(shared_authority_set, shared_voter_state),
  147. ));
  148. io
  149. }
  150. /// Instantiate all Light RPC extensions.
  151. pub fn create_light<C, P, M, F>(deps: LightDeps<C, F, P>) -> jsonrpc_core::IoHandler<M>
  152. where
  153. C: sp_blockchain::HeaderBackend<Block>,
  154. C: Send + Sync + 'static,
  155. F: sc_client_api::light::Fetcher<Block> + 'static,
  156. P: TransactionPool + 'static,
  157. M: jsonrpc_core::Metadata + Default,
  158. {
  159. use substrate_frame_rpc_system::{LightSystem, SystemApi};
  160. let LightDeps {
  161. client,
  162. pool,
  163. remote_blockchain,
  164. fetcher,
  165. } = deps;
  166. let mut io = jsonrpc_core::IoHandler::default();
  167. io.extend_with(SystemApi::<Hash, AccountId, Index>::to_delegate(
  168. LightSystem::new(client, remote_blockchain, fetcher, pool),
  169. ));
  170. io
  171. }