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

refactor(relay): revise public API to follow naming convention #3238

Merged
merged 34 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6453766
rename relay::Relay to relay::Behaviour.
jxs Dec 13, 2022
0f1c10e
rename client::Client to client::Behaviour.
jxs Dec 13, 2022
796425a
rename client::transport::ClientTransport to client::transport::Trans…
jxs Dec 13, 2022
f0c8429
rename relay::v2::client::RelayError to relay::v2::client::Error.
jxs Dec 13, 2022
9963cfa
rename relay::v2::client::RelayListener to relay::v2::client::Listener.
jxs Dec 13, 2022
604178b
add CHANGELOG.md entries
jxs Dec 13, 2022
395d516
add missing links to CHANGELOG.md
jxs Dec 13, 2022
50207ba
move relay::v2 root symbols to relay::
jxs Dec 14, 2022
948f87b
move relay::v2::client symbols to relay::client and use them across t…
jxs Dec 14, 2022
dcbca3a
move relay::v2::copy_future module to relay::copy_future and use it a…
jxs Dec 14, 2022
db1ff9f
move relay::v2::relay symbols to relay::behaviour,
jxs Dec 14, 2022
2171c36
chore(ROADMAP): Mark Kademlia efficient querying as done (#3233)
mxinden Dec 13, 2022
e6e4005
feat: move Identify I/O from NetworkBehaviour to ConnectionHandler (#…
jxs Dec 13, 2022
f15d82a
ci(caching): make caching more effective (#3215)
thomaseizinger Dec 13, 2022
63f03be
refactor(swarm)!: deprecate `PollParameters` where possible (#3153)
thomaseizinger Dec 14, 2022
8744f97
Merge branch 'master' of github.com:libp2p/rust-libp2p into rename-relay
jxs Dec 14, 2022
009aadc
fix typo on client deprecation message.
jxs Dec 14, 2022
b4bb917
Update CHANGELOG.md.
jxs Dec 14, 2022
e32d990
Merge branch 'master' of github.com:libp2p/rust-libp2p into rename-relay
jxs Dec 14, 2022
59d3225
Merge branch 'master' of github.com:libp2p/rust-libp2p into rename-relay
jxs Dec 14, 2022
4134363
multiple updates:
jxs Dec 14, 2022
9eb0e21
deprecate client::Behaviour::new_transport_and_behaviour,
jxs Dec 15, 2022
238c779
move UpgradeErrors to inbound or outbound modules.
jxs Dec 15, 2022
43ea4a8
make client::Transport types private.
jxs Dec 15, 2022
12c1128
Merge branch 'master' of github.com:libp2p/rust-libp2p into rename-relay
jxs Dec 15, 2022
64e6dd9
review: apply multiple suggestions:
jxs Dec 16, 2022
bc66c5b
review: address left deprecation messages,
jxs Dec 17, 2022
6f63553
Merge branch 'master' of github.com:libp2p/rust-libp2p into rename-relay
jxs Dec 17, 2022
91241e7
Merge branch 'master' of github.com:libp2p/rust-libp2p into rename-relay
jxs Dec 20, 2022
e3a8f0a
adress Thomas multiple review suggestions.
jxs Dec 21, 2022
7f653b3
Merge branch 'master' of github.com:libp2p/rust-libp2p into rename-relay
jxs Dec 21, 2022
0951271
review: apply suggestions.
jxs Dec 23, 2022
f3bf4bd
Merge branch 'master' into rename-relay
jxs Dec 24, 2022
c532b34
Merge branch 'master' into rename-relay
mxinden Jan 2, 2023
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
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
6 changes: 3 additions & 3 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::client;
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
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) = 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: client::Behaviour,
ping: ping::Behaviour,
identify: identify::Behaviour,
dcutr: dcutr::behaviour::Behaviour,
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::Behaviour,
}

#[derive(Debug)]
enum ClientEvent {
Relay(client::Event),
Relay(relay::client::Event),
Dcutr(dcutr::behaviour::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,5 +1,15 @@
# 0.15.0 [unreleased]

- Rename types as per [discussion 2174].
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
`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 3235].

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

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

# 0.14.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 Down Expand Up @@ -98,7 +98,7 @@ fn main() -> Result<(), Box<dyn Error>> {
prelude = "libp2p_swarm::derive_prelude"
)]
struct Behaviour {
relay: Relay,
relay: relay::Behaviour,
ping: ping::Behaviour,
identify: identify::Behaviour,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
mod handler;
pub mod rate_limiter;

use crate::v2::message_proto;
use crate::v2::protocol::inbound_hop;
use crate::message_proto;
use crate::protocol::{inbound_hop, outbound_stop};
use either::Either;
use instant::Instant;
use libp2p_core::connection::ConnectionId;
Expand All @@ -41,9 +41,7 @@ use std::ops::Add;
use std::task::{Context, Poll};
use std::time::Duration;

use super::protocol::outbound_stop;

/// Configuration for the [`Relay`] [`NetworkBehaviour`].
/// Configuration for the relay [`Behaviour`].
///
/// # Panics
///
Expand Down Expand Up @@ -126,7 +124,7 @@ impl Default for Config {
}
}

/// The events produced by the [`Relay`] behaviour.
/// The events produced by the relay `Behaviour`.
#[derive(Debug)]
pub enum Event {
/// An inbound reservation request has been accepted.
Expand Down Expand Up @@ -189,9 +187,9 @@ pub enum Event {
},
}

/// [`Relay`] is a [`NetworkBehaviour`] that implements the relay server
/// [`NetworkBehaviour`] implementation of the relay server
/// functionality of the circuit relay v2 protocol.
pub struct Relay {
pub struct Behaviour {
config: Config,

local_peer_id: PeerId,
Expand All @@ -205,7 +203,7 @@ pub struct Relay {
external_addresses: ExternalAddresses,
}

impl Relay {
impl Behaviour {
pub fn new(local_peer_id: PeerId, config: Config) -> Self {
Self {
config,
Expand Down Expand Up @@ -251,7 +249,7 @@ impl Relay {
}
}

impl NetworkBehaviour for Relay {
impl NetworkBehaviour for Behaviour {
type ConnectionHandler = handler::Prototype;
type OutEvent = Event;

Expand Down Expand Up @@ -745,7 +743,7 @@ impl Add<u64> for CircuitId {
}

/// A [`NetworkBehaviourAction`], either complete, or still requiring data from [`PollParameters`]
/// before being returned in [`Relay::poll`].
/// before being returned in [`Behaviour::poll`].
#[allow(clippy::large_enum_variant)]
enum Action {
Done(NetworkBehaviourAction<Event, handler::Prototype>),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::v2::copy_future::CopyFuture;
use crate::v2::message_proto::Status;
use crate::v2::protocol::{inbound_hop, outbound_stop};
use crate::v2::relay::CircuitId;
use crate::behaviour::CircuitId;
use crate::copy_future::CopyFuture;
use crate::message_proto::Status;
use crate::protocol::{inbound_hop, outbound_stop};
use bytes::Bytes;
use either::Either;
use futures::channel::oneshot::{self, Canceled};
Expand Down
51 changes: 51 additions & 0 deletions protocols/relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,60 @@

#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

mod behaviour;
mod copy_future;
mod priv_client;
mod protocol;
pub mod v2;

#[allow(clippy::derive_partial_eq_without_eq)]
mod message_proto {
include!(concat!(env!("OUT_DIR"), "/message_v2.pb.rs"));
}

pub use behaviour::{Behaviour, CircuitId, Config, Event};
pub use protocol::{HOP_PROTOCOL_NAME, STOP_PROTOCOL_NAME};

/// Types related to the relay protocol inbound.
pub mod inbound {
pub mod hop {
pub use crate::protocol::inbound_hop::FatalUpgradeError;
}
pub mod stop {
pub use crate::protocol::inbound_stop::FatalUpgradeError;
}
}

/// Types related to the relay protocol outbound.
pub mod outbound {
pub mod hop {
pub use crate::protocol::outbound_hop::FatalUpgradeError;
}
pub mod stop {
pub use crate::protocol::outbound_stop::FatalUpgradeError;
}
}

/// Everything related to the relay protocol from a client's perspective.
pub mod client {
pub use crate::priv_client::{new, Behaviour, Connection, Event, Transport};

pub mod transport {
pub use crate::priv_client::transport::Error;
}
}

// Check that we can safely cast a `usize` to a `u64`.
static_assertions::const_assert! {
std::mem::size_of::<usize>() <= std::mem::size_of::<u64>()
}

/// The ID of an outgoing / incoming, relay / destination request.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct RequestId(u64);

impl RequestId {
fn new() -> RequestId {
RequestId(rand::random())
}
}
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
File renamed without changes.
Loading