-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
179 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
//! Init command | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
use color_eyre::eyre::{eyre, Result}; | ||
use tracing::{info, warn}; | ||
|
||
use malachite_node::config::Config; | ||
use malachite_test::PrivateKey; | ||
use malachite_test::ValidatorSet as Genesis; | ||
|
||
use crate::example::{generate_config, generate_genesis, generate_private_key}; | ||
|
||
/// Execute the init command | ||
pub fn run( | ||
config_file: &Path, | ||
genesis_file: &Path, | ||
priv_validator_key_file: &Path, | ||
index: usize, | ||
) -> Result<()> { | ||
// Save default configuration | ||
if config_file.exists() { | ||
warn!( | ||
"Configuration file already exists at {:?}, skipping.", | ||
config_file.display() | ||
) | ||
} else { | ||
info!("Saving configuration to {:?}.", config_file); | ||
save_config(config_file, &generate_config(index))?; | ||
} | ||
|
||
// Save default genesis | ||
if genesis_file.exists() { | ||
warn!( | ||
"Genesis file already exists at {:?}, skipping.", | ||
genesis_file.display() | ||
) | ||
} else { | ||
info!("Saving test genesis to {:?}.", genesis_file); | ||
save_genesis(genesis_file, &generate_genesis())?; | ||
} | ||
|
||
// Save default priv_validator_key | ||
if priv_validator_key_file.exists() { | ||
warn!( | ||
"Private key file already exists at {:?}, skipping.", | ||
priv_validator_key_file.display() | ||
) | ||
} else { | ||
info!("Saving private key to {:?}.", priv_validator_key_file); | ||
save_priv_validator_key(priv_validator_key_file, &generate_private_key(index))?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Save configuration to file | ||
pub fn save_config(config_file: &Path, config: &Config) -> Result<()> { | ||
save(config_file, &toml::to_string_pretty(config)?) | ||
} | ||
|
||
/// Save genesis to file | ||
pub fn save_genesis(genesis_file: &Path, genesis: &Genesis) -> Result<()> { | ||
save(genesis_file, &serde_json::to_string_pretty(genesis)?) | ||
} | ||
|
||
/// Save private_key validator key to file | ||
pub fn save_priv_validator_key( | ||
priv_validator_key_file: &Path, | ||
private_key: &PrivateKey, | ||
) -> Result<()> { | ||
save( | ||
priv_validator_key_file, | ||
&serde_json::to_string_pretty(private_key)?, | ||
) | ||
} | ||
|
||
fn save(path: &Path, data: &str) -> Result<()> { | ||
use std::io::Write; | ||
|
||
if let Some(parent_dir) = path.parent() { | ||
fs::create_dir_all(parent_dir).map_err(|e| { | ||
eyre!( | ||
"Failed to create parent directory {:?}: {e:?}", | ||
parent_dir.display() | ||
) | ||
})?; | ||
} | ||
|
||
let mut f = fs::OpenOptions::new() | ||
.write(true) | ||
.create(true) | ||
.truncate(true) | ||
.open(path) | ||
.map_err(|e| { | ||
eyre!( | ||
"Failed to crate configuration file at {:?}: {e:?}", | ||
path.display() | ||
) | ||
})?; | ||
|
||
f.write_all(data.as_bytes()).map_err(|e| { | ||
eyre!( | ||
"Failed to write configuration to {:?}: {e:?}", | ||
path.display() | ||
) | ||
})?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod init; | ||
pub mod start; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use color_eyre::eyre::Result; | ||
|
||
use malachite_actors::util::make_node_actor; | ||
use malachite_node::config::Config; | ||
use malachite_test::{Address, PrivateKey, ValidatorSet}; | ||
use tracing::info; | ||
|
||
pub async fn run(sk: PrivateKey, cfg: Config, vs: ValidatorSet) -> Result<()> { | ||
let val_address = Address::from_public_key(&sk.public_key()); | ||
let moniker = cfg.moniker.clone(); | ||
|
||
info!("[{}] Starting...", &cfg.moniker); | ||
|
||
let (tx_decision, mut rx_decision) = tokio::sync::mpsc::channel(32); | ||
let (actor, handle) = make_node_actor(vs, sk, val_address, tx_decision).await; | ||
|
||
tokio::spawn({ | ||
let actor = actor.clone(); | ||
async move { | ||
tokio::signal::ctrl_c().await.unwrap(); | ||
info!("[{moniker}] Shutting down..."); | ||
actor.stop(None); | ||
} | ||
}); | ||
|
||
while let Some((height, round, value)) = rx_decision.recv().await { | ||
info!( | ||
"[{}] Decision at height {height} and round {round}: {value:?}", | ||
&cfg.moniker | ||
); | ||
} | ||
|
||
handle.await?; | ||
|
||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters