fix: don't report bogus empty bitswap messages

Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
ljedrz 2020-07-31 16:12:55 +02:00
parent 0d74cea3df
commit c0524d5791
2 changed files with 33 additions and 3 deletions

View File

@ -7,7 +7,7 @@
//! will allow providing and reciving IPFS blocks.
use crate::block::Block;
use crate::ledger::{Ledger, Message, Priority};
use crate::protocol::BitswapConfig;
use crate::protocol::{BitswapConfig, MessageWrapper};
use cid::Cid;
use fnv::FnvHashSet;
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
@ -208,7 +208,7 @@ impl Bitswap {
}
impl NetworkBehaviour for Bitswap {
type ProtocolsHandler = OneShotHandler<BitswapConfig, Message, Message>;
type ProtocolsHandler = OneShotHandler<BitswapConfig, Message, MessageWrapper>;
type OutEvent = BitswapEvent;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
@ -234,7 +234,14 @@ impl NetworkBehaviour for Bitswap {
//self.connected_peers.remove(peer_id);
}
fn inject_event(&mut self, source: PeerId, _connection: ConnectionId, mut message: Message) {
fn inject_event(&mut self, source: PeerId, _connection: ConnectionId, message: MessageWrapper) {
let mut message = match message {
// we just sent an outgoing bitswap message, nothing to do here
MessageWrapper::Tx => return,
// we've received a bitswap message, process it
MessageWrapper::Rx(msg) => msg,
};
debug!("bitswap: inject_event from {}: {:?}", source, message);
let current_wantlist = self.local_wantlist();

View File

@ -76,6 +76,29 @@ where
}
}
/// An object to facilitate communication between the `OneShotHandler` and the `BitswapHandler`.
#[derive(Debug)]
pub enum MessageWrapper {
/// We received a `Message` from a remote.
Rx(Message),
/// We successfully sent a `Message`.
Tx,
}
impl From<Message> for MessageWrapper {
#[inline]
fn from(message: Message) -> Self {
Self::Rx(message)
}
}
impl From<()> for MessageWrapper {
#[inline]
fn from(_: ()) -> Self {
Self::Tx
}
}
#[cfg(test)]
mod tests {
/*