use std::{fs::File, io::BufReader, str::FromStr};
use discv5::{Discv5, Enr};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct BootstrapStore {
pub data: Vec<BootstrapNode>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct BootstrapNode {
pub peer_id: String,
pub enr: String,
pub last_seen_p2p_address: String,
pub state: State,
pub direction: Direction,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub enum Direction {
#[serde(rename = "inbound")]
Inbound,
#[serde(rename = "outbound")]
Outbound,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub enum State {
#[serde(rename = "connected")]
Connected,
#[serde(rename = "disconnected")]
Disconnected,
}
pub async fn boostrap(discv5: &mut Discv5, file: Option<String>) -> eyre::Result<()> {
if let Some(f) = file {
let file = File::open(f)?;
let reader = BufReader::new(file);
let bootstrap_store: BootstrapStore = serde_json::from_reader(reader)?;
for node in bootstrap_store.data {
if let Ok(enr) = Enr::from_str(&node.enr) {
let node_id = enr.node_id();
match discv5.add_enr(enr) {
Err(_) => { }
Ok(_) => {
log::debug!("Bootstrapped node: {node_id}");
}
}
}
}
}
Ok(())
}