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
    crate::{
19
        chain_spec,
20
        cli::{Cli, RelayChainCli, Subcommand},
21
        service::{self, frontier_database_dir, NodeConfig},
22
    },
23
    container_chain_template_frontier_runtime::Block,
24
    cumulus_client_service::storage_proof_size::HostFunctions as ReclaimHostFunctions,
25
    cumulus_primitives_core::ParaId,
26
    frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE},
27
    log::{info, warn},
28
    node_common::{command::generate_genesis_block, service::NodeBuilderConfig as _},
29
    parity_scale_codec::Encode,
30
    polkadot_cli::IdentifyVariant,
31
    sc_cli::{
32
        ChainSpec, CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams,
33
        NetworkParams, Result, SharedParams, SubstrateCli,
34
    },
35
    sc_service::{
36
        config::{BasePath, PrometheusConfig},
37
        DatabaseSource,
38
    },
39
    sp_core::hexdisplay::HexDisplay,
40
    sp_runtime::traits::{AccountIdConversion, Block as BlockT},
41
    std::net::SocketAddr,
42
};
43

            
44
136
fn load_spec(id: &str, para_id: ParaId) -> std::result::Result<Box<dyn ChainSpec>, String> {
45
136
    Ok(match id {
46
136
        "dev" => Box::new(chain_spec::development_config(para_id, vec![])),
47
134
        "template-rococo" => Box::new(chain_spec::local_testnet_config(para_id, vec![])),
48
134
        "" | "local" => Box::new(chain_spec::local_testnet_config(para_id, vec![])),
49
134
        path => Box::new(chain_spec::ChainSpec::from_json_file(
50
134
            std::path::PathBuf::from(path),
51
134
        )?),
52
    })
53
136
}
54

            
55
impl SubstrateCli for Cli {
56
402
    fn impl_name() -> String {
57
402
        "Container Chain Frontier Node".into()
58
402
    }
59

            
60
674
    fn impl_version() -> String {
61
674
        env!("SUBSTRATE_CLI_IMPL_VERSION").into()
62
674
    }
63

            
64
136
    fn description() -> String {
65
136
        format!(
66
136
            "Container Chain Frontier Node\n\nThe command-line arguments provided first will be \
67
136
		passed to the parachain node, while the arguments provided after -- will be passed \
68
136
		to the relay chain node.\n\n\
69
136
		{} <parachain-args> -- <relay-chain-args>",
70
136
            Self::executable_name()
71
136
        )
72
136
    }
73

            
74
266
    fn author() -> String {
75
266
        env!("CARGO_PKG_AUTHORS").into()
76
266
    }
77

            
78
136
    fn support_url() -> String {
79
136
        "https://github.com/paritytech/cumulus/issues/new".into()
80
136
    }
81

            
82
130
    fn copyright_start_year() -> i32 {
83
130
        2020
84
130
    }
85

            
86
136
    fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
87
136
        load_spec(id, self.para_id.unwrap_or(2000).into())
88
136
    }
89
}
90

            
91
impl SubstrateCli for RelayChainCli {
92
    fn impl_name() -> String {
93
        "Container Chain Frontier Node".into()
94
    }
95

            
96
    fn impl_version() -> String {
97
        env!("SUBSTRATE_CLI_IMPL_VERSION").into()
98
    }
99

            
100
    fn description() -> String {
101
        format!(
102
            "Container Chain Frontier Node\n\nThe command-line arguments provided first will be \
103
		passed to the parachain node, while the arguments provided after -- will be passed \
104
		to the relay chain node.\n\n\
105
		{} <parachain-args> -- <relay-chain-args>",
106
            Self::executable_name()
107
        )
108
    }
109

            
110
    fn author() -> String {
111
        env!("CARGO_PKG_AUTHORS").into()
112
    }
113

            
114
    fn support_url() -> String {
115
        "https://github.com/paritytech/cumulus/issues/new".into()
116
    }
117

            
118
    fn copyright_start_year() -> i32 {
119
        2020
120
    }
121

            
122
    fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
123
        polkadot_cli::Cli::from_iter([RelayChainCli::executable_name()].iter()).load_spec(id)
124
    }
