discv5_cli/server/
bootstrap.rs1use std::{fs::File, io::BufReader, str::FromStr};
2
3use discv5::{Discv5, Enr};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd, Hash)]
8pub struct BootstrapStore {
9 pub data: Vec<BootstrapNode>,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd, Hash)]
15pub struct BootstrapNode {
16 pub peer_id: String,
18 pub enr: String,
20 pub last_seen_p2p_address: String,
22 pub state: State,
24 pub direction: Direction,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd, Hash)]
30pub enum Direction {
31 #[serde(rename = "inbound")]
33 Inbound,
34 #[serde(rename = "outbound")]
36 Outbound,
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd, Hash)]
41pub enum State {
42 #[serde(rename = "connected")]
44 Connected,
45 #[serde(rename = "disconnected")]
47 Disconnected,
48}
49
50pub async fn boostrap(discv5: &mut Discv5, file: Option<String>) -> eyre::Result<()> {
52 if let Some(f) = file {
53 let file = File::open(f)?;
55 let reader = BufReader::new(file);
56 let bootstrap_store: BootstrapStore = serde_json::from_reader(reader)?;
57
58 for node in bootstrap_store.data {
60 if let Ok(enr) = Enr::from_str(&node.enr) {
62 let node_id = enr.node_id();
63 match discv5.add_enr(enr) {
64 Err(_) => { }
65 Ok(_) => {
66 log::debug!("Bootstrapped node: {node_id}");
67 }
68 }
69 }
70 }
71 }
72
73 Ok(())
74}