command.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2019 Joystream Contributors
  2. // This file is part of Joystream node.
  3. // Joystream node is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // Joystream node is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with Joystream node. If not, see <http://www.gnu.org/licenses/>.
  13. use crate::cli::{Cli, Subcommand};
  14. use crate::node_executor;
  15. use crate::{chain_spec, service};
  16. use crate::service::{new_full_base, new_partial, NewFullBase};
  17. use node_executor::Executor;
  18. use node_runtime::{opaque::Block, RuntimeApi};
  19. use sc_cli::{ChainSpec, Result, Role, RuntimeVersion, SubstrateCli};
  20. use sc_service::PartialComponents;
  21. impl SubstrateCli for Cli {
  22. fn impl_name() -> String {
  23. "Joystream Node".into()
  24. }
  25. fn support_url() -> String {
  26. "https://www.joystream.org/".into()
  27. }
  28. fn copyright_start_year() -> i32 {
  29. 2019
  30. }
  31. fn executable_name() -> String {
  32. "joystream-node".into()
  33. }
  34. fn impl_version() -> String {
  35. env!("SUBSTRATE_CLI_IMPL_VERSION").into()
  36. }
  37. fn description() -> String {
  38. env!("CARGO_PKG_DESCRIPTION").into()
  39. }
  40. fn author() -> String {
  41. env!("CARGO_PKG_AUTHORS").into()
  42. }
  43. fn native_runtime_version(_: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
  44. &node_runtime::VERSION
  45. }
  46. fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
  47. Ok(match id {
  48. "dev" => Box::new(chain_spec::Alternative::Development.load()?),
  49. "local" => Box::new(chain_spec::Alternative::LocalTestnet.load()?),
  50. path => Box::new(chain_spec::ChainSpec::from_json_file(
  51. std::path::PathBuf::from(path),
  52. )?),
  53. })
  54. }
  55. }
  56. /// Parse command line arguments into service configuration.
  57. pub fn run() -> Result<()> {
  58. let cli = Cli::from_args();
  59. match &cli.subcommand {
  60. None => {
  61. let runner = cli.create_runner(&cli.run)?;
  62. runner.run_node_until_exit(|config| match config.role {
  63. Role::Light => service::new_light(config),
  64. _ => service::new_full(config),
  65. })
  66. }
  67. Some(Subcommand::Inspect(cmd)) => {
  68. let runner = cli.create_runner(cmd)?;
  69. runner.sync_run(|config| cmd.run::<Block, RuntimeApi, Executor>(config))
  70. }
  71. Some(Subcommand::Benchmark(cmd)) => {
  72. if cfg!(feature = "runtime-benchmarks") {
  73. let runner = cli.create_runner(cmd)?;
  74. runner.sync_run(|config| cmd.run::<Block, Executor>(config))
  75. } else {
  76. Err("Benchmarking wasn't enabled when building the node. \
  77. You can enable it with `--features runtime-benchmarks`."
  78. .into())
  79. }
  80. }
  81. Some(Subcommand::Key(cmd)) => cmd.run(),
  82. Some(Subcommand::Sign(cmd)) => cmd.run(),
  83. Some(Subcommand::Verify(cmd)) => cmd.run(),
  84. Some(Subcommand::Vanity(cmd)) => cmd.run(),
  85. Some(Subcommand::BuildSpec(cmd)) => {
  86. let runner = cli.create_runner(cmd)?;
  87. runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
  88. }
  89. Some(Subcommand::BuildSyncSpec(cmd)) => {
  90. let runner = cli.create_runner(cmd)?;
  91. runner.async_run(|config| {
  92. let chain_spec = config.chain_spec.cloned_box();
  93. let network_config = config.network.clone();
  94. let NewFullBase {
  95. task_manager,
  96. client,
  97. network_status_sinks,
  98. ..
  99. } = new_full_base(config, |_, _| ())?;
  100. Ok((
  101. cmd.run(chain_spec, network_config, client, network_status_sinks),
  102. task_manager,
  103. ))
  104. })
  105. }
  106. Some(Subcommand::CheckBlock(cmd)) => {
  107. let runner = cli.create_runner(cmd)?;
  108. runner.async_run(|config| {
  109. let PartialComponents {
  110. client,
  111. task_manager,
  112. import_queue,
  113. ..
  114. } = new_partial(&config)?;
  115. Ok((cmd.run(client, import_queue), task_manager))
  116. })
  117. }
  118. Some(Subcommand::ExportBlocks(cmd)) => {
  119. let runner = cli.create_runner(cmd)?;
  120. runner.async_run(|config| {
  121. let PartialComponents {
  122. client,
  123. task_manager,
  124. ..
  125. } = new_partial(&config)?;
  126. Ok((cmd.run(client, config.database), task_manager))
  127. })
  128. }
  129. Some(Subcommand::ExportState(cmd)) => {
  130. let runner = cli.create_runner(cmd)?;
  131. runner.async_run(|config| {
  132. let PartialComponents {
  133. client,
  134. task_manager,
  135. ..
  136. } = new_partial(&config)?;
  137. Ok((cmd.run(client, config.chain_spec), task_manager))
  138. })
  139. }
  140. Some(Subcommand::ImportBlocks(cmd)) => {
  141. let runner = cli.create_runner(cmd)?;
  142. runner.async_run(|config| {
  143. let PartialComponents {
  144. client,
  145. task_manager,
  146. import_queue,
  147. ..
  148. } = new_partial(&config)?;
  149. Ok((cmd.run(client, import_queue), task_manager))
  150. })
  151. }
  152. Some(Subcommand::PurgeChain(cmd)) => {
  153. let runner = cli.create_runner(cmd)?;
  154. runner.sync_run(|config| cmd.run(config.database))
  155. }
  156. Some(Subcommand::Revert(cmd)) => {
  157. let runner = cli.create_runner(cmd)?;
  158. runner.async_run(|config| {
  159. let PartialComponents {
  160. client,
  161. task_manager,
  162. backend,
  163. ..
  164. } = new_partial(&config)?;
  165. Ok((cmd.run(client, backend), task_manager))
  166. })
  167. }
  168. }
  169. }