Skip to content

Commit

Permalink
feat(cli): run command
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-dn committed Sep 18, 2023
1 parent 397da56 commit fde7b57
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 32 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3760,6 +3760,7 @@ name = "lightning-cli"
version = "0.1.0"
dependencies = [
"anyhow",
"atomo-rocks",
"clap 4.4.2",
"fleek-crypto",
"infusion",
Expand All @@ -3780,12 +3781,17 @@ dependencies = [
"lightning-service-executor",
"lightning-signer",
"lightning-topology",
"lightning-types",
"log",
"log4rs",
"mock",
"reqwest",
"resolved-pathbuf",
"serde",
"serde_json",
"tokio",
"toml 0.7.6",
"tracing",
]

[[package]]
Expand Down
6 changes: 6 additions & 0 deletions core/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lightning-rpc = { path = "../rpc" }
lightning-service-executor = { path = "../service-executor" }
lightning-signer = { path = "../signer" }
lightning-topology = { path = "../topology" }
lightning-types = { path = "../types" }
mock = { path = "../mock" }
tokio.workspace = true
clap = { version = "4.4.2", features = ["derive"] }
Expand All @@ -39,3 +40,8 @@ log.workspace = true
toml = "0.7"
infusion.workspace = true
fleek-crypto.workspace = true
tracing = "0.1"
serde.workspace = true
atomo-rocks.workspace = true
serde_json = "1.0"
reqwest = { version = "0.11", features = ["json"] }
27 changes: 23 additions & 4 deletions core/cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::Result;
use lightning_interfaces::infu_collection::Collection;
use lightning_signer::Signer;
use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
use log4rs::append::rolling_file::policy::compound::roll::fixed_window::FixedWindowRoller;
Expand All @@ -12,7 +14,10 @@ use log4rs::filter::threshold::ThresholdFilter;
use resolved_pathbuf::ResolvedPathBuf;

use crate::args::{Args, Command};
use crate::commands::run::CustomStartShutdown;
use crate::commands::{dev, key, print_config, run};
use crate::config::TomlConfigProvider;
use crate::node::{FinalTypes, WithMockConsensus};
use crate::utils::fs::ensure_parent_exist;
use crate::utils::log_filter::CustomLogFilter;

Expand All @@ -32,11 +37,25 @@ impl Cli {
.expect("Failed to resolve config path");
ensure_parent_exist(&config_path)?;

match self.args.with_mock_consensus {
true => self.exec::<WithMockConsensus>(config_path, None).await,
false => self.exec::<FinalTypes>(config_path, None).await,
}
}

async fn exec<C>(
self,
config_path: ResolvedPathBuf,
custom_start_shutdown: Option<CustomStartShutdown<C>>,
) -> Result<()>
where
C: Collection<ConfigProviderInterface = TomlConfigProvider<C>, SignerInterface = Signer<C>>,
{
match self.args.cmd {
Command::Run => run::exec().await,
Command::Key(cmd) => key::exec(cmd, config_path).await,
Command::PrintConfig { default } => print_config::exec(default, config_path).await,
Command::Dev(cmd) => dev::exec(cmd, config_path).await,
Command::Run => run::exec::<C>(config_path, custom_start_shutdown).await,
Command::Key(cmd) => key::exec::<C>(cmd, config_path).await,
Command::PrintConfig { default } => print_config::exec::<C>(default, config_path).await,
Command::Dev(cmd) => dev::exec::<C>(cmd, config_path).await,
}
}

Expand Down
14 changes: 8 additions & 6 deletions core/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ use resolved_pathbuf::ResolvedPathBuf;

use crate::args::DevSubCmd;
use crate::config::TomlConfigProvider;
use crate::node::FinalTypes;

pub async fn exec(cmd: DevSubCmd, config_path: ResolvedPathBuf) -> Result<()> {
pub async fn exec<C>(cmd: DevSubCmd, config_path: ResolvedPathBuf) -> Result<()>
where
C: Collection<ConfigProviderInterface = TomlConfigProvider<C>>,
{
match cmd {
DevSubCmd::InitOnly => init::<FinalTypes>(config_path).await,
DevSubCmd::ShowOrder => show_order::<FinalTypes>().await,
DevSubCmd::DepGraph => dep_graph::<FinalTypes>().await,
DevSubCmd::Store { input } => store::<FinalTypes>(input, config_path).await,
DevSubCmd::InitOnly => init::<C>(config_path).await,
DevSubCmd::ShowOrder => show_order::<C>().await,
DevSubCmd::DepGraph => dep_graph::<C>().await,
DevSubCmd::Store { input } => store::<C>(input, config_path).await,
}
}

Expand Down
10 changes: 6 additions & 4 deletions core/cli/src/commands/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ use resolved_pathbuf::ResolvedPathBuf;

use crate::args::KeySubCmd;
use crate::config::TomlConfigProvider;
use crate::node::FinalTypes;

pub async fn exec(cmd: KeySubCmd, config_path: ResolvedPathBuf) -> Result<()> {
pub async fn exec<C: Collection<SignerInterface = Signer<C>>>(
cmd: KeySubCmd,
config_path: ResolvedPathBuf,
) -> Result<()> {
match cmd {
KeySubCmd::Show => show_key::<FinalTypes>(config_path).await,
KeySubCmd::Generate => generate_key::<FinalTypes>(config_path).await,
KeySubCmd::Show => show_key::<C>(config_path).await,
KeySubCmd::Generate => generate_key::<C>(config_path).await,
}
}

Expand Down
7 changes: 3 additions & 4 deletions core/cli/src/commands/print_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use lightning_interfaces::infu_collection::{Collection, Node};
use resolved_pathbuf::ResolvedPathBuf;

use crate::config::TomlConfigProvider;
use crate::node::FinalTypes;

pub async fn exec(default: bool, config_path: ResolvedPathBuf) -> Result<()> {
pub async fn exec<C: Collection>(default: bool, config_path: ResolvedPathBuf) -> Result<()> {
match default {
true => print_default::<FinalTypes>().await,
false => print::<FinalTypes>(config_path).await,
true => print_default::<C>().await,
false => print::<C>(config_path).await,
}
}

Expand Down
61 changes: 60 additions & 1 deletion core/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
use std::future::Future;
use std::pin::Pin;

use anyhow::Result;
use lightning_application::app::Application;
use lightning_blockstore::blockstore::Blockstore;
use lightning_blockstore_server::BlockStoreServer;
use lightning_interfaces::infu_collection::{Collection, Node};
use lightning_interfaces::ConfigProviderInterface;
use lightning_signer::Signer;
use resolved_pathbuf::ResolvedPathBuf;

use crate::config::TomlConfigProvider;
use crate::shutdown::ShutdownController;
use crate::testnet_sync;

pub type CustomStartShutdown<C> = Box<dyn for<'a> Fn(&'a Node<C>, bool) -> Fut<'a>>;
pub type Fut<'a> = Pin<Box<dyn Future<Output = ()> + 'a>>;
pub async fn exec<C>(
config_path: ResolvedPathBuf,
custom_start_shutdown: Option<CustomStartShutdown<C>>,
) -> Result<()>
where
C: Collection<ConfigProviderInterface = TomlConfigProvider<C>>,
{
let shutdown_controller = ShutdownController::default();
shutdown_controller.install_handlers();

let config = TomlConfigProvider::<C>::load_or_write_config(config_path).await?;

// testnet sync
let signer_config = config.get::<Signer<C>>();
let app_config = config.get::<Application<C>>();
let blockstore_config = config.get::<Blockstore<C>>();
let block_server_config = config.get::<BlockStoreServer<C>>();
if app_config.testnet {
testnet_sync::sync(
signer_config,
app_config,
blockstore_config,
block_server_config,
)
.await;
}

let node =
Node::<C>::init(config).map_err(|e| anyhow::anyhow!("Could not start the node: {e}"))?;

if let Some(cb) = &custom_start_shutdown {
((cb)(&node, true)).await;
} else {
node.start().await;
}

shutdown_controller.wait_for_shutdown().await;

if let Some(cb) = custom_start_shutdown {
((cb)(&node, false)).await;
} else {
node.shutdown().await;
}

pub async fn exec() -> Result<()> {
Ok(())
}
2 changes: 2 additions & 0 deletions core/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod cli;
mod commands;
mod config;
mod node;
mod shutdown;
mod testnet_sync;
mod utils;

use std::process::exit;
Expand Down
29 changes: 16 additions & 13 deletions core/cli/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ use lightning_consensus::consensus::Consensus;
use lightning_dht::dht::Dht;
use lightning_fetcher::fetcher::Fetcher;
use lightning_handshake::handshake::Handshake;
use lightning_interfaces::infu_collection::{Collection, CollectionBase};
use lightning_interfaces::infu_collection::{
Collection,
CollectionBase,
ConsensusInterfaceContainer,
ConsensusInterfaceModifier,
};
use lightning_notifier::Notifier;
use lightning_origin_ipfs::IPFSOrigin;
use lightning_rep_collector::ReputationAggregator;
Expand All @@ -15,6 +20,7 @@ use lightning_rpc::server::Rpc;
use lightning_service_executor::shim::ServiceExecutor;
use lightning_signer::Signer;
use lightning_topology::Topology;
use mock::consensus::MockConsensus;

use crate::config::TomlConfigProvider;

Expand Down Expand Up @@ -43,15 +49,12 @@ impl CollectionBase for FinalTypes {
type FetcherInterface<C: Collection> = Fetcher<C>;
}

/*
* // Create the collection modifier that can inject the mock consensus
* // into the FinalTypes (or other collections.).
*
* pub struct UseMockConsensusMarker;
* impl ConsensusInterfaceContainer for UseMockConsensusMarker {
* type ConsensusInterface<C: Collection> = MockConsensus<C>;
* }
*
* pub type WithMockConsensus<O = FinalTypes> =
* ConsensusInterfaceModifier<UseMockConsensusMarker, O>;
*/
// Create the collection modifier that can inject the mock consensus
// into the FinalTypes (or other collections.).

pub struct UseMockConsensusMarker;
impl ConsensusInterfaceContainer for UseMockConsensusMarker {
type ConsensusInterface<C: Collection> = MockConsensus<C>;
}

pub type WithMockConsensus<O = FinalTypes> = ConsensusInterfaceModifier<UseMockConsensusMarker, O>;

0 comments on commit fde7b57

Please sign in to comment.