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

swarm/behaviour: Replace inject_* with on_event #3011

Merged
merged 77 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
90a1349
swarm/behaviour: Replace inject_* with on_event
mxinden Sep 3, 2022
de5c46d
swarm/behaviour: Delegate to on_event from inject_*
mxinden Sep 7, 2022
021a7f1
swarm/behaviour: Address code review
mxinden Sep 7, 2022
ff08ccd
Merge branch 'master' of https://github.com/libp2p/rust-libp2p into b…
mxinden Sep 7, 2022
3d8561d
Merge branch 'master' of https://github.com/libp2p/rust-libp2p into b…
mxinden Sep 17, 2022
07a4f20
swarm/behaviour: Add on_connection_handler_event
mxinden Sep 17, 2022
99a4b64
swarm/behaviour: Use slice
mxinden Sep 17, 2022
46f20fa
swarm/behaviour: Rename to maybe_map_handler
mxinden Sep 17, 2022
63b70fe
swarm/behaviour: Provide default for on_swarm and on_connection
mxinden Sep 17, 2022
1a33678
review: address suggestions
jxs Oct 11, 2022
09ca723
autonat/behaviour: Replace inject_* with on_event
jxs Oct 11, 2022
af7cf9b
Merge branch 'master' of github.com:libp2p/rust-libp2p into behaviour…
jxs Oct 11, 2022
4f5cc26
swarm: remove no longer required DummyBehaviour
jxs Oct 11, 2022
510fd5e
autonat/behaviour: remove no longer needed inject methods
jxs Oct 11, 2022
9bb6714
Merge branch 'master' of github.com:libp2p/rust-libp2p into behaviour…
jxs Oct 11, 2022
9b60343
swarm/behaviour: use full definition instead of type alias,
jxs Oct 12, 2022
b3743da
Merge branch 'master' into behaviour-on-event
jxs Oct 13, 2022
feccba0
swarm/behaviour: update FromSwarm enum variants to structs
jxs Oct 14, 2022
6dc84f8
autonat/behaviour: update on_swarm_event,
jxs Oct 14, 2022
e28fac5
request-response/lib: replace inject_* methods with on_*,
jxs Oct 14, 2022
80b2345
Merge branch 'master' of github.com:libp2p/rust-libp2p into behaviour…
jxs Oct 15, 2022
db4bfad
autonat/behaviour: add missing on_connection_handler_event
jxs Oct 15, 2022
aebd4a9
swarm/lib: revert back None -> peer_id
jxs Oct 15, 2022
fb63892
dcutr/behaviour: replace inject_* methods with on_*
jxs Oct 15, 2022
790d848
floodsub/layer: replace inject_* methods with on_*
jxs Oct 15, 2022
d24c92c
gossipsub/behaviour: replace inject_* methods with on_*
jxs Oct 15, 2022
e88c75d
identify/behaviour: replace inject_* methods with on_*
jxs Oct 15, 2022
0ccb577
kad/behaviour: replace inject_* methods with on_*
jxs Oct 15, 2022
e18046a
mdns/behaviour: replace inject_* methods with on_*
jxs Oct 17, 2022
133ca33
ping/lib: replace inject_* methods with on_*,
jxs Oct 17, 2022
df73ad1
relay: replace inject_* methods with on_*
jxs Oct 17, 2022
0af9601
rendezvous: replace inject_* methods with on_*
jxs Oct 17, 2022
44e0e7c
general: update changelogs
jxs Oct 17, 2022
c037679
request-response: update changelog
jxs Oct 17, 2022
92edc14
general: apply cargo fmt and clippy
jxs Oct 17, 2022
1da43f1
general: apply cargo fmt and clipy
jxs Oct 17, 2022
29f5bad
swarm/test: replace inject_* with on_* methods
jxs Oct 17, 2022
ac345b3
swarm: apply cargo fmt and clipy
jxs Oct 17, 2022
bdba3af
relay: cargo clippy
jxs Oct 17, 2022
2166857
Merge branch 'master' into behaviour-on-event
jxs Oct 17, 2022
18e717a
review: Update CHANGELOG according to Thomas review.
jxs Oct 19, 2022
30dbe46
review: remove probably not needed comments
jxs Oct 19, 2022
1a2aaeb
review: address Thomas reviews
jxs Oct 19, 2022
2e58d65
review: cargo fmt
jxs Oct 19, 2022
9c52704
review: Update CHANGELOG links to proper ones.
jxs Oct 20, 2022
f9037a2
review update swarm dependency on protocols
jxs Oct 20, 2022
0c27ea6
review: address Thomas review comments
jxs Oct 20, 2022
632bad4
review: minimize diff changes,
jxs Oct 20, 2022
59f9c56
review: update protocol crates versions,
jxs Oct 20, 2022
d095dc4
review: address Max's review comments
jxs Oct 20, 2022
e9bf9c5
review: update FromSwarm matching cases,
jxs Oct 21, 2022
52c6df1
review: remove no longer required swarm public_api test
jxs Oct 23, 2022
3300d7f
review: Address Max's review.
jxs Oct 26, 2022
c259abc
swarm: update deprcated warnings since version
jxs Oct 27, 2022
6695976
review: Address Thomas suggestions
jxs Nov 2, 2022
7090644
swarm-derive: update proc macro to use on_swarm_event,
jxs Nov 2, 2022
ae3e68a
review: Address Max suggestions
jxs Nov 3, 2022
7a68218
Merge branch 'master' of github.com:libp2p/rust-libp2p into behaviour…
jxs Nov 3, 2022
c1d053a
review: remove non need newline
jxs Nov 3, 2022
a369ee7
review: re-introduce trailing whitespace for less review diff
jxs Nov 3, 2022
fe61298
review: re-introduce trailing whitespace for less review
jxs Nov 3, 2022
eddf631
Merge branch 'master' of github.com:libp2p/rust-libp2p into behaviour…
jxs Nov 4, 2022
7c99636
review: address max suggestion,
jxs Nov 4, 2022
ba56bfd
review: nit, improve all variants matchingstyle
jxs Nov 4, 2022
6d25374
Merge branch 'master' of github.com:libp2p/rust-libp2p into behaviour…
jxs Nov 4, 2022
7d8a2b7
review: replace inject_event on missing parts,
jxs Nov 6, 2022
b9baeb3
review: address Thomas suggestion
jxs Nov 7, 2022
bb15c1f
Merge branch 'master' into behaviour-on-event
jxs Nov 11, 2022
ed3f5cf
Merge branch 'master' of github.com:libp2p/rust-libp2p into behaviour…
jxs Nov 14, 2022
73e0dcf
review: revert to call inject on inner types.
jxs Nov 14, 2022
10a5a62
Merge branch 'behaviour-on-event' into behaviour-on-event-2
jxs Nov 14, 2022
eb307bd
Merge branch 'master' of github.com:libp2p/rust-libp2p into behaviour…
jxs Nov 15, 2022
f0fd82a
review: revert swarm-derive to call inject on inner types.
jxs Nov 15, 2022
d11950b
Merge branch 'master' of github.com:libp2p/rust-libp2p into behaviour…
jxs Nov 15, 2022
dcf8235
review: revert missing var names on swarm-derive.
jxs Nov 15, 2022
cc2b56e
Merge branch 'master' of github.com:libp2p/rust-libp2p into behaviour…
jxs Nov 15, 2022
8604715
Merge branch 'master' into behaviour-on-event
mergify[bot] Nov 17, 2022
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
5 changes: 5 additions & 0 deletions protocols/autonat/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

