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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate 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.

// Substrate 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 Substrate.  If not, see <http://www.gnu.org/licenses/>.

use crate::chain_spec;
use crate::cli::{Cli, Subcommand};
use crate::service::{
    self, alcyone_chain_ops, general_chain_ops, mainnet_chain_ops, new_full_base, AlcyoneExecutor,
    GeneralExecutor, IsNetwork, MainnetExecutor, Network, NewChainOps, NewFullBase,
};
use core::future::Future;
use log::info;
use polymesh_primitives::Block;
use sc_cli::{ChainSpec, RuntimeVersion};
pub use sc_cli::{Result, SubstrateCli};
use sc_service::{config::Role, Configuration, TaskManager};

impl SubstrateCli for Cli {
    fn impl_name() -> String {
        "Polymesh Node".into()
    }

    fn impl_version() -> String {
        env!("CARGO_PKG_VERSION").into()
    }

    fn description() -> String {
        env!("CARGO_PKG_DESCRIPTION").into()
    }

    fn author() -> String {
        env!("CARGO_PKG_AUTHORS").into()
    }

    fn support_url() -> String {
        "https://github.com/PolymathNetwork/polymesh/issues/new".into()
    }

    fn copyright_start_year() -> i32 {
        2017
    }

    fn executable_name() -> String {
        "polymesh".into()
    }

    fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
        Ok(match id {
            "dev" => Box::new(chain_spec::general::develop_config()),
            "local" => Box::new(chain_spec::general::local_config()),
            "alcyone-dev" => Box::new(chain_spec::alcyone_testnet::develop_config()),
            "alcyone-local" => Box::new(chain_spec::alcyone_testnet::local_config()),
            "mainnet-dev" => Box::new(chain_spec::polymesh_mainnet::develop_config()),
            "mainnet-local" => Box::new(chain_spec::polymesh_mainnet::local_config()),
            "mainnet-bootstrap" => Box::new(chain_spec::polymesh_mainnet::bootstrap_config()),
            "Mainnet" | "mainnet" | "" => {
                Box::new(chain_spec::polymesh_mainnet::ChainSpec::from_json_bytes(
                    &include_bytes!("./chain_specs/mainnet_raw.json")[..],
                )?)
            }
            "Buffron" | "buffron" => {
                Box::new(chain_spec::alcyone_testnet::ChainSpec::from_json_bytes(
                    &include_bytes!("./chain_specs/buffron_raw.json")[..],
                )?)
            }
            "Alcyone" | "alcyone" => {
                Box::new(chain_spec::alcyone_testnet::ChainSpec::from_json_bytes(
                    &include_bytes!("./chain_specs/alcyone_raw.json")[..],
                )?)
            }
            path => Box::new(chain_spec::general::ChainSpec::from_json_file(
                std::path::PathBuf::from(path),
            )?),
        })
    }

    fn native_runtime_version(chain_spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
        match chain_spec.network() {
            Network::Mainnet => &polymesh_runtime_mainnet::runtime::VERSION,
            Network::Testnet => &polymesh_runtime_testnet::runtime::VERSION,
            Network::Other => &polymesh_runtime_develop::runtime::VERSION,
        }
    }
}

