mod join;
mod relocate;
pub(crate) use join::join_network;
pub(crate) use relocate::JoiningAsRelocated;
#[cfg(not(test))]
use crate::node::Error;
use crate::node::Result;
use sn_interface::network_knowledge::prefix_map::NetworkPrefixMap;
use bls::PublicKey as BlsPublicKey;
use std::{collections::HashSet, net::SocketAddr};
#[cfg(not(test))]
use tokio::{fs::File, io::AsyncReadExt};
type UsedRecipientSaps = HashSet<(SocketAddr, BlsPublicKey)>;
#[cfg(not(test))]
async fn read_prefix_map_from_disk(genesis_key: BlsPublicKey) -> Result<NetworkPrefixMap> {
let read_prefix_map = match dirs_next::home_dir() {
None => None,
Some(mut prefix_map_dir) => {
prefix_map_dir.push(".safe");
prefix_map_dir.push("prefix_maps");
if let Ok(mut prefix_map_file) = File::open(prefix_map_dir.clone()).await {
let mut prefix_map_contents = vec![];
match prefix_map_file.read_to_end(&mut prefix_map_contents).await {
Ok(_) => rmp_serde::from_slice(&prefix_map_contents)
.ok()
.map(|map: NetworkPrefixMap| (map, prefix_map_dir)),
Err(_) => None,
}
} else {
None
}
}
};
match read_prefix_map {
Some((prefix_map, dir)) => {
info!(
"Read PrefixMap from disk successfully from {}",
dir.display()
);
if prefix_map.genesis_key() != genesis_key {
Err(Error::InvalidGenesisKey(prefix_map.genesis_key()))
} else {
Ok(prefix_map)
}
}
None => Ok(NetworkPrefixMap::new(genesis_key)),
}
}
#[cfg(test)]
async fn read_prefix_map_from_disk(genesis_key: BlsPublicKey) -> Result<NetworkPrefixMap> {
Ok(NetworkPrefixMap::new(genesis_key))
}