diff --git a/Cargo.lock b/Cargo.lock index cf6b908..c3e66b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -482,7 +482,7 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" [[package]] name = "beetswap" -version = "0.1.1" +version = "0.2.0" dependencies = [ "anyhow", "asynchronous-codec 0.7.0", @@ -602,9 +602,9 @@ dependencies = [ [[package]] name = "blockstore" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "358358b19add120a5afc3dd1c8e9161d6d06c44dfec2ef8da58b7fe5c369c90d" +checksum = "65439a69f3edc14658a7e1e14336b7e5668741c7eb54a79234686a10727a9be1" dependencies = [ "cid", "dashmap", diff --git a/Cargo.toml b/Cargo.toml index 8147ac9..094ec1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "beetswap" -version = "0.1.1" +version = "0.2.0" edition = "2021" license = "Apache-2.0" description = "Implementation of bitswap protocol for libp2p" @@ -21,7 +21,7 @@ categories = [ [dependencies] asynchronous-codec = "0.7" -blockstore = "0.5" +blockstore = "0.6" bytes = "1" cid = "0.11" fnv = "1.0.5" diff --git a/examples/node.rs b/examples/node.rs index 997f868..a2bda96 100644 --- a/examples/node.rs +++ b/examples/node.rs @@ -22,6 +22,7 @@ //! cargo run --example=node -- -l 9898 bafkreiczsrdrvoybcevpzqmblh3my5fu6ui3tgag3jm3hsxvvhaxhswpyu //! ``` use std::collections::HashMap; +use std::sync::Arc; use std::time::Duration; use anyhow::Result; @@ -71,12 +72,12 @@ async fn main() -> Result<()> { let _guard = init_tracing(); - let store = InMemoryBlockstore::new(); + let blockstore = Arc::new(InMemoryBlockstore::new()); for preload_string in args.preload_blockstore_string { let block = StringBlock(preload_string); let cid = block.cid()?; info!("inserted {cid} with content '{}'", block.0); - store.put_keyed(&cid, block.data()).await?; + blockstore.put_keyed(&cid, block.data()).await?; } let mut swarm = SwarmBuilder::with_new_identity() @@ -91,7 +92,7 @@ async fn main() -> Result<()> { "/ipfs/id/1.0.0".to_string(), key.public(), )), - bitswap: beetswap::Behaviour::new(store), + bitswap: beetswap::Behaviour::new(blockstore), })? .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); diff --git a/src/builder.rs b/src/builder.rs index f02232f..53883eb 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -13,9 +13,10 @@ use crate::{Behaviour, Error, Result}; /// # Example /// /// ```rust,no_run +/// # use std::sync::Arc; /// # use blockstore::InMemoryBlockstore; /// # fn new() -> beetswap::Behaviour<64, InMemoryBlockstore<64>> { -/// beetswap::Behaviour::builder(InMemoryBlockstore::new()) +/// beetswap::Behaviour::builder(Arc::new(InMemoryBlockstore::new())) /// .build() /// # } pub struct BehaviourBuilder @@ -23,7 +24,7 @@ where B: Blockstore + 'static, { protocol_prefix: Option, - blockstore: B, + blockstore: Arc, client: ClientConfig, multihasher: MultihasherTable, } @@ -33,7 +34,7 @@ where B: Blockstore + 'static, { /// Creates a new builder for [`Behaviour`]. - pub(crate) fn new(blockstore: B) -> Self { + pub(crate) fn new(blockstore: Arc) -> Self { BehaviourBuilder { protocol_prefix: None, blockstore, @@ -55,10 +56,11 @@ where /// # Example /// /// ```rust + /// # use std::sync::Arc; /// # use blockstore::InMemoryBlockstore; /// # fn new() -> beetswap::Result>> { /// # Ok( - /// beetswap::Behaviour::builder(InMemoryBlockstore::new()) + /// beetswap::Behaviour::builder(Arc::new(InMemoryBlockstore::new())) /// .protocol_prefix("/celestia/celestia")? /// .build() /// # ) @@ -78,9 +80,10 @@ where /// # Example /// /// ```rust + /// # use std::sync::Arc; /// # use blockstore::InMemoryBlockstore; /// # fn new() -> beetswap::Behaviour<64, InMemoryBlockstore<64>> { - /// beetswap::Behaviour::builder(InMemoryBlockstore::new()) + /// beetswap::Behaviour::builder(Arc::new(InMemoryBlockstore::new())) /// .client_set_send_dont_have(false) /// .build() /// # } @@ -111,7 +114,7 @@ where /// Build a [`Behaviour`]. pub fn build(self) -> Behaviour { - let blockstore = Arc::new(self.blockstore); + let blockstore = self.blockstore; let multihasher = Arc::new(self.multihasher); let protocol_prefix = self.protocol_prefix.as_deref(); @@ -133,7 +136,8 @@ mod tests { #[test] fn invalid_protocol_prefix() { assert!(matches!( - BehaviourBuilder::<64, _>::new(InMemoryBlockstore::<64>::new()).protocol_prefix("foo"), + BehaviourBuilder::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new())) + .protocol_prefix("foo"), Err(Error::InvalidProtocolPrefix(_)) )); } diff --git a/src/client.rs b/src/client.rs index e84670f..d6c3171 100644 --- a/src/client.rs +++ b/src/client.rs @@ -708,8 +708,9 @@ mod tests { #[tokio::test] async fn get_unknown_cid_responds_with_have() { let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new()); - let mut client = - Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new())); + let mut client = Swarm::new_ephemeral(|_| { + Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new())) + }); let (mut server_control, mut server_incoming_streams) = connect_to_server(&mut client, server).await; @@ -788,8 +789,9 @@ mod tests { async fn get_unknown_cid_responds_with_dont_have() { let server1 = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new()); let server2 = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new()); - let mut client = - Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new())); + let mut client = Swarm::new_ephemeral(|_| { + Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new())) + }); let (mut server1_control, mut server1_incoming_streams) = connect_to_server(&mut client, server1).await; @@ -903,8 +905,9 @@ mod tests { #[tokio::test] async fn get_unknown_cid_responds_with_block() { let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new()); - let mut client = - Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new())); + let mut client = Swarm::new_ephemeral(|_| { + Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new())) + }); let (mut server_control, mut server_incoming_streams) = connect_to_server(&mut client, server).await; @@ -983,8 +986,9 @@ mod tests { #[tokio::test] async fn update_wantlist() { let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new()); - let mut client = - Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new())); + let mut client = Swarm::new_ephemeral(|_| { + Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new())) + }); let (_server_control, mut server_incoming_streams) = connect_to_server(&mut client, server).await; @@ -1060,8 +1064,9 @@ mod tests { #[tokio::test] async fn request_then_cancel() { let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new()); - let mut client = - Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new())); + let mut client = Swarm::new_ephemeral(|_| { + Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new())) + }); let (_server_control, mut server_incoming_streams) = connect_to_server(&mut client, server).await; @@ -1129,8 +1134,9 @@ mod tests { #[tokio::test] async fn request_before_connect() { let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new()); - let mut client = - Swarm::new_ephemeral(|_| Behaviour::<64, _>::new(InMemoryBlockstore::<64>::new())); + let mut client = Swarm::new_ephemeral(|_| { + Behaviour::<64, _>::new(Arc::new(InMemoryBlockstore::<64>::new())) + }); let cid1 = cid_of_data(b"x1"); let cid2 = cid_of_data(b"x2"); @@ -1181,7 +1187,7 @@ mod tests { let cid1 = cid_of_data(data1); let cid2 = cid_of_data(b"x2"); - let blockstore = InMemoryBlockstore::<64>::new(); + let blockstore = Arc::new(InMemoryBlockstore::<64>::new()); blockstore.put_keyed(&cid1, data1).await.unwrap(); let server = Swarm::new_ephemeral(|_| libp2p_stream::Behaviour::new()); diff --git a/src/lib.rs b/src/lib.rs index 0f7ce1b..d8b8cd0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -98,12 +98,12 @@ where B: Blockstore + 'static, { /// Creates a new [`Behaviour`] with the default configuration. - pub fn new(blockstore: B) -> Behaviour { + pub fn new(blockstore: Arc) -> Behaviour { BehaviourBuilder::new(blockstore).build() } /// Creates a new [`BehaviourBuilder`]. - pub fn builder(blockstore: B) -> BehaviourBuilder { + pub fn builder(blockstore: Arc) -> BehaviourBuilder { BehaviourBuilder::new(blockstore) } diff --git a/tests/utils/mod.rs b/tests/utils/mod.rs index 6cd87c9..b4e476b 100644 --- a/tests/utils/mod.rs +++ b/tests/utils/mod.rs @@ -1,4 +1,5 @@ use std::future::Future; +use std::sync::Arc; use beetswap::{Error, Event, QueryId}; use blockstore::InMemoryBlockstore; @@ -139,7 +140,7 @@ impl TestBitswapWorker { } pub async fn spawn_node(store: Option>) -> TestBitswapNode { - let store = store.unwrap_or_default(); + let blockstore = Arc::new(store.unwrap_or_default()); let mut swarm = SwarmBuilder::with_new_identity() .with_tokio() @@ -149,7 +150,7 @@ pub async fn spawn_node(store: Option>) -> TestBits libp2p_yamux::Config::default, ) .unwrap() - .with_behaviour(|_key| beetswap::Behaviour::::new(store)) + .with_behaviour(|_key| beetswap::Behaviour::::new(blockstore)) .unwrap() .build();