Skip to content

Commit

Permalink
feat(p2p): make bitswap toglable
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Sep 19, 2022
1 parent 5af38d1 commit 648bcbf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
36 changes: 27 additions & 9 deletions iroh-p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod peer_manager;
pub(crate) struct NodeBehaviour {
ping: Ping,
identify: Identify,
pub(crate) bitswap: Bitswap,
pub(crate) bitswap: Toggle<Bitswap>,
pub(crate) kad: Toggle<Kademlia<MemoryStore>>,
mdns: Toggle<Mdns>,
pub(crate) autonat: Toggle<autonat::Behaviour>,
Expand All @@ -51,10 +51,16 @@ impl NodeBehaviour {
config: &Libp2pConfig,
relay_client: Option<relay::v2::client::Client>,
) -> Result<Self> {
let bs_config = BitswapConfig::default();
let bitswap = Bitswap::new(bs_config);
let peer_manager = PeerManager::default();

let bitswap = if config.bitswap {
let bs_config = BitswapConfig::default();
Some(Bitswap::new(bs_config))
} else {
None
}
.into();

let mdns = if config.mdns {
Some(Mdns::new(Default::default()).await?)
} else {
Expand Down Expand Up @@ -166,28 +172,38 @@ impl NodeBehaviour {

/// Send a block to a peer over bitswap
pub fn send_block(&mut self, peer_id: &PeerId, cid: Cid, data: Bytes) -> Result<()> {
self.bitswap.send_block(peer_id, cid, data);
if let Some(bs) = self.bitswap.as_mut() {
bs.send_block(peer_id, cid, data);
}
Ok(())
}

pub fn cancel_block(&mut self, cid: &Cid) -> Result<()> {
self.bitswap.cancel_block(cid);
if let Some(bs) = self.bitswap.as_mut() {
bs.cancel_block(cid);
}
Ok(())
}

pub fn cancel_want_block(&mut self, cid: &Cid) -> Result<()> {
self.bitswap.cancel_want_block(cid);
if let Some(bs) = self.bitswap.as_mut() {
bs.cancel_want_block(cid);
}
Ok(())
}

/// Send a block have to a peer over bitswap
pub fn send_have_block(&mut self, peer_id: &PeerId, cid: Cid) -> Result<()> {
self.bitswap.send_have_block(peer_id, cid);
if let Some(bs) = self.bitswap.as_mut() {
bs.send_have_block(peer_id, cid);
}
Ok(())
}

pub fn find_providers(&mut self, cid: Cid, priority: Priority) -> Result<()> {
self.bitswap.find_providers(cid, priority);
if let Some(bs) = self.bitswap.as_mut() {
bs.find_providers(cid, priority);
}
Ok(())
}

Expand All @@ -202,7 +218,9 @@ impl NodeBehaviour {
priority: Priority,
providers: HashSet<PeerId>,
) -> Result<(), Box<dyn Error>> {
self.bitswap.want_block(cid, priority, providers);
if let Some(bs) = self.bitswap.as_mut() {
bs.want_block(cid, priority, providers);
}
Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions iroh-p2p/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub struct Libp2pConfig {
pub bootstrap_peers: Vec<Multiaddr>,
/// Mdns discovery enabled.
pub mdns: bool,
/// Bitswap discovery enabled.
pub bitswap: bool,
/// Kademlia discovery enabled.
pub kademlia: bool,
/// Autonat holepunching enabled.
Expand Down Expand Up @@ -159,6 +161,7 @@ impl Default for Libp2pConfig {
relay_server: true,
relay_client: true,
gossipsub: true,
bitswap: true,
max_conns_pending_out: 256,
max_conns_pending_in: 256,
max_conns_in: 256,
Expand Down Expand Up @@ -293,6 +296,7 @@ mod tests {
expect.insert("kademlia".to_string(), Value::new(None, default.kademlia));
expect.insert("autonat".to_string(), Value::new(None, default.autonat));
expect.insert("mdns".to_string(), Value::new(None, default.mdns));
expect.insert("bitswap".to_string(), Value::new(None, default.bitswap));
expect.insert(
"relay_server".to_string(),
Value::new(None, default.relay_server),
Expand Down
12 changes: 6 additions & 6 deletions iroh-p2p/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,9 @@ impl<KeyStorage: Storage> Node<KeyStorage> {
.swarm
.behaviour()
.bitswap
.supported_protocols()
.to_vec();
.as_ref()
.map(|bs| bs.supported_protocols().to_vec())
.unwrap_or_default();
let mut protocol_bs_name = None;
for protocol in protocols {
let p = protocol.as_bytes();
Expand All @@ -678,10 +679,9 @@ impl<KeyStorage: Storage> Node<KeyStorage> {
}
}
if protocol_bs_name.is_some() {
self.swarm
.behaviour_mut()
.bitswap
.add_peer(peer_id, protocol_bs_name);
if let Some(bs) = self.swarm.behaviour_mut().bitswap.as_mut() {
bs.add_peer(peer_id, protocol_bs_name);
}
}
} else if p == b"/libp2p/autonat/1.0.0" {
// TODO: expose protocol name on `libp2p::autonat`.
Expand Down

0 comments on commit 648bcbf

Please sign in to comment.