This repository has been archived by the owner on Sep 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev: First pass on a
bootstrap
CLI in noosphere-ns to spin up DHT
bootstrap nodes for the name system.
- Loading branch information
Showing
6 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#![cfg(not(target_arch = "wasm32"))] | ||
|
||
use anyhow::{anyhow, Result}; | ||
use clap::Parser; | ||
use home; | ||
use noosphere::key::{InsecureKeyStorage, KeyStorage}; | ||
use noosphere_ns::{ | ||
dht::{DHTConfig, DHTNode}, | ||
Multiaddr, Validator, BOOTSTRAP_PEERS, | ||
}; | ||
use noosphere_storage::{db::SphereDb, memory::MemoryStorageProvider}; | ||
use std::path::PathBuf; | ||
use tokio::{self, signal}; | ||
use ucan_key_support::ed25519::Ed25519KeyMaterial; | ||
|
||
#[derive(Parser)] | ||
#[clap(name = "bootstrap")] | ||
pub struct CLI { | ||
/// The name of the Noosphere keypair to use stored in `~/.noosphere/keys/`. | ||
#[clap(short, long)] | ||
key: String, | ||
|
||
/// The listening port of this DHT node. | ||
#[clap(short, long)] | ||
port: Option<u16>, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let args = CLI::parse(); | ||
let key_storage = InsecureKeyStorage::new(&get_keys_dir()?)?; | ||
let key_material: Ed25519KeyMaterial = { | ||
if let Some(km) = key_storage.read_key(&args.key).await?.take() { | ||
km | ||
} else { | ||
println!("Creating key \"{}\" in `~/.noosphere/keys/`.", &args.key); | ||
key_storage.create_key(&args.key).await? | ||
} | ||
}; | ||
let listening_address: Option<Multiaddr> = args | ||
.port | ||
.or_else(|| Some(0)) | ||
.map(|port| { | ||
format!("/ip4/127.0.0.1/tcp/{}", port) | ||
.parse() | ||
.expect("parseable") | ||
}) | ||
.take(); | ||
|
||
run_bootstrap_node(&key_material, listening_address).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn get_keys_dir() -> Result<PathBuf> { | ||
Ok(home::home_dir() | ||
.ok_or_else(|| anyhow!("Could not discover home directory."))? | ||
.join(".noosphere")) | ||
} | ||
|
||
fn filter_bootstrap_peers(self_address: &Multiaddr, peers: &[Multiaddr]) -> Vec<Multiaddr> { | ||
peers.iter().filter(|addr| *addr != self_address).collect() | ||
} | ||
|
||
async fn run_bootstrap_node( | ||
key_material: &Ed25519KeyMaterial, | ||
listening_address: Option<Multiaddr>, | ||
) -> Result<()> { | ||
let store = SphereDb::new(&MemoryStorageProvider::default()).await?; | ||
let config = DHTConfig { | ||
listening_address, | ||
..Default::default() | ||
}; | ||
let mut node = DHTNode::new(key_material, None, Validator::new(&store), &config)?; | ||
let listening_address = node.p2p_address().expect("has address").to_owned(); | ||
let bootstrap_peers = filter_bootstrap_peers(&listening_address, BOOTSTRAP_PEERS); | ||
|
||
println!("Using bootstrap peers at {:#?}", &bootstrap_peers); | ||
node.add_peers(&bootstrap_peers)?; | ||
node.run()?; | ||
println!("Listening on {}...", listening_address); | ||
|
||
println!("Bootstrapping..."); | ||
node.bootstrap().await?; | ||
println!("Bootstrapped."); | ||
|
||
signal::ctrl_c().await?; | ||
node.terminate()?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters