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
    clap::Parser,
19
    node_common::service::Sealing,
20
    sc_cli::{CliConfiguration, NodeKeyParams, SharedParams},
21
    std::path::PathBuf,
22
};
23

            
24
/// Sub-commands supported by the collator.
25
#[derive(Debug, clap::Subcommand)]
26
#[allow(clippy::large_enum_variant)]
27
pub enum Subcommand {
28
    /// Build a chain specification.
29
    BuildSpec(BuildSpecCmd),
30

            
31
    /// Validate blocks.
32
    CheckBlock(sc_cli::CheckBlockCmd),
33

            
34
    /// Export blocks.
35
    ExportBlocks(sc_cli::ExportBlocksCmd),
36

            
37
    /// Export the state of a given block into a chain spec.
38
    ExportState(sc_cli::ExportStateCmd),
39

            
40
    /// Import blocks.
41
    ImportBlocks(sc_cli::ImportBlocksCmd),
42

            
43
    /// Revert the chain to a previous state.
44
    Revert(sc_cli::RevertCmd),
45

            
46
    /// Remove the whole chain.
47
    PurgeChain(cumulus_client_cli::PurgeChainCmd),
48

            
49
    /// Export the genesis state of the parachain.
50
    #[command(alias = "export-genesis-state")]
51
    ExportGenesisHead(cumulus_client_cli::ExportGenesisHeadCommand),
52

            
53
    /// Export the genesis wasm of the parachain.
54
    ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand),
55

            
56
    /// Sub-commands concerned with benchmarking.
57
    /// The pallet benchmarking moved to the `pallet` sub-command.
58
    #[command(subcommand)]
59
    Benchmark(frame_benchmarking_cli::BenchmarkCmd),
60

            
61
    /// Precompile the WASM runtime into native code
62
    PrecompileWasm(sc_cli::PrecompileWasmCmd),
63
}
64

            
65
#[derive(Debug, Parser)]
66
#[group(skip)]
67
pub struct RunCmd {
68
    #[clap(flatten)]
69
    pub base: cumulus_client_cli::RunCmd,
70

            
71
    /// Size in bytes of the LRU cache for block data.
72
    #[arg(long, default_value = "300000000")]
73
    pub eth_log_block_cache: usize,
74

            
75
    /// Size in bytes of the LRU cache for transactions statuses data.
76
    #[arg(long, default_value = "300000000")]
77
    pub eth_statuses_cache: usize,
78

            
79
    /// Maximum number of logs in a query.
80
    #[arg(long, default_value = "10000")]
81
    pub max_past_logs: u32,
82

            
83
    /// Id of the parachain this collator collates for.
84
    #[arg(long)]
85
    pub parachain_id: Option<u32>,
86

            
87
    /// Maximum fee history cache size.
88
    #[arg(long, default_value = "2048")]
89
    pub fee_history_limit: u64,
90

            
91
    /// When blocks should be sealed in the dev service.
92
    ///
93
    /// Options are "instant", "manual", or timer interval in milliseconds
94
    #[arg(long, default_value = "instant")]
95
    pub sealing: Sealing,
96
}
97

            
98
impl std::ops::Deref for RunCmd {
99
    type Target = cumulus_client_cli::RunCmd;
100

            
101
260
    fn deref(&self) -> &Self::Target {
102
260
        &self.base
103
260
    }
104
}
105

            
106
#[derive(Debug, clap::Parser)]
107
#[command(
108
    propagate_version = true,
109
    args_conflicts_with_subcommands = true,
110
    subcommand_negates_reqs = true
111
)]
112
pub struct Cli {
113
    #[command(subcommand)]
114
    pub subcommand: Option<Subcommand>,
115

            
116
    #[command(flatten)]
117
    pub run: RunCmd,
118

            
119
    /// Disable automatic hardware benchmarks.
120
    ///
121
    /// By default these benchmarks are automatically ran at startup and measure
122
    /// the CPU speed, the memory bandwidth and the disk speed.
123
    ///
124
    /// The results are then printed out in the logs, and also sent as part of
125
    /// telemetry, if telemetry is enabled.
126
    #[arg(long)]
127
    pub no_hardware_benchmarks: bool,
128

            
129
    /// Relay chain arguments
130
    #[arg(raw = true)]
131
    pub relay_chain_args: Vec<String>,
132

            
133
    /// Optional parachain id that should be used to build chain spec.
134
    #[arg(long)]
135
    pub para_id: Option<u32>,
136
}
137

            
138
#[derive(Debug)]
139
pub struct RelayChainCli {
140
    /// The actual relay chain cli object.
141
    pub base: polkadot_cli::RunCmd,
142

            
143
    /// Optional chain id that should be passed to the relay chain.
144
    pub chain_id: Option<String>,
145

            
146
    /// The base path that should be used by the relay chain.
147
    pub base_path: PathBuf,
148
}
149

            
150
impl RelayChainCli {
151
    /// Parse the relay chain CLI parameters using the para chain `Configuration`.
152
130
    pub fn new<'a>(
153
130
        para_config: &sc_service::Configuration,
154
130
        relay_chain_args: impl Iterator<Item = &'a String>,
155
130
    ) -> Self {
156
130
        let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
157
130
        let chain_id = extension.map(|e| e.relay_chain.clone());
158
130
        let base_path = para_config.base_path.path().join("polkadot");
159
130
        Self {
160
130
            base_path,
161
130
            chain_id,
162
130
            base: clap::Parser::parse_from(relay_chain_args),
163
130
        }
164
130
    }
165
}
166

            
167
/// The `build-spec` command used to build a specification.
168
#[derive(Debug, Clone, clap::Parser)]
169
pub struct BuildSpecCmd {
170
    #[clap(flatten)]
171
    pub base: sc_cli::BuildSpecCmd,
172

            
173
    /// Id of the parachain this spec is for. Note that this overrides the `--chain` param.
174
    #[arg(long, conflicts_with = "chain")]
175
    #[arg(long)]
176
    pub parachain_id: Option<u32>,
177

            
178
    /// List of bootnodes to add to chain spec
179
    #[arg(long)]
180
    pub add_bootnode: Vec<String>,
181
}
182

            
183
impl CliConfiguration for BuildSpecCmd {
184
48
    fn shared_params(&self) -> &SharedParams {
185
48
        &self.base.shared_params
186
48
    }
187

            
188
4
    fn node_key_params(&self) -> Option<&NodeKeyParams> {
189
4
        Some(&self.base.node_key_params)
190
4
    }
191
}
192

            
193
pub struct RpcConfig {
194
    pub eth_log_block_cache: usize,
195
    pub eth_statuses_cache: usize,
196
    pub fee_history_limit: u64,
197
    pub max_past_logs: u32,
198
}