subspace_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 jsonrpsee::RpcModule;
10use mmr_rpc::{Mmr, MmrApiServer};
11use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
12use sc_client_api::{AuxStore, BlockBackend};
13use sc_consensus_subspace::archiver::{
14    ArchivedSegmentNotification, ObjectMappingNotification, SegmentHeadersStore,
15};
16use sc_consensus_subspace::notification::SubspaceNotificationStream;
17use sc_consensus_subspace::slot_worker::{
18    NewSlotNotification, RewardSigningNotification, SubspaceSyncOracle,
19};
20use sc_consensus_subspace_rpc::{SubspaceRpc, SubspaceRpcApiServer, SubspaceRpcConfig};
21use sc_rpc::SubscriptionTaskExecutor;
22use sc_transaction_pool_api::TransactionPool;
23use sp_api::ProvideRuntimeApi;
24use sp_block_builder::BlockBuilder;
25use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
26use sp_consensus::SyncOracle;
27use sp_consensus_subspace::SubspaceApi;
28use sp_objects::ObjectsApi;
29use std::sync::Arc;
30use subspace_core_primitives::{BlockNumber, PublicKey};
31use subspace_erasure_coding::ErasureCoding;
32use subspace_kzg::Kzg;
33use subspace_networking::libp2p::Multiaddr;
34use subspace_runtime_primitives::opaque::Block;
35use subspace_runtime_primitives::{AccountId, Balance, Nonce};
36use substrate_frame_rpc_system::{System, SystemApiServer};
37
38/// Full client dependencies.
39pub struct FullDeps<C, P, SO, AS, B>
40where
41    SO: SyncOracle + Send + Sync + Clone,
42{
43    /// The client instance to use.
44    pub client: Arc<C>,
45    /// Transaction pool instance.
46    pub pool: Arc<P>,
47    /// Executor to drive the subscription manager in the Grandpa RPC handler.
48    pub subscription_executor: SubscriptionTaskExecutor,
49    /// A stream with notifications about new slot arrival with ability to send solution back.
50    pub new_slot_notification_stream: SubspaceNotificationStream<NewSlotNotification>,
51    /// A stream with notifications about headers that need to be signed with ability to send
52    /// signature back.
53    pub reward_signing_notification_stream: SubspaceNotificationStream<RewardSigningNotification>,
54    /// A stream with notifications about mappings.
55    pub object_mapping_notification_stream: SubspaceNotificationStream<ObjectMappingNotification>,
56    /// A stream with notifications about archived segment creation.
57    pub archived_segment_notification_stream:
58        SubspaceNotificationStream<ArchivedSegmentNotification>,
59    /// Bootstrap nodes for DSN.
60    pub dsn_bootstrap_nodes: Vec<Multiaddr>,
61    /// Segment header provider.
62    pub segment_headers_store: SegmentHeadersStore<AS>,
63    /// Subspace sync oracle.
64    pub sync_oracle: SubspaceSyncOracle<SO>,
65    /// Kzg instance.
66    pub kzg: Kzg,
67    /// Erasure coding instance.
68    pub erasure_coding: ErasureCoding,
69    /// Backend used by the node.
70    pub backend: Arc<B>,
71}
72
73/// Instantiate all full RPC extensions.
74pub fn create_full<C, P, SO, AS, B>(
75    deps: FullDeps<C, P, SO, AS, B>,
76) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
77where
78    C: ProvideRuntimeApi<Block>
79        + BlockBackend<Block>
80        + HeaderBackend<Block>
81        + HeaderMetadata<Block, Error = BlockChainError>
82        + Send
83        + Sync
84        + 'static,
85    C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
86        + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
87        + BlockBuilder<Block>
88        + SubspaceApi<Block, PublicKey>
89        + mmr_rpc::MmrRuntimeApi<Block, <Block as sp_runtime::traits::Block>::Hash, BlockNumber>
90        + ObjectsApi<Block>,
91    P: TransactionPool + 'static,
92    SO: SyncOracle + Send + Sync + Clone + 'static,
93    AS: AuxStore + Send + Sync + 'static,
94    B: sc_client_api::Backend<Block> + Send + Sync + 'static,
95    B::State: sc_client_api::StateBackend<sp_runtime::traits::HashingFor<Block>>,
96{
97    let mut module = RpcModule::new(());
98    let FullDeps {
99        client,
100        pool,
101        subscription_executor,
102        new_slot_notification_stream,
103        reward_signing_notification_stream,
104        object_mapping_notification_stream,
105        archived_segment_notification_stream,
106        dsn_bootstrap_nodes,
107        segment_headers_store,
108        sync_oracle,
109        kzg,
110        erasure_coding,
111        backend,
112    } = deps;
113
114    module.merge(System::new(client.clone(), pool).into_rpc())?;
115    module.merge(TransactionPayment::new(client.clone()).into_rpc())?;
116
117    module.merge(
118        SubspaceRpc::new(SubspaceRpcConfig {
119            client: client.clone(),
120            subscription_executor,
121            new_slot_notification_stream,
122            reward_signing_notification_stream,
123            object_mapping_notification_stream,
124            archived_segment_notification_stream,
125            dsn_bootstrap_nodes,
126            segment_headers_store,
127            sync_oracle,
128            kzg,
129            erasure_coding,
130        })?
131        .into_rpc(),
132    )?;
133    module.merge(
134        Mmr::new(
135            client,
136            backend
137                .offchain_storage()
138                .ok_or("Backend doesn't provide the required offchain storage")?,
139        )
140        .into_rpc(),
141    )?;
142
143    Ok(module)
144}