Skip to content

Commit 71c4e08

Browse files
committed
feat: Accept Arc to allow shared blockstore
1 parent 3494715 commit 71c4e08

File tree

7 files changed

+44
-32
lines changed

7 files changed

+44
-32
lines changed

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "beetswap"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
edition = "2021"
55
license = "Apache-2.0"
66
description = "Implementation of bitswap protocol for libp2p"
@@ -21,7 +21,7 @@ categories = [
2121

2222
[dependencies]
2323
asynchronous-codec = "0.7"
24-
blockstore = "0.5"
24+
blockstore = "0.6"
2525
bytes = "1"
2626
cid = "0.11"
2727
fnv = "1.0.5"

examples/node.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
//! cargo run --example=node -- -l 9898 bafkreiczsrdrvoybcevpzqmblh3my5fu6ui3tgag3jm3hsxvvhaxhswpyu
2323
//! ```
2424
use std::collections::HashMap;
25+
use std::sync::Arc;
2526
use std::time::Duration;
2627

2728
use anyhow::Result;
@@ -71,12 +72,12 @@ async fn main() -> Result<()> {
7172

7273
let _guard = init_tracing();
7374

74-
let store = InMemoryBlockstore::new();
75+
let blockstore = Arc::new(InMemoryBlockstore::new());
7576
for preload_string in args.preload_blockstore_string {
7677
let block = StringBlock(preload_string);
7778
let cid = block.cid()?;
7879
info!("inserted {cid} with content '{}'", block.0);
79-
store.put_keyed(&cid, block.data()).await?;
80+
blockstore.put_keyed(&cid, block.data()).await?;
8081
}
8182

8283
let mut swarm = SwarmBuilder::with_new_identity()
@@ -91,7 +92,7 @@ async fn main() -> Result<()> {
9192
"/ipfs/id/1.0.0".to_string(),
9293
key.public(),
9394
)),
94-
bitswap: beetswap::Behaviour::new(store),
95+
bitswap: beetswap::Behaviour::new(blockstore),
9596
})?
9697
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
9798
.build();

src/builder.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ use crate::{Behaviour, Error, Result};
1313
/// # Example
1414
///
1515
/// ```rust,no_run
16+
/// # use std::sync::Arc;
1617
/// # use blockstore::InMemoryBlockstore;
1718
/// # fn new() -> beetswap::Behaviour<64, InMemoryBlockstore<64>> {
18-
/// beetswap::Behaviour::builder(InMemoryBlockstore::new())
19+
/// beetswap::Behaviour::builder(Arc::new(InMemoryBlockstore::new()))
1920
/// .build()
2021
/// # }
2122
pub struct BehaviourBuilder<const S: usize, B>
2223
where
2324
B: Blockstore + 'static,
2425
{
2526
protocol_prefix: Option<String>,
26-
blockstore: B,
27+
blockstore: Arc<B>,
2728
client: ClientConfig,
2829
multihasher: MultihasherTable<S>,
2930
}
@@ -33,7 +34,7 @@ where
3334
B: Blockstore + 'static,
3435
{
3536
/// Creates a new builder for [`Behaviour`].
36-
pub(crate) fn new(blockstore: B) -> Self {
37+
pub(crate) fn new(blockstore: Arc<B>) -> Self {
3738
BehaviourBuilder {
3839
protocol_prefix: None,
3940
blockstore,
@@ -55,10 +56,11 @@ where
5556
/// # Example
5657
///
5758
/// ```rust
59+
/// # use std::sync::Arc;
5860
/// # use blockstore::InMemoryBlockstore;
5961
/// # fn new() -> beetswap::Result<beetswap::Behaviour<64, InMemoryBlockstore<64>>> {
6062
/// # Ok(
61-
/// beetswap::Behaviour::builder(InMemoryBlockstore::new())
63+
/// beetswap::Behaviour::builder(Arc::new(InMemoryBlockstore::new()))
6264
/// .protocol_prefix("/celestia/celestia")?
6365
/// .build()
6466
/// # )
@@ -78,9 +80,10 @@ where
7880
/// # Example
7981
///
8082
/// ```rust
83+
/// # use std::sync::Arc;
8184
/// # use blockstore::InMemoryBlockstore;
8285
/// # fn new() -> beetswap::Behaviour<64, InMemoryBlockstore<64>> {
83-
/// beetswap::Behaviour::builder(InMemoryBlockstore::new())
86+
/// beetswap::Behaviour::builder(Arc::new(InMemoryBlockstore::new()))
8487
/// .client_set_send_dont_have(false)
8588
/// .build()
8689
/// # }
@@ -111,7 +114,7 @@ where
111114

112115
/// Build a [`Behaviour`].
113116
pub fn build(self) -> Behaviour<S, B> {
114-
let blockstore = Arc::new(self.blockstore);
117+
let blockstore = self.blockstore;
115118
let multihasher = Arc::new(self.multihasher);
116119
let protocol_prefix = self.protocol_prefix.as_deref();
117120

@@ -133,7 +136,8 @@ mod tests {
133136
#[test]
134137
fn invalid_protocol_prefix() {
135138
assert!(matches!(
136-
BehaviourBuilder::<64, _>::new(InMemoryBlockstore::<64>::new()).protocol_prefix("foo"),
139+
BehaviourBuilder::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new()))
140+
.protocol_prefix("foo"),
137141
Err(Error::InvalidProtocolPrefix(_))
138142
));
139143
}

src/client.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,9 @@ mod tests {
708708
#[tokio::test]
709709
async fn get_unknown_cid_responds_with_have() {
710710
let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new());
711-
let mut client =
712-
Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new()));
711+
let mut client = Swarm::new_ephemeral(|_| {
712+
Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new()))
713+
});
713714

714715
let (mut server_control, mut server_incoming_streams) =
715716
connect_to_server(&mut client, server).await;
@@ -788,8 +789,9 @@ mod tests {
788789
async fn get_unknown_cid_responds_with_dont_have() {
789790
let server1 = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new());
790791
let server2 = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new());
791-
let mut client =
792-
Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new()));
792+
let mut client = Swarm::new_ephemeral(|_| {
793+
Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new()))
794+
});
793795