- Update to `libp2p-request-response` `v0.23.0`.

- Replace `Behaviour`'s `NetworkBehaviour` implemention `inject_*` methods with the new `on_*` methods.
See [PR 3011].

[PR 3011]: https://github.com/libp2p/rust-libp2p/pull/3011

# 0.8.0

- Update to `libp2p-core` `v0.37.0`.
Expand Down
215 changes: 121 additions & 94 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ pub use as_server::{InboundProbeError, InboundProbeEvent};
use futures_timer::Delay;
use instant::Instant;
use libp2p_core::{
connection::ConnectionId, multiaddr::Protocol, transport::ListenerId, ConnectedPoint, Endpoint,
Multiaddr, PeerId,
connection::ConnectionId, multiaddr::Protocol, ConnectedPoint, Endpoint, Multiaddr, PeerId,
};
use libp2p_request_response::{
handler::RequestResponseHandlerEvent, ProtocolSupport, RequestId, RequestResponse,
RequestResponseConfig, RequestResponseEvent, RequestResponseMessage, ResponseChannel,
ProtocolSupport, RequestId, RequestResponse, RequestResponseConfig, RequestResponseEvent,
RequestResponseMessage, ResponseChannel,
};
use libp2p_swarm::{
DialError, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
behaviour::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr,
ExpiredListenAddr, FromSwarm,
},
ConnectionHandler, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction,
PollParameters,
};
use std::{
collections::{HashMap, VecDeque},
Expand Down Expand Up @@ -298,43 +302,32 @@ impl Behaviour {
ongoing_inbound: &mut self.ongoing_inbound,
}
}
}

