Skip to content

Commit 13ed437

Browse files
committed
fix: move back to upstream bitswap crate
1 parent 6ef03eb commit 13ed437

10 files changed

Lines changed: 400 additions & 250 deletions

File tree

Cargo.lock

Lines changed: 295 additions & 216 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,13 @@ libipld = { version = "0.14", default-features = false, features = ["dag-cbor",
100100
libipld-core = "0.14"
101101
libipld-macro = "0.14"
102102
libp2p = { version = "0.50", default-features = false }
103-
libp2p-bitswap.branch = "forest-libp2p@0.50"
104-
libp2p-bitswap.features = ["compat"]
105-
libp2p-bitswap.git = "https://github.com/hanabi1224/libp2p-bitswap"
103+
# FIXME: use `crate.io` version once changes are merged and released
104+
libp2p-bitswap = { git = "https://github.com/ipfs-rust/libp2p-bitswap", rev = "refs/pull/42/head", features = [
105+
"compat",
106+
] }
106107
libsecp256k1 = "0.7"
107108
log = "0.4"
108-
lru = "0.8"
109+
lru = "0.9"
109110
multibase = "0.9"
110111
multihash = "0.16"
111112
nonempty = "0.8.0"

node/db/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ serde = { workspace = true, features = ["derive"] }
3737
thiserror.workspace = true
3838

3939
[dev-dependencies]
40+
multihash.workspace = true
41+
rand.workspace = true
4042
tempfile.workspace = true

node/db/src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,45 @@ pub trait DBStatistics {
134134
None
135135
}
136136
}
137+
138+
// FIXME: We should propose `cid@0.9` upgrade to `fvm_ipld_blockstore` to match `libp2p_bitswap`
139+
/// Temporary workaround for `cid` version mismatch in upstream `libp2p_bitswap` and `fvm_ipld_blockstore` crates
140+
pub(crate) trait CidCompat {
141+
fn compat(&self) -> libipld::Cid;
142+
}
143+
144+
impl CidCompat for libp2p_bitswap::libipld::Cid {
145+
fn compat(&self) -> libipld::Cid {
146+
libipld::Cid::read_bytes(self.to_bytes().as_slice()).expect("Infallible")
147+
}
148+
}
149+
150+
/// Temporary workaround for `cid` version mismatch in upstream `libp2p_bitswap` and `fvm_ipld_blockstore` crates
151+
pub trait CidCompatBitswap {
152+
fn compat(&self) -> libp2p_bitswap::libipld::Cid;
153+
}
154+
155+
impl CidCompatBitswap for libipld::Cid {
156+
fn compat(&self) -> libp2p_bitswap::libipld::Cid {
157+
libp2p_bitswap::libipld::Cid::read_bytes(self.to_bytes().as_slice()).expect("Infallible")
158+
}
159+
}
160+
161+
#[cfg(test)]
162+
mod tests {
163+
use super::*;
164+
use multihash::MultihashDigest;
165+
use rand::{rngs::OsRng, RngCore};
166+
167+
#[test]
168+
fn test_cid_compat_roundtrip() {
169+
const DAG_CBOR: u64 = 0x71;
170+
171+
let mut bytes = [0; 1024];
172+
OsRng.fill_bytes(&mut bytes);
173+
let cid = libipld::Cid::new_v1(DAG_CBOR, multihash::Code::Blake2b256.digest(&bytes));
174+
175+
assert_eq!(cid.to_string(), cid.compat().to_string());
176+
assert_eq!(cid.compat().to_string(), cid.compat().compat().to_string());
177+
}
178+
}

node/db/src/memory.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

44
use crate::utils::bitswap_missing_blocks;
5+
use crate::CidCompat;
56

67
use super::{Error, Store};
78
use anyhow::Result;
@@ -76,21 +77,24 @@ impl Blockstore for MemoryDB {
7677
}
7778

7879
impl BitswapStore for MemoryDB {
79-
type Params = libipld::DefaultParams;
80+
type Params = libp2p_bitswap::libipld::DefaultParams;
8081

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

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

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

93-
fn missing_blocks(&mut self, cid: &Cid) -> Result<Vec<Cid>> {
94+
fn missing_blocks(
95+
&mut self,
96+
cid: &libp2p_bitswap::libipld::Cid,
97+
) -> Result<Vec<libp2p_bitswap::libipld::Cid>> {
9498
bitswap_missing_blocks::<_, Self::Params>(self, cid)
9599
}
96100
}

node/db/src/parity_db.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use super::errors::Error;
55
use crate::parity_db_config::ParityDbConfig;
66
use crate::utils::bitswap_missing_blocks;
7-
use crate::{DBStatistics, Store};
7+
use crate::{CidCompat, DBStatistics, Store};
88
use anyhow::anyhow;
99
use cid::Cid;
1010
use fvm_ipld_blockstore::Blockstore;
@@ -132,21 +132,27 @@ impl Blockstore for ParityDb {
132132
impl BitswapStore for ParityDb {
133133
/// `fvm_ipld_encoding::DAG_CBOR(0x71)` is covered by [`libipld::DefaultParams`]
134134
/// under feature `dag-cbor`
135-
type Params = libipld::DefaultParams;
135+
type Params = libp2p_bitswap::libipld::DefaultParams;
136136

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

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

145-
fn insert(&mut self, block: &libipld::Block<Self::Params>) -> anyhow::Result<()> {
146-
self.put_keyed(block.cid(), block.data())
145+
fn insert(
146+
&mut self,
147+
block: &libp2p_bitswap::libipld::Block<Self::Params>,
148+
) -> anyhow::Result<()> {
149+
self.put_keyed(&block.cid().compat(), block.data())
147150
}
148151

149-
fn missing_blocks(&mut self, cid: &Cid) -> anyhow::Result<Vec<Cid>> {
152+
fn missing_blocks(
153+
&mut self,
154+
cid: &libp2p_bitswap::libipld::Cid,
155+
) -> anyhow::Result<Vec<libp2p_bitswap::libipld::Cid>> {
150156
bitswap_missing_blocks::<_, Self::Params>(self, cid)
151157
}
152158
}

node/db/src/rocks.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use super::errors::Error;
55
use super::Store;
66
use crate::rocks_config::RocksDbConfig;
7+
use crate::CidCompat;
78
use crate::{metrics, utils::bitswap_missing_blocks, DBStatistics};
89
use anyhow::anyhow;
910
use cid::Cid;
@@ -300,21 +301,27 @@ impl Blockstore for RocksDb {
300301
impl BitswapStore for RocksDb {
301302
/// `fvm_ipld_encoding::DAG_CBOR(0x71)` is covered by [`libipld::DefaultParams`]
302303
/// under feature `dag-cbor`
303-
type Params = libipld::DefaultParams;
304+
type Params = libp2p_bitswap::libipld::DefaultParams;
304305

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

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

313-
fn insert(&mut self, block: &libipld::Block<Self::Params>) -> anyhow::Result<()> {
314-
self.put_keyed(block.cid(), block.data())
314+
fn insert(
315+
&mut self,
316+
block: &libp2p_bitswap::libipld::Block<Self::Params>,
317+
) -> anyhow::Result<()> {
318+
self.put_keyed(&block.cid().compat(), block.data())
315319
}
316320

317-
fn missing_blocks(&mut self, cid: &Cid) -> anyhow::Result<Vec<Cid>> {
321+
fn missing_blocks(
322+
&mut self,
323+
cid: &libp2p_bitswap::libipld::Cid,
324+
) -> anyhow::Result<Vec<libp2p_bitswap::libipld::Cid>> {
318325
bitswap_missing_blocks::<_, Self::Params>(self, cid)
319326
}
320327
}

node/db/src/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright 2019-2022 ChainSafe Systems
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

4-
use cid::Cid;
4+
use crate::*;
55
use fvm_ipld_blockstore::Blockstore;
6-
use libipld::{prelude::*, store::StoreParams, Ipld};
6+
use libp2p_bitswap::libipld::{prelude::*, store::StoreParams, Block, Cid, Ipld};
77

88
pub(super) fn bitswap_missing_blocks<BS: Blockstore, P: StoreParams>(
99
bs: &mut BS,
@@ -15,8 +15,8 @@ where
1515
let mut stack = vec![*cid];
1616
let mut missing = vec![];
1717
while let Some(cid) = stack.pop() {
18-
if let Some(data) = bs.get(&cid)? {
19-
let block = libipld::Block::<P>::new_unchecked(cid, data);
18+
if let Some(data) = bs.get(&cid.compat())? {
19+
let block = Block::<P>::new_unchecked(cid, data);
2020
block.references(&mut stack)?;
2121
} else {
2222
missing.push(cid);

node/forest_libp2p/src/behaviour.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::{
1111
hello::{HelloCodec, HelloProtocolName},
1212
};
1313
use cid::Cid;
14+
use forest_db::CidCompatBitswap;
1415
use forest_encoding::blake2b_256;
15-
use libipld::store::StoreParams;
1616
use libp2p::swarm::NetworkBehaviour;
1717
use libp2p::{core::identity::Keypair, kad::QueryId};
1818
use libp2p::{core::PeerId, gossipsub::GossipsubMessage};
@@ -28,6 +28,7 @@ use libp2p::{
2828
metrics::{Metrics, Recorder},
2929
request_response::{ProtocolSupport, RequestResponse, RequestResponseConfig},
3030
};
31+
use libp2p_bitswap::libipld::store::StoreParams;
3132
use libp2p_bitswap::{Bitswap, BitswapConfig, BitswapStore};
3233
use log::{debug, warn};
3334
use std::collections::{HashMap, HashSet};
@@ -88,7 +89,13 @@ impl<P: StoreParams> ForestBehaviour<P> {
8889
)
8990
.unwrap();
9091

91-
let bitswap = Bitswap::new(BitswapConfig::new(), db);
92+
let bitswap = Bitswap::new(
93+
BitswapConfig {
94+
compat_protocol_name: b"/chain/ipfs/bitswap/1.2.0",
95+
..Default::default()
96+
},
97+
db,
98+
);
9299
if let Err(err) = bitswap.register_metrics(prometheus::default_registry()) {
93100
warn!("Fail to register prometheus metrics for libp2p_bitswap: {err}");
94101
}
@@ -155,7 +162,9 @@ impl<P: StoreParams> ForestBehaviour<P> {
155162
pub fn want_block(&mut self, cid: Cid) -> anyhow::Result<libp2p_bitswap::QueryId> {
156163
debug!("want {}", cid.to_string());
157164
let peers = self.discovery.peers().iter().cloned().collect();
158-
let query_id = self.bitswap.sync(cid, peers, [cid].into_iter());
165+
let query_id = self
166+
.bitswap
167+
.sync(cid.compat(), peers, [cid.compat()].into_iter());
159168
Ok(query_id)
160169
}
161170
}

node/forest_libp2p/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use futures::select;
2222
use futures_util::stream::StreamExt;
2323
use fvm_ipld_blockstore::Blockstore;
2424
use fvm_ipld_encoding::from_slice;
25-
use libipld::store::StoreParams;
2625
use libp2p::gossipsub::GossipsubEvent;
2726
pub use libp2p::gossipsub::IdentTopic;
2827
pub use libp2p::gossipsub::Topic;
@@ -43,6 +42,7 @@ use libp2p::{
4342
yamux, PeerId, Swarm, Transport,
4443
};
4544
use libp2p::{core::Multiaddr, swarm::SwarmBuilder};
45+
use libp2p_bitswap::libipld::store::StoreParams;
4646
use libp2p_bitswap::{BitswapEvent, BitswapStore};
4747
use log::{debug, error, info, trace, warn};
4848
use std::collections::HashMap;

0 commit comments

Comments
 (0)