125
}
126

            
127
macro_rules! construct_async_run {
128
	(|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{
129
		let runner = $cli.create_runner($cmd)?;
130
		runner.async_run(|mut $config| {
131
			let $components = NodeConfig::new_builder(&mut $config, None)?;
132
			let inner = { $( $code )* };
133

            
134
            let task_manager = $components.task_manager;
135
			inner.map(|v| (v, task_manager))
136
		})
137
	}}
138
}
139

            
140
/// Parse command line arguments into service configuration.
141
136
pub fn run() -> Result<()> {
142
136
    let cli = Cli::from_args();
143

            
144
6
    match &cli.subcommand {
145
4
        Some(Subcommand::BuildSpec(cmd)) => {
146
4
            let runner = cli.create_runner(cmd)?;
147
4
            runner.sync_run(|config| {
148
4
                let chain_spec = if let Some(para_id) = cmd.parachain_id {
149
                    if cmd.base.shared_params.dev {
150
                        Box::new(chain_spec::development_config(
151
                            para_id.into(),
152
                            cmd.add_bootnode.clone(),
153
                        ))
154
                    } else {
155
                        Box::new(chain_spec::local_testnet_config(
156
                            para_id.into(),
157
                            cmd.add_bootnode.clone(),
158
                        ))
159
                    }
160
                } else {
161
4
                    config.chain_spec
162
                };
163
4
                cmd.base.run(chain_spec, config.network)
164
4
            })
165
        }
166
        Some(Subcommand::CheckBlock(cmd)) => {
167
            construct_async_run!(|components, cli, cmd, config| {
168
                let (_, import_queue) = service::import_queue(&config, &components);
169
                Ok(cmd.run(components.client, import_queue))
170
            })
171
        }
172
        Some(Subcommand::ExportBlocks(cmd)) => {
173
            construct_async_run!(|components, cli, cmd, config| {
174
                Ok(cmd.run(components.client, config.database))
175
            })
176
        }
177
        Some(Subcommand::ExportState(cmd)) => {
178
            construct_async_run!(|components, cli, cmd, config| {
179
                Ok(cmd.run(components.client, config.chain_spec))
180
            })
181
        }
182
        Some(Subcommand::ImportBlocks(cmd)) => {
183
            construct_async_run!(|components, cli, cmd, config| {
184
                let (_, import_queue) = service::import_queue(&config, &components);
185
                Ok(cmd.run(components.client, import_queue))
186
            })
187
        }
188
        Some(Subcommand::Revert(cmd)) => {
189
            construct_async_run!(|components, cli, cmd, config| {
190
                Ok(cmd.run(components.client, components.backend, None))
191
            })
192
        }
193
        Some(Subcommand::PurgeChain(cmd)) => {
194
            let runner = cli.create_runner(cmd)?;
195

            
196
            runner.sync_run(|config| {
197
                // Remove Frontier offchain db
198
                let frontier_database_config = match config.database {
199
                    DatabaseSource::RocksDb { .. } => DatabaseSource::RocksDb {
200
                        path: frontier_database_dir(&config, "db"),
201
                        cache_size: 0,
202
                    },
203
                    DatabaseSource::ParityDb { .. } => DatabaseSource::ParityDb {
204
                        path: frontier_database_dir(&config, "paritydb"),
205
                    },
206
                    _ => {
207
                        return Err(format!("Cannot purge `{:?}` database", config.database).into())
208
                    }
209
                };
210

            
211
                cmd.base.run(frontier_database_config)?;
212

            
213
                let polkadot_cli = RelayChainCli::new(
214
                    &config,
215
                    [RelayChainCli::executable_name()]
216
                        .iter()
217
                        .chain(cli.relay_chain_args.iter()),
218
                );
219

            
220
                let polkadot_config = SubstrateCli::create_configuration(
221
                    &polkadot_cli,
222
                    &polkadot_cli,
223
                    config.tokio_handle.clone(),
224
                )
225
                .map_err(|err| format!("Relay chain argument error: {}", err))?;
226

            
227
                cmd.run(config, polkadot_config)
228
            })
229
        }
230
        Some(Subcommand::ExportGenesisHead(cmd)) => {
231
            let runner = cli.create_runner(cmd)?;
232
            runner.sync_run(|config| {
233
                let partials = NodeConfig::new_builder(&config, None)?;
234
                cmd.run(partials.client)
235
            })
236
        }
237
        Some(Subcommand::ExportGenesisWasm(cmd)) => {
238
            let runner = cli.create_runner(cmd)?;
239
            runner.sync_run(|_config| {
240
                let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?;
241
                cmd.run(&*spec)
242
            })
243
        }
244
        Some(Subcommand::Benchmark(cmd)) => {
245
            let runner = cli.create_runner(cmd)?;
246
            // Switch on the concrete benchmark sub-command-
247
            match cmd {
248
                BenchmarkCmd::Pallet(cmd) => {
249
                    if cfg!(feature = "runtime-benchmarks") {
250
                        runner.sync_run(|config| {
251
                            cmd.run_with_spec::<sp_runtime::traits::HashingFor<Block>, ReclaimHostFunctions>(Some(
252
                                config.chain_spec,
253
                            ))
254
                        })
255
                    } else {
256
                        Err("Benchmarking wasn't enabled when building the node. \
257
			  You can enable it with `--features runtime-benchmarks`."
258
                            .into())
259
                    }
260
                }
261
                BenchmarkCmd::Block(cmd) => runner.sync_run(|config| {
262
                    let partials = NodeConfig::new_builder(&config, None)?;
263
                    cmd.run(partials.client)
264
                }),
265
                #[cfg(not(feature = "runtime-benchmarks"))]
266
                BenchmarkCmd::Storage(_) => Err(sc_cli::Error::Input(
267
                    "Compile with --features=runtime-benchmarks \
268
						to enable storage benchmarks."
269
                        .into(),
270
                )),
271
                #[cfg(feature = "runtime-benchmarks")]
272
                BenchmarkCmd::Storage(cmd) => runner.sync_run(|config| {
273
                    let partials = NodeConfig::new_builder(&config, None)?;
274
                    let db = partials.backend.expose_db();
275
                    let storage = partials.backend.expose_storage();
276
                    cmd.run(config, partials.client.clone(), db, storage)
277
                }),
278
                BenchmarkCmd::Machine(cmd) => {
279
                    runner.sync_run(|config| cmd.run(&config, SUBSTRATE_REFERENCE_HARDWARE.clone()))
280
                }
281
                // NOTE: this allows the Client to leniently implement
282
                // new benchmark commands without requiring a companion MR.
283
                #[allow(unreachable_patterns)]
284
                _ => Err("Benchmarking sub-command unsupported".into()),
285
            }
286
        }
287
2
        Some(Subcommand::PrecompileWasm(cmd)) => {
288
2
            let runner = cli.create_runner(cmd)?;
289
2
            runner.async_run(|config| {
290
2
                let partials = NodeConfig::new_builder(&config, None)?;
291
2
                Ok((
292
2
                    cmd.run(partials.backend, config.chain_spec),
293
2
                    partials.task_manager,
294
2
                ))
295
2
            })
296
        }
297
        None => {
298
130
            let runner = cli.create_runner(&cli.run.normalize())?;
299
130
            let collator_options = cli.run.collator_options();
300
130

            
301
130
            runner.run_node_until_exit(|config| async move {
302
130
				let hwbench = (!cli.no_hardware_benchmarks).then_some(
303
130
					config.database.path().map(|database_path| {
304
130
						let _ = std::fs::create_dir_all(database_path);
305
130
						sc_sysinfo::gather_hwbench(Some(database_path))
306
130
					})).flatten();
307

            
308
130
				let para_id = chain_spec::Extensions::try_get(&*config.chain_spec)
309
130
					.map(|e| e.para_id)
310
130
					.ok_or("Could not find parachain ID in chain-spec.")?;
311

            
312
130
				let polkadot_cli = RelayChainCli::new(
313
130
					&config,
314
130
					[RelayChainCli::executable_name()].iter().chain(cli.relay_chain_args.iter()),
315
130
				);
316
130

            
317
130
                let rpc_config = crate::cli::RpcConfig {
318
130
					eth_log_block_cache: cli.run.eth_log_block_cache,
319
130
					eth_statuses_cache: cli.run.eth_statuses_cache,
320
130
					fee_history_limit: cli.run.fee_history_limit,
321
130
					max_past_logs: cli.run.max_past_logs,
322
130
				};
323
130

            
324
130
                let extension = chain_spec::Extensions::try_get(&*config.chain_spec);
325
130

            
326
130
				let relay_chain_id = extension.map(|e| e.relay_chain.clone());
327

            
328
130
                let dev_service =
329
130
					config.chain_spec.is_dev() || relay_chain_id == Some("dev-service".to_string());
330

            
331
130
                let id = ParaId::from(para_id);
332
130

            
333
130
				if dev_service {
334
130
					return crate::service::start_dev_node(config, cli.run.sealing, rpc_config, id, hwbench).await
335
130
                    .map_err(Into::into)
336
				}
337

            
338

            
339
				let parachain_account =
340
					AccountIdConversion::<polkadot_primitives::AccountId>::into_account_truncating(&id);
341

            
342
                // We log both genesis states for reference, as fetching it from runtime would take significant time
343
				let block_state_v0: Block = generate_genesis_block(&*config.chain_spec, sp_runtime::StateVersion::V0)
344
					.map_err(|e| format!("{:?}", e))?;
345
                let block_state_v1: Block = generate_genesis_block(&*config.chain_spec, sp_runtime::StateVersion::V1)
346
					.map_err(|e| format!("{:?}", e))?;
347

            
348
				let genesis_state_v0 = format!("0x{:?}", HexDisplay::from(&block_state_v0.header().encode()));
349
				let genesis_state_v1 = format!("0x{:?}", HexDisplay::from(&block_state_v1.header().encode()));
350

            
351
				let tokio_handle = config.tokio_handle.clone();
352
				let polkadot_config =
353
					SubstrateCli::create_configuration(&polkadot_cli, &polkadot_cli, tokio_handle)
354
						.map_err(|err| format!("Relay chain argument error: {}", err))?;
355

            
356
				info!("Parachain id: {:?}", id);
357
				info!("Parachain Account: {}", parachain_account);
358
				info!("Parachain genesis state V0: {}", genesis_state_v0);
359
                info!("Parachain genesis state V1: {}", genesis_state_v1);
360

            
361
				info!("Is collating: {}", if config.role.is_authority() { "yes" } else { "no" });
362

            
363
                if let cumulus_client_cli::RelayChainMode::ExternalRpc(rpc_target_urls) =
364
                collator_options.clone().relay_chain_mode {
365
                    if !rpc_target_urls.is_empty() && !cli.relay_chain_args.is_empty() {
366
                        warn!("Detected relay chain node arguments together with --relay-chain-rpc-url. This command starts a minimal Polkadot node that only uses a network-related subset of all relay chain CLI options.");
367
                    }
368
                }
369

            
370
                crate::service::start_parachain_node(
371
                    config,
372
                    polkadot_config,
373
                    collator_options,
374
                    id,
375
                    rpc_config,
376
                    hwbench,
377
                )
378
                    .await
379
                    .map(|r| r.0)
380
                    .map_err(Into::into)
381
260
			})
382
        }
383
    }
384
136
}
385

            
386
impl DefaultConfigurationValues for RelayChainCli {
387
    fn p2p_listen_port() -> u16 {
388
        30334
389
    }
390

            
391
    fn rpc_listen_port() -> u16 {
392
        9945
393
    }
394

            
395
    fn prometheus_listen_port() -> u16 {
396
        9616
397
    }
398
}
399

            
400
impl CliConfiguration<Self> for RelayChainCli {
401
    fn shared_params(&self) -> &SharedParams {
402
        self.base.base.shared_params()
403
    }
404

            
405
    fn import_params(&self) -> Option<&ImportParams> {
406
        self.base.base.import_params()
407
    }
408

            
409
    fn network_params(&self) -> Option<&NetworkParams> {
410
        self.base.base.network_params()
411
    }
412

            
413
    fn keystore_params(&self) -> Option<&KeystoreParams> {
414
        self.base.base.keystore_params()
415
    }
416

            
417
    fn base_path(&self) -> Result<Option<BasePath>> {
418
        Ok(self
419
            .shared_params()
420
            .base_path()?
421
            .or_else(|| Some(self.base_path.clone().into())))
422
    }
423

            
424
    fn rpc_addr(&self, default_listen_port: u16) -> Result<Option<SocketAddr>> {
425
        self.base.base.rpc_addr(default_listen_port)
426
    }
427

            
428
    fn prometheus_config(
429
        &self,
430
        default_listen_port: u16,
431
        chain_spec: &Box<dyn ChainSpec>,
432
    ) -> Result<Option<PrometheusConfig>> {
433
        self.base
434
            .base
435
            .prometheus_config(default_listen_port, chain_spec)
436
    }
437

            
438
    fn init<F>(
439
        &self,
440
        _support_url: &String,
441
        _impl_version: &String,
442
        _logger_hook: F,
443
        _config: &sc_service::Configuration,
444
    ) -> Result<()>
445
    where
446
        F: FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Configuration),
