Skip to content

Commit

Permalink
Merge branch 'feature/dns-address'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed May 20, 2024
2 parents d1f2c5a + 4c4a9ee commit 37f51f2
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 86 deletions.
8 changes: 4 additions & 4 deletions cli/src/node/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::net::Ipv4Addr;
use std::net::{IpAddr, Ipv4Addr};
use std::path::Path;

use anyhow::Result;
Expand Down Expand Up @@ -32,12 +32,12 @@ pub struct NodeConfig {
/// Public IP address of the node.
///
/// Default: resolved automatically.
pub public_ip: Option<Ipv4Addr>,
pub public_ip: Option<IpAddr>,

/// Ip address to listen on.
///
/// Default: 0.0.0.0
pub local_ip: Ipv4Addr,
pub local_ip: IpAddr,

/// Default: 30000.
pub port: u16,
Expand All @@ -63,7 +63,7 @@ impl Default for NodeConfig {
fn default() -> Self {
Self {
public_ip: None,
local_ip: Ipv4Addr::UNSPECIFIED,
local_ip: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
port: 30000,
network: NetworkConfig::default(),
dht: DhtConfig::default(),
Expand Down
6 changes: 3 additions & 3 deletions cli/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::net::{Ipv4Addr, SocketAddr};
use std::net::{IpAddr, SocketAddr};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -218,11 +218,11 @@ fn init_logger(logger_config: Option<PathBuf>) -> Result<()> {
Ok(())
}

async fn resolve_public_ip(ip: Option<Ipv4Addr>) -> Result<Ipv4Addr> {
async fn resolve_public_ip(ip: Option<IpAddr>) -> Result<IpAddr> {
match ip {
Some(address) => Ok(address),
None => match public_ip::addr_v4().await {
Some(address) => Ok(address),
Some(address) => Ok(IpAddr::V4(address)),
None => anyhow::bail!("failed to resolve public IP address"),
},
}
Expand Down
10 changes: 5 additions & 5 deletions consensus/examples/consensus_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::{fmt, EnvFilter, Layer};
use tycho_consensus::test_utils::drain_anchors;
use tycho_consensus::{Engine, InputBufferStub};
use tycho_network::{DhtConfig, NetworkConfig, PeerId, PeerInfo};
use tycho_network::{Address, DhtConfig, NetworkConfig, PeerId, PeerInfo};
use tycho_util::time::now_sec;

#[tokio::main]
Expand Down Expand Up @@ -179,8 +179,9 @@ impl CmdGenKey {
/// generate a dht node info
#[derive(Parser)]
struct CmdGenDht {
/// local node address
addr: SocketAddr,
/// a list of node addresses
#[clap(required = true)]
addr: Vec<Address>,

/// node secret key
#[clap(long)]
Expand All @@ -195,8 +196,7 @@ impl CmdGenDht {
fn run(self) -> Result<()> {
let secret_key = parse_key(&self.key)?;
let key_pair = KeyPair::from(&secret_key);
let entry =
tycho_consensus::test_utils::make_peer_info(&key_pair, self.addr.into(), self.ttl);
let entry = tycho_consensus::test_utils::make_peer_info(&key_pair, self.addr, self.ttl);
let output = if std::io::stdin().is_terminal() {
serde_json::to_string_pretty(&entry)
} else {
Expand Down
8 changes: 5 additions & 3 deletions consensus/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ pub fn genesis() -> Arc<Point> {
})
}

pub fn make_peer_info(keypair: &KeyPair, address: Address, ttl: Option<u32>) -> PeerInfo {
pub fn make_peer_info(keypair: &KeyPair, address_list: Vec<Address>, ttl: Option<u32>) -> PeerInfo {
let peer_id = PeerId::from(keypair.public_key);

let now = now_sec();
let mut peer_info = PeerInfo {
id: peer_id,
address_list: vec![address.clone()].into_boxed_slice(),
address_list: address_list.into_boxed_slice(),
created_at: now,
expires_at: ttl.unwrap_or(u32::MAX),
signature: Box::new([0; 64]),
Expand Down Expand Up @@ -136,7 +136,9 @@ mod tests {
let peer_info = keys
.iter()
.zip(addresses.iter())
.map(|((_, key_pair), addr)| Arc::new(make_peer_info(key_pair, addr.clone(), None)))
.map(|((_, key_pair), addr)| {
Arc::new(make_peer_info(key_pair, vec![addr.clone()], None))
})
.collect::<Vec<_>>();

let mut handles = vec![];
Expand Down
15 changes: 10 additions & 5 deletions network/examples/network_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ impl CmdGenKey {
/// generate a dht node info
#[derive(Parser)]
struct CmdGenDht {
/// local node address
addr: SocketAddr,
/// a list of node addresses
#[clap(required = true)]
addr: Vec<Address>,

/// node secret key
#[clap(long)]
Expand All @@ -161,7 +162,7 @@ struct CmdGenDht {

impl CmdGenDht {
fn run(self) -> Result<()> {
let entry = Node::make_peer_info(parse_key(&self.key)?, self.addr.into(), self.ttl);
let entry = Node::make_peer_info(parse_key(&self.key)?, self.addr, self.ttl);
let output = if std::io::stdin().is_terminal() {
serde_json::to_string_pretty(&entry)
} else {
Expand Down Expand Up @@ -235,14 +236,18 @@ impl Node {
Ok(Self { network, dht })
}

fn make_peer_info(key: ed25519::SecretKey, address: Address, ttl: Option<u32>) -> PeerInfo {
fn make_peer_info(
key: ed25519::SecretKey,
address_list: Vec<Address>,
ttl: Option<u32>,
) -> PeerInfo {
let keypair = ed25519::KeyPair::from(&key);
let peer_id = PeerId::from(keypair.public_key);

let now = now_sec();
let mut node_info = PeerInfo {
id: peer_id,
address_list: vec![address].into_boxed_slice(),
address_list: address_list.into_boxed_slice(),
created_at: now,
expires_at: ttl.unwrap_or(u32::MAX),
signature: Box::new([0; 64]),
Expand Down
14 changes: 6 additions & 8 deletions network/src/network/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,15 @@ impl ConnectionManager {
fn dial_peer(&mut self, address: Address, peer_id: &PeerId, callback: CallbackTx) {
async fn dial_peer_task(
seqno: u32,
connecting: Result<Connecting>,
endpoint: Arc<Endpoint>,
address: Address,
peer_id: PeerId,
config: Arc<NetworkConfig>,
) -> ConnectingOutput {
let fut = async {
let connection = ConnectionClosedOnDrop::new(connecting?.await?);
let address = address.resolve().await?;
let connecting = endpoint.connect_with_expected_id(&address, &peer_id)?;
let connection = ConnectionClosedOnDrop::new(connecting.await?);
handshake(&connection).await?;
Ok(connection)
};
Expand Down Expand Up @@ -580,14 +582,10 @@ impl ConnectionManager {
};

if let Some(entry) = entry {
let target_address = address.clone();
let connecting = self
.endpoint
.connect_with_expected_id(address.clone(), peer_id);
entry.abort_handle = Some(self.pending_connections.spawn(dial_peer_task(
entry.last_seqno,
connecting,
target_address,
self.endpoint.clone(),
address.clone(),
*peer_id,
self.config.clone(),
)));
Expand Down
10 changes: 4 additions & 6 deletions network/src/network/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anyhow::Result;

use crate::network::config::EndpointConfig;
use crate::network::connection::{parse_peer_identity, Connection};
use crate::types::{Address, Direction, PeerId};
use crate::types::{Direction, PeerId};

pub(crate) struct Endpoint {
inner: quinn::Endpoint,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl Endpoint {
/// Connect to a remote endpoint expecting it to have the provided peer id.
pub fn connect_with_expected_id(
&self,
address: Address,
address: &SocketAddr,
peer_id: &PeerId,
) -> Result<Connecting> {
let config = self.config.make_client_config_for_peer_id(peer_id)?;
Expand All @@ -86,12 +86,10 @@ impl Endpoint {
fn connect_with_client_config(
&self,
config: quinn::ClientConfig,
address: Address,
address: &SocketAddr,
) -> Result<Connecting> {
let address = address.resolve()?;

self.inner
.connect_with(config, address, &self.config.service_name)
.connect_with(config, *address, &self.config.service_name)
.map_err(Into::into)
.map(Connecting::new_outbound)
}
Expand Down
3 changes: 2 additions & 1 deletion network/src/proto.tl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ transport.peerId key:int256 = transport.PeerId;

transport.address.ipv4 ip:int port:int = transport.Address;
transport.address.ipv6 ip:int128 port:int = transport.Address;
transport.address.dns hostname:bytes post:int = transport.Address;

// DHT
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -209,4 +210,4 @@ overlay.exchangeRandomPublicEntries
*
* @param overlay_id overlay id
*/
overlay.prefix overlay_id:int256 = True;
overlay.prefix overlay_id:int256 = True;
Loading

0 comments on commit 37f51f2

Please sign in to comment.