Skip to content

Commit

Permalink
cli implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdud007 committed Jul 3, 2024
1 parent b88d57d commit 9352098
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 198 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async-stream = "0.3.5"
bincode = "1.3"
cairo-proof-parser = { git = "https://github.com/Okm165/cairo-proof-parser", rev = "97a04bbee07330311b38d6f4cecfed3acb237626" }
cairo-vm = { git = "https://github.com/lambdaclass/cairo-vm.git", tag = "v1.0.0-rc3" }
clap = { version = "4.4.4", features = ["derive"] }
futures = "0.3.30"
futures-core = "0.3.30"
futures-util = "0.3.30"
Expand Down Expand Up @@ -78,4 +79,4 @@ zetina-executor = { path = "crates/executor" }
zetina-peer = { path = "crates/peer" }
zetina-prover = { path = "crates/prover" }
zetina-runner = { path = "crates/runner" }
zetina-tests = { path = "crates/tests" }
zetina-tests = { path = "crates/tests" }
3 changes: 2 additions & 1 deletion crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ publish = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap.workspace = true
cairo-vm.workspace = true
rand.workspace = true
futures.workspace = true
Expand All @@ -27,4 +28,4 @@ starknet.workspace = true
strum.workspace = true
tempfile.workspace = true
thiserror.workspace = true
tokio.workspace = true
tokio.workspace = true
1 change: 1 addition & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ pub mod network;
pub mod node_account;
pub mod process;
pub mod topic;
pub mod value_parser;

// mod fuzzing;
3 changes: 2 additions & 1 deletion crates/common/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
This defines the network that the node is connected to.
*/

