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
pub use sc_rpc::DenyUnsafe;
25

            
26
use {
27
    container_chain_template_simple_runtime::{opaque::Block, AccountId, Hash, Index as Nonce},
28
    cumulus_primitives_core::ParaId,
29
    manual_xcm_rpc::{ManualXcm, ManualXcmApiServer as _},
30
    sc_client_api::AuxStore,
31
    sc_consensus_manual_seal::{
32
        rpc::{ManualSeal, ManualSealApiServer as _},
33
        EngineCommand,
34
    },
35
    sc_transaction_pool_api::TransactionPool,
36
    sp_api::ProvideRuntimeApi,
37
    sp_block_builder::BlockBuilder,
38
    sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata},
39
    std::sync::Arc,
40
};
41

            
42
/// A type representing all RPC extensions.
43
pub type RpcExtension = jsonrpsee::RpcModule<()>;
44

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

            
59
/// Instantiate all RPC extensions.
60
128
pub fn create_full<C, P>(
61
128
    deps: FullDeps<C, P>,
62
128
) -> Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>>
63
128
where
64
128
    C: ProvideRuntimeApi<Block>
65
128
        + HeaderBackend<Block>
66
128
        + AuxStore
67
128
        + HeaderMetadata<Block, Error = BlockChainError>
68
128
        + Send
69
128
        + Sync
70
128
        + 'static,
71
128
    C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
72
128
    C::Api: BlockBuilder<Block>,
73
128
    P: TransactionPool + Sync + Send + 'static,
74
128
{
75
    use substrate_frame_rpc_system::{System, SystemApiServer};
76

            
77
128
    let mut module = RpcExtension::new(());
78
128
    let FullDeps {
79
128
        client,
80
128
        pool,
81
128
        deny_unsafe,
82
128
        command_sink,
83
128
        xcm_senders,
84
128
    } = deps;
85
128

            
86
128
    module.merge(System::new(client, pool, deny_unsafe).into_rpc())?;
87

            
88
    // Manual seal
89
128
    if let Some(command_sink) = command_sink {
90
128
        module.merge(
91
128
            // We provide the rpc handler with the sending end of the channel to allow the rpc
92
128
            // send EngineCommands to the background block authorship task.
93
128
            ManualSeal::new(command_sink).into_rpc(),
94
128
        )?;
95
    };
96

            
97
128
    if let Some((downward_message_channel, hrmp_message_channel)) = xcm_senders {
98
128
        module.merge(
99
128
            ManualXcm {
100
128
                downward_message_channel,
101
128
                hrmp_message_channel,
102
128
            }
103
128
            .into_rpc(),
104
128
        )?;
105
    }
106

            
107
128
    Ok(module)
108
128
}