-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(net): Remove NetworkAddress (#55)
### Description Removes the `NetworkAddress` which is effectively used as a conversion utility between the [`SocketAddr`](https://doc.rust-lang.org/stable/std/net/enum.SocketAddr.html) and [`MulitAddr`](https://docs.rs/libp2p/latest/libp2p/struct.Multiaddr.html). Closes #52 --------- Co-authored-by: nicolas <[email protected]>
- Loading branch information
1 parent
1482685
commit 9c08589
Showing
6 changed files
with
75 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
//! Common types for the Networking Crate. | ||
pub mod address; | ||
pub mod enr; | ||
pub mod envelope; | ||
pub mod payload; | ||
pub mod peer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! Peer Types | ||
use discv5::enr::{CombinedKey, Enr}; | ||
use eyre::Result; | ||
use libp2p::{multiaddr::Protocol, Multiaddr}; | ||
use std::net::{IpAddr, SocketAddr}; | ||
|
||
/// A wrapper around a peer's [SocketAddr]. | ||
#[derive(Debug)] | ||
pub struct Peer { | ||
/// The peer's [SocketAddr]. | ||
pub socket: SocketAddr, | ||
} | ||
|
||
impl TryFrom<&Enr<CombinedKey>> for Peer { | ||
type Error = eyre::Report; | ||
|
||
/// Converts an [Enr] to a Peer | ||
fn try_from(value: &Enr<CombinedKey>) -> Result<Self> { | ||
let ip = value.ip4().ok_or(eyre::eyre!("missing ip"))?; | ||
let port = value.tcp4().ok_or(eyre::eyre!("missing port"))?; | ||
let socket = SocketAddr::new(IpAddr::V4(ip), port); | ||
Ok(Peer { socket }) | ||
} | ||
} | ||
|
||
impl From<Peer> for Multiaddr { | ||
/// Converts a Peer to a [Multiaddr] | ||
fn from(value: Peer) -> Self { | ||
let mut multiaddr = Multiaddr::empty(); | ||
match value.socket.ip() { | ||
IpAddr::V4(ip) => multiaddr.push(Protocol::Ip4(ip)), | ||
IpAddr::V6(ip) => multiaddr.push(Protocol::Ip6(ip)), | ||
} | ||
multiaddr.push(Protocol::Tcp(value.socket.port())); | ||
multiaddr | ||
} | ||
} |