1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// This file is part of Darwinia.
//
// Copyright (C) 2018-2022 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Darwinia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

// --- crates.io ---
#[cfg(feature = "template")]
use structopt::clap::arg_enum;
use structopt::StructOpt;
// --- paritytech ---
#[cfg(feature = "runtime-benchmarks")]
use frame_benchmarking_cli::BenchmarkCmd;
use sc_cli::*;
#[cfg(feature = "try-runtime")]
use try_runtime_cli::TryRuntimeCmd;
// --- darwinia-network ---
use drml_rpc::EthRpcConfig;

/// An overarching CLI command definition.
#[derive(Debug, StructOpt)]
pub struct Cli {
	/// Possible subcommand with parameters.
	#[structopt(subcommand)]
	pub subcommand: Option<Subcommand>,

	#[allow(missing_docs)]
	#[structopt(flatten)]
	pub run: Run,
}

#[allow(missing_docs)]
#[derive(Debug, StructOpt)]
pub struct Run {
	#[allow(missing_docs)]
	#[structopt(flatten)]
	pub base: RunCmd,

	/// Force using Pangolin native runtime.
	#[structopt(long = "force-pangolin")]
	pub force_pangolin: bool,

	/// Disable the authority discovery module on validator or sentry nodes.
	///
	/// Enabled by default on validator and sentry nodes. Always disabled on non
	/// validator or sentry nodes.
	///
	/// When enabled:
	///
	/// (1) As a validator node: Make oneself discoverable by publishing either
	///     ones own network addresses, or the ones of ones sentry nodes
	///     (configured via the `sentry-nodes` flag).
	///
	/// (2) As a validator or sentry node: Discover addresses of validators or
	///     addresses of their sentry nodes and maintain a permanent connection
	///     to a subset.
	#[structopt(long = "disable-authority-discovery")]
	pub authority_discovery_disabled: bool,

	#[allow(missing_docs)]
	#[structopt(flatten)]
	pub dvm_args: DvmArgs,
}

/// Possible subcommands of the main binary.
#[derive(Debug, StructOpt)]
pub enum Subcommand {
	/// Build a chain specification.
	BuildSpec(BuildSpecCmd),

	/// Validate blocks.
	CheckBlock(CheckBlockCmd),

	/// Export blocks.
	ExportBlocks(ExportBlocksCmd),

	/// Export the state of a given block into a chain spec.
	ExportState(ExportStateCmd),

	/// Import blocks.
	ImportBlocks(ImportBlocksCmd),

	/// Remove the whole chain.
	PurgeChain(PurgeChainCmd),

	/// Revert the chain to a previous state.
	Revert(RevertCmd),

	/// Key management cli utilities
	Key(KeySubcommand),

	/// Verify a signature for a message, provided on STDIN, with a given (public or secret) key.
	Verify(VerifyCmd),

	/// Generate a seed that provides a vanity address.
	Vanity(VanityCmd),

	/// Sign a message, with a given (secret) key.
	Sign(SignCmd),

	/// The custom benchmark subcommand benchmarking runtime pallets.
	#[cfg(feature = "runtime-benchmarks")]
	#[structopt(name = "benchmark", about = "Benchmark runtime pallets.")]
	Benchmark(BenchmarkCmd),

	/// Try some experimental command on the runtime. This includes migration and runtime-upgrade
	/// testing.
	#[cfg(feature = "try-runtime")]
	TryRuntime(TryRuntimeCmd),
}

#[derive(Debug, StructOpt)]
pub struct DvmArgs {
	/// Enable EVM tracing module on a non-authority node.
	// Remove the restriction for testnets.
	// #[structopt(long, conflicts_with = "validator", require_delimiter = true)]
	#[structopt(long, require_delimiter = true)]
	pub ethapi_debug_targets: Vec<String>,

	/// Number of concurrent tracing tasks. Meant to be shared by both "debug" and "trace" modules.
	#[structopt(long, default_value = "10")]
	pub ethapi_max_permits: u32,

	/// Maximum number of trace entries a single request of `trace_filter` is allowed to return.
	/// A request asking for more or an unbounded one going over this limit will both return an
	/// error.
	#[structopt(long, default_value = "500")]
	pub ethapi_trace_max_count: u32,

	/// Duration (in seconds) after which the cache of `trace_filter` for a given block will be
	/// discarded.
	#[structopt(long, default_value = "300")]
	pub ethapi_trace_cache_duration: u64,

	/// Size of the LRU cache for block data and their transaction statuses.
	#[structopt(long, default_value = "3000")]
	pub eth_log_block_cache: usize,

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

	/// Maximum fee history cache size.
	#[structopt(long, default_value = "2048")]
	pub fee_history_limit: u64,

	/// Choose sealing method.
	#[cfg(feature = "template")]
	#[structopt(long = "sealing", default_value)]
	pub sealing: Sealing,

	#[cfg(feature = "template")]
	#[structopt(long = "enable-dev-signer")]
	pub enable_dev_signer: bool,
}
impl DvmArgs {
	pub fn build_eth_rpc_config(&self) -> EthRpcConfig {
		EthRpcConfig {
			ethapi_debug_targets: self.ethapi_debug_targets.clone(),
			ethapi_max_permits: self.ethapi_max_permits,
			ethapi_trace_max_count: self.ethapi_trace_max_count,
			ethapi_trace_cache_duration: self.ethapi_trace_cache_duration,
			eth_log_block_cache: self.eth_log_block_cache,
			max_past_logs: self.max_past_logs,
			fee_history_limit: self.fee_history_limit,
		}
	}
}

#[cfg(feature = "template")]
arg_enum! {
	/// Available Sealing methods.
	#[derive(Debug, Copy, Clone, StructOpt)]
	pub enum Sealing {
		// Seal using rpc method.
		Manual,
		// Seal when transaction is executed.
		Instant,
	}
}
#[cfg(feature = "template")]
impl Sealing {
	pub fn is_manual(&self) -> bool {
		match self {
			Self::Manual => true,
			Self::Instant => false,
		}
	}
}
#[cfg(feature = "template")]
impl Default for Sealing {
	fn default() -> Sealing {
		Sealing::Manual
	}
}