-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
975 additions
and
493 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::net::{Ipv4Addr, Ipv6Addr}; | ||
|
||
use discv5::ListenConfig; | ||
|
||
const DEFAULT_UDP_PORT: u16 = 9000; | ||
|
||
pub struct Config { | ||
/// The ipv4 address to broadcast to peers about which address we are listening on. | ||
pub ipv4_addr: Option<Ipv4Addr>, | ||
|
||
/// The ipv6 address to broadcast to peers about which address we are listening on. | ||
pub ipv6_addr: Option<Ipv6Addr>, | ||
|
||
/// The udp4 port to broadcast to peers in order to reach back for discovery. | ||
pub enr_udp4_port: Option<u16>, | ||
|
||
/// The tcp4 port to broadcast to peers in order to reach back for libp2p services. | ||
pub enr_tcp4_port: Option<u16>, | ||
|
||
/// The udp6 port to broadcast to peers in order to reach back for discovery. | ||
pub enr_udp6_port: Option<u16>, | ||
|
||
/// The tcp6 port to broadcast to peers in order to reach back for libp2p services. | ||
pub enr_tcp6_port: Option<u16>, | ||
} | ||
|
||
impl Default for Config { | ||
fn default() -> Self { | ||
Self { | ||
ipv4_addr: Some(Ipv4Addr::UNSPECIFIED), | ||
ipv6_addr: None, | ||
enr_udp4_port: Some(DEFAULT_UDP_PORT), | ||
enr_tcp4_port: None, | ||
enr_udp6_port: None, | ||
enr_tcp6_port: None, | ||
} | ||
} | ||
} | ||
|
||
impl Config { | ||
pub fn to_listen_config(&self) -> ListenConfig { | ||
match (self.ipv4_addr, self.ipv6_addr) { | ||
(None, None) => todo!(), | ||
(None, Some(ip)) => ListenConfig::Ipv6 { | ||
ip, | ||
port: self.enr_udp6_port.unwrap_or(DEFAULT_UDP_PORT), | ||
}, | ||
(Some(ip), None) => ListenConfig::Ipv4 { | ||
ip, | ||
port: self.enr_udp4_port.unwrap_or(DEFAULT_UDP_PORT), | ||
}, | ||
(Some(ipv4), Some(ipv6)) => ListenConfig::DualStack { | ||
ipv4, | ||
ipv4_port: self.enr_udp4_port.unwrap_or(DEFAULT_UDP_PORT), | ||
ipv6, | ||
ipv6_port: self.enr_udp6_port.unwrap_or(DEFAULT_UDP_PORT), | ||
}, | ||
} | ||
} | ||
} |
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 +1,51 @@ | ||
pub struct Discovery {} | ||
use std::task::Poll; | ||
|
||
use discv5::{enr::CombinedKey, Discv5, Discv5ConfigBuilder, Enr}; | ||
use libp2p::swarm::{dummy::ConnectionHandler, NetworkBehaviour}; | ||
|
||
use crate::config::Config; | ||
|
||
pub struct Discovery { | ||
pub discovery: Discv5, | ||
} | ||
|
||
impl Discovery { | ||
pub fn new(enr: Enr, key: CombinedKey, config: Config) -> anyhow::Result<Self> { | ||
let config = Discv5ConfigBuilder::new(config.to_listen_config()).build(); | ||
let discovery: Discv5<_> = Discv5::new(enr, key, config).map_err(|e| anyhow::anyhow!(e))?; | ||
Ok(Self { discovery }) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum Event { | ||
Nothing, | ||
} | ||
|
||
impl NetworkBehaviour for Discovery { | ||
type ConnectionHandler = ConnectionHandler; | ||
type OutEvent = Event; | ||
fn new_handler(&mut self) -> Self::ConnectionHandler { | ||
ConnectionHandler | ||
} | ||
|
||
fn addresses_of_peer(&mut self, _: &libp2p::PeerId) -> Vec<libp2p::Multiaddr> { | ||
todo!() | ||
} | ||
fn poll( | ||
&mut self, | ||
_cx: &mut std::task::Context<'_>, | ||
_params: &mut impl libp2p::swarm::PollParameters, | ||
) -> Poll<libp2p::swarm::ToSwarm<Self::OutEvent, libp2p::swarm::THandlerInEvent<Self>>> { | ||
Poll::Pending | ||
} | ||
fn on_swarm_event(&mut self, _event: libp2p::swarm::FromSwarm<Self::ConnectionHandler>) {} | ||
|
||
fn on_connection_handler_event( | ||
&mut self, | ||
_peer_id: libp2p::PeerId, | ||
_connection_id: libp2p::swarm::ConnectionId, | ||
_event: libp2p::swarm::THandlerOutEvent<Self>, | ||
) { | ||
} | ||
} |
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,68 @@ | ||
use discv5::{ | ||
enr::{ | ||
k256::{ecdsa::VerifyingKey, CompressedPoint}, | ||
CombinedKey, CombinedPublicKey, EnrBuilder, | ||
}, | ||
Enr, | ||
}; | ||
use libp2p::{ | ||
identity::{secp256k1, Keypair, PublicKey}, | ||
PeerId, | ||
}; | ||
|
||
use crate::config::Config; | ||
|
||
pub fn keypair_to_combine(keypair: Keypair) -> anyhow::Result<CombinedKey> { | ||
match keypair.try_into_secp256k1() { | ||
Ok(key) => { | ||
let secret = | ||
discv5::enr::k256::ecdsa::SigningKey::from_bytes(&key.secret().to_bytes().into()) | ||
.expect("libp2p key must be valid"); | ||
Ok(CombinedKey::Secp256k1(secret)) | ||
} | ||
Err(_) => anyhow::bail!("libp2p key must be either secp256k1"), | ||
} | ||
} | ||
|
||
pub fn build_enr(enr_key: &CombinedKey, config: &Config) -> anyhow::Result<Enr> { | ||
let mut enr_builder = EnrBuilder::new("v4"); | ||
if let Some(ip) = config.ipv4_addr { | ||
enr_builder.ip4(ip); | ||
} | ||
if let Some(ip) = config.ipv6_addr { | ||
enr_builder.ip6(ip); | ||
} | ||
if let Some(port) = config.enr_tcp4_port { | ||
enr_builder.tcp4(port); | ||
} | ||
if let Some(port) = config.enr_tcp6_port { | ||
enr_builder.tcp6(port); | ||
} | ||
if let Some(port) = config.enr_udp4_port { | ||
enr_builder.udp4(port); | ||
} | ||
if let Some(port) = config.enr_udp6_port { | ||
enr_builder.udp6(port); | ||
} | ||
|
||
let enr = enr_builder.build(enr_key)?; | ||
Ok(enr) | ||
} | ||
|
||
pub trait CombineKeyPubExt { | ||
fn to_peer_id(&self) -> anyhow::Result<PeerId>; | ||
} | ||
|
||
impl CombineKeyPubExt for CombinedPublicKey { | ||
fn to_peer_id(&self) -> anyhow::Result<PeerId> { | ||
let pub_key: PublicKey = match self { | ||
CombinedPublicKey::Secp256k1(pk) => { | ||
PublicKey::from(secp256k1::PublicKey::try_from_bytes( | ||
<&VerifyingKey as Into<CompressedPoint>>::into(pk).as_slice(), | ||
)?) | ||
} | ||
_ => anyhow::bail!("Only secp256k1 is supported"), | ||
}; | ||
Ok(PeerId::from_public_key(&pub_key)) | ||
} | ||
} |
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
Oops, something went wrong.