Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Add P2P beacon mode (#47)
Browse files Browse the repository at this point in the history
P2P beacon mode allows for lightweight node instance to run, with only
purpose to coordinate other nodes' peer discovery. This is effectively
a special node in the network that bridges others.
  • Loading branch information
tuommaki authored Jan 22, 2024
1 parent 34f5e3a commit e4f0575
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
27 changes: 26 additions & 1 deletion crates/node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Config {
long,
long_help = "",
env = "GEVULOT_P2P_DISCOVERY_ADDR",
default_value = "bootstrap.p2p.devnet.gevulot.com"
default_value = "34.88.251.176:9999"
)]
pub p2p_discovery_addrs: Vec<String>,

Expand Down Expand Up @@ -136,6 +136,25 @@ pub enum PeerCommand {
},
}

#[derive(Debug, Args)]
pub struct P2PBeaconConfig {
#[arg(
long,
long_help = "P2P listen address",
env = "GEVULOT_P2P_LISTEN_ADDR",
default_value = "127.0.0.1:9999"
)]
pub p2p_listen_addr: SocketAddr,

#[arg(
long,
long_help = "P2P PSK passphrase",
env = "GEVULOT_PSK_PASSPHRASE",
default_value = "Pack my box with five dozen liquor jugs."
)]
pub p2p_psk_passphrase: String,
}

#[derive(Debug, Subcommand)]
pub enum GenerateCommand {
NodeKey {
Expand Down Expand Up @@ -184,6 +203,12 @@ pub enum Command {
op: PeerCommand,
},

/// P2PBeacon is a run mode where only P2P code is executed. Used for coordinating other nodes in the network.
P2PBeacon {
#[command(flatten)]
config: P2PBeaconConfig,
},

/// Run the node.
Run {
#[command(flatten)]
Expand Down
28 changes: 27 additions & 1 deletion crates/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use std::{
use asset_manager::AssetManager;
use async_trait::async_trait;
use clap::Parser;
use cli::{Cli, Command, Config, GenerateCommand, NodeKeyOptions, PeerCommand, ShowCommand};
use cli::{
Cli, Command, Config, GenerateCommand, NodeKeyOptions, P2PBeaconConfig, PeerCommand,
ShowCommand,
};
use eyre::Result;
use gevulot_node::types;
use libsecp256k1::{PublicKey, SecretKey};
Expand Down Expand Up @@ -87,6 +90,7 @@ async fn main() -> Result<()> {
db.acl_deny(&key).await
}
},
Command::P2PBeacon { config } => p2p_beacon(config).await,
Command::Run { config } => run(Arc::new(config)).await,
Command::Show { op } => match op {
ShowCommand::PublicKey { key_file } => {
Expand Down Expand Up @@ -297,6 +301,28 @@ async fn run(config: Arc<Config>) -> Result<()> {
}
}

/// p2p_beacon brings up P2P networking but nothing else. This function can be
/// used for independent P2P network beacon that provides connectivity for
/// others, while it doesn't participate in the Gevulot's operational side
/// in any other way - i.e. this won't handle transactions in any way.
async fn p2p_beacon(config: P2PBeaconConfig) -> Result<()> {
let p2p = Arc::new(
networking::P2P::new(
"gevulot-network",
config.p2p_listen_addr,
&config.p2p_psk_passphrase,
)
.await,
);

let p2p_addr = p2p.node().start_listening().await?;
tracing::info!("listening for p2p at {}", p2p_addr);

loop {
sleep(Duration::from_secs(1));
}
}

fn read_node_key(node_key_file: &PathBuf) -> Result<SecretKey> {
let bs = match std::fs::read(node_key_file) {
Ok(key_data) => key_data,
Expand Down

0 comments on commit e4f0575

Please sign in to comment.