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
/// The `export-chain-spec` command used to export a specification.
37
#[derive(Debug, Clone, clap::Parser)]
38
pub struct ExportChainSpecCmd<ExtraFields = EmptyExtra>
39
where
40
    ExtraFields: clap::Args,
41
{
42
    #[clap(flatten)]
43
    pub base: sc_cli::ExportChainSpecCmd,
44

            
45
    #[clap(flatten)]
46
    pub extra: ExtraFields,
47
}
48

            
49
#[derive(Debug, Clone, clap::Args, Default)]
50
pub struct EmptyExtra {}
51

            
52
impl<T> CliConfiguration for BuildSpecCmd<T>
53
where
54
    T: clap::Args,
55
{
56
    fn shared_params(&self) -> &SharedParams {
57
        &self.base.shared_params
58
    }
59

            
60
    fn node_key_params(&self) -> Option<&NodeKeyParams> {
61
        Some(&self.base.node_key_params)
62
    }
63
}
64

            
65
#[derive(Debug)]
66
pub struct ContainerNodeRelayChainCli<N: Get<&'static str>> {
67
    /// The actual relay chain cli object.
68
    pub base: polkadot_cli::RunCmd,
69

            
70
    /// Optional chain id that should be passed to the relay chain.
71
    pub chain_id: Option<String>,
72

            
73
    /// The base path that should be used by the relay chain.
74
    pub base_path: PathBuf,
75

            
76
    /// Phantom type for storing node name
77
    _marker: std::marker::PhantomData<N>,
78
}
79

            
80
impl<N: Get<&'static str>> ContainerNodeRelayChainCli<N> {
81
    /// Parse the relay chain CLI parameters using the para chain `Configuration`.
82
242
    pub fn new<'a>(
83
242
        para_config: &sc_service::Configuration,
84
242
        relay_chain_args: impl Iterator<Item = &'a String>,
85
242
    ) -> Self {
86
242
        let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
87
242
        let chain_id = extension.map(|e| e.relay_chain.clone());
88
242
        let base_path = para_config.base_path.path().join("polkadot");
89
242
        Self {
90
242
            base_path,
91
242
            chain_id,
92
242
            base: clap::Parser::parse_from(relay_chain_args),
93
242
            _marker: std::marker::PhantomData,
94
242
        }
95
242
    }
96
}
97

            
98
#[derive(Debug)]
99
pub struct RelayChainCli {
100
    /// The actual relay chain cli object.
101
    pub base: polkadot_cli::RunCmd,
102

            
103
    /// Optional chain id that should be passed to the relay chain.
104
    pub chain_id: Option<String>,
105

            
106
    /// The base path that should be used by the relay chain.
107
    pub base_path: PathBuf,
108

            
109
    /// Is this a tanssi solochain? Used to select default chain spec.
110
    pub solochain: bool,
111
}
112

            
113
impl RelayChainCli {
114
    /// Parse the relay chain CLI parameters using the para chain `Configuration`.
115
200
    pub fn new<'a>(
116
200
        para_config: &sc_service::Configuration,
117
200
        relay_chain_args: impl Iterator<Item = &'a String>,
118
200
    ) -> Self {
119
200
        let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
120
200
        let chain_id = extension.map(|e| e.relay_chain.clone());
121
200
        let base_path = para_config.base_path.path().join("polkadot");
122

            
123
200
        Self {
124
200
            base_path,
125
200
            chain_id,
126
200
            base: clap::Parser::parse_from(relay_chain_args),
127
200
            solochain: false,
128
200
        }
129
200
    }
130
}
131

            
132
/// Sub-commands supported by the collator.
133
#[derive(Debug, clap::Subcommand)]
134
#[allow(clippy::large_enum_variant)]
135
pub enum Subcommand<B, E>
136
where
137
    B: clap::Args,
138
    E: clap::Args,
139
{
140
    /// Build a chain specification.
141
    #[deprecated(
142
        note = "build-spec command will be removed after 1/04/2026. Use export-chain-spec command instead"
143
    )]
144
    BuildSpec(B),
145

            
146
    /// Export the chain specification.
147
    ExportChainSpec(E),
148

            
149
    /// Validate blocks.
150
    CheckBlock(sc_cli::CheckBlockCmd),
151

            
152
    /// Export blocks.
153
    ExportBlocks(sc_cli::ExportBlocksCmd),
154

            
155
    /// Export the state of a given block into a chain spec.
156
    ExportState(sc_cli::ExportStateCmd),
157

            
158
    /// Import blocks.
159
    ImportBlocks(sc_cli::ImportBlocksCmd),
160

            
161
    /// Revert the chain to a previous state.
162
    Revert(sc_cli::RevertCmd),
163

            
164
    /// Remove the whole chain.
165
    PurgeChain(cumulus_client_cli::PurgeChainCmd),
166

            
167
    /// Export the genesis state of the parachain.
168
    #[command(alias = "export-genesis-state")]
169
    ExportGenesisHead(cumulus_client_cli::ExportGenesisHeadCommand),
170

            
171
    /// Export the genesis wasm of the parachain.
172
    ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand),
173

            
174
    /// Sub-commands concerned with benchmarking.
175
    /// The pallet benchmarking moved to the `pallet` sub-command.
176
    #[command(subcommand)]
177
    Benchmark(frame_benchmarking_cli::BenchmarkCmd),
178

            
179
    /// Precompile the WASM runtime into native code
180
    PrecompileWasm(sc_cli::PrecompileWasmCmd),
181
}