impl NetworkBehaviour for Behaviour {
type ConnectionHandler = <RequestResponse<AutoNatCodec> as NetworkBehaviour>::ConnectionHandler;
type OutEvent = Event;

fn inject_connection_established(
fn on_connection_established(
&mut self,
peer: &PeerId,
conn: &ConnectionId,
endpoint: &ConnectedPoint,
failed_addresses: Option<&Vec<Multiaddr>>,
other_established: usize,
) {
self.inner.inject_connection_established(
peer,
conn,
ConnectionEstablished {
peer_id: peer,
connection_id: conn,
endpoint,
failed_addresses,
other_established,
);
let connections = self.connected.entry(*peer).or_default();
..
}: ConnectionEstablished,
) {
let connections = self.connected.entry(peer).or_default();
let addr = endpoint.get_remote_address();
let observed_addr =
if !endpoint.is_relayed() && (!self.config.only_global_ips || addr.is_global_ip()) {
Some(addr.clone())
} else {
None
};
connections.insert(*conn, observed_addr);
connections.insert(conn, observed_addr);

match endpoint {
ConnectedPoint::Dialer {
address,
role_override: Endpoint::Dialer,
} => {
if let Some(event) = self.as_server().on_outbound_connection(peer, address) {
if let Some(event) = self.as_server().on_outbound_connection(&peer, address) {
self.pending_out_events
.push_back(Event::InboundProbe(event));
}
Expand All @@ -351,79 +344,83 @@ impl NetworkBehaviour for Behaviour {
}
}

fn inject_connection_closed(
fn on_connection_closed(
&mut self,
peer: &PeerId,
conn: &ConnectionId,
endpoint: &ConnectedPoint,
handler: <Self::ConnectionHandler as IntoConnectionHandler>::Handler,
remaining_established: usize,
ConnectionClosed {
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
) {
self.inner
.inject_connection_closed(peer, conn, endpoint, handler, remaining_established);
.on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}));

if remaining_established == 0 {
self.connected.remove(peer);
self.connected.remove(&peer_id);
} else {
let connections = self.connected.get_mut(peer).expect("Peer is connected.");
connections.remove(conn);
let connections = self
.connected
.get_mut(&peer_id)
.expect("Peer is connected.");
connections.remove(&connection_id);
}
}

fn inject_dial_failure(
fn on_dial_failure(
&mut self,
peer: Option<PeerId>,
handler: Self::ConnectionHandler,
error: &DialError,
DialFailure {
peer_id,
handler,
error,
}: DialFailure<<Self as NetworkBehaviour>::ConnectionHandler>,
) {
self.inner.inject_dial_failure(peer, handler, error);
if let Some(event) = self.as_server().on_outbound_dial_error(peer, error) {
self.inner
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
peer_id,
handler,
error,
}));
if let Some(event) = self.as_server().on_outbound_dial_error(peer_id, error) {
self.pending_out_events
.push_back(Event::InboundProbe(event));
}
}

fn inject_address_change(
fn on_address_change(
&mut self,
peer: &PeerId,
conn: &ConnectionId,
old: &ConnectedPoint,
new: &ConnectedPoint,
AddressChange {
peer_id: peer,
connection_id: conn,
old,
new,
}: AddressChange,
) {
self.inner.inject_address_change(peer, conn, old, new);

if old.is_relayed() && new.is_relayed() {
return;
}
let connections = self.connected.get_mut(peer).expect("Peer is connected.");
let connections = self.connected.get_mut(&peer).expect("Peer is connected.");
let addr = new.get_remote_address();
let observed_addr =
if !new.is_relayed() && (!self.config.only_global_ips || addr.is_global_ip()) {
Some(addr.clone())
} else {
None
};
connections.insert(*conn, observed_addr);
}

fn inject_new_listen_addr(&mut self, id: ListenerId, addr: &Multiaddr) {
self.inner.inject_new_listen_addr(id, addr);
self.as_client().on_new_address();
}

fn inject_expired_listen_addr(&mut self, id: ListenerId, addr: &Multiaddr) {
self.inner.inject_expired_listen_addr(id, addr);
self.as_client().on_expired_address(addr);
}

fn inject_new_external_addr(&mut self, addr: &Multiaddr) {
self.inner.inject_new_external_addr(addr);
self.as_client().on_new_address();
connections.insert(conn, observed_addr);
}
}

fn inject_expired_external_addr(&mut self, addr: &Multiaddr) {
self.inner.inject_expired_external_addr(addr);
self.as_client().on_expired_address(addr);
}
impl NetworkBehaviour for Behaviour {
type ConnectionHandler = <RequestResponse<AutoNatCodec> as NetworkBehaviour>::ConnectionHandler;
type OutEvent = Event;

fn poll(&mut self, cx: &mut Context<'_>, params: &mut impl PollParameters) -> Poll<Action> {
loop {
Expand Down Expand Up @@ -478,35 +475,65 @@ impl NetworkBehaviour for Behaviour {
self.inner.addresses_of_peer(peer)
}

fn inject_event(
&mut self,
peer_id: PeerId,
conn: ConnectionId,
event: RequestResponseHandlerEvent<AutoNatCodec>,
) {
self.inner.inject_event(peer_id, conn, event)
fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
match event {
FromSwarm::ConnectionEstablished(connection_established) => {
self.inner
.on_swarm_event(FromSwarm::ConnectionEstablished(connection_established));
mxinden marked this conversation as resolved.
Show resolved Hide resolved
self.on_connection_established(connection_established)
}
FromSwarm::ConnectionClosed(connection_closed) => {
self.on_connection_closed(connection_closed)
}
FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure),
FromSwarm::AddressChange(address_change) => {
self.inner
.on_swarm_event(FromSwarm::AddressChange(address_change));
self.on_address_change(address_change)
}
listen_addr @ FromSwarm::NewListenAddr(_) => {
self.inner.on_swarm_event(listen_addr);
self.as_client().on_new_address();
}
FromSwarm::ExpiredListenAddr(ExpiredListenAddr { listener_id, addr }) => {
self.inner
.on_swarm_event(FromSwarm::ExpiredListenAddr(ExpiredListenAddr {
listener_id,
addr,
}));
self.as_client().on_expired_address(addr);
}
FromSwarm::ExpiredExternalAddr(ExpiredExternalAddr { addr }) => {
self.inner
.on_swarm_event(FromSwarm::ExpiredExternalAddr(ExpiredExternalAddr { addr }));
self.as_client().on_expired_address(addr);
}
external_addr @ FromSwarm::NewExternalAddr(_) => {
self.inner.on_swarm_event(external_addr);
self.as_client().on_new_address();
}
listen_failure @ FromSwarm::ListenFailure(_) => {
self.inner.on_swarm_event(listen_failure)
}
new_listener @ FromSwarm::NewListener(_) => self.inner.on_swarm_event(new_listener),
listener_error @ FromSwarm::ListenerError(_) => {
self.inner.on_swarm_event(listener_error)
}
listener_closed @ FromSwarm::ListenerClosed(_) => {
self.inner.on_swarm_event(listener_closed)
}
}
}

fn inject_listen_failure(
fn on_connection_handler_event(
&mut self,
local_addr: &Multiaddr,
send_back_addr: &Multiaddr,
handler: Self::ConnectionHandler,
peer_id: PeerId,
connection_id: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as
ConnectionHandler>::OutEvent,
) {
self.inner
.inject_listen_failure(local_addr, send_back_addr, handler)
}

fn inject_new_listener(&mut self, id: ListenerId) {
self.inner.inject_new_listener(id)
}

fn inject_listener_error(&mut self, id: ListenerId, err: &(dyn std::error::Error + 'static)) {
self.inner.inject_listener_error(id, err)
}

fn inject_listener_closed(&mut self, id: ListenerId, reason: Result<(), &std::io::Error>) {
self.inner.inject_listener_closed(id, reason)
.on_connection_handler_event(peer_id, connection_id, event)
}
}

Expand Down
5 changes: 5 additions & 0 deletions protocols/dcutr/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

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

- Replace `Behaviour`'s `NetworkBehaviour` implemention `inject_*` methods with the new `on_*` methods.
See [PR 3011].

[PR 3011]: https://github.com/libp2p/rust-libp2p/pull/3011

# 0.7.0

- Update to `libp2p-core` `v0.37.0`.
Expand Down
Loading