Skip to content

Commit

Permalink
chore: network_id to network
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdud007 committed Apr 13, 2024
1 parent 0b059eb commit 9a3e35c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 31 deletions.
7 changes: 3 additions & 4 deletions crates/delegator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sharp_p2p_peer::network::{get_network_id, Network};
use sharp_p2p_peer::network::Network;
use sharp_p2p_peer::node::{Node, NodeConfig, NodeType};
use sharp_p2p_peer::store::Store;
use std::error::Error;
Expand All @@ -10,15 +10,14 @@ use tracing_subscriber::EnvFilter;
async fn main() -> Result<(), Box<dyn Error>> {
let _ = tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).try_init();

// 1. Config network arguments
// 1. Generate keypair for the node
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(),
Network::Sepolia,
p2p_local_keypair,
Vec::new(),
store,
Expand Down
15 changes: 4 additions & 11 deletions crates/executor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sharp_p2p_peer::network::{get_network_id, Network};
use sharp_p2p_peer::network::Network;
use sharp_p2p_peer::node::{Node, NodeConfig, NodeType};
use sharp_p2p_peer::store::Store;
use std::error::Error;
Expand All @@ -12,20 +12,13 @@ use tracing_subscriber::EnvFilter;
async fn main() -> Result<(), Box<dyn Error>> {
let _ = tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).try_init();

// 1. Config network arguments
// 1. Generate keypair for the node
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_config =
NodeConfig::new(NodeType::Executor, Network::Sepolia, p2p_local_keypair, Vec::new(), store);
let node = Node::new(node_config).await.unwrap();
println!("node: {:?}", node);

Expand Down
10 changes: 6 additions & 4 deletions crates/peer/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ pub enum Network {
Sepolia,
}

pub fn get_network_id(network: Network) -> &'static str {
match network {
Network::Mainnet => "mainnet",
Network::Sepolia => "sepolia",
impl Network {
pub fn as_str(&self) -> &'static str {
match self {
Network::Mainnet => "mainnet",
Network::Sepolia => "sepolia",
}
}
}
7 changes: 4 additions & 3 deletions crates/peer/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use libp2p::Multiaddr;
use std::error::Error;
use std::sync::Arc;

use crate::network::Network;
use crate::registry::RegistryHandler;
use crate::store::Store;
use crate::swarm::SwarmRunner;
Expand All @@ -15,7 +16,7 @@ pub enum NodeType {
pub struct NodeConfig {
pub node_type: NodeType,
/// An id of the network to connect to.
pub network_id: String,
pub network: Network,
/// 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.
Expand All @@ -27,12 +28,12 @@ pub struct NodeConfig {
impl NodeConfig {
pub fn new(
node_type: NodeType,
network_id: String,
network: Network,
p2p_local_keypair: Keypair,
p2p_listen_on: Vec<Multiaddr>,
store: Store,
) -> Self {
Self { node_type, network_id, p2p_local_keypair, p2p_listen_on, store }
Self { node_type, network, p2p_local_keypair, p2p_listen_on, store }
}
}

Expand Down
19 changes: 10 additions & 9 deletions crates/peer/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
use libp2p::{mdns, noise, tcp, yamux, Multiaddr, PeerId, Swarm, SwarmBuilder};
use tokio::io::{self, AsyncBufReadExt};

use crate::network::Network;
use crate::node::{NodeConfig, NodeType};

#[derive(NetworkBehaviour)]
Expand All @@ -29,16 +30,16 @@ impl Topic {
}
}

pub(crate) fn gossipsub_ident_topic(network: &str, topic: Topic) -> IdentTopic {
let network = network.trim_matches('/');
let topic = topic.as_str().trim_matches('/');
pub(crate) fn gossipsub_ident_topic(network: Network, topic: Topic) -> IdentTopic {
let network = network.as_str();
let topic = topic.as_str();
let s = format!("/{network}/{topic}");
IdentTopic::new(s)
}

pub struct SwarmRunner {
swarm: Swarm<PeerBehaviour>,
network_id: String,
network: Network,
}

impl SwarmRunner {
Expand All @@ -61,16 +62,16 @@ impl SwarmRunner {
swarm.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

Ok(SwarmRunner { swarm, network_id: node_config.network_id.clone() })
Ok(SwarmRunner { swarm, network: node_config.network })
}

pub async fn run(&mut self, node_type: NodeType) {
// Read full lines from stdin
let mut stdin = io::BufReader::new(io::stdin()).lines();

let publish_topic = match node_type {
NodeType::Delegator => gossipsub_ident_topic(&self.network_id, Topic::NewJob),
NodeType::Executor => gossipsub_ident_topic(&self.network_id, Topic::PickedJob),
NodeType::Delegator => gossipsub_ident_topic(self.network, Topic::NewJob),
NodeType::Executor => gossipsub_ident_topic(self.network, Topic::PickedJob),
};

loop {
Expand Down Expand Up @@ -126,9 +127,9 @@ fn init_gossip(node_config: &NodeConfig) -> Result<gossipsub::Behaviour, Box<dyn
gossipsub::Behaviour::new(message_authenticity, config).unwrap();

// `new-job` is the topic about new job to be proven
let new_job_topic = gossipsub_ident_topic(&node_config.network_id, Topic::NewJob);
let new_job_topic = gossipsub_ident_topic(node_config.network, Topic::NewJob);
// `picked-job` is the topic about picked job to processing prover
let picked_job_topic = gossipsub_ident_topic(&node_config.network_id, Topic::PickedJob);
let picked_job_topic = gossipsub_ident_topic(node_config.network, Topic::PickedJob);

match node_config.node_type {
NodeType::Delegator => {
Expand Down

0 comments on commit 9a3e35c

Please sign in to comment.