use crate::{DhtClient, DhtConfig, NameSystem};
use anyhow::Result;
use libp2p::Multiaddr;
use noosphere_core::authority::generate_ed25519_key;
use ucan::store::UcanJwtStore;
pub struct NameSystemNetwork {
nodes: Vec<NameSystem>,
address: Multiaddr,
}
impl NameSystemNetwork {
pub fn nodes(&self) -> &Vec<NameSystem> {
&self.nodes
}
pub fn nodes_mut(&mut self) -> &mut Vec<NameSystem> {
&mut self.nodes
}
pub fn get(&self, index: usize) -> Option<&NameSystem> {
self.nodes.get(index)
}
pub fn get_mut(&mut self, index: usize) -> Option<&mut NameSystem> {
self.nodes.get_mut(index)
}
pub fn address(&self) -> &Multiaddr {
&self.address
}
pub async fn generate<S: UcanJwtStore + Clone + 'static>(
node_count: usize,
store: Option<S>,
) -> Result<Self> {
let mut bootstrap_address: Option<Multiaddr> = None;
let mut nodes = vec![];
for _ in 0..node_count {
let key = generate_ed25519_key();
let node = NameSystem::new(&key, DhtConfig::default(), store.clone())?;
let address = node.listen("/ip4/127.0.0.1/tcp/0".parse()?).await?;
if let Some(address) = bootstrap_address.as_ref() {
node.add_peers(vec![address.to_owned()]).await?;
} else {
bootstrap_address = Some(address);
}
nodes.push(node);
}
Ok(NameSystemNetwork {
nodes,
address: bootstrap_address.unwrap(),
})
}
}