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

Commit

Permalink
Move bitswap into it's own crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Craven committed Mar 13, 2020
1 parent d755576 commit 1428034
Show file tree
Hide file tree
Showing 28 changed files with 554 additions and 630 deletions.
70 changes: 47 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ nightly = []
all = ["rocksdb"]

[dependencies]
async-std = { version = "1.5.0", features = [ "unstable", "std" ] }
anyhow = "1.0.26"
async-std = { version = "1.5.0", features = [ "attributes", "std" ] }
async-trait = "0.1.24"
bitswap = { path = "bitswap" }
byteorder = "1.3.4"
crossbeam = "0.7.3"
dirs = "2.0.2"
domain = { git = "https://github.com/nlnetlabs/domain", features = ["resolv"] }
env_logger = "0.7.1"
failure = "0.1.7"
fnv = "1.0.6"
futures = { version = "0.3.4", features = [ "compat", "io-compat", "async-await" ] }
libipld = { version = "0.1.0", features = ["dag-pb"] }
Expand All @@ -28,16 +29,19 @@ multihash = "0.10.1"
prost = "0.6.1"
rand = "0.7.3"
rocksdb = { version = "0.13.0", optional = true }
rustc-serialize = "0.3.24"
serde = "1.0.104"
serde_derive = "1.0.104"
serde_json = "1.0.48"
thiserror = "1.0.11"

[build-dependencies]
prost-build = "0.6.1"

[dev-dependencies]
env_logger = "0.7.1"

[workspace]
members = [ "http" ]
members = [ "bitswap", "http" ]

[patch.crates-io]
ctr = { git = "https://github.com/koivunej/stream-ciphers.git", branch = "ctr128-64to128" }
21 changes: 21 additions & 0 deletions bitswap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "bitswap"
version = "0.1.0"
authors = ["David Craven <[email protected]>"]
edition = "2018"

[build-dependencies]
prost-build = "0.6.1"

[dependencies]
anyhow = "1.0.26"
async-std = "1.5.0"
async-trait = "0.1.24"
fnv = "1.0.6"
futures = "0.3.4"
libipld = "0.1.0"
libp2p-core = "0.16.0"
libp2p-swarm = "0.16.1"
log = "0.4.8"
prost = "0.6.1"
thiserror = "1.0.11"
3 changes: 3 additions & 0 deletions bitswap/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
prost_build::compile_protos(&["src/bitswap_pb.proto"], &["src"]).unwrap();
}
30 changes: 13 additions & 17 deletions src/bitswap/behaviour.rs → bitswap/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
//!
//! The `Bitswap` struct implements the `NetworkBehaviour` trait. When used, it
//! will allow providing and reciving IPFS blocks.
use crate::bitswap::ledger::{Ledger, Message, Priority, I, O};
use crate::bitswap::protocol::BitswapConfig;
use crate::bitswap::strategy::{Strategy, StrategyEvent};
use crate::block::{Block, Cid};
use crate::p2p::SwarmTypes;
use crate::block::Block;
use crate::ledger::{Ledger, Message, Priority, I, O};
use crate::protocol::BitswapConfig;
use crate::strategy::{Strategy, StrategyEvent};
use fnv::FnvHashSet;
use futures::task::Context;
use futures::task::Poll;
use libp2p::core::ConnectedPoint;
use libp2p::swarm::protocols_handler::{IntoProtocolsHandler, OneShotHandler, ProtocolsHandler};
use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p::{Multiaddr, PeerId};
use libipld::cid::Cid;
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::protocols_handler::{IntoProtocolsHandler, OneShotHandler, ProtocolsHandler};
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use std::collections::{HashMap, VecDeque};

