Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
364 changes: 225 additions & 139 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ libipld = { version = "0.14", default-features = false, features = ["dag-cbor",
libipld-core = "0.14"
libipld-macro = "0.14"
libp2p = { version = "0.50", default-features = false }
libp2p-bitswap.branch = "forest-libp2p@0.50"
libp2p-bitswap.features = ["compat"]
libp2p-bitswap.git = "https://github.com/hanabi1224/libp2p-bitswap"
# FIXME: use `crate.io` version once changes are merged and released
libp2p-bitswap = { git = "https://github.com/ipfs-rust/libp2p-bitswap", rev = "refs/pull/42/head", features = [
"compat",
] }
libsecp256k1 = "0.7"
log = "0.4"
lru = "0.8"
lru = "0.9"
multibase = "0.9"
multihash = "0.16"
nonempty = "0.8.0"
Expand Down
2 changes: 2 additions & 0 deletions node/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ serde = { workspace = true, features = ["derive"] }
thiserror.workspace = true

[dev-dependencies]
multihash.workspace = true
rand.workspace = true
tempfile.workspace = true
42 changes: 42 additions & 0 deletions node/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,45 @@ pub trait DBStatistics {
None
}
}

// FIXME: We should propose `cid@0.9` upgrade to `fvm_ipld_blockstore` to match `libp2p_bitswap`
/// Temporary workaround for `cid` version mismatch in upstream `libp2p_bitswap` and `fvm_ipld_blockstore` crates
pub(crate) trait CidCompat {
fn compat(&self) -> libipld::Cid;
}

impl CidCompat for libp2p_bitswap::libipld::Cid {
fn compat(&self) -> libipld::Cid {
libipld::Cid::read_bytes(self.to_bytes().as_slice()).expect("Infallible")
}
}

/// Temporary workaround for `cid` version mismatch in upstream `libp2p_bitswap` and `fvm_ipld_blockstore` crates
pub trait CidCompatBitswap {
fn compat(&self) -> libp2p_bitswap::libipld::Cid;
}

impl CidCompatBitswap for libipld::Cid {
fn compat(&self) -> libp2p_bitswap::libipld::Cid {
libp2p_bitswap::libipld::Cid::read_bytes(self.to_bytes().as_slice()).expect("Infallible")
}
}

#[cfg(test)]
mod tests {
use super::*;
use multihash::MultihashDigest;
use rand::{rngs::OsRng, RngCore};

#[test]
fn test_cid_compat_roundtrip() {
const DAG_CBOR: u64 = 0x71;

let mut bytes = [0; 1024];
OsRng.fill_bytes(&mut bytes);
let cid = libipld::Cid::new_v1(DAG_CBOR, multihash::Code::Blake2b256.digest(&bytes));

assert_eq!(cid.to_string(), cid.compat().to_string());
assert_eq!(cid.compat().to_string(), cid.compat().compat().to_string());
}
}
18 changes: 11 additions & 7 deletions node/db/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::utils::bitswap_missing_blocks;
use crate::CidCompat;

use super::{Error, Store};
use anyhow::Result;
Expand Down Expand Up @@ -76,21 +77,24 @@ impl Blockstore for MemoryDB {
}

impl BitswapStore for MemoryDB {
type Params = libipld::DefaultParams;
type Params = libp2p_bitswap::libipld::DefaultParams;

fn contains(&mut self, cid: &Cid) -> Result<bool> {
fn contains(&mut self, cid: &libp2p_bitswap::libipld::Cid) -> Result<bool> {
Ok(self.exists(cid.to_bytes())?)
}

fn get(&mut self, cid: &Cid) -> Result<Option<Vec<u8>>> {
Blockstore::get(self, cid)
fn get(&mut self, cid: &libp2p_bitswap::libipld::Cid) -> Result<Option<Vec<u8>>> {
Blockstore::get(self, &cid.compat())
}

fn insert(&mut self, block: &libipld::Block<Self::Params>) -> Result<()> {
self.put_keyed(block.cid(), block.data())
fn insert(&mut self, block: &libp2p_bitswap::libipld::Block<Self::Params>) -> Result<()> {
self.put_keyed(&block.cid().compat(), block.data())
}

fn missing_blocks(&mut self, cid: &Cid) -> Result<Vec<Cid>> {
fn missing_blocks(
&mut self,
cid: &libp2p_bitswap::libipld::Cid,
) -> Result<Vec<libp2p_bitswap::libipld::Cid>> {
bitswap_missing_blocks::<_, Self::Params>(self, cid)
}
}
22 changes: 14 additions & 8 deletions node/db/src/parity_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use super::errors::Error;
use crate::parity_db_config::ParityDbConfig;
use crate::utils::bitswap_missing_blocks;
use crate::{DBStatistics, Store};
use crate::{CidCompat, DBStatistics, Store};
use anyhow::anyhow;
use cid::Cid;
use fvm_ipld_blockstore::Blockstore;
Expand Down Expand Up @@ -132,21 +132,27 @@ impl Blockstore for ParityDb {
impl BitswapStore for ParityDb {
/// `fvm_ipld_encoding::DAG_CBOR(0x71)` is covered by [`libipld::DefaultParams`]
/// under feature `dag-cbor`
type Params = libipld::DefaultParams;
type Params = libp2p_bitswap::libipld::DefaultParams;

fn contains(&mut self, cid: &Cid) -> anyhow::Result<bool> {
fn contains(&mut self, cid: &libp2p_bitswap::libipld::Cid) -> anyhow::Result<bool> {
Ok(self.exists(cid.to_bytes())?)
}

fn get(&mut self, cid: &Cid) -> anyhow::Result<Option<Vec<u8>>> {
Blockstore::get(self, cid)
fn get(&mut self, cid: &libp2p_bitswap::libipld::Cid) -> anyhow::Result<Option<Vec<u8>>> {
Blockstore::get(self, &cid.compat())
}

fn insert(&mut self, block: &libipld::Block<Self::Params>) -> anyhow::Result<()> {
self.put_keyed(block.cid(), block.data())
fn insert(
&mut self,
block: &libp2p_bitswap::libipld::Block<Self::Params>,
) -> anyhow::Result<()> {
self.put_keyed(&block.cid().compat(), block.data())
}

fn missing_blocks(&mut self, cid: &Cid) -> anyhow::Result<Vec<Cid>> {
fn missing_blocks(
&mut self,
cid: &libp2p_bitswap::libipld::Cid,
) -> anyhow::Result<Vec<libp2p_bitswap::libipld::Cid>> {
bitswap_missing_blocks::<_, Self::Params>(self, cid)
}
}
Expand Down
21 changes: 14 additions & 7 deletions node/db/src/rocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use super::errors::Error;
use super::Store;
use crate::rocks_config::RocksDbConfig;
use crate::CidCompat;
use crate::{metrics, utils::bitswap_missing_blocks, DBStatistics};
use anyhow::anyhow;
use cid::Cid;
Expand Down Expand Up @@ -300,21 +301,27 @@ impl Blockstore for RocksDb {
impl BitswapStore for RocksDb {
/// `fvm_ipld_encoding::DAG_CBOR(0x71)` is covered by [`libipld::DefaultParams`]
/// under feature `dag-cbor`
type Params = libipld::DefaultParams;
type Params = libp2p_bitswap::libipld::DefaultParams;

fn contains(&mut self, cid: &Cid) -> anyhow::Result<bool> {
fn contains(&mut self, cid: &libp2p_bitswap::libipld::Cid) -> anyhow::Result<bool> {
Ok(self.exists(cid.to_bytes())?)
}

fn get(&mut self, cid: &Cid) -> anyhow::Result<Option<Vec<u8>>> {
Blockstore::get(self, cid)
fn get(&mut self, cid: &libp2p_bitswap::libipld::Cid) -> anyhow::Result<Option<Vec<u8>>> {
Blockstore::get(self, &cid.compat())
}

fn insert(&mut self, block: &libipld::Block<Self::Params>) -> anyhow::Result<()> {
self.put_keyed(block.cid(), block.data())
fn insert(
&mut self,
block: &libp2p_bitswap::libipld::Block<Self::Params>,
) -> anyhow::Result<()> {
self.put_keyed(&block.cid().compat(), block.data())
}

fn missing_blocks(&mut self, cid: &Cid) -> anyhow::Result<Vec<Cid>> {
fn missing_blocks(
&mut self,
cid: &libp2p_bitswap::libipld::Cid,
) -> anyhow::Result<Vec<libp2p_bitswap::libipld::Cid>> {
bitswap_missing_blocks::<_, Self::Params>(self, cid)
}
}
Expand Down
8 changes: 4 additions & 4 deletions node/db/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use cid::Cid;
use crate::*;
use fvm_ipld_blockstore::Blockstore;
use libipld::{prelude::*, store::StoreParams, Ipld};
use libp2p_bitswap::libipld::{prelude::*, store::StoreParams, Block, Cid, Ipld};

pub(super) fn bitswap_missing_blocks<BS: Blockstore, P: StoreParams>(
bs: &mut BS,
Expand All @@ -15,8 +15,8 @@ where
let mut stack = vec![*cid];
let mut missing = vec![];
while let Some(cid) = stack.pop() {
if let Some(data) = bs.get(&cid)? {
let block = libipld::Block::<P>::new_unchecked(cid, data);
if let Some(data) = bs.get(&cid.compat())? {
let block = Block::<P>::new_unchecked(cid, data);
block.references(&mut stack)?;
} else {
missing.push(cid);
Expand Down
15 changes: 12 additions & 3 deletions node/forest_libp2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
hello::{HelloCodec, HelloProtocolName},
};
use cid::Cid;
use forest_db::CidCompatBitswap;
use forest_encoding::blake2b_256;
use libipld::store::StoreParams;
use libp2p::swarm::NetworkBehaviour;
use libp2p::{core::identity::Keypair, kad::QueryId};
use libp2p::{core::PeerId, gossipsub::GossipsubMessage};
Expand All @@ -28,6 +28,7 @@ use libp2p::{
metrics::{Metrics, Recorder},
request_response::{ProtocolSupport, RequestResponse, RequestResponseConfig},
};
use libp2p_bitswap::libipld::store::StoreParams;
use libp2p_bitswap::{Bitswap, BitswapConfig, BitswapStore};
use log::{debug, warn};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -88,7 +89,13 @@ impl<P: StoreParams> ForestBehaviour<P> {
)
.unwrap();

let bitswap = Bitswap::new(BitswapConfig::new(), db);
let bitswap = Bitswap::new(
BitswapConfig {
compat_protocol_name: b"/chain/ipfs/bitswap/1.2.0",
..Default::default()
},
db,
);
if let Err(err) = bitswap.register_metrics(prometheus::default_registry()) {
warn!("Fail to register prometheus metrics for libp2p_bitswap: {err}");
}
Expand Down Expand Up @@ -155,7 +162,9 @@ impl<P: StoreParams> ForestBehaviour<P> {
pub fn want_block(&mut self, cid: Cid) -> anyhow::Result<libp2p_bitswap::QueryId> {
debug!("want {}", cid.to_string());
let peers = self.discovery.peers().iter().cloned().collect();
let query_id = self.bitswap.sync(cid, peers, [cid].into_iter());
let query_id = self
.bitswap
.sync(cid.compat(), peers, [cid.compat()].into_iter());
Ok(query_id)
}
}
2 changes: 1 addition & 1 deletion node/forest_libp2p/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use futures::select;
use futures_util::stream::StreamExt;
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::from_slice;
use libipld::store::StoreParams;
use libp2p::gossipsub::GossipsubEvent;
pub use libp2p::gossipsub::IdentTopic;
pub use libp2p::gossipsub::Topic;
Expand All @@ -43,6 +42,7 @@ use libp2p::{
yamux, PeerId, Swarm, Transport,
};
use libp2p::{core::Multiaddr, swarm::SwarmBuilder};
use libp2p_bitswap::libipld::store::StoreParams;
use libp2p_bitswap::{BitswapEvent, BitswapStore};
use log::{debug, error, info, trace, warn};
use std::collections::HashMap;
Expand Down