Skip to content

Commit

Permalink
init: basic gossip peer with node impl
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdud007 committed Apr 13, 2024
1 parent 5b84746 commit 10fc769
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 15 deletions.
25 changes: 21 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ overflow-checks = true

[workspace]
resolver = "2"
members = [ "crates/common", "crates/delegator", "crates/executor", "crates/peer" , "crates/prover"]
members = [
"crates/common",
"crates/delegator",
"crates/executor",
"crates/peer",
"crates/prover",
]
exclude = []

[workspace.package]
Expand All @@ -15,11 +21,21 @@ license-file = "LICENSE"
[workspace.dependencies]
async-process = "2.2.0"
cairo-felt = "0.9.1"
cairo-proof-parser = { git = "https://github.com/Okm165/cairo-proof-parser", rev = "97a04bbee07330311b38d6f4cecfed3acb237626"}
cairo-proof-parser = { git = "https://github.com/Okm165/cairo-proof-parser", rev = "97a04bbee07330311b38d6f4cecfed3acb237626" }
futures = "0.3.30"
hex = "0.4.3"
itertools = "0.12.1"
libp2p = { version = "0.53.2", features = [ "tokio", "gossipsub", "kad", "mdns", "noise", "macros", "tcp", "yamux", "quic"] }
libp2p = { version = "0.53.2", features = [
"tokio",
"gossipsub",
"kad",
"mdns",
"noise",
"macros",
"tcp",
"yamux",
"quic",
] }
num-bigint = "0.4.4"
serde = "1.0.197"
serde_json = "1.0.115"
Expand All @@ -32,4 +48,5 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
sharp-p2p-common = { path = "crates/common" }
sharp-p2p-delegator = { path = "crates/delegator" }
sharp-p2p-executor = { path = "crates/executor" }
sharp-p2p-prover = { path = "crates/prover" }
sharp-p2p-prover = { path = "crates/prover" }
sharp-p2p-peer = { path = "crates/peer" }
4 changes: 3 additions & 1 deletion crates/delegator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ license-file.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libp2p.workspace = true
tokio.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
tracing.workspace = true
sharp-p2p-peer.workspace = true
33 changes: 31 additions & 2 deletions crates/delegator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
fn main() {
println!("Hello, world!");
use sharp_p2p_peer::network::{get_network_id, Network};
use sharp_p2p_peer::node::{Node, NodeConfig, NodeType};
use sharp_p2p_peer::store::Store;
use std::error::Error;
use std::time::Duration;
use tokio::time::sleep;
use tracing_subscriber::EnvFilter;

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

// 1. Config network arguments
let p2p_local_keypair = libp2p::identity::Keypair::generate_ed25519();
let network_id = get_network_id(Network::Sepolia);

// 2. Initiate a new node to sync with other peers
let store = Store::new();
let node_config = NodeConfig::new(
NodeType::Delegator,
network_id.to_string(),
p2p_local_keypair,
Vec::new(),
store,
);
let node = Node::new(node_config).await.unwrap();
println!("node: {:?}", node);

loop {
sleep(Duration::from_secs(1)).await;
}
}
4 changes: 3 additions & 1 deletion crates/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ license-file.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libp2p.workspace = true
tokio.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
tracing.workspace = true
sharp-p2p-peer.workspace = true
1 change: 1 addition & 0 deletions crates/executor/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Executor
36 changes: 34 additions & 2 deletions crates/executor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
fn main() {
println!("Hello, world!");
use sharp_p2p_peer::network::{get_network_id, Network};
use sharp_p2p_peer::node::{Node, NodeConfig, NodeType};
use sharp_p2p_peer::store::Store;
use std::error::Error;

use std::time::Duration;
use tokio::time::sleep;

use tracing_subscriber::EnvFilter;

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

// 1. Config network arguments
let p2p_local_keypair = libp2p::identity::Keypair::generate_ed25519();
let network_id = get_network_id(Network::Sepolia);

// 2. Initiate a new node to sync with other peers

let store = Store::new();
let node_config = NodeConfig::new(
NodeType::Executor,
network_id.to_string(),
p2p_local_keypair,
Vec::new(),
store,
);
let node = Node::new(node_config).await.unwrap();
println!("node: {:?}", node);

loop {
sleep(Duration::from_secs(1)).await;
}
}
5 changes: 3 additions & 2 deletions crates/peer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "peer"
name = "sharp-p2p-peer"
version.workspace = true
edition.workspace = true
repository.workspace = true
Expand All @@ -11,4 +11,5 @@ license-file.workspace = true
libp2p.workspace = true
tokio.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
tracing.workspace = true
sharp-p2p-common.workspace = true
4 changes: 4 additions & 0 deletions crates/peer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod network;
pub mod node;
pub mod store;
pub mod swarm;
3 changes: 0 additions & 3 deletions crates/peer/src/main.rs

This file was deleted.

14 changes: 14 additions & 0 deletions crates/peer/src/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Network {
/// Starknet mainnet.
Mainnet,
/// Sepolia testnet.
Sepolia,
}

pub fn get_network_id(network: Network) -> &'static str {
match network {
Network::Mainnet => "mainnet",
Network::Sepolia => "sepolia",
}
}
54 changes: 54 additions & 0 deletions crates/peer/src/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use libp2p::identity::Keypair;
use libp2p::Multiaddr;
use std::error::Error;
use std::sync::Arc;
use tokio::spawn;

use crate::store::Store;
use crate::swarm::SwarmRunner;

pub enum NodeType {
Delegator,
Executor,
}

pub struct NodeConfig {
pub node_type: NodeType,
/// An id of the network to connect to.
pub network_id: String,
/// The keypair to be used as [`Node`]s identity.
pub p2p_local_keypair: Keypair,
/// List of the addresses where [`Node`] will listen for incoming connections.
pub p2p_listen_on: Vec<Multiaddr>,
/// The store for job record.
pub store: Store,
}

impl NodeConfig {
pub fn new(
node_type: NodeType,
network_id: String,
p2p_local_keypair: Keypair,
p2p_listen_on: Vec<Multiaddr>,
store: Store,
) -> Self {
Self { node_type, network_id, p2p_local_keypair, p2p_listen_on, store }
}
}

#[derive(Debug)]
pub struct Node {
pub store: Arc<Store>,
}

impl Node {
pub async fn new(node_config: NodeConfig) -> Result<Self, Box<dyn Error>> {
let mut swarm_runner = SwarmRunner::new(&node_config)?;
spawn(async move {
swarm_runner.run(node_config.node_type).await;
});
let store = Arc::new(node_config.store);

Ok(Self { store })
}
}
22 changes: 22 additions & 0 deletions crates/peer/src/store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use sharp_p2p_common::job::Job;

use std::collections::VecDeque;

#[derive(Debug)]
pub struct Store {
/// For delegator, FIFO queue to publish message
/// For executor, FIFO queue to prove job
pub job_queue: VecDeque<Job>,
}

impl Store {
pub fn new() -> Self {
Self { job_queue: VecDeque::new() }
}
}

impl Default for Store {
fn default() -> Self {
Self::new()
}
}
Loading

0 comments on commit 10fc769

Please sign in to comment.