/// Parses Polymesh specific CLI arguments and run the service.
pub fn run() -> Result<()> {
    let mut cli = Cli::from_args();
    if cli.run.operator {
        cli.run.base.validator = true;
    }
    match &cli.subcommand {
        None => {
            let runtime = cli.create_runner(&cli.run.base)?;
            let chain_spec = &runtime.config().chain_spec;

            //let authority_discovery_enabled = cli.run.authority_discovery_enabled;
            info!(
                "Reserved nodes: {:?}",
                cli.run.base.network_params.reserved_nodes
            );

            let network = chain_spec.network();
            runtime.run_node_until_exit(|config| match (network, &config.role) {
                (Network::Mainnet, Role::Light) => service::mainnet_new_light(config),
                (Network::Mainnet, _) => service::mainnet_new_full(config),
                (Network::Testnet, Role::Light) => service::alcyone_new_light(config),
                (Network::Testnet, _) => service::alcyone_new_full(config),
                (Network::Other, Role::Light) => service::general_new_light(config),
                (Network::Other, _) => service::general_new_full(config),
            })
        }
        Some(Subcommand::BuildSpec(cmd)) => {
            let runner = cli.create_runner(cmd)?;
            runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
        }
        Some(Subcommand::BuildSyncSpec(cmd)) => {
            let runner = cli.create_runner(cmd)?;
            match runner.config().chain_spec.network() {
                Network::Mainnet => {
                    runner.async_run(|config| {
                        let chain_spec = config.chain_spec.cloned_box();
                        let network_config = config.network.clone();
                        let NewFullBase {
                            task_manager,
                            client,
                            network_status_sinks,
                            ..
                        } = new_full_base::<
                            polymesh_runtime_mainnet::RuntimeApi,
                            MainnetExecutor,
                            _,
                            _,
                        >(config, |_, _| ())?;
                        Ok((
                            cmd.run(chain_spec, network_config, client, network_status_sinks),
                            task_manager,
                        ))
                    })
                }
                Network::Testnet => {
                    runner.async_run(|config| {
                        let chain_spec = config.chain_spec.cloned_box();
                        let network_config = config.network.clone();
                        let NewFullBase {
                            task_manager,
                            client,
                            network_status_sinks,
                            ..
                        } = new_full_base::<
                            polymesh_runtime_testnet::RuntimeApi,
                            AlcyoneExecutor,
                            _,
                            _,
                        >(config, |_, _| ())?;
                        Ok((
                            cmd.run(chain_spec, network_config, client, network_status_sinks),
                            task_manager,
                        ))
                    })
                }
                Network::Other => {
                    runner.async_run(|config| {
                        let chain_spec = config.chain_spec.cloned_box();
                        let network_config = config.network.clone();
                        let NewFullBase {
                            task_manager,
                            client,
                            network_status_sinks,
                            ..
                        } = new_full_base::<
                            polymesh_runtime_develop::RuntimeApi,
                            GeneralExecutor,
                            _,
                            _,
                        >(config, |_, _| ())?;
                        Ok((
                            cmd.run(chain_spec, network_config, client, network_status_sinks),
                            task_manager,
                        ))
                    })
                }
            }
        }
        Some(Subcommand::CheckBlock(cmd)) => async_run(
            &cli,
            cmd,
            |(c, _, iq, tm), _| Ok((cmd.run(c, iq), tm)),
            |(c, _, iq, tm), _| Ok((cmd.run(c, iq), tm)),
            |(c, _, iq, tm), _| Ok((cmd.run(c, iq), tm)),
        ),
        Some(Subcommand::ExportBlocks(cmd)) => async_run(
            &cli,
            cmd,
            |(c, .., tm), config| Ok((cmd.run(c, config.database), tm)),
            |(c, .., tm), config| Ok((cmd.run(c, config.database), tm)),
            |(c, .., tm), config| Ok((cmd.run(c, config.database), tm)),
        ),
        Some(Subcommand::ExportState(cmd)) => async_run(
            &cli,
            cmd,
            |(c, .., tm), config| Ok((cmd.run(c, config.chain_spec), tm)),
            |(c, .., tm), config| Ok((cmd.run(c, config.chain_spec), tm)),
            |(c, .., tm), config| Ok((cmd.run(c, config.chain_spec), tm)),
        ),
        Some(Subcommand::ImportBlocks(cmd)) => async_run(
            &cli,
            cmd,
            |(c, _, iq, tm), _| Ok((cmd.run(c, iq), tm)),
            |(c, _, iq, tm), _| Ok((cmd.run(c, iq), tm)),
            |(c, _, iq, tm), _| Ok((cmd.run(c, iq), tm)),
        ),
        Some(Subcommand::PurgeChain(cmd)) => {
            let runner = cli.create_runner(cmd)?;
            runner.sync_run(|config| cmd.run(config.database))
        }
        Some(Subcommand::Revert(cmd)) => async_run(
            &cli,
            cmd,
            |(c, b, _, tm), _| Ok((cmd.run(c, b), tm)),
            |(c, b, _, tm), _| Ok((cmd.run(c, b), tm)),
            |(c, b, _, tm), _| Ok((cmd.run(c, b), tm)),
        ),
        Some(Subcommand::Benchmark(cmd)) => {
            if cfg!(feature = "runtime-benchmarks") {
                let runner = cli.create_runner(cmd)?;
                let chain_spec = &runner.config().chain_spec;

                match chain_spec.network() {
                    Network::Mainnet => {
                        runner.sync_run(|config| cmd.run::<Block, service::MainnetExecutor>(config))
                    }
                    Network::Testnet => {
                        runner.sync_run(|config| cmd.run::<Block, service::AlcyoneExecutor>(config))
                    }
                    Network::Other => {
                        runner.sync_run(|config| cmd.run::<Block, service::GeneralExecutor>(config))
                    }
                }
            } else {
                Err("Benchmarking wasn't enabled when building the node. \
				You can enable it with `--features runtime-benchmarks`."
                    .into())
            }
        }
    }
}

fn async_run<F, G, H>(
    cli: &impl sc_cli::SubstrateCli,
    cmd: &impl sc_cli::CliConfiguration,
    mainnet: impl FnOnce(
        NewChainOps<polymesh_runtime_mainnet::RuntimeApi, MainnetExecutor>,
        Configuration,
    ) -> Result<(F, TaskManager)>,
    alcyone: impl FnOnce(
        NewChainOps<polymesh_runtime_testnet::RuntimeApi, AlcyoneExecutor>,
        Configuration,
    ) -> Result<(G, TaskManager)>,
    general: impl FnOnce(
        NewChainOps<polymesh_runtime_develop::RuntimeApi, GeneralExecutor>,
        Configuration,
    ) -> Result<(H, TaskManager)>,
) -> sc_service::Result<(), sc_cli::Error>
where
    F: Future<Output = Result<()>>,
    G: Future<Output = Result<()>>,
    H: Future<Output = Result<()>>,
{
    let runner = cli.create_runner(cmd)?;
    match runner.config().chain_spec.network() {
        Network::Mainnet => {
            runner.async_run(|mut config| mainnet(mainnet_chain_ops(&mut config)?, config))
        }
        Network::Testnet => {
            runner.async_run(|mut config| alcyone(alcyone_chain_ops(&mut config)?, config))
        }
        Network::Other => {
            runner.async_run(|mut config| general(general_chain_ops(&mut config)?, config))
        }
    }
}