use clap::ValueEnum;
use starknet::core::chain_id;
use starknet_crypto::FieldElement;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, ValueEnum, Clone, Copy)]
pub enum Network {
Mainnet,
Sepolia,
Expand Down
3 changes: 1 addition & 2 deletions crates/common/src/node_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<P> NodeAccount<P>
where
P: Provider + Sync + Send + 'static,
{
pub fn new(private_key: Vec<u8>, address: Vec<u8>, network: Network, provider: P) -> Self {
pub fn new(private_key: Vec<u8>, address: FieldElement, network: Network, provider: P) -> Self {
let secret_key = libp2p::identity::ecdsa::SecretKey::try_from_bytes(private_key.as_slice())
.expect("Failed to create secret key from private key.");
let p2p_keypair =
Expand All @@ -33,7 +33,6 @@ where
FieldElement::from_byte_slice_be(private_key.as_slice()).unwrap(),
);
let signer = LocalWallet::from(signing_key.clone());
let address = FieldElement::from_byte_slice_be(address.as_slice()).unwrap();
let network = network.to_field_element();
let account =
SingleOwnerAccount::new(provider, signer, address, network, ExecutionEncoding::New);
Expand Down
13 changes: 13 additions & 0 deletions crates/common/src/value_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use starknet_crypto::FieldElement;

/// Parse bytes from hex string
pub fn parse_bytes(arg: &str) -> Result<Vec<u8>, String> {
hex::decode(arg).map_err(|e| format!("Failed to parse bytes: {}", e))
}

/// Parse FieldElement from hex string
pub fn parse_field_element(arg: &str) -> Result<FieldElement, String> {
hex::decode(arg)
.map(|bytes| FieldElement::from_byte_slice_be(bytes.as_slice()).unwrap())
.map_err(|e| format!("Failed to parse bytes: {}", e))
}
7 changes: 6 additions & 1 deletion crates/delegator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ readme = "README.md"
repository.workspace = true
version.workspace = true

[[bin]]
name = "zetina-submit"
path = "src/main.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap.workspace = true
futures.workspace = true
hex.workspace = true
libp2p.workspace = true
Expand All @@ -31,4 +36,4 @@ thiserror.workspace = true
tonic-health.workspace = true

[build-dependencies]
tonic-build.workspace = true
tonic-build.workspace = true
92 changes: 92 additions & 0 deletions crates/delegator/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use clap::{command, Parser};
use libp2p::gossipsub;
use starknet::{
core::types::FieldElement,
providers::{jsonrpc::HttpTransport, JsonRpcClient, Url},
};
use tokio::sync::{broadcast, mpsc};
use tonic::transport::Server;
use tracing_subscriber::EnvFilter;
use zetina_common::{
graceful_shutdown::shutdown_signal,
job_witness::JobWitness,
network::Network,
node_account::NodeAccount,
topic::{gossipsub_ident_topic, Topic},
value_parser::{parse_bytes, parse_field_element},
};

use crate::{
behavior_handler::BehaviourHandler,
swarm::SwarmRunner,
tonic::{proto::delegator_service_server::DelegatorServiceServer, DelegatorGRPCServer},
};

#[derive(Debug, Parser)]
#[command(name = "zetina-submit")]
#[command(version, about, long_about = None)]
pub struct DelegatorCommand {
pub network: Network,
#[arg(value_parser = parse_bytes)]
pub private_key: Vec<u8>,
#[arg(value_parser = parse_field_element)]
pub account_address: FieldElement,
pub rpc_url: Url,
}

pub async fn run() -> Result<(), Box<dyn std::error::Error>> {
let _ = tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).try_init();
let delegator_command = DelegatorCommand::parse();
let DelegatorCommand { network, private_key, account_address, rpc_url } = delegator_command;

// // TODO: common setup in node initiate binary
// let network = Network::Sepolia;
// let private_key =
// hex::decode("018ef9563461ec2d88236d59039babf44c97d8bf6200d01d81170f1f60a78f32")?;
// let account_address =
// hex::decode("cdd51fbc4e008f4ef807eaf26f5043521ef5931bbb1e04032a25bd845d286b")?;
// let url = "https://starknet-sepolia.public.blastapi.io";

let node_account = NodeAccount::new(
private_key,
account_address,
network,
JsonRpcClient::new(HttpTransport::new(rpc_url)),
);

// Generate topic
let new_job_topic = gossipsub_ident_topic(network, Topic::NewJob);
let picked_job_topic = gossipsub_ident_topic(network, Topic::PickedJob);
let finished_job_topic = gossipsub_ident_topic(network, Topic::FinishedJob);

let (swarm_events_tx, swarm_events_rx) = mpsc::channel::<gossipsub::Event>(100);
let (job_witness_tx, job_witness_rx) = broadcast::channel::<JobWitness>(100);

let (new_job_topic_tx, new_job_topic_rx) = mpsc::channel::<Vec<u8>>(100);

SwarmRunner::new(
node_account.get_keypair(),
vec![new_job_topic.to_owned(), picked_job_topic, finished_job_topic],
vec![(new_job_topic.to_owned(), new_job_topic_rx)],
swarm_events_tx,
)?;

BehaviourHandler::new(job_witness_tx, swarm_events_rx);

let server = DelegatorGRPCServer::new(
node_account.get_signing_key().to_owned(),
new_job_topic_tx,
job_witness_rx,
);

let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter.set_serving::<DelegatorServiceServer<DelegatorGRPCServer>>().await;

Server::builder()
.add_service(health_service)
.add_service(DelegatorServiceServer::new(server))
.serve_with_shutdown("[::]:50051".parse().unwrap(), shutdown_signal())
.await?;

Ok(())
}
21 changes: 21 additions & 0 deletions crates/delegator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use thiserror::Error;
use zetina_common::job_witness::JobWitness;

pub mod behavior_handler;
pub mod cli;
pub mod swarm;
pub mod tonic;

pub use cli::run;

#[derive(Error, Debug)]
pub enum DelegatorError {
#[error("broadcast_send_error")]
BroadcastSendError(#[from] tokio::sync::broadcast::error::SendError<JobWitness>),

#[error("io")]
Io(#[from] std::io::Error),

#[error("serde")]
Serde(#[from] serde_json::Error),
}
86 changes: 1 addition & 85 deletions crates/delegator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,88 +1,4 @@
pub mod behavior_handler;
pub mod swarm;
pub mod tonic;

use ::tonic::transport::Server;
use behavior_handler::BehaviourHandler;
use libp2p::gossipsub;
use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient, Url};
use swarm::SwarmRunner;
use thiserror::Error;
use tokio::sync::{broadcast, mpsc};
use tonic::{proto::delegator_service_server::DelegatorServiceServer, DelegatorGRPCServer};
use tracing_subscriber::EnvFilter;
use zetina_common::{
graceful_shutdown::shutdown_signal,
job_witness::JobWitness,
network::Network,
node_account::NodeAccount,
topic::{gossipsub_ident_topic, Topic},
};

#[derive(Error, Debug)]
pub enum DelegatorError {
#[error("broadcast_send_error")]
BroadcastSendError(#[from] tokio::sync::broadcast::error::SendError<JobWitness>),

#[error("io")]
Io(#[from] std::io::Error),

#[error("serde")]
Serde(#[from] serde_json::Error),
}

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

// TODO: common setup in node initiate binary
let network = Network::Sepolia;
let private_key =
hex::decode("018ef9563461ec2d88236d59039babf44c97d8bf6200d01d81170f1f60a78f32")?;
let account_address =
hex::decode("cdd51fbc4e008f4ef807eaf26f5043521ef5931bbb1e04032a25bd845d286b")?;
let url = "https://starknet-sepolia.public.blastapi.io";

let node_account = NodeAccount::new(
private_key,
account_address,
network,
JsonRpcClient::new(HttpTransport::new(Url::parse(url)?)),
);

// Generate topic
let new_job_topic = gossipsub_ident_topic(network, Topic::NewJob);
let picked_job_topic = gossipsub_ident_topic(network, Topic::PickedJob);
let finished_job_topic = gossipsub_ident_topic(network, Topic::FinishedJob);

let (swarm_events_tx, swarm_events_rx) = mpsc::channel::<gossipsub::Event>(100);
let (job_witness_tx, job_witness_rx) = broadcast::channel::<JobWitness>(100);

let (new_job_topic_tx, new_job_topic_rx) = mpsc::channel::<Vec<u8>>(100);

SwarmRunner::new(
node_account.get_keypair(),
vec![new_job_topic.to_owned(), picked_job_topic, finished_job_topic],
vec![(new_job_topic.to_owned(), new_job_topic_rx)],
swarm_events_tx,
)?;

BehaviourHandler::new(job_witness_tx, swarm_events_rx);

let server = DelegatorGRPCServer::new(
node_account.get_signing_key().to_owned(),
new_job_topic_tx,
job_witness_rx,
);

let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter.set_serving::<DelegatorServiceServer<DelegatorGRPCServer>>().await;

Server::builder()
.add_service(health_service)
.add_service(DelegatorServiceServer::new(server))
.serve_with_shutdown("[::]:50051".parse().unwrap(), shutdown_signal())
.await?;

Ok(())
zetina_delegator::run().await
}
7 changes: 6 additions & 1 deletion crates/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ readme = "README.md"
repository.workspace = true
version.workspace = true

[[bin]]
name = "zetina-executor"
path = "src/main.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap.workspace = true
futures.workspace = true
hex.workspace = true
libp2p.workspace = true
Expand All @@ -32,4 +37,4 @@ thiserror.workspace = true
tonic-health.workspace = true

[build-dependencies]
tonic-build.workspace = true
tonic-build.workspace = true
Loading

0 comments on commit 9352098

Please sign in to comment.