Skip to content

Commit

Permalink
wip: establish connection
Browse files Browse the repository at this point in the history
  • Loading branch information
iduartgomez committed Jul 20, 2024
1 parent 7a9ee9d commit 6df2d9d
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 125 deletions.
1 change: 1 addition & 0 deletions crates/core/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ pub(crate) enum NodeEvent {
// Try connecting to the given peer.
ConnectPeer {
peer: PeerId,
tx: Transaction,
callback: tokio::sync::mpsc::Sender<Result<(), ()>>,
},
Disconnect {
Expand Down
31 changes: 8 additions & 23 deletions crates/core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl NodeConfig {
config.network_api.port
);
if let Some(peer_id) = &config.peer_id {
tracing::info!("Node external address: {}", peer_id.addr());
tracing::info!("Node external address: {}", peer_id.addr);
}
Ok(NodeConfig {
should_connect: true,
Expand Down Expand Up @@ -911,10 +911,13 @@ where
Ok(())
}

/*
- Cuando es un gateway: se define desde el inicio del nodo
- Cuando es un peer regular: se define en el momento de la conexión con el gateway
*/
/// The identifier of a peer in the network is composed of its address and public key.
///
/// A regular peer will have its `PeerId` set when it connects to a gateway as it get's
/// its external address from the gateway.
///
/// A gateway will have its `PeerId` set when it is created since it will know its own address
/// from the start.
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
pub struct PeerId {
pub addr: SocketAddr,
Expand All @@ -933,28 +936,10 @@ impl PartialOrd for PeerId {
}
}

// impl FromStr for PeerId {
// type Err = anyhow::Error;
// fn from_str(s: &str) -> Result<Self, Self::Err> {
// Ok(Self {
// addr: s.parse()?,
// pub_key: TransportKeypair::new().public,
// })
// }
// }

impl PeerId {
pub fn new(addr: SocketAddr, pub_key: TransportPublicKey) -> Self {
Self { addr, pub_key }
}

pub fn addr(&self) -> SocketAddr {
self.addr
}

pub fn pub_key(&self) -> &TransportPublicKey {
&self.pub_key
}
}

thread_local! {
Expand Down
6 changes: 3 additions & 3 deletions crates/core/src/node/network_bridge/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(super) enum HandshakeError {
TransportError(#[from] TransportError),
}

#[cfg_attr(test, derive(Debug))]
#[derive(Debug)]
pub(super) enum Event {
/// An inbound connection to a peer was successfully established at a gateway.
InboundConnection(InboundJoinRequest),
Expand Down Expand Up @@ -515,7 +515,7 @@ async fn wait_for_gw_confirmation(
todo!()
}

#[cfg_attr(test, derive(Debug))]
#[derive(Debug)]
pub(super) struct InboundJoinRequest {
pub conn: PeerConnection,
pub id: Transaction,
Expand Down Expand Up @@ -991,7 +991,7 @@ mod tests {
assert!(matches!(connection, ConnectOp { .. }));
Ok(())
}
other => Ok(()),
other => Err(anyhow!("Unexpected event: {:?}", other)),
}
};

Expand Down
Loading

0 comments on commit 6df2d9d

Please sign in to comment.