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

Commit ab0aec3

Browse files
author
Joonas Koivunen
committed
refactor: depend in p2p::Behaviour on Arc<Repo<_>>
... instead of Ipfs. this was the original idea in #343 perhaps.
1 parent 2180135 commit ab0aec3

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,18 +415,22 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
415415

416416
let (to_task, receiver) = channel::<IpfsEvent>(1);
417417

418+
let facade_span = options
419+
.span
420+
.take()
421+
.unwrap_or_else(|| tracing::trace_span!("ipfs"));
422+
423+
let swarm_span = tracing::trace_span!(parent: facade_span.clone(), "swarm");
424+
418425
let ipfs = Ipfs {
419-
span: options
420-
.span
421-
.take()
422-
.unwrap_or_else(|| tracing::trace_span!("ipfs")),
423-
repo,
426+
span: facade_span,
427+
repo: repo.clone(),
424428
keys: DebuggableKeypair(keys),
425429
to_task,
426430
};
427431

428432
let swarm_options = SwarmOptions::from(&options);
429-
let swarm = create_swarm(swarm_options, ipfs.clone()).await?;
433+
let swarm = create_swarm(swarm_options, swarm_span, repo).await?;
430434

431435
let IpfsOptions {
432436
listening_addrs, ..

src/p2p/behaviour.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use super::pubsub::Pubsub;
22
use super::swarm::{Connection, Disconnector, SwarmApi};
33
use crate::config::BOOTSTRAP_NODES;
44
use crate::p2p::{MultiaddrWithPeerId, SwarmOptions};
5-
use crate::repo::BlockPut;
5+
use crate::repo::{BlockPut, Repo};
66
use crate::subscription::{SubscriptionFuture, SubscriptionRegistry};
7-
use crate::{Ipfs, IpfsTypes};
7+
use crate::IpfsTypes;
88
use anyhow::anyhow;
99
use bitswap::{Bitswap, BitswapEvent};
1010
use cid::Cid;
@@ -16,16 +16,15 @@ use libp2p::mdns::{MdnsEvent, TokioMdns};
1616
use libp2p::ping::{Ping, PingEvent};
1717
use libp2p::swarm::toggle::Toggle;
1818
use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourEventProcess};
19-
use libp2p::NetworkBehaviour;
2019
use multibase::Base;
2120
use std::{convert::TryInto, sync::Arc};
2221
use tokio::task;
2322

2423
/// Behaviour type.
25-
#[derive(NetworkBehaviour)]
24+
#[derive(libp2p::NetworkBehaviour)]
2625
pub struct Behaviour<Types: IpfsTypes> {
2726
#[behaviour(ignore)]
28-
ipfs: Ipfs<Types>,
27+
repo: Arc<Repo<Types>>,
2928
mdns: Toggle<TokioMdns>,
3029
kademlia: Kademlia<MemoryStore>,
3130
#[behaviour(ignore)]
@@ -314,11 +313,11 @@ impl<Types: IpfsTypes> NetworkBehaviourEventProcess<BitswapEvent> for Behaviour<
314313
fn inject_event(&mut self, event: BitswapEvent) {
315314
match event {
316315
BitswapEvent::ReceivedBlock(peer_id, block) => {
317-
let ipfs = self.ipfs.clone();
316+
let repo = self.repo.clone();
318317
let peer_stats = Arc::clone(&self.bitswap.stats.get(&peer_id).unwrap());
319318
task::spawn(async move {
320319
let bytes = block.data().len() as u64;
321-
let res = ipfs.repo.put_block(block.clone()).await;
320+
let res = repo.put_block(block.clone()).await;
322321
match res {
323322
Ok((_, uniqueness)) => match uniqueness {
324323
BlockPut::NewBlock => peer_stats.update_incoming_unique(bytes),
@@ -343,10 +342,10 @@ impl<Types: IpfsTypes> NetworkBehaviourEventProcess<BitswapEvent> for Behaviour<
343342
);
344343

345344
let queued_blocks = self.bitswap().queued_blocks.clone();
346-
let ipfs = self.ipfs.clone();
345+
let repo = self.repo.clone();
347346

348347
task::spawn(async move {
349-
match ipfs.repo.get_block_now(&cid).await {
348+
match repo.get_block_now(&cid).await {
350349
Ok(Some(block)) => {
351350
let _ = queued_blocks.unbounded_send((peer_id, block));
352351
}
@@ -413,7 +412,7 @@ impl<Types: IpfsTypes> NetworkBehaviourEventProcess<IdentifyEvent> for Behaviour
413412

414413
impl<Types: IpfsTypes> Behaviour<Types> {
415414
/// Create a Kademlia behaviour with the IPFS bootstrap nodes.
416-
pub async fn new(options: SwarmOptions, ipfs: Ipfs<Types>) -> Self {
415+
pub async fn new(options: SwarmOptions, repo: Arc<Repo<Types>>) -> Self {
417416
info!("net: starting with peer id {}", options.peer_id);
418417

419418
let mdns = if options.mdns {
@@ -454,7 +453,7 @@ impl<Types: IpfsTypes> Behaviour<Types> {
454453
}
455454

456455
Behaviour {
457-
ipfs,
456+
repo,
458457
mdns,
459458
kademlia,
460459
kad_subscriptions: Default::default(),
@@ -646,7 +645,7 @@ impl<Types: IpfsTypes> Behaviour<Types> {
646645
/// Create a IPFS behaviour with the IPFS bootstrap nodes.
647646
pub async fn build_behaviour<TIpfsTypes: IpfsTypes>(
648647
options: SwarmOptions,
649-
ipfs: Ipfs<TIpfsTypes>,
648+
repo: Arc<Repo<TIpfsTypes>>,
650649
) -> Behaviour<TIpfsTypes> {
651-
Behaviour::new(options, ipfs).await
650+
Behaviour::new(options, repo).await
652651
}

src/p2p/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! P2P handling for IPFS nodes.
2-
use crate::{Ipfs, IpfsOptions, IpfsTypes};
2+
use crate::repo::Repo;
3+
use crate::{IpfsOptions, IpfsTypes};
34
use libp2p::identity::Keypair;
45
use libp2p::Swarm;
56
use libp2p::{Multiaddr, PeerId};
67
use std::io;
8+
use std::sync::Arc;
79
use tracing::Span;
810

911
pub(crate) mod addr;
@@ -46,17 +48,16 @@ impl From<&IpfsOptions> for SwarmOptions {
4648
/// Creates a new IPFS swarm.
4749
pub async fn create_swarm<TIpfsTypes: IpfsTypes>(
4850
options: SwarmOptions,
49-
ipfs: Ipfs<TIpfsTypes>,
51+
swarm_span: Span,
52+
repo: Arc<Repo<TIpfsTypes>>,
5053
) -> io::Result<TSwarm<TIpfsTypes>> {
5154
let peer_id = options.peer_id.clone();
5255

5356
// Set up an encrypted TCP transport over the Mplex protocol.
5457
let transport = transport::build_transport(options.keypair.clone())?;
5558

56-
let swarm_span = ipfs.span.clone();
57-
5859
// Create a Kademlia behaviour
59-
let behaviour = behaviour::build_behaviour(options, ipfs).await;
60+
let behaviour = behaviour::build_behaviour(options, repo).await;
6061

6162
// Create a Swarm
6263
let swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id)

0 commit comments

Comments
 (0)