794796
let (mut server1_control, mut server1_incoming_streams) =
795797
connect_to_server(&mut client, server1).await;
@@ -903,8 +905,9 @@ mod tests {
903905
#[tokio::test]
904906
async fn get_unknown_cid_responds_with_block() {
905907
let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new());
906-
let mut client =
907-
Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new()));
908+
let mut client = Swarm::new_ephemeral(|_| {
909+
Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new()))
910+
});
908911

909912
let (mut server_control, mut server_incoming_streams) =
910913
connect_to_server(&mut client, server).await;
@@ -983,8 +986,9 @@ mod tests {
983986
#[tokio::test]
984987
async fn update_wantlist() {
985988
let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new());
986-
let mut client =
987-
Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new()));
989+
let mut client = Swarm::new_ephemeral(|_| {
990+
Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new()))
991+
});
988992

989993
let (_server_control, mut server_incoming_streams) =
990994
connect_to_server(&mut client, server).await;
@@ -1060,8 +1064,9 @@ mod tests {
10601064
#[tokio::test]
10611065
async fn request_then_cancel() {
10621066
let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new());
1063-
let mut client =
1064-
Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new()));
1067+
let mut client = Swarm::new_ephemeral(|_| {
1068+
Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new()))
1069+
});
10651070

10661071
let (_server_control, mut server_incoming_streams) =
10671072
connect_to_server(&mut client, server).await;
@@ -1129,8 +1134,9 @@ mod tests {
11291134
#[tokio::test]
11301135
async fn request_before_connect() {
11311136
let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new());
1132-
let mut client =
1133-
Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new()));
1137+
let mut client = Swarm::new_ephemeral(|_| {
1138+
Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new()))
1139+
});
11341140

11351141
let cid1 = cid_of_data(b"x1");
11361142
let cid2 = cid_of_data(b"x2");
@@ -1181,7 +1187,7 @@ mod tests {
11811187
let cid1 = cid_of_data(data1);
11821188
let cid2 = cid_of_data(b"x2");
11831189

1184-
let blockstore = InMemoryBlockstore::<64>::new();
1190+
let blockstore = Arc::new(InMemoryBlockstore::<64>::new());
11851191
blockstore.put_keyed(&cid1, data1).await.unwrap();
11861192

11871193
let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new());

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ where
9898
B: Blockstore + 'static,
9999
{
100100
/// Creates a new [`Behaviour`] with the default configuration.
101-
pub fn new(blockstore: B) -> Behaviour<MAX_MULTIHASH_SIZE, B> {
101+
pub fn new(blockstore: Arc<B>) -> Behaviour<MAX_MULTIHASH_SIZE, B> {
102102
BehaviourBuilder::new(blockstore).build()
103103
}
104104

105105
/// Creates a new [`BehaviourBuilder`].
106-
pub fn builder(blockstore: B) -> BehaviourBuilder<MAX_MULTIHASH_SIZE, B> {
106+
pub fn builder(blockstore: Arc<B>) -> BehaviourBuilder<MAX_MULTIHASH_SIZE, B> {
107107
BehaviourBuilder::new(blockstore)
108108
}
109109

tests/utils/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::future::Future;
2+
use std::sync::Arc;
23

34
use beetswap::{Error, Event, QueryId};
45
use blockstore::InMemoryBlockstore;
@@ -139,7 +140,7 @@ impl TestBitswapWorker {
139140
}
140141

141142
pub async fn spawn_node(store: Option<InMemoryBlockstore<CID_SIZE>>) -> TestBitswapNode {
142-
let store = store.unwrap_or_default();
143+
let blockstore = Arc::new(store.unwrap_or_default());
143144

144145
let mut swarm = SwarmBuilder::with_new_identity()
145146
.with_tokio()
@@ -149,7 +150,7 @@ pub async fn spawn_node(store: Option<InMemoryBlockstore<CID_SIZE>>) -> TestBits
149150
libp2p_yamux::Config::default,
150151
)
151152
.unwrap()
152-
.with_behaviour(|_key| beetswap::Behaviour::<CID_SIZE, _>::new(store))
153+
.with_behaviour(|_key| beetswap::Behaviour::<CID_SIZE, _>::new(blockstore))
153154
.unwrap()
154155
.build();
155156

0 commit comments

Comments
 (0)