/// Network behaviour that handles sending and receiving IPFS blocks.
pub struct Bitswap<TSwarmTypes: SwarmTypes> {
pub struct Bitswap<TStrategy> {
/// Queue of events to report to the user.
events: VecDeque<NetworkBehaviourAction<Message<O>, ()>>,
/// List of peers to send messages to.
Expand All @@ -30,12 +29,12 @@ pub struct Bitswap<TSwarmTypes: SwarmTypes> {
/// Wanted blocks
wanted_blocks: HashMap<Cid, Priority>,
/// Strategy
strategy: TSwarmTypes::TStrategy,
strategy: TStrategy,
}

impl<TSwarmTypes: SwarmTypes> Bitswap<TSwarmTypes> {
impl<TStrategy> Bitswap<TStrategy> {
/// Creates a `Bitswap`.
pub fn new(strategy: TSwarmTypes::TStrategy) -> Self {
pub fn new(strategy: TStrategy) -> Self {
debug!("bitswap: new");
Bitswap {
events: VecDeque::new(),
Expand Down Expand Up @@ -130,10 +129,7 @@ impl<TSwarmTypes: SwarmTypes> Bitswap<TSwarmTypes> {
}
}

impl<TSwarmTypes> NetworkBehaviour for Bitswap<TSwarmTypes>
where
TSwarmTypes: SwarmTypes,
{
impl<TStrategy: Strategy> NetworkBehaviour for Bitswap<TStrategy> {
type ProtocolsHandler = OneShotHandler<BitswapConfig, Message<O>, InnerMessage>;
type OutEvent = ();

Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions bitswap/src/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use libipld::cid::Cid;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Block {
pub cid: Cid,
pub data: Box<[u8]>,
}

impl Block {
pub fn new(data: Box<[u8]>, cid: Cid) -> Self {
Self { cid, data }
}

pub fn cid(&self) -> &Cid {
&self.cid
}

pub fn data(&self) -> &[u8] {
&self.data
}
}
11 changes: 11 additions & 0 deletions bitswap/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum BitswapError {
#[error("Error while reading from socket: {0}")]
ReadError(#[from] libp2p_core::upgrade::ReadOneError),
#[error("Error while decoding bitswap message: {0}")]
ProtobufError(#[from] prost::DecodeError),
#[error("Error while parsing cid: {0}")]
Cid(#[from] libipld::cid::Error),
}
25 changes: 14 additions & 11 deletions src/bitswap/ledger.rs → bitswap/src/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::bitswap::bitswap_pb;
use crate::block::{Block, Cid};
use crate::error::Error;
use libipld::cid::Prefix;
use crate::bitswap_pb;
use crate::block::Block;
use crate::error::BitswapError;
use core::convert::TryFrom;
use core::marker::PhantomData;
use libipld::cid::{Cid, Prefix};
use prost::Message as ProstMessage;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::marker::PhantomData;

pub type Priority = i32;

Expand Down Expand Up @@ -109,7 +109,7 @@ impl<T> Message<T> {
}

/// Returns the list of blocks.
pub fn blocks(&self) -> &Vec<Block> {
pub fn blocks(&self) -> &[Block] {
&self.blocks
}

Expand All @@ -119,7 +119,7 @@ impl<T> Message<T> {
}

/// Returns the list of cancelled blocks.
pub fn cancel(&self) -> &Vec<Cid> {
pub fn cancel(&self) -> &[Cid] {
&self.cancel
}

Expand Down Expand Up @@ -191,7 +191,7 @@ impl Message<O> {
}

impl TryFrom<&[u8]> for Message<I> {
type Error = Error;
type Error = BitswapError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let proto: bitswap_pb::Message = bitswap_pb::Message::decode(bytes)?;
let mut message = Message::new();
Expand All @@ -206,7 +206,10 @@ impl TryFrom<&[u8]> for Message<I> {
for payload in proto.payload {
let prefix = Prefix::new_from_bytes(&payload.prefix)?;
let cid = Cid::new_from_prefix(&prefix, &payload.data);
let block = Block::new(payload.data.to_vec().into_boxed_slice(), cid);
let block = Block {
cid,
data: payload.data.to_vec().into_boxed_slice(),
};
message.add_block(block);
}
Ok(message)
Expand All @@ -215,7 +218,7 @@ impl TryFrom<&[u8]> for Message<I> {

impl Message<I> {
/// Creates a `Message` from bytes that were received from a substream.
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, BitswapError> {
Self::try_from(bytes)
}
}
Expand Down
Loading

0 comments on commit 1428034

Please sign in to comment.