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

            
85
/// Sub-commands supported by the collator.
86
#[derive(Debug, clap::Subcommand)]
87
#[allow(clippy::large_enum_variant)]
88
pub enum Subcommand<B>
89
where
90
    B: clap::Args,
91
{
92
    /// Build a chain specification.
93
    BuildSpec(B),
94

            
95
    /// Validate blocks.
96
    CheckBlock(sc_cli::CheckBlockCmd),
97

            
98
    /// Export blocks.
99
    ExportBlocks(sc_cli::ExportBlocksCmd),
100

            
101
    /// Export the state of a given block into a chain spec.
102
    ExportState(sc_cli::ExportStateCmd),
103

            
104
    /// Import blocks.
105
    ImportBlocks(sc_cli::ImportBlocksCmd),
106

            
107
    /// Revert the chain to a previous state.
108
    Revert(sc_cli::RevertCmd),
109

            
110
    /// Remove the whole chain.
111
    PurgeChain(cumulus_client_cli::PurgeChainCmd),
112

            
113
    /// Export the genesis state of the parachain.
114
    #[command(alias = "export-genesis-state")]
115
    ExportGenesisHead(cumulus_client_cli::ExportGenesisHeadCommand),
116

            
117
    /// Export the genesis wasm of the parachain.
118
    ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand),
119

            
120
    /// Sub-commands concerned with benchmarking.
121
    /// The pallet benchmarking moved to the `pallet` sub-command.
122
    #[command(subcommand)]
123
    Benchmark(frame_benchmarking_cli::BenchmarkCmd),
124

            
125
    /// Precompile the WASM runtime into native code
126
    PrecompileWasm(sc_cli::PrecompileWasmCmd),
127
}