diff --git a/bitswap/src/behaviour.rs b/bitswap/src/behaviour.rs index a99d6b5b..cfd608c3 100644 --- a/bitswap/src/behaviour.rs +++ b/bitswap/src/behaviour.rs @@ -45,6 +45,16 @@ impl Bitswap { } } + /// Return the wantlist of the local node + pub fn local_wantlist(&self) -> Vec<(Cid, Priority)> { + self.wanted_blocks.iter().map(|(cid, prio)| (cid.clone(), *prio)).collect() + } + + /// Return the wantlist of a peer, if known + pub fn peer_wantlist(&self, peer: &PeerId) -> Option> { + self.connected_peers.get(peer).map(Ledger::wantlist) + } + /// Connect to peer. /// /// Called from Kademlia behaviour. diff --git a/bitswap/src/ledger.rs b/bitswap/src/ledger.rs index b034d251..bb93fe43 100644 --- a/bitswap/src/ledger.rs +++ b/bitswap/src/ledger.rs @@ -70,6 +70,11 @@ impl Ledger { self.received_want_list.insert(cid.to_owned(), *priority); } } + + /// Returns the blocks wanted by the peer in unspecified order + pub fn wantlist(&self) -> Vec<(Cid, Priority)> { + self.received_want_list.iter().map(|(cid, prio)| (cid.clone(), *prio)).collect() + } } #[derive(Debug, Clone, PartialEq)] diff --git a/src/lib.rs b/src/lib.rs index 5dce5d9a..702fee9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -635,7 +635,12 @@ impl Future for IpfsFuture { let _ = ret.send(self.swarm.pubsub().subscribed_topics()); } IpfsEvent::WantList(peer, ret) => { - todo!() + let list = if let Some(peer) = peer { + self.swarm.bitswap().peer_wantlist(&peer).unwrap_or_default() + } else { + self.swarm.bitswap().local_wantlist() + }; + let _ = ret.send(list); } IpfsEvent::BitswapStats(ret) => { todo!() diff --git a/src/p2p/behaviour.rs b/src/p2p/behaviour.rs index a6c4ce84..f25c7dc7 100644 --- a/src/p2p/behaviour.rs +++ b/src/p2p/behaviour.rs @@ -236,6 +236,10 @@ impl Behaviour { pub fn pubsub(&mut self) -> &mut Pubsub { &mut self.pubsub } + + pub fn bitswap(&mut self) -> &mut Bitswap { + &mut self.bitswap + } } /// Create a IPFS behaviour with the IPFS bootstrap nodes.