Skip to content

Commit

Permalink
chore: Use default handler in bitswap behaviour (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Jun 4, 2024
1 parent 6849519 commit 9d82ff9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- chore: Cleanup Keystore logic and implementation. [PR 222](https://github.com/dariusc93/rust-ipfs/pull/222)
- feat: Add {Into}AddPeerOpt. [PR 226](https://github.com/dariusc93/rust-ipfs/pull/226)
- refactor: Simplify bitswap WantSession. [PR 234](https://github.com/dariusc93/rust-ipfs/pull/234)
- chore: Use default handler in bitswap behaviour. [PR 235](https://github.com/dariusc93/rust-ipfs/pull/235)

# 0.11.19
- chore: Pin getrandom to 0.2.14 due to libc 0.2.154 being yanked.
Expand Down
19 changes: 3 additions & 16 deletions src/p2p/bitswap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use libp2p::{
swarm::{
behaviour::ConnectionEstablished, dial_opts::DialOpts, ConnectionClosed, ConnectionDenied,
ConnectionId, DialFailure, FromSwarm, NetworkBehaviour, NotifyHandler, OneShotHandler,
OneShotHandlerConfig, SubstreamProtocol, THandler, THandlerInEvent, THandlerOutEvent,
ToSwarm,
THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
},
Multiaddr, PeerId,
};
Expand Down Expand Up @@ -334,13 +333,7 @@ impl NetworkBehaviour for Behaviour {
_: &Multiaddr,
_: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(OneShotHandler::new(
SubstreamProtocol::new(Default::default(), ()),
OneShotHandlerConfig {
max_dial_negotiated: 100,
..Default::default()
},
))
Ok(OneShotHandler::default())
}

fn handle_established_outbound_connection(
Expand All @@ -350,13 +343,7 @@ impl NetworkBehaviour for Behaviour {
_: &Multiaddr,
_: Endpoint,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(OneShotHandler::new(
SubstreamProtocol::new(Default::default(), ()),
OneShotHandlerConfig {
max_dial_negotiated: 100,
..Default::default()
},
))
Ok(OneShotHandler::default())
}

fn on_connection_handler_event(
Expand Down
9 changes: 3 additions & 6 deletions src/p2p/bitswap/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ impl Stream for WantSession {
loop {
match &mut this.state {
WantSessionState::Idle => {
this.waker = Some(cx.waker().clone());
if let Some(peer_id) = this.cancel.pop_back() {
// Kick start the cancel requests.
return Poll::Ready(Some(WantSessionEvent::SendCancel { peer_id }));
Expand All @@ -309,6 +308,8 @@ impl Stream for WantSession {
}
}

this.waker = Some(cx.waker().clone());

return Poll::Pending;
}
WantSessionState::NextBlock { previous_peer_id } => {
Expand Down Expand Up @@ -379,8 +380,6 @@ impl Stream for WantSession {
Ok(cid) => {
tracing::info!(session = %self.cid, block = %cid, name = "want_session", "block stored in block store");
self.state = WantSessionState::Complete;

cx.waker().wake_by_ref();
return Poll::Ready(Some(WantSessionEvent::BlockStored));
}
Err(e) => {
Expand All @@ -401,7 +400,6 @@ impl Stream for WantSession {

this.cancel.extend(peers);
// Wake up the task so the stream would poll any cancel requests
cx.waker().wake_by_ref();
this.state = WantSessionState::Idle;
}
}
Expand Down Expand Up @@ -485,7 +483,7 @@ impl HaveSession {

pub fn want_block(&mut self, peer_id: PeerId, send_dont_have: bool) {
if self.want.contains_key(&peer_id) {
tracing::warn!(session = %self.cid, %peer_id, "peer requested block");
tracing::warn!(session = %self.cid, %peer_id, "peer already requested block. Ignoring additional request");
return;
}

Expand Down Expand Up @@ -643,7 +641,6 @@ impl Stream for HaveSession {
HaveSessionState::ContainBlock { fut } => {
let have = ready!(fut.poll_unpin(cx)).unwrap_or_default();
this.have = Some(have);
cx.waker().wake_by_ref();
this.state = HaveSessionState::Idle;
}
// Maybe we should have a lock on a single lock to prevent GC from cleaning it up or being removed while waiting for it to be
Expand Down

0 comments on commit 9d82ff9

Please sign in to comment.