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

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

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

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

            
73
132
    let mut module = RpcExtension::new(());
74
132
    let FullDeps {
75
132
        client,
76
132
        pool,
77
132
        command_sink,
78
132
        xcm_senders,
79
132
    } = deps;
80
132

            
81
132
    module.merge(System::new(client, pool).into_rpc())?;
82

            
83
    // Manual seal
84
132
    if let Some(command_sink) = command_sink {
85
132
        module.merge(
86
132
            // We provide the rpc handler with the sending end of the channel to allow the rpc
87
132
            // send EngineCommands to the background block authorship task.
88
132
            ManualSeal::new(command_sink).into_rpc(),
89
132
        )?;
90
    };
91

            
92
132
    if let Some((downward_message_channel, hrmp_message_channel)) = xcm_senders {
93
132
        module.merge(
94
132
            ManualXcm {
95
132
                downward_message_channel,
96
132
                hrmp_message_channel,
97
132
            }
98
132
            .into_rpc(),
99
132
        )?;
100
    }
101

            
102
132
    Ok(module)
103
132
}