Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Relay Client and DCUtR #113

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ secp256k1 = ["libp2p/secp256k1"]
async_global = ["async-global-executor", "libp2p/async-std"]
tokio = ["tokio-crate", "libp2p/tokio"]
telemetry = ["tide", "async_global"]
serde = ["libp2p/serde"]
# Makes it possible to exchange data via Bitswap with a go-ipfs node
compat = ["libp2p-bitswap/compat"]

Expand Down Expand Up @@ -54,6 +55,9 @@ features = [
"dns",
"gossipsub",
"identify",
"autonat",
"dcutr",
"relay",
"kad",
"mdns",
"mplex",
Expand Down
75 changes: 55 additions & 20 deletions src/net/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use libp2p::mdns::Mdns;
use libp2p::mdns::TokioMdns as Mdns;
use libp2p::{
core::ConnectedPoint,
dcutr::behaviour::{Behaviour as Dcutr, Event as DcutrEvent},
gossipsub::{Gossipsub, GossipsubEvent, GossipsubMessage, IdentTopic, MessageAuthenticity},
identify,
kad::{
Expand All @@ -27,6 +28,9 @@ use libp2p::{
},
mdns::MdnsEvent,
ping,
relay::v2::client::{
transport::ClientTransport, Client as RelayClient, Event as RelayClientEvent,
},
swarm::{
behaviour::toggle::Toggle, AddressRecord, ConnectionError, ConnectionHandler,
IntoConnectionHandler, NetworkBehaviour,
Expand Down Expand Up @@ -100,8 +104,10 @@ pub struct NetworkBackendBehaviour<P: StoreParams> {
peers: AddressBook,
kad: Toggle<Kademlia<MemoryStore>>,
mdns: Toggle<Mdns>,
ping: Toggle<ping::Behaviour>,
identify: Toggle<identify::Behaviour>,
ping: Toggle<ping::Ping>,
relay_client: Toggle<RelayClient>,
dcutr: Toggle<Dcutr>,
identify: Toggle<identify::Identify>,
bitswap: Toggle<Bitswap<P>>,
gossipsub: Toggle<Gossipsub>,
broadcast: Toggle<Broadcast>,
Expand Down Expand Up @@ -316,6 +322,22 @@ impl<P: StoreParams> NetworkBackendBehaviour<P> {
}
}

impl<P: StoreParams> NetworkBackendBehaviour<P> {
pub fn inject_relay_client_event(&mut self, event: RelayClientEvent) {
match event {
_ => {}
}
}
}

impl<P: StoreParams> NetworkBackendBehaviour<P> {
pub fn inject_dcutr_event(&mut self, event: DcutrEvent) {
match event {
_ => {}
}
}
}

#[derive(Debug, Error)]
#[error("{0:?}")]
pub struct GossipsubPublishError(pub libp2p::gossipsub::error::PublishError);
Expand Down Expand Up @@ -408,7 +430,7 @@ impl<P: StoreParams> NetworkBackendBehaviour<P> {
listeners: Writer<FnvHashSet<Multiaddr>>,
peers: Writer<FnvHashMap<PeerId, PeerInfo>>,
external: Writer<Vec<AddressRecord>>,
) -> Result<Self> {
) -> Result<(Self, Option<ClientTransport>)> {
let node_key = libp2p::identity::Keypair::Ed25519(config.node_key.clone());
let node_name = config.node_name.clone();
let peer_id = node_key.public().to_peer_id();
Expand Down Expand Up @@ -438,28 +460,41 @@ impl<P: StoreParams> NetworkBackendBehaviour<P> {
} else {
None
};
let dcutr = config.dcutr.then(Dcutr::new);
let (transport, relay_client) = match config.relay_client {
true => {
let (transport, client) = RelayClient::new_transport_and_behaviour(peer_id);
(Some(transport), Some(client))
}
false => (None, None),
};
let broadcast = config.broadcast.take().map(Broadcast::new);
let bitswap = config
.bitswap
.take()
.map(|config| Bitswap::new(config, store));
Ok(Self {
peers: AddressBook::new(
peer_id,
config.port_reuse,
config.enable_loopback,
listeners,
peers,
external,
),
mdns: mdns.into(),
kad: kad.into(),
ping: ping.into(),
identify: identify.into(),
bitswap: bitswap.into(),
gossipsub: gossipsub.into(),
broadcast: broadcast.into(),
})
Ok((
Self {
peers: AddressBook::new(
peer_id,
config.port_reuse,
config.enable_loopback,
listeners,
peers,
external,
),
mdns: mdns.into(),
kad: kad.into(),
ping: ping.into(),
identify: identify.into(),
bitswap: bitswap.into(),
gossipsub: gossipsub.into(),
dcutr: dcutr.into(),
relay_client: relay_client.into(),
broadcast: broadcast.into(),
},
transport,
))
}

pub fn add_address(&mut self, peer_id: &PeerId, addr: Multiaddr, source: AddressSource) {
Expand Down
6 changes: 6 additions & 0 deletions src/net/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub struct NetworkConfig {
pub identify: Option<IdentifyConfig>,
/// Gossipsub config.
pub gossipsub: Option<GossipsubConfig>,
/// Enables Dcutr
pub dcutr: bool,
/// Enables Circuit Relay v2 (Client)
pub relay_client: bool,
/// Broadcast config.
pub broadcast: Option<BroadcastConfig>,
/// Bitswap config.
Expand Down Expand Up @@ -79,6 +83,8 @@ impl NetworkConfig {
ping: None,
identify: Some(identify),
gossipsub: Some(GossipsubConfig::default()),
dcutr: false,
relay_client: false,
broadcast: Some(BroadcastConfig::default()),
bitswap: Some(BitswapConfig::default()),
}
Expand Down
Loading