Skip to content

Commit

Permalink
feat(cli): add init command
Browse files Browse the repository at this point in the history
  • Loading branch information
snormore committed Jun 5, 2024
1 parent f1cab80 commit 1773ded
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 61 deletions.
41 changes: 40 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,21 @@ dependencies = [
"syn 1.0.109",
]

[[package]]
name = "assert_cmd"
version = "2.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed72493ac66d5804837f480ab3766c72bdfab91a65e565fc54fa9e42db0073a8"
dependencies = [
"anstyle",
"bstr",
"doc-comment",
"predicates 3.1.0",
"predicates-core",
"predicates-tree",
"wait-timeout",
]

[[package]]
name = "assert_matches"
version = "1.5.0"
Expand Down Expand Up @@ -1378,6 +1393,17 @@ dependencies = [
"tinyvec",
]

[[package]]
name = "bstr"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05efc5cfd9110c8416e471df0e96702d58690178e206e61b7173706673c93706"
dependencies = [
"memchr",
"regex-automata 0.4.3",
"serde",
]

[[package]]
name = "bulletproofs"
version = "4.0.0"
Expand Down Expand Up @@ -5975,6 +6001,7 @@ name = "lightning-cli"
version = "0.1.0"
dependencies = [
"anyhow",
"assert_cmd",
"aya",
"aya-log",
"clap 4.5.4",
Expand Down Expand Up @@ -6014,6 +6041,7 @@ dependencies = [
"serde",
"serde_json",
"serial_test",
"tempdir",
"tokio",
"tracing",
"tracing-subscriber",
Expand Down Expand Up @@ -7286,7 +7314,7 @@ dependencies = [
"fragile",
"lazy_static",
"mockall_derive",
"predicates",
"predicates 2.1.5",
"predicates-tree",
]

Expand Down Expand Up @@ -9041,6 +9069,17 @@ dependencies = [
"regex",
]

[[package]]
name = "predicates"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68b87bfd4605926cdfefc1c3b5f8fe560e3feca9d5552cf68c466d3d8236c7e8"
dependencies = [
"anstyle",
"difflib",
"predicates-core",
]

[[package]]
name = "predicates-core"
version = "1.0.6"
Expand Down
6 changes: 0 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ ethers = "2.0.10"
num-bigint = "0.4.0"
base64 = "0.21"
bytes = "1.4"
bigdecimal = "0.3.1"
futures = "0.3"
futures-util = "0.3"
jsonrpc-v2 = { version = "0.11.0", features = ["easy-errors"] }
lru = "0.10.0"
multihash = "0.19"
quinn = "0.10"
rand = { version = "0.8.5", features = ["small_rng"] }
reqwest = "0.11.18"
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -61,7 +57,6 @@ zeroize = "1.6"
scc = "1.8.1"
num-traits = "0.2.15"
num-derive = "0.3.3"
dirs = "5.0.1"
lazy_static = "1.4.0"
schemars = "0.8.1"
rkyv = { version = "0.7.44", features = [
Expand All @@ -82,7 +77,6 @@ atomo-rocks = { path = "lib/atomo-rocks" }
fleek-crypto = { path = "lib/fleek-crypto" }
fleek-ipld = { path = "lib/fleek-ipld" }
hp-fixed = { path = "lib/hp-fixed" }
blake3-tree = { path = "lib/blake3-tree" }
ink-quill = { path = "lib/ink-quill" }
resolved-pathbuf = { path = "lib/resolved-pathbuf" }
panic-report = { path = "lib/panic-report" }
Expand Down
8 changes: 6 additions & 2 deletions core/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ default-run = "lightning-node"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lightning-application = { path = "../application" }
lightning-ebpf-service = { path = "../../etc/ebpf/service" }
lightning-interfaces = { path = "../interfaces" }
lightning-node = { path = "../node" }
Expand Down Expand Up @@ -37,7 +38,6 @@ workspace-hack = { version = "0.1", path = "../../etc/workspace-hack" }

[dev-dependencies]
serial_test = "3.0.0"
lightning-application = { path = "../application" }
lightning-syncronizer = { path = "../syncronizer" }
lightning-broadcast = { path = "../broadcast" }
lightning-consensus = { path = "../consensus" }
Expand All @@ -54,6 +54,8 @@ lightning-archive = { path = "../archive" }
lightning-pinger = { path = "../pinger" }
lightning-rpc = { path = "../rpc" }
fleek-blake3 = "1.5"
assert_cmd = "2.0.14"
tempdir.workspace = true

[features]
default = []
Expand All @@ -62,7 +64,9 @@ services = ["lightning-final-bindings/services"]
dev = ["lightning-tui/logger", "tui-logger"]

[target.'cfg(linux)'.dependencies]
aya = { git = "https://github.com/aya-rs/aya", rev = "e5d107d", features = ["async_tokio"], optional = true }
aya = { git = "https://github.com/aya-rs/aya", rev = "e5d107d", features = [
"async_tokio",
], optional = true }
aya-log = { git = "https://github.com/aya-rs/aya", rev = "e5d107d", optional = true }

[[bin]]
Expand Down
32 changes: 29 additions & 3 deletions core/cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use clap::{arg, ArgAction, Parser, Subcommand};
use clap::{arg, ArgAction, Parser, Subcommand, ValueEnum};
use lightning_application::network::Network;
use lightning_utils::config::LIGHTNING_HOME_DIR;

use crate::commands::admin::AdminSubCmd;
Expand Down Expand Up @@ -28,10 +29,37 @@ pub struct Args {
pub cmd: Command,
}

#[derive(ValueEnum, Debug, Clone)]
pub enum NetworkArg {
LocalnetExample,
TestnetStable,
}

impl From<NetworkArg> for Network {
fn from(network: NetworkArg) -> Self {
match network {
NetworkArg::LocalnetExample => Network::LocalnetExample,
NetworkArg::TestnetStable => Network::TestnetStable,
}
}
}

#[derive(Subcommand)]
pub enum Command {
/// Run the full node.
Run,
/// Initialize the node configuration and genesis block.
Init {
/// The built-in network genesis configuration to use.
#[clap(value_enum, long, short)]
network: NetworkArg,
/// Whether to not generate keys during initialization. The default is to generate keys.
#[clap(long)]
no_generate_keys: bool,
/// Whether to not apply gensis block during initialization. The default is to apply it.
#[clap(long)]
no_apply_genesis: bool,
},
/// Key management utilities.
#[command(subcommand)]
Keys(KeySubCmd),
Expand All @@ -54,8 +82,6 @@ pub enum Command {

#[derive(Subcommand, Clone)]
pub enum DevSubCmd {
/// Initialize every service without starting the node.
InitOnly,
/// Dump the mermaid dependency graph of services.
DepGraph,
/// Store the provided files to the blockstore.
Expand Down
15 changes: 14 additions & 1 deletion core/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing_subscriber::prelude::*;
use tracing_subscriber::EnvFilter;

use crate::args::{Args, Command};
use crate::commands::{admin, dev, keys, opt, print_config, run};
use crate::commands::{admin, dev, init, keys, opt, print_config, run};
use crate::utils::fs::ensure_parent_exist;

pub struct Cli {
Expand Down Expand Up @@ -45,6 +45,19 @@ impl Cli {
{
match self.args.cmd {
Command::Run => run::exec::<C>(config_path).await,
Command::Init {
network,
no_generate_keys,
no_apply_genesis,
} => {
init::exec::<C>(
config_path,
network.into(),
no_generate_keys,
no_apply_genesis,
)
.await
},
Command::Keys(cmd) => keys::exec::<C>(cmd, config_path).await,
Command::Opt(cmd) => opt::exec::<C>(cmd, config_path).await,
Command::PrintConfig { default } => print_config::exec::<C>(default, config_path).await,
Expand Down
13 changes: 1 addition & 12 deletions core/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,12 @@ where
C: Collection<ConfigProviderInterface = TomlConfigProvider<C>>,
{
match cmd {
DevSubCmd::InitOnly => init::<C>(config_path).await,
DevSubCmd::DepGraph => dep_graph::<C>().await,
DevSubCmd::Store { input } => store(input).await,
DevSubCmd::Fetch { remote, hash } => fetch::<C>(config_path, hash, remote).await,
}
}

async fn init<C: Collection<ConfigProviderInterface = TomlConfigProvider<C>>>(
config_path: ResolvedPathBuf,
) -> Result<()> {
let config = TomlConfigProvider::<C>::load_or_write_config(config_path).await?;
Node::<C>::init(config)
.map_err(|e| anyhow::anyhow!("Node Initialization failed: {e:?}"))
.context("Could not start the node.")?;
Ok(())
}

async fn dep_graph<C: Collection>() -> Result<()> {
let graph = C::build_graph();
let mermaid = graph.viz("Lightning Dependency Graph");
Expand Down Expand Up @@ -98,7 +87,7 @@ async fn fetch<C: Collection<ConfigProviderInterface = TomlConfigProvider<C>>>(
};
let hash_string = fleek_blake3::Hash::from(hash).to_string();

let config = TomlConfigProvider::<C>::load_or_write_config(config_path).await?;
let config = TomlConfigProvider::<C>::load(config_path)?;
let mut node = Node::<C>::init(config)
.map_err(|e| anyhow::anyhow!("Node Initialization failed: {e:?}"))
.context("Could not start the node.")?;
Expand Down
61 changes: 61 additions & 0 deletions core/cli/src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use anyhow::{anyhow, Context, Result};
use lightning_application::app::Application;
use lightning_application::config::Config as AppConfig;
use lightning_application::network::Network;
use lightning_final_bindings::FinalTypes;
use lightning_interfaces::prelude::*;
use lightning_utils::config::TomlConfigProvider;
use resolved_pathbuf::ResolvedPathBuf;
use tracing::info;

pub async fn exec<C>(
config_path: ResolvedPathBuf,
network: Network,
no_generate_keys: bool,
no_apply_genesis: bool,
) -> Result<()>
where
C: Collection<ConfigProviderInterface = TomlConfigProvider<C>>,
{
// Error if the configuration file already exists.
if config_path.exists() {
return Err(anyhow!(
"Configuration file already exists at {}",
config_path.to_str().unwrap()
));
}

// Initialize a new configuration with defaults.
let config = TomlConfigProvider::<C>::new();
<C as Collection>::capture_configs(&config);

// Set network field in the configuration.
config.inject::<Application<FinalTypes>>(AppConfig {
network: Some(network),
..Default::default()
});

// Write the configuration file.
config.write(&config_path)?;
info!(
"Configuration file written to {}",
config_path.to_str().unwrap()
);

// Generate keys if requested.
if !no_generate_keys {
let keystore_config = config.get::<C::KeystoreInterface>();
C::KeystoreInterface::generate_keys(keystore_config, true)?;
}

// Execute genesis if requested.
if no_generate_keys {
info!(
"Not loading genesis block, since keys were not generated. It will be loaded when starting the node after you generate or import keys."
);
} else if !no_apply_genesis {
Node::<C>::init(config).context("Failed to execute genesis")?;
}

Ok(())
}
2 changes: 1 addition & 1 deletion core/cli/src/commands/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use resolved_pathbuf::ResolvedPathBuf;
use crate::args::KeySubCmd;

pub async fn exec<C: Collection>(cmd: KeySubCmd, config_path: ResolvedPathBuf) -> Result<()> {
let config_provider = TomlConfigProvider::<C>::load_or_write_config(config_path).await?;
let config_provider = TomlConfigProvider::<C>::load(config_path)?;
let config = config_provider.get::<C::KeystoreInterface>();

match cmd {
Expand Down
1 change: 1 addition & 0 deletions core/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod admin;
pub mod dev;
#[cfg(target_os = "linux")]
pub mod ebpf;
pub mod init;
pub mod keys;
pub mod opt;
pub mod print_config;
Expand Down
6 changes: 3 additions & 3 deletions core/cli/src/commands/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn opt_in<C: Collection>(config_path: ResolvedPathBuf) -> Result<()> {
);
get_user_confirmation();

let config = TomlConfigProvider::<C>::load_or_write_config(config_path).await?;
let config = TomlConfigProvider::<C>::load(config_path)?;
let app_config = config.get::<<C as Collection>::ApplicationInterface>();

let (public_key, secret_key) = load_secret_key::<C>(config).await?;
Expand Down Expand Up @@ -113,7 +113,7 @@ async fn opt_out<C: Collection>(config_path: ResolvedPathBuf) -> Result<()> {
);
get_user_confirmation();

let config = TomlConfigProvider::<C>::load_or_write_config(config_path).await?;
let config = TomlConfigProvider::<C>::load(config_path)?;
let app_config = config.get::<<C as Collection>::ApplicationInterface>();

let (public_key, secret_key) = load_secret_key::<C>(config).await?;
Expand Down Expand Up @@ -168,7 +168,7 @@ async fn opt_out<C: Collection>(config_path: ResolvedPathBuf) -> Result<()> {
}

async fn status<C: Collection>(config_path: ResolvedPathBuf) -> Result<()> {
let config = TomlConfigProvider::<C>::load_or_write_config(config_path).await?;
let config = TomlConfigProvider::<C>::load(config_path)?;
let app_config = config.get::<<C as Collection>::ApplicationInterface>();

let (public_key, _secret_key) = load_secret_key::<C>(config).await?;
Expand Down
2 changes: 1 addition & 1 deletion core/cli/src/commands/print_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn print_default<C: Collection>() -> Result<()> {
}

async fn print<C: Collection>(config_path: ResolvedPathBuf) -> Result<()> {
let config = TomlConfigProvider::<C>::load_or_write_config(config_path).await?;
let config = TomlConfigProvider::<C>::load(config_path)?;
println!("{}", config.serialize_config());
Ok(())
}
2 changes: 1 addition & 1 deletion core/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ where
let shutdown_controller = ShutdownController::default();
shutdown_controller.install_handlers();

let config = TomlConfigProvider::<C>::load_or_write_config(config_path).await?;
let config = TomlConfigProvider::<C>::load(config_path)?;
let app_config = config.get::<<C as Collection>::ApplicationInterface>();

let provider = MultiThreadedProvider::default();
Expand Down
Loading

0 comments on commit 1773ded

Please sign in to comment.