Skip to content

Commit

Permalink
Merge pull request #79 from ourzora/relay-and-dht-improvements
Browse files Browse the repository at this point in the history
Relay and dht improvements
  • Loading branch information
ligustah authored May 1, 2024
2 parents 236ba57 + 7de0b93 commit 9cc2f6c
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 119 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ libp2p = { version = "0.53.2", features = [
"quic",
"relay",
"autonat",
"dcutr"
"dcutr",
"serde",
] }
libp2p-relay-manager = "0.2.2"
tokio = { version = "1.36.0", features = ["full"] }
Expand Down
65 changes: 52 additions & 13 deletions src/api/routes.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use std::path::Display;

use axum::extract::{Path, Query, State};
use axum::http::StatusCode;
use axum::Json;
use const_hex::ToHexExt;
use itertools::Itertools;
use libp2p::autonat::NatStatus;
use serde::Serialize;
use sqlx::{Executor, Row};

use crate::api::AppState;
use crate::controller::ControllerCommands;
use crate::p2p::NetworkState;
use crate::rules::Results;
use crate::storage;
use crate::storage::{get_for_id_and_kind, QueryOptions};
use crate::types::{PremintName, PremintTypes};
use axum::extract::{Path, Query, State};
use axum::http::StatusCode;
use axum::Json;
use serde::Serialize;
use sqlx::{Executor, Row};

pub async fn list_all(
State(state): State<AppState>,
Expand Down Expand Up @@ -185,7 +191,23 @@ pub struct NodeInfoResponse {
pub num_peers: u64,
pub dht_peers: Vec<Vec<String>>,
pub gossipsub_peers: Vec<String>,
pub all_external_addresses: Vec<Vec<String>>,
pub external_addresses: Vec<String>,
pub providing: Vec<String>,
pub listeners: Vec<String>,
pub nat_status: String,
}

trait StringOrHex {
fn to_string(&self) -> String;
}

impl StringOrHex for Vec<u8> {
fn to_string(&self) -> String {
match String::from_utf8(self.clone()) {
Ok(value) => value,
Err(_) => self.encode_hex(),
}
}
}

impl From<NetworkState> for NodeInfoResponse {
Expand All @@ -195,24 +217,41 @@ impl From<NetworkState> for NodeInfoResponse {
network_info,
dht_peers,
gossipsub_peers,
all_external_addresses,
external_addresses,
listeners,
providing,
nat_status,
..
} = state;

let dht_peers = dht_peers
.into_iter()
.map(|peer| peer.iter().map(|p| p.to_string()).collect())
.map(|peer| peer.iter().map(ToString::to_string).collect())
.collect();
let gossipsub_peers = gossipsub_peers.into_iter().map(|p| p.to_string()).collect();
let all_external_addresses = all_external_addresses
.into_iter()
.map(|peer| peer.into_iter().map(|p| p.to_string()).collect())
let gossipsub_peers = gossipsub_peers.iter().map(ToString::to_string).collect();
let external_addresses = external_addresses.iter().map(ToString::to_string).collect();
let providing = providing
.iter()
.take(100)
.map(|record| record.key.to_vec().to_string())
.collect();
let listeners = listeners.iter().map(ToString::to_string).collect();
let nat_status = match nat_status {
NatStatus::Private => "Private",
NatStatus::Unknown => "Unknown",
NatStatus::Public(..) => "Public",
}
.to_string();

Self {
local_peer_id: local_peer_id.to_string(),
num_peers: network_info.num_peers() as u64,
dht_peers,
gossipsub_peers,
all_external_addresses,
external_addresses,
providing,
listeners,
nat_status,
}
}
}
50 changes: 8 additions & 42 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ pub struct Config {

#[envconfig(from = "SYNC_LOOKBACK_HOURS", default = "6")]
pub sync_lookback_hours: u64,

#[envconfig(from = "ENABLE_RELAY", default = "false")]
pub enable_relay: bool,
}

impl Config {
Expand All @@ -104,6 +107,7 @@ impl Config {
rate_limit_rps: 1,
boot_nodes: BootNodes::None,
sync_lookback_hours: 6,
enable_relay: false,
}
}
}
Expand Down Expand Up @@ -198,7 +202,7 @@ pub fn init() -> Config {

#[cfg(test)]
mod test {
use crate::config::{BootNodes, ChainInclusionMode};
use crate::config::{BootNodes, ChainInclusionMode, Config};
use std::env;
use std::str::FromStr;

Expand All @@ -210,28 +214,9 @@ mod test {

#[test]
fn test_premint_names() {
let config = super::Config {
secret: "0x7948efac1e9dbfb77691541df857b3142ea88f5b75b37dfca506f1f1c5d659ee"
.to_string(),
peer_port: 7777,
connect_external: false,
db_url: None,
persist_state: false,
prune_minted_premints: false,
api_port: 0,
peer_limit: 1000,
let config = Config {
supported_premint_types: "simple,zora_premint_v2".to_string(),
chain_inclusion_mode: ChainInclusionMode::Check,
supported_chain_ids: "7777777".to_string(),
trusted_peers: None,
node_id: None,
external_address: None,
interactive: false,
enable_rpc: true,
admin_api_secret: None,
rate_limit_rps: 1,
boot_nodes: BootNodes::Chain,
sync_lookback_hours: 0,
..Config::test_default()
};

let names = config.premint_names();
Expand All @@ -240,27 +225,8 @@ mod test {
assert_eq!(names[1].0, "zora_premint_v2");

let config = super::Config {
secret: "0x7948efac1e9dbfb77691541df857b3142ea88f5b75b37dfca506f1f1c5d659ee"
.to_string(),
peer_port: 7777,
connect_external: false,
db_url: None,
persist_state: false,
prune_minted_premints: false,
api_port: 0,
peer_limit: 1000,
supported_premint_types: "zora_premint_v2".to_string(),
chain_inclusion_mode: ChainInclusionMode::Check,
supported_chain_ids: "7777777".to_string(),
trusted_peers: None,
node_id: None,
external_address: None,
interactive: false,
enable_rpc: true,
admin_api_secret: None,
rate_limit_rps: 1,
boot_nodes: BootNodes::None,
sync_lookback_hours: 0,
..Config::test_default()
};

let names = config.premint_names();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod chain_list;
pub mod config;
pub mod controller;
pub mod metrics;
pub mod multiaddr_ext;
pub mod p2p;
pub mod premints;
pub mod rules;
Expand Down
26 changes: 26 additions & 0 deletions src/multiaddr_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use libp2p::multiaddr::Protocol;
use libp2p::PeerId;

pub trait MultiaddrExt {
// get the last peer id from this address
fn peer_id(&self) -> Option<PeerId>;

fn is_relayed(&self) -> bool;
}

impl MultiaddrExt for libp2p::Multiaddr {
fn peer_id(&self) -> Option<PeerId> {
let mut last = None;
self.iter().for_each(|component| {
if let Protocol::P2p(key) = component {
last = Some(key.clone());
}
});

last
}

fn is_relayed(&self) -> bool {
self.iter().any(|addr| matches!(addr, Protocol::P2pCircuit))
}
}
Loading

0 comments on commit 9cc2f6c

Please sign in to comment.