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
use {
18
    sc_cli::{CliConfiguration, NodeKeyParams, SharedParams},
19
    sp_runtime::traits::Get,
20
    std::path::PathBuf,
21
};
22

            
23
/// The `build-spec` command used to build a specification.
24
#[derive(Debug, Clone, clap::Parser)]
25
pub struct BuildSpecCmd<ExtraFields = EmptyExtra>
26
where
27
    ExtraFields: clap::Args,
28
{
29
    #[clap(flatten)]
30
    pub base: sc_cli::BuildSpecCmd,
31

            
32
    #[clap(flatten)]
33
    pub extra: ExtraFields,
34
}
35

            
36
#[derive(Debug, Clone, clap::Args, Default)]
37
pub struct EmptyExtra {}
38

            
39
impl<T> CliConfiguration for BuildSpecCmd<T>
40
where
41
    T: clap::Args,
42
{
43
132
    fn shared_params(&self) -> &SharedParams {
44
132
        &self.base.shared_params
45
132
    }
46

            
47
12
    fn node_key_params(&self) -> Option<&NodeKeyParams> {
48
12
        Some(&self.base.node_key_params)
49
12
    }
50
}
51

            
52
#[derive(Debug)]
53
pub struct ContainerNodeRelayChainCli<N: Get<&'static str>> {
54
    /// The actual relay chain cli object.
55
    pub base: polkadot_cli::RunCmd,
56

            
57
    /// Optional chain id that should be passed to the relay chain.
58
    pub chain_id: Option<String>,
59

            
60
    /// The base path that should be used by the relay chain.
61
    pub base_path: PathBuf,
62

            
63
    /// Phantom type for storing node name
64
    _marker: std::marker::PhantomData<N>,
65
}
66

            
67
impl<N: Get<&'static str>> ContainerNodeRelayChainCli<N> {
68
    /// Parse the relay chain CLI parameters using the para chain `Configuration`.
69
238
    pub fn new<'a>(
70
238
        para_config: &sc_service::Configuration,
71
238
        relay_chain_args: impl Iterator<Item = &'a String>,
72
238
    ) -> Self {
73
238
        let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
74
238
        let chain_id = extension.map(|e| e.relay_chain.clone());
75
238
        let base_path = para_config.base_path.path().join("polkadot");
76
238
        Self {
77
238
            base_path,
78
238
            chain_id,
79
238
            base: clap::Parser::parse_from(relay_chain_args),
80
238
            _marker: std::marker::PhantomData,
81
238
        }
82
238
    }
83
}
84

            
85
#[derive(Debug)]
86
pub struct RelayChainCli {
87
    /// The actual relay chain cli object.
88
    pub base: polkadot_cli::RunCmd,
89

            
90
    /// Optional chain id that should be passed to the relay chain.
91
    pub chain_id: Option<String>,
92

            
93
    /// The base path that should be used by the relay chain.
94
    pub base_path: PathBuf,
95

            
96
    /// Is this a tanssi solochain? Used to select default chain spec.
97
    pub solochain: bool,
98
}
99

            
100
impl RelayChainCli {
101
    /// Parse the relay chain CLI parameters using the para chain `Configuration`.
102
198
    pub fn new<'a>(
103
198
        para_config: &sc_service::Configuration,
104
198
        relay_chain_args: impl Iterator<Item = &'a String>,
105
198
    ) -> Self {
106
198
        let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
107
198
        let chain_id = extension.map(|e| e.relay_chain.clone());
108
198
        let base_path = para_config.base_path.path().join("polkadot");
109

            
110
198
        Self {
111
198
            base_path,
112
198
            chain_id,
113
198
            base: clap::Parser::parse_from(relay_chain_args),
114
198
            solochain: false,
115
198
        }
116
198
    }
117
}
118

            
119
/// Sub-commands supported by the collator.
120
#[derive(Debug, clap::Subcommand)]
121
#[allow(clippy::large_enum_variant)]
122
pub enum Subcommand<B>
123
where
124
    B: clap::Args,
125
{
126
    /// Build a chain specification.
127
    BuildSpec(B),
128

            
129
    /// Validate blocks.
130
    CheckBlock(sc_cli::CheckBlockCmd),
131

            
132
    /// Export blocks.
133
    ExportBlocks(sc_cli::ExportBlocksCmd),
134

            
135
    /// Export the state of a given block into a chain spec.
136
    ExportState(sc_cli::ExportStateCmd),
137

            
138
    /// Import blocks.
139
    ImportBlocks(sc_cli::ImportBlocksCmd),
140

            
141
    /// Revert the chain to a previous state.
142
    Revert(sc_cli::RevertCmd),
143

            
144
    /// Remove the whole chain.
145
    PurgeChain(cumulus_client_cli::PurgeChainCmd),
146

            
147
    /// Export the genesis state of the parachain.
148
    #[command(alias = "export-genesis-state")]
149
    ExportGenesisHead(cumulus_client_cli::ExportGenesisHeadCommand),
150

            
151
    /// Export the genesis wasm of the parachain.
152
    ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand),
153

            
154
    /// Sub-commands concerned with benchmarking.
155
    /// The pallet benchmarking moved to the `pallet` sub-command.
156
    #[command(subcommand)]
157
    Benchmark(frame_benchmarking_cli::BenchmarkCmd),
158

            
159
    /// Precompile the WASM runtime into native code
160
    PrecompileWasm(sc_cli::PrecompileWasmCmd),
161
}