Skip to content

Commit

Permalink
refactor(relay): revise public API to follow naming convention (#3238)
Browse files Browse the repository at this point in the history
Continues addressing #2217.
  • Loading branch information
jxs authored Jan 2, 2023
1 parent 68d0f88 commit 9c96bbb
Show file tree
Hide file tree
Showing 24 changed files with 396 additions and 310 deletions.
4 changes: 2 additions & 2 deletions misc/metrics/src/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ impl super::Recorder<libp2p_identify::Event> for Metrics {
#[cfg(feature = "ping")]
libp2p_ping::PROTOCOL_NAME,
#[cfg(feature = "relay")]
libp2p_relay::v2::STOP_PROTOCOL_NAME,
libp2p_relay::STOP_PROTOCOL_NAME,
#[cfg(feature = "relay")]
libp2p_relay::v2::HOP_PROTOCOL_NAME,
libp2p_relay::HOP_PROTOCOL_NAME,
];

allowed_protocols.contains(&p.as_bytes())
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ impl Recorder<libp2p_ping::Event> for Metrics {
}

#[cfg(feature = "relay")]
impl Recorder<libp2p_relay::v2::relay::Event> for Metrics {
fn record(&self, event: &libp2p_relay::v2::relay::Event) {
impl Recorder<libp2p_relay::Event> for Metrics {
fn record(&self, event: &libp2p_relay::Event) {
self.relay.record(event)
}
}
Expand Down
44 changes: 16 additions & 28 deletions misc/metrics/src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,47 +63,35 @@ enum EventType {
CircuitClosed,
}

impl From<&libp2p_relay::v2::relay::Event> for EventType {
fn from(event: &libp2p_relay::v2::relay::Event) -> Self {
impl From<&libp2p_relay::Event> for EventType {
fn from(event: &libp2p_relay::Event) -> Self {
match event {
libp2p_relay::v2::relay::Event::ReservationReqAccepted { .. } => {
EventType::ReservationReqAccepted
}
libp2p_relay::v2::relay::Event::ReservationReqAcceptFailed { .. } => {
libp2p_relay::Event::ReservationReqAccepted { .. } => EventType::ReservationReqAccepted,
libp2p_relay::Event::ReservationReqAcceptFailed { .. } => {
EventType::ReservationReqAcceptFailed
}
libp2p_relay::v2::relay::Event::ReservationReqDenied { .. } => {
EventType::ReservationReqDenied
}
libp2p_relay::v2::relay::Event::ReservationReqDenyFailed { .. } => {
libp2p_relay::Event::ReservationReqDenied { .. } => EventType::ReservationReqDenied,
libp2p_relay::Event::ReservationReqDenyFailed { .. } => {
EventType::ReservationReqDenyFailed
}
libp2p_relay::v2::relay::Event::ReservationTimedOut { .. } => {
EventType::ReservationTimedOut
}
libp2p_relay::v2::relay::Event::CircuitReqReceiveFailed { .. } => {
libp2p_relay::Event::ReservationTimedOut { .. } => EventType::ReservationTimedOut,
libp2p_relay::Event::CircuitReqReceiveFailed { .. } => {
EventType::CircuitReqReceiveFailed
}
libp2p_relay::v2::relay::Event::CircuitReqDenied { .. } => EventType::CircuitReqDenied,
libp2p_relay::v2::relay::Event::CircuitReqOutboundConnectFailed { .. } => {
libp2p_relay::Event::CircuitReqDenied { .. } => EventType::CircuitReqDenied,
libp2p_relay::Event::CircuitReqOutboundConnectFailed { .. } => {
EventType::CircuitReqOutboundConnectFailed
}
libp2p_relay::v2::relay::Event::CircuitReqDenyFailed { .. } => {
EventType::CircuitReqDenyFailed
}
libp2p_relay::v2::relay::Event::CircuitReqAccepted { .. } => {
EventType::CircuitReqAccepted
}
libp2p_relay::v2::relay::Event::CircuitReqAcceptFailed { .. } => {
EventType::CircuitReqAcceptFailed
}
libp2p_relay::v2::relay::Event::CircuitClosed { .. } => EventType::CircuitClosed,
libp2p_relay::Event::CircuitReqDenyFailed { .. } => EventType::CircuitReqDenyFailed,
libp2p_relay::Event::CircuitReqAccepted { .. } => EventType::CircuitReqAccepted,
libp2p_relay::Event::CircuitReqAcceptFailed { .. } => EventType::CircuitReqAcceptFailed,
libp2p_relay::Event::CircuitClosed { .. } => EventType::CircuitClosed,
}
}
}

impl super::Recorder<libp2p_relay::v2::relay::Event> for Metrics {
fn record(&self, event: &libp2p_relay::v2::relay::Event) {
impl super::Recorder<libp2p_relay::Event> for Metrics {
fn record(&self, event: &libp2p_relay::Event) {
self.events
.get_or_create(&EventLabels {
event: event.into(),
Expand Down
18 changes: 9 additions & 9 deletions protocols/dcutr/examples/dcutr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use libp2p_dns::DnsConfig;
use libp2p_identify as identify;
use libp2p_noise as noise;
use libp2p_ping as ping;
use libp2p_relay::v2::client::{self, Client};
use libp2p_relay as relay;
use libp2p_swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
use libp2p_tcp as tcp;
use log::info;
Expand Down Expand Up @@ -87,7 +87,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let local_peer_id = PeerId::from(local_key.public());
info!("Local peer id: {:?}", local_peer_id);

let (relay_transport, client) = Client::new_transport_and_behaviour(local_peer_id);
let (relay_transport, client) = relay::client::new(local_peer_id);

let transport = OrTransport::new(
relay_transport,
Expand All @@ -111,7 +111,7 @@ fn main() -> Result<(), Box<dyn Error>> {
prelude = "libp2p_swarm::derive_prelude"
)]
struct Behaviour {
relay_client: Client,
relay_client: relay::client::Behaviour,
ping: ping::Behaviour,
identify: identify::Behaviour,
dcutr: dcutr::Behaviour,
Expand All @@ -122,7 +122,7 @@ fn main() -> Result<(), Box<dyn Error>> {
enum Event {
Ping(ping::Event),
Identify(identify::Event),
Relay(client::Event),
Relay(relay::client::Event),
Dcutr(dcutr::Event),
}

Expand All @@ -138,8 +138,8 @@ fn main() -> Result<(), Box<dyn Error>> {
}
}

impl From<client::Event> for Event {
fn from(e: client::Event) -> Self {
impl From<relay::client::Event> for Event {
fn from(e: relay::client::Event) -> Self {
Event::Relay(e)
}
}
Expand Down Expand Up @@ -252,9 +252,9 @@ fn main() -> Result<(), Box<dyn Error>> {
SwarmEvent::NewListenAddr { address, .. } => {
info!("Listening on {:?}", address);
}
SwarmEvent::Behaviour(Event::Relay(client::Event::ReservationReqAccepted {
..
})) => {
SwarmEvent::Behaviour(Event::Relay(
relay::client::Event::ReservationReqAccepted { .. },
)) => {
assert!(opts.mode == Mode::Listen);
info!("Relay accepted our reservation request.");
}
Expand Down
31 changes: 16 additions & 15 deletions protocols/dcutr/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ use libp2p_core::PublicKey;
use libp2p_core::{identity, PeerId};
use libp2p_dcutr as dcutr;
use libp2p_plaintext::PlainText2Config;
use libp2p_relay::v2::client;
use libp2p_relay::v2::relay;
use libp2p_relay as relay;
use libp2p_swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
use std::time::Duration;

Expand Down Expand Up @@ -91,7 +90,7 @@ fn connect() {
));
}

fn build_relay() -> Swarm<relay::Relay> {
fn build_relay() -> Swarm<relay::Behaviour> {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let local_peer_id = local_public_key.to_peer_id();
Expand All @@ -100,7 +99,7 @@ fn build_relay() -> Swarm<relay::Relay> {

Swarm::with_threadpool_executor(
transport,
relay::Relay::new(
relay::Behaviour::new(
local_peer_id,
relay::Config {
reservation_duration: Duration::from_secs(2),
Expand All @@ -116,7 +115,7 @@ fn build_client() -> Swarm<Client> {
let local_public_key = local_key.public();
let local_peer_id = local_public_key.to_peer_id();

let (relay_transport, behaviour) = client::Client::new_transport_and_behaviour(local_peer_id);
let (relay_transport, behaviour) = relay::client::new(local_peer_id);
let transport = build_transport(
OrTransport::new(relay_transport, MemoryTransport::default()).boxed(),
local_public_key,
Expand Down Expand Up @@ -153,18 +152,18 @@ where
prelude = "libp2p_swarm::derive_prelude"
)]
struct Client {
relay: client::Client,
relay: relay::client::Behaviour,
dcutr: dcutr::Behaviour,
}

#[derive(Debug)]
enum ClientEvent {
Relay(client::Event),
Relay(relay::client::Event),
Dcutr(dcutr::Event),
}

impl From<client::Event> for ClientEvent {
fn from(event: client::Event) -> Self {
impl From<relay::client::Event> for ClientEvent {
fn from(event: relay::client::Event) -> Self {
ClientEvent::Relay(event)
}
}
Expand Down Expand Up @@ -198,11 +197,13 @@ async fn wait_for_reservation(
break;
}
}
SwarmEvent::Behaviour(ClientEvent::Relay(client::Event::ReservationReqAccepted {
relay_peer_id: peer_id,
renewal,
..
})) if relay_peer_id == peer_id && renewal == is_renewal => {
SwarmEvent::Behaviour(ClientEvent::Relay(
relay::client::Event::ReservationReqAccepted {
relay_peer_id: peer_id,
renewal,
..
},
)) if relay_peer_id == peer_id && renewal == is_renewal => {
reservation_req_accepted = true;
if new_listen_addr_for_relayed_addr {
break;
Expand All @@ -226,7 +227,7 @@ async fn wait_for_connection_established(client: &mut Swarm<Client>, addr: &Mult
}
SwarmEvent::Dialing(_) => {}
SwarmEvent::Behaviour(ClientEvent::Relay(
client::Event::OutboundCircuitEstablished { .. },
relay::client::Event::OutboundCircuitEstablished { .. },
)) => {}
SwarmEvent::ConnectionEstablished { .. } => {}
e => panic!("{e:?}"),
Expand Down
10 changes: 10 additions & 0 deletions protocols/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# 0.15.0 [unreleased]

- Rename types as per [discussion 2174].
`Relay` has been renamed to `Behaviour`.
The `Relay`, and `Client` prefixes have been removed from various types like `ClientTransport`.
the `v2` namespace has also been removed, users should prefer importing the relay protocol as a module (`use libp2p::relay;`),
and refer to its types via `relay::`. For example: `relay::Behaviour` or `relay::client::Behaviour`.
See [PR 3238].

- Update to `libp2p-core` `v0.39.0`.

- Update to `libp2p-swarm` `v0.42.0`.

[PR 3238]: https://github.com/libp2p/rust-libp2p/pull/3238
[discussion 2174]: https://github.com/libp2p/rust-libp2p/issues/2174

# 0.14.0

- Update to `prost-codec` `v0.3.0`.
Expand Down
2 changes: 1 addition & 1 deletion protocols/relay/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
// DEALINGS IN THE SOFTWARE.

fn main() {
prost_build::compile_protos(&["src/v2/message.proto"], &["src/v2"]).unwrap();
prost_build::compile_protos(&["src/message.proto"], &["src/"]).unwrap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use libp2p_core::{identity, Multiaddr, PeerId, Transport};
use libp2p_identify as identify;
use libp2p_noise as noise;
use libp2p_ping as ping;
use libp2p_relay::v2::relay::{self, Relay};
use libp2p_relay as relay;
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent};
use libp2p_tcp as tcp;
use std::error::Error;
Expand Down Expand Up @@ -57,7 +57,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.boxed();

let behaviour = Behaviour {
relay: Relay::new(local_peer_id, Default::default()),
relay: relay::Behaviour::new(local_peer_id, Default::default()),
ping: ping::Behaviour::new(ping::Config::new()),
identify: identify::Behaviour::new(identify::Config::new(
"/TODO/0.0.1".to_string(),
Expand All @@ -79,7 +79,7 @@ fn main() -> Result<(), Box<dyn Error>> {
block_on(async {
loop {
match swarm.next().await.expect("Infinite Stream.") {
SwarmEvent::Behaviour(Event::Relay(event)) => {
SwarmEvent::Behaviour(event) => {
println!("{event:?}")
}
SwarmEvent::NewListenAddr { address, .. } => {
Expand All @@ -92,42 +92,13 @@ fn main() -> Result<(), Box<dyn Error>> {
}

#[derive(NetworkBehaviour)]
#[behaviour(
out_event = "Event",
event_process = false,
prelude = "libp2p_swarm::derive_prelude"
)]
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
struct Behaviour {
relay: Relay,
relay: relay::Behaviour,
ping: ping::Behaviour,
identify: identify::Behaviour,
}

#[derive(Debug)]
enum Event {
Ping(ping::Event),
Identify(identify::Event),
Relay(relay::Event),
}

impl From<ping::Event> for Event {
fn from(e: ping::Event) -> Self {
Event::Ping(e)
}
}

impl From<identify::Event> for Event {
fn from(e: identify::Event) -> Self {
Event::Identify(e)
}
}

impl From<relay::Event> for Event {
fn from(e: relay::Event) -> Self {
Event::Relay(e)
}
}

fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {
let mut bytes = [0u8; 32];
bytes[0] = secret_key_seed;
Expand Down
Loading

0 comments on commit 9c96bbb

Please sign in to comment.