Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

Commit c0524d5

Browse files
committed
fix: don't report bogus empty bitswap messages
Signed-off-by: ljedrz <ljedrz@gmail.com>
1 parent 0d74cea commit c0524d5

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

bitswap/src/behaviour.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! will allow providing and reciving IPFS blocks.
88
use crate::block::Block;
99
use crate::ledger::{Ledger, Message, Priority};
10-
use crate::protocol::BitswapConfig;
10+
use crate::protocol::{BitswapConfig, MessageWrapper};
1111
use cid::Cid;
1212
use fnv::FnvHashSet;
1313
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
@@ -208,7 +208,7 @@ impl Bitswap {
208208
}
209209

210210
impl NetworkBehaviour for Bitswap {
211-
type ProtocolsHandler = OneShotHandler<BitswapConfig, Message, Message>;
211+
type ProtocolsHandler = OneShotHandler<BitswapConfig, Message, MessageWrapper>;
212212
type OutEvent = BitswapEvent;
213213

214214
fn new_handler(&mut self) -> Self::ProtocolsHandler {
@@ -234,7 +234,14 @@ impl NetworkBehaviour for Bitswap {
234234
//self.connected_peers.remove(peer_id);
235235
}
236236

237-
fn inject_event(&mut self, source: PeerId, _connection: ConnectionId, mut message: Message) {
237+
fn inject_event(&mut self, source: PeerId, _connection: ConnectionId, message: MessageWrapper) {
238+
let mut message = match message {
239+
// we just sent an outgoing bitswap message, nothing to do here
240+
MessageWrapper::Tx => return,
241+
// we've received a bitswap message, process it
242+
MessageWrapper::Rx(msg) => msg,
243+
};
244+
238245
debug!("bitswap: inject_event from {}: {:?}", source, message);
239246

240247
let current_wantlist = self.local_wantlist();

bitswap/src/protocol.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,29 @@ where
7676
}
7777
}
7878

79+
/// An object to facilitate communication between the `OneShotHandler` and the `BitswapHandler`.
80+
#[derive(Debug)]
81+
pub enum MessageWrapper {
82+
/// We received a `Message` from a remote.
83+
Rx(Message),
84+
/// We successfully sent a `Message`.
85+
Tx,
86+
}
87+
88+
impl From<Message> for MessageWrapper {
89+
#[inline]
90+
fn from(message: Message) -> Self {
91+
Self::Rx(message)
92+
}
93+
}
94+
95+
impl From<()> for MessageWrapper {
96+
#[inline]
97+
fn from(_: ()) -> Self {
98+
Self::Tx
99+
}
100+
}
101+
79102
#[cfg(test)]
80103
mod tests {
81104
/*

0 commit comments

Comments
 (0)