Skip to content

Commit

Permalink
chore(code): Refactor Starknet app config loading (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
romac authored Mar 5, 2025
1 parent 201eaf8 commit 1cb493b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 60 deletions.
49 changes: 11 additions & 38 deletions code/crates/starknet/app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::time::Duration;

use color_eyre::eyre::Context;

use malachitebft_app::node::{MakeConfigSettings, Node};
use malachitebft_app::node::Node;
use malachitebft_config::{LogFormat, LogLevel};
use malachitebft_starknet_host::config::Config;
use malachitebft_starknet_host::node::StarknetNode;
use malachitebft_starknet_host::node::{ConfigSource, StarknetNode};
use malachitebft_test_cli::args::{Args, Commands};
use malachitebft_test_cli::{logging, runtime};

Expand All @@ -26,7 +23,9 @@ pub fn main() -> color_eyre::Result<()> {
match &args.command {
Commands::Start(cmd) => {
// Redefine the node with the valid configuration.
let node = StarknetNode::new(home_dir, config_file, cmd.start_height)?;
let node =
StarknetNode::new(home_dir, ConfigSource::File(config_file), cmd.start_height);

let config = node.load_config()?;

// This is a drop guard responsible for flushing any remaining logs when the program terminates.
Expand All @@ -44,11 +43,7 @@ pub fn main() -> color_eyre::Result<()> {
Commands::Init(cmd) => {
let _guard = logging::init(LogLevel::Info, LogFormat::Plaintext);

let node = &StarknetNode {
home_dir: home_dir.clone(),
config: default_config(),
start_height: None,
};
let node = &StarknetNode::new(home_dir.clone(), ConfigSource::Default, None);

cmd.run(
node,
Expand All @@ -64,7 +59,7 @@ pub fn main() -> color_eyre::Result<()> {

let node = &StarknetNode {
home_dir: home_dir.clone(),
config: default_config(),
config_source: ConfigSource::Default,
start_height: None,
};

Expand All @@ -77,7 +72,7 @@ pub fn main() -> color_eyre::Result<()> {

let node = &StarknetNode {
home_dir: home_dir.clone(),
config: default_config(),
config_source: ConfigSource::Default,
start_height: None,
};

Expand All @@ -87,26 +82,6 @@ pub fn main() -> color_eyre::Result<()> {
}
}

fn default_config() -> Config {
use malachitebft_app::node::CanMakeConfig;
use malachitebft_config::*;

let settings = MakeConfigSettings {
runtime: RuntimeConfig::SingleThreaded,
transport: TransportProtocol::Tcp,
discovery: DiscoveryConfig {
enabled: true,
bootstrap_protocol: BootstrapProtocol::Kademlia,
selector: Selector::Random,
num_outbound_peers: 6,
num_inbound_peers: 4,
ephemeral_connection_timeout: Duration::from_millis(100),
},
};

StarknetNode::make_config(1, 3, settings)
}

#[cfg(test)]
mod tests {
use std::fs;
Expand All @@ -116,12 +91,10 @@ mod tests {
use color_eyre::eyre;
use color_eyre::eyre::eyre;

use malachitebft_starknet_host::node::StarknetNode;
use malachitebft_starknet_host::node::{ConfigSource, StarknetNode};
use malachitebft_test_cli::args::{Args, Commands};
use malachitebft_test_cli::cmd::init::*;

use super::default_config;

#[test]
fn running_init_creates_config_files() -> eyre::Result<()> {
let tmp = tempfile::tempdir()?;
Expand All @@ -132,7 +105,7 @@ mod tests {

let node = &StarknetNode {
home_dir: tmp.path().to_owned(),
config: default_config(),
config_source: ConfigSource::Default,
start_height: None,
};

Expand Down Expand Up @@ -175,7 +148,7 @@ mod tests {

let node = &StarknetNode {
home_dir: tmp.path().to_owned(),
config: default_config(),
config_source: ConfigSource::Default,
start_height: None,
};

Expand Down
36 changes: 18 additions & 18 deletions code/crates/starknet/host/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,27 @@ impl NodeHandle<MockContext> for Handle {
}
}

#[derive(Clone, Debug)]
pub enum ConfigSource {
File(PathBuf),
Value(Box<Config>),
Default,
}

#[derive(Clone, Debug)]
pub struct StarknetNode {
pub home_dir: PathBuf,
pub config: Config,
pub config_source: ConfigSource,
pub start_height: Option<u64>,
}

impl StarknetNode {
pub fn new(
home_dir: PathBuf,
config_file: PathBuf,
start_height: Option<u64>,
) -> eyre::Result<Self> {
let config = load_config(&config_file, Some("MALACHITE"))?;

Ok(Self {
pub fn new(home_dir: PathBuf, config_source: ConfigSource, start_height: Option<u64>) -> Self {
Self {
home_dir,
config,
config_source,
start_height,
})
}
}

pub fn genesis_file(&self) -> PathBuf {
Expand All @@ -110,7 +111,11 @@ impl Node for StarknetNode {
}

fn load_config(&self) -> eyre::Result<Self::Config> {
Ok(self.config.clone())
match self.config_source {
ConfigSource::File(ref path) => load_config(path, Some("MALACHITE")),
ConfigSource::Value(ref config) => Ok(*config.clone()),
ConfigSource::Default => Ok(default_config()),
}
}

fn get_address(&self, pk: &PublicKey) -> Address {
Expand Down Expand Up @@ -413,7 +418,6 @@ fn make_distributed_config(
}
}

#[cfg(test)]
fn default_config() -> Config {
use malachitebft_config::{DiscoveryConfig, RuntimeConfig, TransportProtocol};

Expand Down Expand Up @@ -443,11 +447,7 @@ fn test_starknet_node() {
std::fs::create_dir_all(temp_path.join("config")).unwrap();

// Create default configuration
let node = StarknetNode {
home_dir: temp_path.clone(),
config: default_config(),
start_height: Some(1),
};
let node = StarknetNode::new(temp_path.clone(), ConfigSource::Default, Some(1));

// Create configuration files
use malachitebft_test_cli::*;
Expand Down
4 changes: 2 additions & 2 deletions code/crates/starknet/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use malachitebft_starknet_host::config::Config;
use rand::rngs::StdRng;
use rand::SeedableRng;

use malachitebft_starknet_host::node::{Handle, StarknetNode};
use malachitebft_starknet_host::node::{ConfigSource, Handle, StarknetNode};
use malachitebft_starknet_host::types::{Height, MockContext, PrivateKey, Validator, ValidatorSet};
use malachitebft_test_framework::HasTestRunner;
use malachitebft_test_framework::{NodeRunner, TestNode};
Expand Down Expand Up @@ -90,8 +90,8 @@ impl NodeRunner<MockContext> for TestRunner {
let home_dir = &self.home_dir[&id].clone();

let app = StarknetNode {
config: self.generate_config(id),
home_dir: home_dir.clone(),
config_source: ConfigSource::Value(Box::new(self.generate_config(id))),
start_height: Some(self.start_height[&id].as_u64()),
};

Expand Down
2 changes: 0 additions & 2 deletions code/crates/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod address;
mod context;
mod genesis;
mod height;
// mod node;
mod proposal;
mod proposal_part;
mod signing;
Expand All @@ -23,7 +22,6 @@ pub use crate::address::*;
pub use crate::context::*;
pub use crate::genesis::*;
pub use crate::height::*;
// pub use crate::node::*;
pub use crate::proposal::*;
pub use crate::proposal_part::*;
pub use crate::signing::*;
Expand Down

0 comments on commit 1cb493b

Please sign in to comment.