Skip to content

Commit

Permalink
feat(e2e): add bootstrapper to spawn_swarm binary
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-wright committed Aug 17, 2023
1 parent 797dd0c commit f0e7100
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
66 changes: 63 additions & 3 deletions core/e2e/src/bin/spawn_swarm.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
use std::time::SystemTime;
use std::{sync::Arc, thread, time::SystemTime};

use anyhow::Result;
use clap::Parser;
use fleek_crypto::PublicKey;
use lightning_e2e::{swarm::Swarm, utils::shutdown};
use fleek_crypto::{NodeSecretKey, PublicKey, SecretKey};
use lightning_application::query_runner::QueryRunner;
use lightning_dht::{
config::{Bootstrapper, Config as DhtConfig},
dht::Builder as DhtBuilder,
};
use lightning_e2e::{
swarm::Swarm,
utils::{
networking::{PortAssigner, Transport},
shutdown,
},
};
use lightning_interfaces::WithStartAndShutdown;
use lightning_topology::Topology;
use resolved_pathbuf::ResolvedPathBuf;
use tokio::sync::Notify;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand All @@ -22,6 +36,45 @@ struct Cli {
async fn main() -> Result<()> {
let args = Cli::parse();

// Start bootstrapper
let mut port_assigner = PortAssigner::default();
let bootstrapper_port = port_assigner
.get_port(12001, 13000, Transport::Udp)
.expect("Failed to assign port");

let bootstrapper_address = format!("0.0.0.0:{bootstrapper_port}").parse().unwrap();
let bootstrapper_config = DhtConfig {
address: bootstrapper_address,
bootstrappers: vec![],
};
let bootstrap_secret_key = NodeSecretKey::generate();
let bootstrap_shutdown_notify = Arc::new(Notify::new());
let bootstrap_ready = Arc::new(Notify::new());
let bootstrap_ready_rx = bootstrap_ready.clone();
let bootstrap_shutdown_notify_rx = bootstrap_shutdown_notify.clone();

let key_cloned = bootstrap_secret_key.clone();
let _bootstrap_handle = thread::spawn(move || {
let mut builder = tokio::runtime::Builder::new_multi_thread();
let runtime = builder
.enable_all()
.build()
.expect("Failed to build tokio runtime for node container.");

runtime.block_on(async move {
let builder = DhtBuilder::new(key_cloned, bootstrapper_config);
let dht = builder.build::<Topology<QueryRunner>>().unwrap();
dht.start().await;
bootstrap_ready_rx.notify_one();

bootstrap_shutdown_notify_rx.notified().await;
dht.shutdown().await;
});
});

// Wait for bootstrapper to start
bootstrap_ready.notified().await;

let epoch_start = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
Expand All @@ -30,9 +83,16 @@ async fn main() -> Result<()> {
let path = ResolvedPathBuf::try_from("~/.lightning-test/e2e/spawn-swarm").unwrap();
let swarm = Swarm::builder()
.with_directory(path)
.with_min_port(12001)
.with_max_port(13000)
.with_num_nodes(args.num_nodes)
.with_epoch_time(args.epoch_time)
.with_epoch_start(epoch_start)
.with_bootstrappers(vec![Bootstrapper {
address: bootstrapper_address,
network_public_key: bootstrap_secret_key.to_pk(),
}])
.with_port_assigner(port_assigner)
.build();
swarm.launch().await.unwrap();

Expand Down
2 changes: 0 additions & 2 deletions core/e2e/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,6 @@ impl SwarmBuilder {

nodes.insert(node_secret_key.to_pk(), (config, owner_secret_key, i));
}

println!("GENESIS LEN: {}", genesis.committee.len());
let nodes = nodes
.into_iter()
.map(|(node_pub_key, (config, owner_secret_key, index))| {
Expand Down
1 change: 1 addition & 0 deletions core/e2e/tests/dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ async fn e2e_dht() -> Result<()> {
address: bootstrapper_address,
network_public_key: bootstrap_secret_key.to_pk(),
}])
.with_port_assigner(port_assigner)
.build();
swarm.launch().await.unwrap();

Expand Down

0 comments on commit f0e7100

Please sign in to comment.