Files
bft-crdt-experiment/side-node/src/init/mod.rs

121 lines
4.0 KiB
Rust
Raw Normal View History

2024-06-06 15:54:33 +01:00
use std::path::PathBuf;
2024-06-06 14:39:50 +01:00
2024-06-06 15:54:33 +01:00
use config::SideNodeConfig;
2024-06-06 14:39:50 +01:00
2024-06-18 16:35:56 +01:00
use crate::{bft_crdt_keys, bitcoin_keys, utils};
2024-06-06 14:39:50 +01:00
pub(crate) mod config;
2024-06-06 13:54:15 +01:00
2024-06-06 15:54:33 +01:00
pub(crate) fn init(home: PathBuf, config: SideNodeConfig) -> Result<(), std::io::Error> {
2024-06-06 15:30:47 +01:00
ensure_side_directory_exists(&home)?;
let (bft_crdt_key_path, bitcoin_key_path, config_path) = utils::side_paths(home.clone());
2024-06-06 14:39:50 +01:00
println!("Writing bft crdt key to: {:?}", bft_crdt_key_path);
2024-06-18 16:35:56 +01:00
bft_crdt_keys::write(&bft_crdt_key_path)?;
println!("Writing bitcoin key to: {:?}", bitcoin_key_path);
bitcoin_keys::write(&bitcoin_key_path)?;
2024-06-06 13:54:15 +01:00
2024-06-06 16:03:36 +01:00
println!("Writing config to: {:?}", config_path);
config::write_toml(&config, &config_path).expect("unable to write config file");
2024-06-06 16:03:36 +01:00
2024-06-06 14:39:50 +01:00
Ok(())
}
/// Ensures that the directory at side_dir exists, so we have a place
/// to store our key files and config file.
2024-06-06 15:30:47 +01:00
fn ensure_side_directory_exists(side_dir: &PathBuf) -> Result<(), std::io::Error> {
if side_dir.exists() {
return Ok(());
}
println!(
"Config directory doesn't exist, creating at: {:?}",
side_dir
);
std::fs::create_dir_all(side_dir)
2024-06-06 14:39:50 +01:00
}
2024-06-06 13:54:15 +01:00
#[cfg(test)]
mod tests {
2024-06-13 11:34:41 +01:00
use std::{fs, path::Path, str::FromStr};
2024-06-06 18:55:48 +01:00
use fastcrypto::{
ed25519::Ed25519KeyPair,
traits::{EncodeDecodeBase64, KeyPair, ToFromBytes},
};
2024-06-06 14:39:50 +01:00
use super::*;
2024-06-06 13:54:15 +01:00
2024-06-13 11:34:41 +01:00
/// Generates a SideNodeConfig with a unique name for each test.
/// This is necessary because the tests run in parallel and we
/// don't want them to interfere with each other - without a unique
/// name, the tests would all try to write to the same directory and we
/// get test indeterminacy
fn side_node_config() -> (SideNodeConfig, String) {
let name = format!("test-{}", uuid::Uuid::new_v4()).to_string();
(SideNodeConfig { name: name.clone() }, name)
2024-06-06 15:54:33 +01:00
}
2024-06-18 16:00:02 +01:00
#[test]
fn creates_bitcoin_keys() {
let (config, name) = side_node_config();
let side_dir = format!("/tmp/side/{name}");
let mut bitcoin_keys_path = PathBuf::new();
bitcoin_keys_path.push(side_dir.clone());
bitcoin_keys_path.push(utils::BITCOIN_KEY_FILE);
2024-06-18 16:00:02 +01:00
let _ = init(PathBuf::from_str(&side_dir).unwrap(), config);
assert!(bitcoin_keys_path.exists());
2024-06-18 16:00:02 +01:00
// check that the pem is readable
// let data = fs::read_to_string(bitcoin_keys_path).expect("couldn't read key file");
// let keys = Ed25519KeyPair::decode_base64(&data).expect("couldn't load keypair from file");
// assert_eq!(keys.public().as_bytes().len(), 32);
2024-06-18 16:00:02 +01:00
}
2024-06-06 13:54:15 +01:00
#[test]
fn creates_side_node_directory() {
2024-06-13 11:34:41 +01:00
let (config, name) = side_node_config();
let side_dir = format!("/tmp/side/{name}");
2024-06-13 11:34:41 +01:00
let mut test_home = PathBuf::new();
test_home.push(side_dir);
let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap();
2024-06-13 11:34:41 +01:00
let _ = init(test_home.clone(), config);
2024-06-06 13:54:15 +01:00
assert!(std::path::Path::new(node_dir).exists());
}
2024-06-06 14:39:50 +01:00
#[test]
2024-06-18 16:00:02 +01:00
fn creates_bft_crdt_key_file() {
2024-06-13 11:34:41 +01:00
let (config, name) = side_node_config();
let side_dir = format!("/tmp/side/{name}");
2024-06-13 11:34:41 +01:00
let mut key_file_path = PathBuf::new();
key_file_path.push(side_dir.clone());
2024-06-18 16:00:02 +01:00
key_file_path.push(utils::BFT_CRDT_KEY_FILE);
2024-06-13 11:34:41 +01:00
let _ = init(PathBuf::from_str(&side_dir).unwrap(), config);
assert!(key_file_path.exists());
// check that the pem is readable
2024-06-13 11:34:41 +01:00
let data = fs::read_to_string(key_file_path).expect("couldn't read key file");
2024-06-06 18:55:48 +01:00
let keys = Ed25519KeyPair::decode_base64(&data).expect("couldn't load keypair from file");
assert_eq!(keys.public().as_bytes().len(), 32);
2024-06-06 14:39:50 +01:00
}
2024-06-06 13:54:15 +01:00
#[test]
fn creates_config_file() {
2024-06-13 11:34:41 +01:00
let (config, name) = side_node_config();
let side_dir = format!("/tmp/side/{name}");
let mut config_file_path = PathBuf::new();
config_file_path.push(side_dir.clone());
config_file_path.push(utils::CONFIG_FILE);
2024-06-13 11:34:41 +01:00
let _ = init(PathBuf::from_str(&side_dir).unwrap(), config);
assert!(config_file_path.exists());
}
2024-05-29 16:47:35 +01:00
}