domain_service/
rpc.rs

1//! A collection of node-specific RPC methods.
2//!
3//! Substrate provides the `sc-rpc` crate, which defines the core RPC layer
4//! used by Substrate nodes. This file extends those RPC definitions with
5//! capabilities that are specific to this project's runtime configuration.
6
7#![warn(missing_docs)]
8
9use domain_runtime_primitives::{Balance, Nonce};
10use jsonrpsee::RpcModule;
11use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
12use sc_client_api::{AuxStore, BlockBackend};
13use sc_network::service::traits::NetworkService;
14use sc_network_sync::SyncingService;
15use sc_service::{DatabaseSource, SpawnTaskHandle};
16use sc_transaction_pool_api::TransactionPool;
17use serde::de::DeserializeOwned;
18use sp_api::ProvideRuntimeApi;
19use sp_block_builder::BlockBuilder;
20use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
21use sp_core::{Decode, Encode};
22use sp_runtime::traits::Block as BlockT;
23use std::fmt::{Debug, Display};
24use std::sync::Arc;
25use substrate_frame_rpc_system::{System, SystemApiServer};
26use substrate_prometheus_endpoint::Registry;
27
28/// Full RPC dependencies.
29pub struct FullDeps<Block: BlockT, Client, TP, BE, CIDP> {
30    /// The client instance to use.
31    pub client: Arc<Client>,
32    /// The chain backend.
33    pub backend: Arc<BE>,
34    /// Transaction pool instance.
35    pub pool: Arc<TP>,
36    /// Network service
37    pub network: Arc<dyn NetworkService>,
38    /// Chain syncing service
39    pub sync: Arc<SyncingService<Block>>,
40    /// Is node running as authority.
41    pub is_authority: bool,
42    /// Prometheus registry
43    pub prometheus_registry: Option<Registry>,
44    /// Database source
45    pub database_source: DatabaseSource,
46    /// Task Spawner.
47    pub task_spawner: SpawnTaskHandle,
48    /// Create inherent data provider
49    pub create_inherent_data_provider: CIDP,
50}
51
52impl<Block: BlockT, Client, TP, BE, CIDP: Clone> Clone for FullDeps<Block, Client, TP, BE, CIDP> {
53    fn clone(&self) -> Self {
54        Self {
55            client: self.client.clone(),
56            backend: self.backend.clone(),
57            pool: self.pool.clone(),
58            network: self.network.clone(),
59            sync: self.sync.clone(),
60            is_authority: self.is_authority,
61            task_spawner: self.task_spawner.clone(),
62            prometheus_registry: self.prometheus_registry.clone(),
63            database_source: self.database_source.clone(),
64            create_inherent_data_provider: self.create_inherent_data_provider.clone(),
65        }
66    }
67}
68
69/// Instantiate all RPC extensions.
70pub fn create_full<Block, Client, P, AccountId, BE, CIDP>(
71    deps: FullDeps<Block, Client, P, BE, CIDP>,
72) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
73where
74    Block: BlockT,
75    Client: ProvideRuntimeApi<Block>
76        + BlockBackend<Block>
77        + HeaderBackend<Block>
78        + AuxStore
79        + HeaderMetadata<Block, Error = BlockChainError>
80        + Send
81        + Sync
82        + 'static,
83    Client::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
84        + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
85        + BlockBuilder<Block>,
86    P: TransactionPool + Sync + Send + 'static,
87    AccountId: DeserializeOwned + Encode + Debug + Decode + Display + Clone + Sync + Send + 'static,
88{
89    let mut module = RpcModule::new(());
90    let FullDeps { client, pool, .. } = deps;
91
92    module.merge(System::new(client.clone(), pool).into_rpc())?;
93    module.merge(TransactionPayment::new(client).into_rpc())?;
94
95    Ok(module)
96}