447
    {
448
        unreachable!("PolkadotCli is never initialized; qed");
449
    }
450

            
451
    fn chain_id(&self, is_dev: bool) -> Result<String> {
452
        let chain_id = self.base.base.chain_id(is_dev)?;
453

            
454
        Ok(if chain_id.is_empty() {
455
            self.chain_id.clone().unwrap_or_default()
456
        } else {
457
            chain_id
458
        })
459
    }
460

            
461
    fn role(&self, is_dev: bool) -> Result<sc_service::Role> {
462
        self.base.base.role(is_dev)
463
    }
464

            
465
    fn transaction_pool(&self, is_dev: bool) -> Result<sc_service::config::TransactionPoolOptions> {
466
        self.base.base.transaction_pool(is_dev)
467
    }
468

            
469
    fn trie_cache_maximum_size(&self) -> Result<Option<usize>> {
470
        self.base.base.trie_cache_maximum_size()
471
    }
472

            
473
    fn rpc_methods(&self) -> Result<sc_service::config::RpcMethods> {
474
        self.base.base.rpc_methods()
475
    }
476

            
477
    fn rpc_max_connections(&self) -> Result<u32> {
478
        self.base.base.rpc_max_connections()
479
    }
480

            
481
    fn rpc_cors(&self, is_dev: bool) -> Result<Option<Vec<String>>> {
482
        self.base.base.rpc_cors(is_dev)
483
    }
484

            
485
    fn default_heap_pages(&self) -> Result<Option<u64>> {
486
        self.base.base.default_heap_pages()
487
    }
488

            
489
    fn force_authoring(&self) -> Result<bool> {
490
        self.base.base.force_authoring()
491
    }
492

            
493
    fn disable_grandpa(&self) -> Result<bool> {
494
        self.base.base.disable_grandpa()
495
    }
496

            
497
    fn max_runtime_instances(&self) -> Result<Option<usize>> {
498
        self.base.base.max_runtime_instances()
499
    }
500

            
501
    fn announce_block(&self) -> Result<bool> {
502
        self.base.base.announce_block()
503
    }
504

            
505
    fn telemetry_endpoints(
506
        &self,
507
        chain_spec: &Box<dyn ChainSpec>,
508
    ) -> Result<Option<sc_telemetry::TelemetryEndpoints>> {
509
        self.base.base.telemetry_endpoints(chain_spec)
510
    }
511

            
512
    fn node_name(&self) -> Result<String> {
513
        self.base.base.node_name()
514
    }
515
}