diff --git a/tools/config/src/config_cli.rs b/tools/config/src/config_cli.rs index f24dfaf39..aa907ffc3 100644 --- a/tools/config/src/config_cli.rs +++ b/tools/config/src/config_cli.rs @@ -1,3 +1,4 @@ +use crate::fullnode_config::{download_genesis, init_fullnode_yaml}; use crate::host::initialize_validator_configs; use crate::{legacy_config, make_profile}; use anyhow::{Context, Result}; @@ -93,9 +94,9 @@ enum ConfigSub { /// Generate a fullnode dir and add fullnode.yaml from template FullnodeInit { - /// path to libra config and data files defaults to $HOME/.libra - #[clap(short, long)] - home_path: Option, + /// path to libra config and data files defaults to $HOME/.libra + #[clap(short, long)] + home_path: Option, }, } @@ -230,8 +231,11 @@ impl ConfigCli { Ok(()) } Some(ConfigSub::FullnodeInit { home_path }) => { + download_genesis(home_path.to_owned()).await?; + + init_fullnode_yaml(home_path.to_owned()).await?; - Ok(()) + Ok(()) } _ => { println!("Sometimes I'm right and I can be wrong. My own beliefs are in my song. The butcher, the banker, the drummer and then. Makes no difference what group I'm in."); diff --git a/tools/config/src/fullnode_config.rs b/tools/config/src/fullnode_config.rs index 69c08bf55..5aaa50bf8 100644 --- a/tools/config/src/fullnode_config.rs +++ b/tools/config/src/fullnode_config.rs @@ -1,45 +1,54 @@ -use std::{collections::HashMap, path::{PathBuf, Path}}; use diem_config::config::NodeConfig; -use diem_types::{PeerId, network_address::NetworkAddress, waypoint::Waypoint}; +use diem_types::{network_address::NetworkAddress, waypoint::Waypoint, PeerId}; use libra_types::legacy_types::app_cfg::default_file_path; +use std::{ + collections::HashMap, + path::{Path, PathBuf}, +}; +/// fetch seed peers and make a yaml file from template +pub async fn init_fullnode_yaml(home_dir: Option) -> anyhow::Result<()> { + let waypoint = get_genesis_waypoint(home_dir.clone()).await?; -pub fn add_peers_to_yaml(path: &Path, peers: HashMap> ) -> anyhow::Result<()> { - let string = std::fs::read_to_string(path)?; - let mut parsed: NodeConfig = serde_yaml::from_str(&string)?; + let yaml = make_fullnode_yaml(home_dir.clone(), waypoint)?; - parsed.full_node_networks.iter_mut().for_each(move |e| { - if e.network_id.is_public_network() { - e.seed_addrs = peers.clone(); - } - }); + let home = home_dir.unwrap_or_else(default_file_path); + let p = home.join("fullnode.yaml"); - let ser = serde_yaml::to_string(&parsed)?; - std::fs::write(path, ser)?; + std::fs::write(&p, yaml)?; + let peers = fetch_seed_addresses(None).await?; - Ok(()) -} - -/// download genesis blob -pub async fn download_genesis(home_dir: Option) -> anyhow::Result<()>{ + add_peers_to_yaml(&p, peers)?; - let bytes = reqwest::get("https://github.com/0xzoz/blob/raw/main/genesis.blob") - .await? - .bytes() - .await?; + Ok(()) +} - let home = home_dir.unwrap_or_else(default_file_path); - let p = home.join("genesis.blob"); +pub fn add_peers_to_yaml( + path: &Path, + peers: HashMap>, +) -> anyhow::Result<()> { + let string = std::fs::read_to_string(path)?; + let mut parsed: NodeConfig = serde_yaml::from_str(&string)?; - std::fs::write(p, bytes)?; + parsed.full_node_networks.iter_mut().for_each(move |e| { + if e.network_id.is_public_network() { + e.seed_addrs = peers.clone(); + } + }); - Ok(()) + let ser = serde_yaml::to_string(&parsed)?; + std::fs::write(path, ser)?; + Ok(()) } /// get seed peers from an upstream url -pub async fn fetch_seed_addresses(url: Option<&str>) -> anyhow::Result>> { - let u = url.unwrap_or("https://raw.githubusercontent.com/0LNetworkCommunity/seed-peers/main/seed_peers.yaml"); +pub async fn fetch_seed_addresses( + url: Option<&str>, +) -> anyhow::Result>> { + let u = url.unwrap_or( + "https://raw.githubusercontent.com/0LNetworkCommunity/seed-peers/main/seed_peers.yaml", + ); let res = reqwest::get(u).await?; let out = res.text().await?; let seeds: HashMap> = serde_yaml::from_str(&out)?; @@ -47,13 +56,13 @@ pub async fn fetch_seed_addresses(url: Option<&str>) -> anyhow::Result, waypoint: Waypoint) -> anyhow::Result { let home_dir = home_dir.unwrap_or_else(libra_types::global_config_dir); let path = home_dir.display().to_string(); - let template = format!(" + let template = format!( + " base: role: 'full_node' data_dir: '{path}/data' @@ -76,60 +85,90 @@ api: enabled: true address: '0.0.0.0:8080' " -); + ); Ok(template) } +/// download genesis blob +pub async fn download_genesis(home_dir: Option) -> anyhow::Result<()> { + let bytes = reqwest::get("https://github.com/0xzoz/blob/raw/main/genesis.blob") + .await? + .bytes() + .await?; + + let home = home_dir.unwrap_or_else(default_file_path); + let p = home.join("genesis.blob"); + + std::fs::write(p, bytes)?; + + Ok(()) +} + +pub async fn get_genesis_waypoint(home_dir: Option) -> anyhow::Result { + let wp_string = reqwest::get("https://raw.githubusercontent.com/0xzoz/blob/main/waypoint.txt") + .await? + .text() + .await?; + + let home = home_dir.unwrap_or_else(default_file_path); + let p = home.join("waypoint.txt"); + + std::fs::write(p, &wp_string)?; + + wp_string.parse() +} #[tokio::test] async fn get_peers() { - let seed = fetch_seed_addresses(None).await.unwrap(); + let seed = fetch_seed_addresses(None).await.unwrap(); - dbg!(&seed); + dbg!(&seed); } #[tokio::test] async fn get_yaml() { - use std::str::FromStr; - let p = diem_temppath::TempPath::new().path().to_owned(); + use std::str::FromStr; + let p = diem_temppath::TempPath::new().path().to_owned(); - let seeds = fetch_seed_addresses(None).await.unwrap(); + let seeds = fetch_seed_addresses(None).await.unwrap(); - let y = make_fullnode_yaml( - Some(p.clone()), - Waypoint::from_str("0:95023f4d6a7e24cac3e52cad29697184db260214210b57aef3f1031ad4d8c02c").unwrap(), - ).unwrap(); + let y = make_fullnode_yaml( + Some(p.clone()), + Waypoint::from_str("0:95023f4d6a7e24cac3e52cad29697184db260214210b57aef3f1031ad4d8c02c") + .unwrap(), + ) + .unwrap(); - std::fs::write(&p, y).unwrap(); + std::fs::write(&p, y).unwrap(); - // let mut parsed: NodeConfig = serde_yaml::from_str(&y).unwrap(); - // parsed.full_node_networks.iter_mut().for_each(move |e| { - // if e.network_id.is_public_network() { - // e.seed_addrs = seeds.clone(); - // } - // }); + // let mut parsed: NodeConfig = serde_yaml::from_str(&y).unwrap(); + // parsed.full_node_networks.iter_mut().for_each(move |e| { + // if e.network_id.is_public_network() { + // e.seed_addrs = seeds.clone(); + // } + // }); - add_peers_to_yaml(&p, seeds).unwrap(); + add_peers_to_yaml(&p, seeds).unwrap(); - let text = std::fs::read_to_string(&p).unwrap(); + let text = std::fs::read_to_string(&p).unwrap(); - dbg!(&text); + dbg!(&text); } #[tokio::test] async fn persist_genesis() { - let mut p = diem_temppath::TempPath::new(); - p.create_as_dir().unwrap(); - p.persist(); - - let path = p.path().to_owned(); - - download_genesis(Some(path)).await.unwrap(); - let l = std::fs::read_dir(p.path()) - .unwrap() - .nth(0) - .unwrap() - .unwrap(); - assert!(l.file_name().to_str().unwrap().contains("genesis.blob")); - // dbg!(&l); + let mut p = diem_temppath::TempPath::new(); + p.create_as_dir().unwrap(); + p.persist(); + + let path = p.path().to_owned(); + + download_genesis(Some(path)).await.unwrap(); + let l = std::fs::read_dir(p.path()) + .unwrap() + .next() + .unwrap() + .unwrap(); + assert!(l.file_name().to_str().unwrap().contains("genesis.blob")); + // dbg!(&l); } diff --git a/tools/config/src/lib.rs b/tools/config/src/lib.rs index 6c01b645b..e76ceafb4 100644 --- a/tools/config/src/lib.rs +++ b/tools/config/src/lib.rs @@ -1,6 +1,6 @@ pub mod config_cli; +pub mod fullnode_config; pub mod host; pub mod legacy_config; pub mod make_profile; pub mod node_yaml; -pub mod fullnode_config; \ No newline at end of file