Skip to content

Commit

Permalink
cli priv key and dial addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Jul 5, 2024
1 parent 7da4908 commit 5b07e43
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ hyper = { version = "1.0", features = [] }
hyper-util = { version = "0.1", features = ["tokio", "server-auto", "http1"] }
tower = { version = "0.4", features = ["util"] }
tower-http = { version = "0.5", features = ["timeout", "trace", "cors"] }
clap = { version = "4.0", features = ["derive"] }

zetina-common = { path = "crates/common" }
zetina-compiler = { path = "crates/compiler" }
Expand Down
3 changes: 2 additions & 1 deletion crates/delegator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ axum.workspace = true
hyper.workspace = true
tower.workspace = true
hyper-util.workspace = true
tower-http.workspace = true
tower-http.workspace = true
clap.workspace = true
18 changes: 16 additions & 2 deletions crates/delegator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use axum::{
routing::{get, post},
Router,
};
use clap::Parser;
use delegator::Delegator;
use libp2p::gossipsub;
use std::time::Duration;
Expand All @@ -30,14 +31,26 @@ use zetina_common::{
topic::{gossipsub_ident_topic, Topic},
};

#[derive(Parser)]
struct Cli {
/// The private key as a hex string
#[arg(short, long)]
private_key: String,

#[arg(short, long)]
dial_addresses: Vec<String>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _ = tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).try_init();

// Parse command line arguments
let cli = Cli::parse();

// TODO: common setup in node initiate binary
let network = Network::Sepolia;
let private_key =
hex::decode("018ef9563461ec2d88236d59039babf44c97d8bf6200d01d81170f1f60a78f32")?;
let private_key = hex::decode(cli.private_key)?;

let node_account = NodeAccount::new(private_key);

Expand All @@ -55,6 +68,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (job_topic_tx, job_topic_rx) = mpsc::channel::<Vec<u8>>(100);

SwarmRunner::new(
cli.dial_addresses,
&p2p_keypair,
vec![job_topic.to_owned(), picked_job_topic, finished_job_topic],
vec![(job_topic, job_topic_rx)],
Expand Down
13 changes: 11 additions & 2 deletions crates/delegator/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use futures::StreamExt;
use libp2p::gossipsub::{self, IdentTopic};
use libp2p::identity::Keypair;
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
use libp2p::{noise, tcp, yamux, SwarmBuilder};
use libp2p::{noise, tcp, yamux, Multiaddr, SwarmBuilder};
use std::error::Error;
use std::str::FromStr;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tracing::{debug, error};
use tracing::{debug, error, info};
use zetina_common::graceful_shutdown::shutdown_signal;

#[derive(NetworkBehaviour)]
Expand All @@ -21,6 +22,7 @@ pub struct SwarmRunner {

impl SwarmRunner {
pub fn new(
dial_addresses: Vec<String>,
p2p_local_keypair: &Keypair,
subscribe_topics: Vec<IdentTopic>,
mut transmit_topics: Vec<(IdentTopic, mpsc::Receiver<Vec<u8>>)>,
Expand All @@ -45,6 +47,13 @@ impl SwarmRunner {
swarm.behaviour_mut().gossipsub.subscribe(&topic)?;
}

// Reach out to other nodes if specified
for to_dial in dial_addresses {
let addr: Multiaddr = Multiaddr::from_str(&to_dial)?;
swarm.dial(addr)?;
info!("Dialed {to_dial:?}")
}

swarm.listen_on("/ip4/0.0.0.0/udp/5678/quic-v1".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/tcp/5679".parse()?)?;

Expand Down
3 changes: 2 additions & 1 deletion crates/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ axum.workspace = true
hyper.workspace = true
tower.workspace = true
hyper-util.workspace = true
tower-http.workspace = true
tower-http.workspace = true
clap.workspace = true
18 changes: 16 additions & 2 deletions crates/executor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod executor;
pub mod swarm;

use axum::Router;
use clap::Parser;
use executor::Executor;
use libp2p::gossipsub;
use std::time::Duration;
Expand All @@ -18,10 +19,23 @@ use zetina_common::{
use zetina_prover::stone_prover::StoneProver;
use zetina_runner::cairo_runner::CairoRunner;

#[derive(Parser)]
struct Cli {
/// The private key as a hex string
#[arg(short, long)]
private_key: String,

#[arg(short, long)]
dial_addresses: Vec<String>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _ = tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).try_init();

// Parse command line arguments
let cli = Cli::parse();

let ws_root = std::path::PathBuf::from(
std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env not present"),
)
Expand All @@ -30,8 +44,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// TODO: common setup in node initiate binary
let network = Network::Sepolia;
let private_key =
hex::decode("07c7a41c77c7a3b19e7c77485854fc88b09ed7041361595920009f81236d55d2")?;
let private_key = hex::decode(cli.private_key)?;

let node_account = NodeAccount::new(private_key);

Expand All @@ -45,6 +58,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (finished_job_topic_tx, finished_job_topic_rx) = mpsc::channel::<Vec<u8>>(1000);

SwarmRunner::new(
cli.dial_addresses,
node_account.get_keypair(),
vec![new_job_topic, picked_job_topic.to_owned(), finished_job_topic.to_owned()],
vec![
Expand Down
13 changes: 11 additions & 2 deletions crates/executor/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use futures::StreamExt;
use libp2p::gossipsub::{self, IdentTopic};
use libp2p::identity::Keypair;
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
use libp2p::{noise, tcp, yamux, SwarmBuilder};
use libp2p::{noise, tcp, yamux, Multiaddr, SwarmBuilder};
use std::error::Error;
use std::str::FromStr;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tracing::{debug, error};
use tracing::{debug, error, info};
use zetina_common::graceful_shutdown::shutdown_signal;

#[derive(NetworkBehaviour)]
Expand All @@ -21,6 +22,7 @@ pub struct SwarmRunner {

impl SwarmRunner {
pub fn new(
dial_addresses: Vec<String>,
p2p_local_keypair: &Keypair,
subscribe_topics: Vec<IdentTopic>,
mut transmit_topics: Vec<(IdentTopic, mpsc::Receiver<Vec<u8>>)>,
Expand Down Expand Up @@ -48,6 +50,13 @@ impl SwarmRunner {
swarm.listen_on("/ip4/0.0.0.0/udp/5678/quic-v1".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/tcp/5679".parse()?)?;

// Reach out to other nodes if specified
for to_dial in dial_addresses {
let addr: Multiaddr = Multiaddr::from_str(&to_dial)?;
swarm.dial(addr)?;
info!("Dialed {to_dial:?}")
}

Ok(SwarmRunner {
handle: Some(tokio::spawn(async move {
// TODO make it nicer solution, extensible not manual!
Expand Down

0 comments on commit 5b07e43

Please sign in to comment.