1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
//! A collection of node-specific RPC methods.
18
//! Substrate provides the `sc-rpc` crate, which defines the core RPC layer
19
//! used by Substrate nodes. This file extends those RPC definitions with
20
//! capabilities that are specific to this project's runtime configuration.
21

            
22
#![warn(missing_docs)]
23

            
24
use {
25
    cumulus_primitives_core::ParaId,
26
    dancebox_runtime::{opaque::Block, AccountId, Index as Nonce},
27
    manual_xcm_rpc::{ManualXcm, ManualXcmApiServer},
28
    polkadot_primitives::Hash,
29
    sc_client_api::{AuxStore, UsageProvider},
30
    sc_consensus_manual_seal::{
31
        rpc::{ManualSeal, ManualSealApiServer},
32
        EngineCommand,
33
    },
34
    sc_rpc::DenyUnsafe,
35
    sc_transaction_pool_api::TransactionPool,
36
    services_payment_rpc::{
37
        ServicesPayment, ServicesPaymentApiServer as _, ServicesPaymentRuntimeApi,
38
    },
39
    sp_api::ProvideRuntimeApi,
40
    sp_block_builder::BlockBuilder,
41
    sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata},
42
    std::sync::Arc,
43
    stream_payment_rpc::{StreamPayment, StreamPaymentApiServer as _, StreamPaymentRuntimeApi},
44
};
45

            
46
/// A type representing all RPC extensions.
47
pub type RpcExtension = jsonrpsee::RpcModule<()>;
48

            
49
/// Full client dependencies
50
pub struct FullDeps<C, P> {
51
    /// The client instance to use.
52
    pub client: Arc<C>,
53
    /// Transaction pool instance.
54
    pub pool: Arc<P>,
55
    /// Whether to deny unsafe calls
56
    pub deny_unsafe: DenyUnsafe,
57
    /// Manual seal command sink
58
    pub command_sink: Option<futures::channel::mpsc::Sender<EngineCommand<Hash>>>,
59
    /// Channels for manual xcm messages (downward, hrmp)
60
    pub xcm_senders: Option<(flume::Sender<Vec<u8>>, flume::Sender<(ParaId, Vec<u8>)>)>,
61
}
62

            
63
/// Instantiate all RPC extensions.
64
pub fn create_full<C, P>(
65
    deps: FullDeps<C, P>,
66
) -> Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>>
67
where
68
    C: ProvideRuntimeApi<Block>
69
        + HeaderBackend<Block>
70
        + AuxStore
71
        + HeaderMetadata<Block, Error = BlockChainError>
72
        + Send
73
        + Sync
74
        + UsageProvider<Block>
75
        + 'static,
76
    C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
77
    C::Api: BlockBuilder<Block>,
78
    C::Api: StreamPaymentRuntimeApi<Block, u64, u128, u128>,
79
    C::Api: ServicesPaymentRuntimeApi<Block, u128, ParaId>,
80
    P: TransactionPool + Sync + Send + 'static,
81
{
82
    use substrate_frame_rpc_system::{System, SystemApiServer};
83

            
84
    let mut module = RpcExtension::new(());
85
    let FullDeps {
86
        client,
87
        pool,
88
        deny_unsafe,
89
        command_sink,
90
        xcm_senders,
91
    } = deps;
92

            
93
    module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?;
94
    module.merge(StreamPayment::<_, Block>::new(client.clone()).into_rpc())?;
95
    module.merge(ServicesPayment::<_, Block>::new(client).into_rpc())?;
96

            
97
    if let Some(command_sink) = command_sink {
98
        module.merge(
99
            // We provide the rpc handler with the sending end of the channel to allow the rpc
100
            // send EngineCommands to the background block authorship task.
101
            ManualSeal::new(command_sink).into_rpc(),
102
        )?;
103
    };
104

            
105
    if let Some((downward_message_channel, hrmp_message_channel)) = xcm_senders {
106
        module.merge(
107
            ManualXcm {
108
                downward_message_channel,
109
                hrmp_message_channel,
110
            }
111
            .into_rpc(),
112
        )?;
113
    }
114

            
115
    Ok(module)
116
}