Skip to content

Commit

Permalink
fullnode config can be created with seed peers from web
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Nov 2, 2023
1 parent 61633c7 commit 51a6e39
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 68 deletions.
12 changes: 8 additions & 4 deletions tools/config/src/config_cli.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<PathBuf>,
/// path to libra config and data files defaults to $HOME/.libra
#[clap(short, long)]
home_path: Option<PathBuf>,
},
}

Expand Down Expand Up @@ -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.");
Expand Down
165 changes: 102 additions & 63 deletions tools/config/src/fullnode_config.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,68 @@
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<PathBuf>) -> anyhow::Result<()> {
let waypoint = get_genesis_waypoint(home_dir.clone()).await?;

pub fn add_peers_to_yaml(path: &Path, peers: HashMap<PeerId, Vec<NetworkAddress>> ) -> 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<PathBuf>) -> 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<PeerId, Vec<NetworkAddress>>,
) -> 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<HashMap<PeerId, Vec<NetworkAddress>>> {
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<HashMap<PeerId, Vec<NetworkAddress>>> {
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<PeerId, Vec<NetworkAddress>> = serde_yaml::from_str(&out)?;

Ok(seeds)
}


/// Create a fullnode yaml to bootstrap node
pub fn make_fullnode_yaml(home_dir: Option<PathBuf>, waypoint: Waypoint) -> anyhow::Result<String> {
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'
Expand All @@ -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<PathBuf>) -> 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<PathBuf>) -> anyhow::Result<Waypoint> {
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);
}
2 changes: 1 addition & 1 deletion tools/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 51a6e39

Please sign in to comment.