Skip to content

Commit

Permalink
Remove lightning_application dependency from CLI crate
Browse files Browse the repository at this point in the history
  • Loading branch information
daltoncoder committed May 14, 2024
1 parent 9a06043 commit 5ce1ef1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
19 changes: 19 additions & 0 deletions core/application/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use std::time::Duration;
use affair::{Executor, TokioSpawn};
use anyhow::{anyhow, Result};
use lightning_interfaces::prelude::*;
use lightning_interfaces::types::{ChainId, NodeInfo};
use tracing::{error, info};

use crate::config::{Config, StorageConfig};
use crate::env::{Env, UpdateWorker};
use crate::genesis::Genesis;
use crate::query_runner::QueryRunner;
pub struct Application<C: Collection> {
update_socket: Mutex<Option<ExecutionEngineSocket>>,
Expand Down Expand Up @@ -119,4 +121,21 @@ impl<C: Collection> ApplicationInterface<C> for Application<C> {
}
}
}

fn get_chain_id() -> Result<ChainId> {
let genesis = Genesis::load()?;

Ok(genesis.chain_id)
}

fn get_genesis_committee() -> Result<Vec<NodeInfo>> {
let genesis = Genesis::load()?;

Ok(genesis
.node_info
.iter()
.filter(|node| node.genesis_committee)
.map(NodeInfo::from)
.collect())
}
}
3 changes: 0 additions & 3 deletions core/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ lightning-node = { path = "../node" }
lightning-final-bindings = { path = "../final-bindings" }
lightning-utils = { path = "../utils" }

# TODO: cli ideally shouldn't depend on this directly
lightning-application = { path = "../application" }

fleek-crypto.workspace = true
resolved-pathbuf.workspace = true
tokio.workspace = true
Expand Down
34 changes: 10 additions & 24 deletions core/cli/src/commands/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use fleek_crypto::{
TransactionSender,
TransactionSignature,
};
use lightning_application::genesis::Genesis;
use lightning_interfaces::prelude::*;
use lightning_interfaces::types::{
ChainId,
Expand Down Expand Up @@ -51,8 +50,8 @@ async fn opt_in<C: Collection>(config_path: ResolvedPathBuf) -> Result<()> {
.connect_timeout(Duration::from_secs(5))
.build()?;

let genesis_committee =
get_genesis_committee().context("Failed to load genesis committee info.")?;
let genesis_committee = C::ApplicationInterface::get_genesis_committee()
.context("Failed to load genesis committee info.")?;

let node_info = query_node_info(public_key, &genesis_committee, &rpc_client)
.await
Expand All @@ -75,7 +74,8 @@ async fn opt_in<C: Collection>(config_path: ResolvedPathBuf) -> Result<()> {
return Ok(());
}

let chain_id = get_chain_id().context("Failed to load Chain ID from genesis.")?;
let chain_id =
C::ApplicationInterface::get_chain_id().context("Failed to load Chain ID from genesis.")?;

let tx = create_update_request(
UpdateMethod::OptIn {},
Expand Down Expand Up @@ -118,8 +118,8 @@ async fn opt_out<C: Collection>(config_path: ResolvedPathBuf) -> Result<()> {
.connect_timeout(Duration::from_secs(5))
.build()?;

let genesis_committee =
get_genesis_committee().context("Failed to load genesis committee info.")?;
let genesis_committee = C::ApplicationInterface::get_genesis_committee()
.context("Failed to load genesis committee info.")?;

let node_info = query_node_info(public_key, &genesis_committee, &rpc_client)
.await
Expand All @@ -132,7 +132,8 @@ async fn opt_out<C: Collection>(config_path: ResolvedPathBuf) -> Result<()> {
return Ok(());
}

let chain_id = get_chain_id().context("Failed to load Chain ID from genesis.")?;
let chain_id =
C::ApplicationInterface::get_chain_id().context("Failed to load Chain ID from genesis.")?;

let tx = create_update_request(
UpdateMethod::OptOut {},
Expand Down Expand Up @@ -170,8 +171,8 @@ async fn status<C: Collection>(config_path: ResolvedPathBuf) -> Result<()> {
.connect_timeout(Duration::from_secs(5))
.build()?;

let genesis_committee =
get_genesis_committee().context("Failed to load genesis committee info.")?;
let genesis_committee = C::ApplicationInterface::get_genesis_committee()
.context("Failed to load genesis committee info.")?;

let node_info = query_node_info(public_key, &genesis_committee, &rpc_client)
.await
Expand Down Expand Up @@ -263,21 +264,6 @@ fn create_update_request(
}
}

fn get_chain_id() -> Result<ChainId> {
let genesis = Genesis::load()?;
Ok(genesis.chain_id)
}

fn get_genesis_committee() -> Result<Vec<NodeInfo>> {
let genesis = Genesis::load()?;
Ok(genesis
.node_info
.iter()
.filter(|node| node.genesis_committee)
.map(NodeInfo::from)
.collect())
}

pub async fn query_node_info(
public_key: NodePublicKey,
nodes: &[NodeInfo],
Expand Down
7 changes: 7 additions & 0 deletions core/interfaces/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use hp_fixed::unsigned::HpUfixed;
use lightning_types::{
AccountInfo,
Blake3Hash,
ChainId,
Committee,
CommodityTypes,
Metadata,
Expand Down Expand Up @@ -87,6 +88,12 @@ pub trait ApplicationInterface<C: Collection>:
checkpoint: Vec<u8>,
checkpoint_hash: [u8; 32],
) -> Result<()>;

/// Used to get the chain id from the genesis file instead of state
fn get_chain_id() -> Result<ChainId>;

/// Returns the committee from the geneis of the network
fn get_genesis_committee() -> Result<Vec<NodeInfo>>;
}

#[interfaces_proc::blank]
Expand Down

0 comments on commit 5ce1ef1

Please sign in to comment.