From 6742638c81f8bda2ba3e3ab72ec520ab22b314e2 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sat, 12 Dec 2020 21:14:15 +0100 Subject: [PATCH] Replace pk,pubkey,PublicKey by id,NodeID,etc --- examples/basalt.rs | 19 ++++--- examples/fullmesh.rs | 3 +- src/conn.rs | 33 ++++++------ src/lib.rs | 1 + src/netapp.rs | 116 ++++++++++++++++++---------------------- src/peering/basalt.rs | 64 ++++++++++------------ src/peering/fullmesh.rs | 92 ++++++++++++++----------------- src/util.rs | 2 + 8 files changed, 150 insertions(+), 180 deletions(-) diff --git a/examples/basalt.rs b/examples/basalt.rs index 4ea4f71..4d7887d 100644 --- a/examples/basalt.rs +++ b/examples/basalt.rs @@ -15,6 +15,7 @@ use netapp::message::*; use netapp::peering::basalt::*; use netapp::proto::*; use netapp::NetApp; +use netapp::NodeID; #[derive(StructOpt, Debug)] #[structopt(name = "netapp")] @@ -91,7 +92,7 @@ async fn main() { for peer in opt.bootstrap_peers.iter() { if let Some(delim) = peer.find('@') { let (key, ip) = peer.split_at(delim); - let pubkey = ed25519::PublicKey::from_slice(&hex::decode(&key).unwrap()).unwrap(); + let pubkey = NodeID::from_slice(&hex::decode(&key).unwrap()).unwrap(); let ip = ip[1..].parse::().unwrap(); bootstrap_peers.push((pubkey, ip)); } @@ -106,16 +107,14 @@ async fn main() { }; let peering = Basalt::new(netapp.clone(), bootstrap_peers, basalt_params); - netapp.add_msg_handler::( - |_from: ed25519::PublicKey, msg: ExampleMessage| { - debug!("Got example message: {:?}, sending example response", msg); - async { - ExampleResponse { - example_field: false, - } + netapp.add_msg_handler::(|_from: NodeID, msg: ExampleMessage| { + debug!("Got example message: {:?}, sending example response", msg); + async { + ExampleResponse { + example_field: false, } - }, - ); + } + }); let listen_addr = opt.listen_addr.parse().unwrap(); let public_addr = opt.public_addr.map(|x| x.parse().unwrap()); diff --git a/examples/fullmesh.rs b/examples/fullmesh.rs index eb954b7..acc0a7b 100644 --- a/examples/fullmesh.rs +++ b/examples/fullmesh.rs @@ -10,6 +10,7 @@ use sodiumoxide::crypto::sign::ed25519; use netapp::peering::fullmesh::*; use netapp::NetApp; +use netapp::NodeID; #[derive(StructOpt, Debug)] #[structopt(name = "netapp")] @@ -71,7 +72,7 @@ async fn main() { for peer in opt.bootstrap_peers.iter() { if let Some(delim) = peer.find('@') { let (key, ip) = peer.split_at(delim); - let pubkey = ed25519::PublicKey::from_slice(&hex::decode(&key).unwrap()).unwrap(); + let pubkey = NodeID::from_slice(&hex::decode(&key).unwrap()).unwrap(); let ip = ip[1..].parse::().unwrap(); bootstrap_peers.push((pubkey, ip)); } diff --git a/src/conn.rs b/src/conn.rs index e965fc9..a4c4f4e 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -6,7 +6,6 @@ use std::sync::{Arc, Mutex}; use bytes::Bytes; use log::{debug, error, trace}; -use sodiumoxide::crypto::sign::ed25519; use tokio::io::split; use tokio::net::TcpStream; use tokio::sync::{mpsc, oneshot, watch}; @@ -26,7 +25,7 @@ use crate::util::*; pub(crate) struct ServerConn { pub(crate) remote_addr: SocketAddr, - pub(crate) peer_pk: ed25519::PublicKey, + pub(crate) peer_id: NodeID, netapp: Arc, @@ -40,18 +39,18 @@ impl ServerConn { let handshake = handshake_server( &mut asyncstd_socket, netapp.netid.clone(), - netapp.pubkey.clone(), + netapp.id.clone(), netapp.privkey.clone(), ) .await?; - let peer_pk = handshake.peer_pk.clone(); + let peer_id = handshake.peer_pk.clone(); let tokio_socket = asyncstd_socket.into_inner(); let remote_addr = tokio_socket.peer_addr().unwrap(); debug!( "Handshake complete (server) with {}@{}", - hex::encode(&peer_pk), + hex::encode(&peer_id), remote_addr ); @@ -70,12 +69,12 @@ impl ServerConn { let conn = Arc::new(ServerConn { netapp: netapp.clone(), remote_addr, - peer_pk: peer_pk.clone(), + peer_id: peer_id.clone(), resp_send, close_send, }); - netapp.connected_as_server(peer_pk.clone(), conn.clone()); + netapp.connected_as_server(peer_id.clone(), conn.clone()); let conn2 = conn.clone(); let conn3 = conn.clone(); @@ -97,7 +96,7 @@ impl ServerConn { .map(|_| ()) .log_err("ServerConn recv_loop/send_loop"); - netapp.disconnected_as_server(&peer_pk, conn); + netapp.disconnected_as_server(&peer_id, conn); Ok(()) } @@ -124,7 +123,7 @@ impl RecvLoop for ServerConn { if let Some(handler) = self.netapp.msg_handlers.load().get(&kind) { let net_handler = &handler.net_handler; - let resp = net_handler(self.peer_pk.clone(), bytes.slice(5..)).await; + let resp = net_handler(self.peer_id.clone(), bytes.slice(5..)).await; self.resp_send .send(Some((id, prio, resp))) .log_err("ServerConn recv_handler send resp"); @@ -133,7 +132,7 @@ impl RecvLoop for ServerConn { } pub(crate) struct ClientConn { pub(crate) remote_addr: SocketAddr, - pub(crate) peer_pk: ed25519::PublicKey, + pub(crate) peer_id: NodeID, query_send: mpsc::UnboundedSender)>>, @@ -147,16 +146,16 @@ impl ClientConn { pub(crate) async fn init( netapp: Arc, socket: TcpStream, - remote_pk: ed25519::PublicKey, + peer_id: NodeID, ) -> Result<(), Error> { let mut asyncstd_socket = TokioCompatExt::wrap(socket); let handshake = handshake_client( &mut asyncstd_socket, netapp.netid.clone(), - netapp.pubkey.clone(), + netapp.id.clone(), netapp.privkey.clone(), - remote_pk.clone(), + peer_id.clone(), ) .await?; @@ -165,7 +164,7 @@ impl ClientConn { debug!( "Handshake complete (client) with {}@{}", - hex::encode(&remote_pk), + hex::encode(&peer_id), remote_addr ); @@ -183,7 +182,7 @@ impl ClientConn { let conn = Arc::new(ClientConn { remote_addr, - peer_pk: remote_pk.clone(), + peer_id: peer_id.clone(), next_query_number: AtomicU16::from(0u16), query_send, inflight: Mutex::new(HashMap::new()), @@ -191,7 +190,7 @@ impl ClientConn { stop_recv_loop, }); - netapp.connected_as_client(remote_pk.clone(), conn.clone()); + netapp.connected_as_client(peer_id.clone(), conn.clone()); tokio::spawn(async move { let conn2 = conn.clone(); @@ -205,7 +204,7 @@ impl ClientConn { .map(|_| ()) .log_err("ClientConn send_loop/recv_loop/dispatch_loop"); - netapp.disconnected_as_client(&remote_pk, conn); + netapp.disconnected_as_client(&peer_id, conn); }); Ok(()) diff --git a/src/lib.rs b/src/lib.rs index af8fbb8..6987b54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,3 +27,4 @@ pub mod netapp; pub mod peering; pub use netapp::*; +pub use util::NodeID; diff --git a/src/netapp.rs b/src/netapp.rs index a34b0d6..9733fb7 100644 --- a/src/netapp.rs +++ b/src/netapp.rs @@ -28,9 +28,7 @@ pub(crate) struct Handler { pub(crate) local_handler: Box Pin + Sync + Send>> + Sync + Send>, pub(crate) net_handler: Box< - dyn Fn(ed25519::PublicKey, Bytes) -> Pin> + Sync + Send>> - + Sync - + Send, + dyn Fn(NodeID, Bytes) -> Pin> + Sync + Send>> + Sync + Send, >, } @@ -49,17 +47,19 @@ pub(crate) struct Handler { pub struct NetApp { listen_params: ArcSwapOption, + /// Network secret key pub netid: auth::Key, - pub pubkey: ed25519::PublicKey, + /// Our peer ID + pub id: NodeID, + /// Private key associated with our peer ID pub privkey: ed25519::SecretKey, - server_conns: RwLock>>, - client_conns: RwLock>>, + server_conns: RwLock>>, + client_conns: RwLock>>, pub(crate) msg_handlers: ArcSwap>>, - on_connected_handler: - ArcSwapOption>, - on_disconnected_handler: ArcSwapOption>, + on_connected_handler: ArcSwapOption>, + on_disconnected_handler: ArcSwapOption>, } struct ListenParams { @@ -67,14 +67,10 @@ struct ListenParams { public_addr: Option, } -async fn net_handler_aux( - handler: Arc, - remote: ed25519::PublicKey, - bytes: Bytes, -) -> Vec +async fn net_handler_aux(handler: Arc, remote: NodeID, bytes: Bytes) -> Vec where M: Message + 'static, - F: Fn(ed25519::PublicKey, M) -> R + Send + Sync + 'static, + F: Fn(NodeID, M) -> R + Send + Sync + 'static, R: Future::Response> + Send + Sync, { debug!( @@ -97,14 +93,10 @@ where rmp_to_vec_all_named(&res).unwrap_or(vec![]) } -async fn local_handler_aux( - handler: Arc, - remote: ed25519::PublicKey, - msg: DynMsg, -) -> DynMsg +async fn local_handler_aux(handler: Arc, remote: NodeID, msg: DynMsg) -> DynMsg where M: Message + 'static, - F: Fn(ed25519::PublicKey, M) -> R + Send + Sync + 'static, + F: Fn(NodeID, M) -> R + Send + Sync + 'static, R: Future::Response> + Send + Sync, { debug!("Handling message of kind {:08x} from ourself", M::KIND); @@ -117,12 +109,14 @@ impl NetApp { /// Creates a new instance of NetApp, which can serve either as a full p2p node, /// or just as a passive client. To upgrade to a full p2p node, spawn a listener /// using `.listen()` + /// + /// Our Peer ID is the public key associated to the secret key given here. pub fn new(netid: auth::Key, privkey: ed25519::SecretKey) -> Arc { - let pubkey = privkey.public_key(); + let id = privkey.public_key(); let netapp = Arc::new(Self { listen_params: ArcSwapOption::new(None), netid, - pubkey, + id, privkey, server_conns: RwLock::new(HashMap::new()), client_conns: RwLock::new(HashMap::new()), @@ -132,12 +126,10 @@ impl NetApp { }); let netapp2 = netapp.clone(); - netapp.add_msg_handler::( - move |from: ed25519::PublicKey, msg: HelloMessage| { - netapp2.handle_hello_message(from, msg); - async { () } - }, - ); + netapp.add_msg_handler::(move |from: NodeID, msg: HelloMessage| { + netapp2.handle_hello_message(from, msg); + async { () } + }); netapp } @@ -147,7 +139,7 @@ impl NetApp { /// as the peering strategy will need to set this itself. pub fn on_connected(&self, handler: F) where - F: Fn(ed25519::PublicKey, SocketAddr, bool) + Sized + Send + Sync + 'static, + F: Fn(NodeID, SocketAddr, bool) + Sized + Send + Sync + 'static, { self.on_connected_handler .store(Some(Arc::new(Box::new(handler)))); @@ -158,7 +150,7 @@ impl NetApp { /// as the peering strategy will need to set this itself. pub fn on_disconnected(&self, handler: F) where - F: Fn(ed25519::PublicKey, bool) + Sized + Send + Sync + 'static, + F: Fn(NodeID, bool) + Sized + Send + Sync + 'static, { self.on_disconnected_handler .store(Some(Arc::new(Box::new(handler)))); @@ -169,19 +161,19 @@ impl NetApp { pub fn add_msg_handler(&self, handler: F) where M: Message + 'static, - F: Fn(ed25519::PublicKey, M) -> R + Send + Sync + 'static, + F: Fn(NodeID, M) -> R + Send + Sync + 'static, R: Future::Response> + Send + Sync + 'static, { let handler = Arc::new(handler); let handler2 = handler.clone(); - let net_handler = Box::new(move |remote: ed25519::PublicKey, bytes: Bytes| { + let net_handler = Box::new(move |remote: NodeID, bytes: Bytes| { let fun: Pin> + Sync + Send>> = Box::pin(net_handler_aux(handler2.clone(), remote, bytes)); fun }); - let self_id = self.pubkey.clone(); + let self_id = self.id.clone(); let local_handler = Box::new(move |msg: DynMsg| { let fun: Pin + Sync + Send>> = Box::pin(local_handler_aux(handler.clone(), self_id, msg)); @@ -232,43 +224,39 @@ impl NetApp { /// This function returns once the connection has been established and a /// successfull handshake was made. At this point we can send messages to /// the other node with `Netapp::request` - pub async fn try_connect( - self: Arc, - ip: SocketAddr, - pk: ed25519::PublicKey, - ) -> Result<(), Error> { + pub async fn try_connect(self: Arc, ip: SocketAddr, id: NodeID) -> Result<(), Error> { // Don't connect to ourself, we don't care // but pretend we did - if pk == self.pubkey { + if id == self.id { tokio::spawn(async move { if let Some(h) = self.on_connected_handler.load().as_ref() { - h(pk, ip, false); + h(id, ip, false); } }); return Ok(()); } // Don't connect if already connected - if self.client_conns.read().unwrap().contains_key(&pk) { + if self.client_conns.read().unwrap().contains_key(&id) { return Ok(()); } let socket = TcpStream::connect(ip).await?; info!("Connected to {}, negotiating handshake...", ip); - ClientConn::init(self, socket, pk.clone()).await?; + ClientConn::init(self, socket, id.clone()).await?; Ok(()) } /// Close the outgoing connection we have to a node specified by its public key, /// if such a connection is currently open. - pub fn disconnect(self: &Arc, pk: &ed25519::PublicKey) { - // If pk is ourself, we're not supposed to have a connection open - if *pk != self.pubkey { - let conn = self.client_conns.write().unwrap().remove(pk); + pub fn disconnect(self: &Arc, id: &NodeID) { + // If id is ourself, we're not supposed to have a connection open + if *id != self.id { + let conn = self.client_conns.write().unwrap().remove(id); if let Some(c) = conn { debug!( "Closing connection to {} ({})", - hex::encode(c.peer_pk), + hex::encode(c.peer_id), c.remote_addr ); c.close(); @@ -279,24 +267,24 @@ impl NetApp { // call on_disconnected_handler immediately, since the connection // was removed - // (if pk == self.pubkey, we pretend we disconnected) - let pk = *pk; + // (if id == self.id, we pretend we disconnected) + let id = *id; let self2 = self.clone(); tokio::spawn(async move { if let Some(h) = self2.on_disconnected_handler.load().as_ref() { - h(pk, false); + h(id, false); } }); } /// Close the incoming connection from a certain client to us, /// if such a connection is currently open. - pub fn server_disconnect(self: &Arc, pk: &ed25519::PublicKey) { - let conn = self.server_conns.read().unwrap().get(pk).cloned(); + pub fn server_disconnect(self: &Arc, id: &NodeID) { + let conn = self.server_conns.read().unwrap().get(id).cloned(); if let Some(c) = conn { debug!( "Closing incoming connection from {} ({})", - hex::encode(c.peer_pk), + hex::encode(c.peer_id), c.remote_addr ); c.close(); @@ -308,7 +296,7 @@ impl NetApp { // Do not yet call the on_connected handler, because we don't know if the remote // has an actual IP address and port we can call them back on. // We will know this when they send a Hello message, which is handled below. - pub(crate) fn connected_as_server(&self, id: ed25519::PublicKey, conn: Arc) { + pub(crate) fn connected_as_server(&self, id: NodeID, conn: Arc) { info!("Accepted connection from {}", hex::encode(id)); self.server_conns.write().unwrap().insert(id, conn); @@ -319,7 +307,7 @@ impl NetApp { // At this point we know they are a full network member, and not just a client, // and we call the on_connected handler so that the peering strategy knows // we have a new potential peer - fn handle_hello_message(&self, id: ed25519::PublicKey, msg: HelloMessage) { + fn handle_hello_message(&self, id: NodeID, msg: HelloMessage) { if let Some(h) = self.on_connected_handler.load().as_ref() { if let Some(c) = self.server_conns.read().unwrap().get(&id) { let remote_ip = msg.server_addr.unwrap_or(c.remote_addr.ip()); @@ -332,7 +320,7 @@ impl NetApp { // Called from conn.rs when an incoming connection is closed. // We deregister the connection from server_conns and call the // handler registered by on_disconnected - pub(crate) fn disconnected_as_server(&self, id: &ed25519::PublicKey, conn: Arc) { + pub(crate) fn disconnected_as_server(&self, id: &NodeID, conn: Arc) { info!("Connection from {} closed", hex::encode(id)); let mut conn_list = self.server_conns.write().unwrap(); @@ -342,7 +330,7 @@ impl NetApp { drop(conn_list); if let Some(h) = self.on_disconnected_handler.load().as_ref() { - h(conn.peer_pk, true); + h(conn.peer_id, true); } } } @@ -355,7 +343,7 @@ impl NetApp { // Since we are ourself listening, we send them a Hello message so that // they know on which port to call us back. (TODO: don't do this if we are // just a simple client and not a full p2p node) - pub(crate) fn connected_as_client(&self, id: ed25519::PublicKey, conn: Arc) { + pub(crate) fn connected_as_client(&self, id: NodeID, conn: Arc) { info!("Connection established to {}", hex::encode(id)); { @@ -366,7 +354,7 @@ impl NetApp { } if let Some(h) = self.on_connected_handler.load().as_ref() { - h(conn.peer_pk, conn.remote_addr, false); + h(conn.peer_id, conn.remote_addr, false); } if let Some(lp) = self.listen_params.load_full() { @@ -389,7 +377,7 @@ impl NetApp { // Called from conn.rs when an outgoinc connection is closed. // The connection is removed from conn_list, and the on_disconnected handler // is called. - pub(crate) fn disconnected_as_client(&self, id: &ed25519::PublicKey, conn: Arc) { + pub(crate) fn disconnected_as_client(&self, id: &NodeID, conn: Arc) { info!("Connection to {} closed", hex::encode(id)); let mut conn_list = self.client_conns.write().unwrap(); if let Some(c) = conn_list.get(id) { @@ -398,7 +386,7 @@ impl NetApp { drop(conn_list); if let Some(h) = self.on_disconnected_handler.load().as_ref() { - h(conn.peer_pk, false); + h(conn.peer_id, false); } } } @@ -412,14 +400,14 @@ impl NetApp { /// The priority is an `u8`, with lower numbers meaning highest priority. pub async fn request( &self, - target: &ed25519::PublicKey, + target: &NodeID, rq: T, prio: RequestPriority, ) -> Result<::Response, Error> where T: Message + 'static, { - if *target == self.pubkey { + if *target == self.id { let handler = self.msg_handlers.load().get(&T::KIND).cloned(); match handler { None => Err(Error::Message(format!( diff --git a/src/peering/basalt.rs b/src/peering/basalt.rs index 4d56319..3c1fc9e 100644 --- a/src/peering/basalt.rs +++ b/src/peering/basalt.rs @@ -9,11 +9,11 @@ use rand::{thread_rng, Rng}; use serde::{Deserialize, Serialize}; use sodiumoxide::crypto::hash; -use sodiumoxide::crypto::sign::ed25519; use crate::message::*; use crate::netapp::*; use crate::proto::*; +use crate::NodeID; // -- Protocol messages -- @@ -41,7 +41,7 @@ type Seed = [u8; 32]; #[derive(Hash, Clone, Copy, Debug, PartialOrd, PartialEq, Eq, Serialize, Deserialize)] struct Peer { - id: ed25519::PublicKey, + id: NodeID, addr: SocketAddr, } @@ -168,7 +168,7 @@ impl BasaltView { } } - fn disconnected(&mut self, id: ed25519::PublicKey) { + fn disconnected(&mut self, id: NodeID) { let mut cleared_slots = vec![]; for i in 0..self.slots.len() { if let Some(p) = self.slots[i].peer { @@ -248,7 +248,7 @@ pub struct Basalt { impl Basalt { pub fn new( netapp: Arc, - bootstrap_list: Vec<(ed25519::PublicKey, SocketAddr)>, + bootstrap_list: Vec<(NodeID, SocketAddr)>, param: BasaltParams, ) -> Arc { let bootstrap_peers = bootstrap_list @@ -272,37 +272,31 @@ impl Basalt { }); let basalt2 = basalt.clone(); - netapp.on_connected( - move |pk: ed25519::PublicKey, addr: SocketAddr, is_incoming: bool| { - basalt2.on_connected(pk, addr, is_incoming); - }, - ); - - let basalt2 = basalt.clone(); - netapp.on_disconnected(move |pk: ed25519::PublicKey, is_incoming: bool| { - basalt2.on_disconnected(pk, is_incoming); + netapp.on_connected(move |id: NodeID, addr: SocketAddr, is_incoming: bool| { + basalt2.on_connected(id, addr, is_incoming); }); let basalt2 = basalt.clone(); - netapp.add_msg_handler::( - move |_from: ed25519::PublicKey, _pullmsg: PullMessage| { - let push_msg = basalt2.make_push_message(); - async move { push_msg } - }, - ); + netapp.on_disconnected(move |id: NodeID, is_incoming: bool| { + basalt2.on_disconnected(id, is_incoming); + }); let basalt2 = basalt.clone(); - netapp.add_msg_handler::( - move |_from: ed25519::PublicKey, push_msg: PushMessage| { - basalt2.handle_peer_list(&push_msg.peers[..]); - async move { () } - }, - ); + netapp.add_msg_handler::(move |_from: NodeID, _pullmsg: PullMessage| { + let push_msg = basalt2.make_push_message(); + async move { push_msg } + }); + + let basalt2 = basalt.clone(); + netapp.add_msg_handler::(move |_from: NodeID, push_msg: PushMessage| { + basalt2.handle_peer_list(&push_msg.peers[..]); + async move { () } + }); basalt } - pub fn sample(&self, count: usize) -> Vec { + pub fn sample(&self, count: usize) -> Vec { self.view .read() .unwrap() @@ -337,7 +331,7 @@ impl Basalt { } } - async fn do_pull(self: Arc, peer: ed25519::PublicKey) { + async fn do_pull(self: Arc, peer: NodeID) { match self .netapp .request(&peer, PullMessage {}, PRIO_NORMAL) @@ -353,7 +347,7 @@ impl Basalt { }; } - async fn do_push(self: Arc, peer: ed25519::PublicKey) { + async fn do_push(self: Arc, peer: NodeID) { let push_msg = self.make_push_message(); match self.netapp.request(&peer, push_msg, PRIO_NORMAL).await { Ok(_) => { @@ -434,12 +428,12 @@ impl Basalt { } } - fn on_connected(self: &Arc, pk: ed25519::PublicKey, addr: SocketAddr, is_incoming: bool) { + fn on_connected(self: &Arc, id: NodeID, addr: SocketAddr, is_incoming: bool) { if is_incoming { - self.handle_peer_list(&[Peer { id: pk, addr }][..]); + self.handle_peer_list(&[Peer { id, addr }][..]); } else { - info!("KYEV C {} {}", hex::encode(pk), addr); - let peer = Peer { id: pk, addr }; + info!("KYEV C {} {}", hex::encode(id), addr); + let peer = Peer { id, addr }; let mut backlog = self.backlog.write().unwrap(); if backlog.get(&peer).is_none() { @@ -459,10 +453,10 @@ impl Basalt { } } - fn on_disconnected(&self, pk: ed25519::PublicKey, is_incoming: bool) { + fn on_disconnected(&self, id: NodeID, is_incoming: bool) { if !is_incoming { - info!("KYEV D {}", hex::encode(pk)); - self.view.write().unwrap().disconnected(pk); + info!("KYEV D {}", hex::encode(id)); + self.view.write().unwrap().disconnected(id); } } diff --git a/src/peering/fullmesh.rs b/src/peering/fullmesh.rs index d6ca08a..a4b9248 100644 --- a/src/peering/fullmesh.rs +++ b/src/peering/fullmesh.rs @@ -8,11 +8,11 @@ use log::{debug, info, trace, warn}; use serde::{Deserialize, Serialize}; use sodiumoxide::crypto::hash; -use sodiumoxide::crypto::sign::ed25519; use crate::message::*; use crate::netapp::*; use crate::proto::*; +use crate::NodeID; const CONN_RETRY_INTERVAL: Duration = Duration::from_secs(30); const CONN_MAX_RETRIES: usize = 10; @@ -34,7 +34,7 @@ impl Message for PingMessage { #[derive(Serialize, Deserialize)] struct PeerListMessage { - pub list: Vec<(ed25519::PublicKey, SocketAddr)>, + pub list: Vec<(NodeID, SocketAddr)>, } impl Message for PeerListMessage { @@ -54,7 +54,7 @@ struct PeerInfo { #[derive(Copy, Clone, Debug)] pub struct PeerInfoPub { - pub id: ed25519::PublicKey, + pub id: NodeID, pub addr: SocketAddr, pub state: PeerConnState, pub last_seen: Option, @@ -86,7 +86,7 @@ pub enum PeerConnState { } struct KnownHosts { - list: HashMap, + list: HashMap, hash: hash::Digest, } @@ -99,9 +99,7 @@ impl KnownHosts { fn update_hash(&mut self) { self.hash = Self::calculate_hash(&self.list); } - fn map_into_vec( - input: &HashMap, - ) -> Vec<(ed25519::PublicKey, SocketAddr)> { + fn map_into_vec(input: &HashMap) -> Vec<(NodeID, SocketAddr)> { let mut list = Vec::with_capacity(input.len()); for (id, peer) in input.iter() { if peer.state == PeerConnState::Connected || peer.state == PeerConnState::Ourself { @@ -110,7 +108,7 @@ impl KnownHosts { } list } - fn calculate_hash(input: &HashMap) -> hash::Digest { + fn calculate_hash(input: &HashMap) -> hash::Digest { let mut list = Self::map_into_vec(input); list.sort(); let mut hash_state = hash::State::new(); @@ -129,15 +127,12 @@ pub struct FullMeshPeeringStrategy { } impl FullMeshPeeringStrategy { - pub fn new( - netapp: Arc, - bootstrap_list: Vec<(ed25519::PublicKey, SocketAddr)>, - ) -> Arc { + pub fn new(netapp: Arc, bootstrap_list: Vec<(NodeID, SocketAddr)>) -> Arc { let mut known_hosts = KnownHosts::new(); - for (pk, addr) in bootstrap_list { - if pk != netapp.pubkey { + for (id, addr) in bootstrap_list { + if id != netapp.id { known_hosts.list.insert( - pk, + id, PeerInfo { addr: addr, state: PeerConnState::Waiting(0, Instant::now()), @@ -155,20 +150,18 @@ impl FullMeshPeeringStrategy { }); let strat2 = strat.clone(); - netapp.add_msg_handler::( - move |from: ed25519::PublicKey, ping: PingMessage| { - let ping_resp = PingMessage { - id: ping.id, - peer_list_hash: strat2.known_hosts.read().unwrap().hash, - }; - debug!("Ping from {}", hex::encode(&from)); - async move { ping_resp } - }, - ); + netapp.add_msg_handler::(move |from: NodeID, ping: PingMessage| { + let ping_resp = PingMessage { + id: ping.id, + peer_list_hash: strat2.known_hosts.read().unwrap().hash, + }; + debug!("Ping from {}", hex::encode(&from)); + async move { ping_resp } + }); let strat2 = strat.clone(); netapp.add_msg_handler::( - move |_from: ed25519::PublicKey, peer_list: PeerListMessage| { + move |_from: NodeID, peer_list: PeerListMessage| { strat2.handle_peer_list(&peer_list.list[..]); let peer_list = KnownHosts::map_into_vec(&strat2.known_hosts.read().unwrap().list); let resp = PeerListMessage { list: peer_list }; @@ -177,17 +170,15 @@ impl FullMeshPeeringStrategy { ); let strat2 = strat.clone(); - netapp.on_connected( - move |pk: ed25519::PublicKey, addr: SocketAddr, is_incoming: bool| { - let strat2 = strat2.clone(); - tokio::spawn(strat2.on_connected(pk, addr, is_incoming)); - }, - ); + netapp.on_connected(move |id: NodeID, addr: SocketAddr, is_incoming: bool| { + let strat2 = strat2.clone(); + tokio::spawn(strat2.on_connected(id, addr, is_incoming)); + }); let strat2 = strat.clone(); - netapp.on_disconnected(move |pk: ed25519::PublicKey, is_incoming: bool| { + netapp.on_disconnected(move |id: NodeID, is_incoming: bool| { let strat2 = strat2.clone(); - tokio::spawn(strat2.on_disconnected(pk, is_incoming)); + tokio::spawn(strat2.on_disconnected(id, is_incoming)); }); strat @@ -254,7 +245,7 @@ impl FullMeshPeeringStrategy { } } - async fn ping(self: Arc, id: ed25519::PublicKey) { + async fn ping(self: Arc, id: NodeID) { let peer_list_hash = self.known_hosts.read().unwrap().hash; let ping_id = self.next_ping_id.fetch_add(1u64, atomic::Ordering::Relaxed); let ping_time = Instant::now(); @@ -295,7 +286,7 @@ impl FullMeshPeeringStrategy { } } - async fn exchange_peers(self: Arc, id: &ed25519::PublicKey) { + async fn exchange_peers(self: Arc, id: &NodeID) { let peer_list = KnownHosts::map_into_vec(&self.known_hosts.read().unwrap().list); let pex_message = PeerListMessage { list: peer_list }; match self.netapp.request(id, pex_message, PRIO_BACKGROUND).await { @@ -306,7 +297,7 @@ impl FullMeshPeeringStrategy { } } - fn handle_peer_list(&self, list: &[(ed25519::PublicKey, SocketAddr)]) { + fn handle_peer_list(&self, list: &[(NodeID, SocketAddr)]) { let mut known_hosts = self.known_hosts.write().unwrap(); for (id, addr) in list.iter() { if !known_hosts.list.contains_key(id) { @@ -315,7 +306,7 @@ impl FullMeshPeeringStrategy { } } - async fn try_connect(self: Arc, id: ed25519::PublicKey, addr: SocketAddr) { + async fn try_connect(self: Arc, id: NodeID, addr: SocketAddr) { let conn_result = self.netapp.clone().try_connect(addr, id.clone()).await; if let Err(e) = conn_result { warn!("Error connecting to {}: {}", hex::encode(id), e); @@ -335,35 +326,30 @@ impl FullMeshPeeringStrategy { } } - async fn on_connected( - self: Arc, - pk: ed25519::PublicKey, - addr: SocketAddr, - is_incoming: bool, - ) { + async fn on_connected(self: Arc, id: NodeID, addr: SocketAddr, is_incoming: bool) { if is_incoming { - if !self.known_hosts.read().unwrap().list.contains_key(&pk) { + if !self.known_hosts.read().unwrap().list.contains_key(&id) { self.known_hosts .write() .unwrap() .list - .insert(pk, self.new_peer(&pk, addr)); + .insert(id, self.new_peer(&id, addr)); } } else { - info!("Successfully connected to {} at {}", hex::encode(&pk), addr); + info!("Successfully connected to {} at {}", hex::encode(&id), addr); let mut known_hosts = self.known_hosts.write().unwrap(); - if let Some(host) = known_hosts.list.get_mut(&pk) { + if let Some(host) = known_hosts.list.get_mut(&id) { host.state = PeerConnState::Connected; known_hosts.update_hash(); } } } - async fn on_disconnected(self: Arc, pk: ed25519::PublicKey, is_incoming: bool) { + async fn on_disconnected(self: Arc, id: NodeID, is_incoming: bool) { if !is_incoming { - info!("Connection to {} was closed", hex::encode(pk)); + info!("Connection to {} was closed", hex::encode(id)); let mut known_hosts = self.known_hosts.write().unwrap(); - if let Some(host) = known_hosts.list.get_mut(&pk) { + if let Some(host) = known_hosts.list.get_mut(&id) { host.state = PeerConnState::Waiting(0, Instant::now()); known_hosts.update_hash(); } @@ -406,8 +392,8 @@ impl FullMeshPeeringStrategy { ret } - fn new_peer(&self, id: &ed25519::PublicKey, addr: SocketAddr) -> PeerInfo { - let state = if *id == self.netapp.pubkey { + fn new_peer(&self, id: &NodeID, addr: SocketAddr) -> PeerInfo { + let state = if *id == self.netapp.id { PeerConnState::Ourself } else { PeerConnState::Waiting(0, Instant::now()) diff --git a/src/util.rs b/src/util.rs index 017ef00..faeea79 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,6 +2,8 @@ use serde::Serialize; use tokio::sync::watch; +pub type NodeID = sodiumoxide::crypto::sign::ed25519::PublicKey; + /// Utility function: encodes any serializable value in MessagePack binary format /// using the RMP library. ///