From 64e65d57aff36b08dc87b88ca7fc6684a2178f18 Mon Sep 17 00:00:00 2001 From: Yiannis Marangos Date: Tue, 30 Jan 2024 15:19:18 +0200 Subject: [PATCH] chore: Remove `Bitswap` prefix from all names (#21) --- src/builder.rs | 43 ++++++++++++++++------------------ src/client.rs | 52 +++++++++++++++++++---------------------- src/lib.rs | 63 ++++++++++++++++++++++---------------------------- 3 files changed, 72 insertions(+), 86 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 5336a68..7a4d20f 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -5,20 +5,19 @@ use blockstore::Blockstore; use crate::client::{ClientBehaviour, ClientConfig}; use crate::multihasher::{Multihasher, MultihasherTable}; use crate::utils::stream_protocol; -use crate::{BitswapBehaviour, BitswapError, Result}; +use crate::{Behaviour, Error, Result}; -/// Builder for [`BitswapBehaviour`]. +/// Builder for [`Behaviour`]. /// /// # Example /// /// ```rust,no_run /// # use blockstore::InMemoryBlockstore; -/// # use beetswap::BitswapBehaviour; -/// # fn new() -> BitswapBehaviour<64, InMemoryBlockstore<64>> { -/// BitswapBehaviour::builder(InMemoryBlockstore::new()) +/// # fn new() -> beetswap::Behaviour<64, InMemoryBlockstore<64>> { +/// beetswap::Behaviour::builder(InMemoryBlockstore::new()) /// .build() /// # } -pub struct BitswapBehaviourBuilder +pub struct BehaviourBuilder where B: Blockstore + Send + Sync + 'static, { @@ -28,13 +27,13 @@ where multihasher: MultihasherTable, } -impl BitswapBehaviourBuilder +impl BehaviourBuilder where B: Blockstore + Send + Sync + 'static, { - /// Creates a new builder for [`BitswapBehaviour`]. + /// Creates a new builder for [`Behaviour`]. pub(crate) fn new(blockstore: B) -> Self { - BitswapBehaviourBuilder { + BehaviourBuilder { protocol_prefix: None, blockstore, client: ClientConfig { @@ -56,10 +55,9 @@ where /// /// ```rust,no_run /// # use blockstore::InMemoryBlockstore; - /// # use beetswap::BitswapBehaviour; - /// # fn new() -> beetswap::Result>> { + /// # fn new() -> beetswap::Result>> { /// # Ok( - /// BitswapBehaviour::builder(InMemoryBlockstore::new()) + /// beetswap::Behaviour::builder(InMemoryBlockstore::new()) /// .protocol_prefix("/celestia/celestia")? /// .build() /// # ) @@ -67,7 +65,7 @@ where /// ``` pub fn protocol_prefix(mut self, prefix: &str) -> Result { if prefix.starts_with('/') { - return Err(BitswapError::InvalidProtocolPrefix(prefix.to_owned())); + return Err(Error::InvalidProtocolPrefix(prefix.to_owned())); } self.protocol_prefix = Some(prefix.to_owned()); @@ -80,9 +78,8 @@ where /// /// ```rust,no_run /// # use blockstore::InMemoryBlockstore; - /// # use beetswap::BitswapBehaviour; - /// # fn new() -> BitswapBehaviour<64, InMemoryBlockstore<64>> { - /// BitswapBehaviour::builder(InMemoryBlockstore::new()) + /// # fn new() -> beetswap::Behaviour<64, InMemoryBlockstore<64>> { + /// beetswap::Behaviour::builder(InMemoryBlockstore::new()) /// .client_set_send_dont_have(false) /// .build() /// # } @@ -93,12 +90,12 @@ where /// Register an extra [`Multihasher`]. /// - /// Every registration adds new hasher to [`BitswapBehaviour`]. Hashers are used to - /// reconstruct the [`Cid`] from the received data. `BitswapBehaviour` will try them + /// Every registration adds new hasher to [`Behaviour`]. Hashers are used to + /// reconstruct the [`Cid`] from the received data. `Behaviour` will try them /// in the reverse order they were registered until one successfully constructs /// [`Multihash`]. /// - /// By default `BitswapBehaviourBuilder` is pre-loaded with [`StandardMultihasher`]. + /// By default `BehaviourBuilder` is pre-loaded with [`StandardMultihasher`]. /// /// [`StandardMultihasher`]: crate::multihasher::StandardMultihasher /// [`Cid`]: cid::CidGeneric @@ -111,15 +108,15 @@ where self } - /// Build a [`BitswapBehaviour`]. - pub fn build(self) -> BitswapBehaviour { + /// Build a [`Behaviour`]. + pub fn build(self) -> Behaviour { let blockstore = Arc::new(self.blockstore); let multihasher = Arc::new(self.multihasher); let protocol_prefix = self.protocol_prefix.as_deref(); - BitswapBehaviour { + Behaviour { protocol: stream_protocol(protocol_prefix, "/ipfs/bitswap/1.2.0") - .expect("prefix checked by BitswapBehaviourBuilder::protocol_prefix"), + .expect("prefix checked by beetswap::BehaviourBuilder::protocol_prefix"), client: ClientBehaviour::new(self.client, blockstore, multihasher, protocol_prefix), } } diff --git a/src/client.rs b/src/client.rs index 2bfa399..6dc0943 100644 --- a/src/client.rs +++ b/src/client.rs @@ -28,13 +28,13 @@ use crate::proto::message::mod_Message::{BlockPresenceType, Wantlist as ProtoWan use crate::proto::message::Message; use crate::utils::{convert_cid, stream_protocol}; use crate::wantlist::{Wantlist, WantlistState}; -use crate::{BitswapError, BitswapEvent, Result, ToBehaviourEvent, ToHandlerEvent}; +use crate::{Error, Event, Result, ToBehaviourEvent, ToHandlerEvent}; const SEND_FULL_INTERVAL: Duration = Duration::from_secs(30); /// ID of an ongoing query. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct BitswapQueryId(u64); +pub struct QueryId(u64); #[derive(Debug)] pub struct ClientConfig { @@ -51,7 +51,7 @@ impl Default for ClientConfig { enum TaskResult { Get( - BitswapQueryId, + QueryId, CidGeneric, Result>, BlockstoreError>, ), @@ -66,12 +66,12 @@ where { store: Arc, protocol: StreamProtocol, - queue: VecDeque>, + queue: VecDeque>, wantlist: Wantlist, peers: FnvHashMap>, - cid_to_queries: FnvHashMap, SmallVec<[BitswapQueryId; 1]>>, + cid_to_queries: FnvHashMap, SmallVec<[QueryId; 1]>>, tasks: FuturesUnordered>>, - query_abort_handle: FnvHashMap, + query_abort_handle: FnvHashMap, next_query_id: u64, waker: Arc, multihasher: Arc>, @@ -105,7 +105,7 @@ where protocol_prefix: Option<&str>, ) -> Self { let protocol = stream_protocol(protocol_prefix, "/ipfs/bitswap/1.2.0") - .expect("prefix checked by BitswapBehaviourBuilder::protocol_prefix"); + .expect("prefix checked by beetswap::BehaviourBuilder::protocol_prefix"); let set_send_dont_have = config.set_send_dont_have; ClientBehaviour { @@ -154,14 +154,14 @@ where } } - fn next_query_id(&mut self) -> BitswapQueryId { - let id = BitswapQueryId(self.next_query_id); + fn next_query_id(&mut self) -> QueryId { + let id = QueryId(self.next_query_id); self.next_query_id += 1; id } /// Schedule a `Blockstore::get` for the specified cid - fn schedule_store_get(&mut self, query_id: BitswapQueryId, cid: CidGeneric) { + fn schedule_store_get(&mut self, query_id: QueryId, cid: CidGeneric) { let store = self.store.clone(); let (handle, reg) = AbortHandle::new_pair(); @@ -193,7 +193,7 @@ where ); } - pub(crate) fn get(&mut self, cid: &CidGeneric) -> BitswapQueryId { + pub(crate) fn get(&mut self, cid: &CidGeneric) -> QueryId { let query_id = self.next_query_id(); match convert_cid(cid) { @@ -204,9 +204,9 @@ where // the requestor on the next `poll`. None => { self.queue - .push_back(ToSwarm::GenerateEvent(BitswapEvent::GetQueryError { + .push_back(ToSwarm::GenerateEvent(Event::GetQueryError { query_id, - error: BitswapError::InvalidMultihashSize, + error: Error::InvalidMultihashSize, })); } } @@ -214,7 +214,7 @@ where query_id } - pub(crate) fn cancel(&mut self, query_id: BitswapQueryId) { + pub(crate) fn cancel(&mut self, query_id: QueryId) { if let Some(abort_handle) = self.query_abort_handle.remove(&query_id) { abort_handle.abort(); } @@ -279,7 +279,7 @@ where if let Some(queries) = self.cid_to_queries.remove(&cid) { for query_id in queries { self.queue - .push_back(ToSwarm::GenerateEvent(BitswapEvent::GetQueryResponse { + .push_back(ToSwarm::GenerateEvent(Event::GetQueryResponse { query_id, data: block.data.clone(), })); @@ -347,7 +347,7 @@ where handler_updated } - pub(crate) fn poll(&mut self, cx: &mut Context) -> Poll> { + pub(crate) fn poll(&mut self, cx: &mut Context) -> Poll> { // Update waker self.waker.register(cx.waker()); @@ -370,12 +370,10 @@ where match task_result { // Blockstore already has the data so return them to the user TaskResult::Get(query_id, _, Ok(Some(data))) => { - return Poll::Ready(ToSwarm::GenerateEvent( - BitswapEvent::GetQueryResponse { - query_id, - data: data.clone(), - }, - )); + return Poll::Ready(ToSwarm::GenerateEvent(Event::GetQueryResponse { + query_id, + data: data.clone(), + })); } // If blockstore doesn't have the data, add CID in the wantlist. @@ -388,7 +386,7 @@ where // Blockstore error TaskResult::Get(query_id, _, Err(e)) => { - return Poll::Ready(ToSwarm::GenerateEvent(BitswapEvent::GetQueryError { + return Poll::Ready(ToSwarm::GenerateEvent(Event::GetQueryError { query_id, error: e.into(), })); @@ -583,7 +581,7 @@ mod tests { let ev = poll_fn(|cx| client.poll(cx)).await; match ev { - ToSwarm::GenerateEvent(BitswapEvent::GetQueryResponse { query_id, data }) => { + ToSwarm::GenerateEvent(Event::GetQueryResponse { query_id, data }) => { if query_id == query_id1 { assert_eq!(data, b"1"); } else if query_id == query_id2 { @@ -782,9 +780,7 @@ mod tests { let ev = poll_fn(|cx| client.poll(cx)).await; let (query_id, data) = match ev { - ToSwarm::GenerateEvent(BitswapEvent::GetQueryResponse { query_id, data }) => { - (query_id, data) - } + ToSwarm::GenerateEvent(Event::GetQueryResponse { query_id, data }) => (query_id, data), _ => unreachable!(), }; @@ -929,7 +925,7 @@ mod tests { } fn expect_send_wantlist_event( - ev: ToSwarm, + ev: ToSwarm, ) -> (PeerId, ProtoWantlist, Arc>) { match ev { ToSwarm::NotifyHandler { diff --git a/src/lib.rs b/src/lib.rs index 7b6f488..48333f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,12 +32,12 @@ use crate::message::Codec; use crate::proto::message::mod_Message::Wantlist as ProtoWantlist; use crate::proto::message::Message; -pub use crate::builder::BitswapBehaviourBuilder; -pub use crate::client::BitswapQueryId; +pub use crate::builder::BehaviourBuilder; +pub use crate::client::QueryId; /// [`NetworkBehaviour`] for Bitswap protocol. #[derive(Debug)] -pub struct BitswapBehaviour +pub struct Behaviour where B: Blockstore + Send + Sync + 'static, { @@ -45,22 +45,16 @@ where client: ClientBehaviour, } -/// Event produced by [`BitswapBehaviour`]. +/// Event produced by [`Behaviour`]. #[derive(Debug)] -pub enum BitswapEvent { - GetQueryResponse { - query_id: BitswapQueryId, - data: Vec, - }, - GetQueryError { - query_id: BitswapQueryId, - error: BitswapError, - }, +pub enum Event { + GetQueryResponse { query_id: QueryId, data: Vec }, + GetQueryError { query_id: QueryId, error: Error }, } /// Representation of all the errors that can occur when interacting with this crate. #[derive(Debug, thiserror::Error)] -pub enum BitswapError { +pub enum Error { #[error("Invalid multihash size")] InvalidMultihashSize, @@ -71,43 +65,44 @@ pub enum BitswapError { Blockstore(#[from] BlockstoreError), } -/// Alias for a [`Result`] with the error type [`BitswapError`]. -pub type Result = std::result::Result; +/// Alias for a [`Result`] with the error type [`beetswap::Error`]. +/// +/// [`beetswap::Error`]: crate::Error +pub type Result = std::result::Result; -impl BitswapBehaviour +impl Behaviour where B: Blockstore + Send + Sync + 'static, { - /// Creates a new [`BitswapBehaviour`] with the default configuration. - pub fn new(blockstore: B) -> BitswapBehaviour { - BitswapBehaviourBuilder::new(blockstore).build() + /// Creates a new [`Behaviour`] with the default configuration. + pub fn new(blockstore: B) -> Behaviour { + BehaviourBuilder::new(blockstore).build() } - /// Creates a new [`BitswapBehaviourBuilder`]. - pub fn builder(blockstore: B) -> BitswapBehaviourBuilder { - BitswapBehaviourBuilder::new(blockstore) + /// Creates a new [`BehaviourBuilder`]. + pub fn builder(blockstore: B) -> BehaviourBuilder { + BehaviourBuilder::new(blockstore) } /// Start a query that returns the raw data of a [`Cid`]. /// /// [`Cid`]: cid::CidGeneric - pub fn get(&mut self, cid: &CidGeneric) -> BitswapQueryId { + pub fn get(&mut self, cid: &CidGeneric) -> QueryId { self.client.get(cid) } /// Cancel an ongoing query. - pub fn cancel(&mut self, query_id: BitswapQueryId) { + pub fn cancel(&mut self, query_id: QueryId) { self.client.cancel(query_id) } } -impl NetworkBehaviour - for BitswapBehaviour +impl NetworkBehaviour for Behaviour where B: Blockstore + Send + Sync + 'static, { - type ConnectionHandler = BitswapConnectionHandler; - type ToSwarm = BitswapEvent; + type ConnectionHandler = ConnHandler; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -116,7 +111,7 @@ where _local_addr: &Multiaddr, _remote_addr: &Multiaddr, ) -> Result { - Ok(BitswapConnectionHandler { + Ok(ConnHandler { peer, protocol: self.protocol.clone(), client_handler: self.client.new_connection_handler(peer), @@ -131,7 +126,7 @@ where _addr: &Multiaddr, _role_override: Endpoint, ) -> Result { - Ok(BitswapConnectionHandler { + Ok(ConnHandler { peer, protocol: self.protocol.clone(), client_handler: self.client.new_connection_handler(peer), @@ -184,16 +179,14 @@ pub enum ToHandlerEvent { #[derive(Debug)] #[doc(hidden)] -pub struct BitswapConnectionHandler { +pub struct ConnHandler { peer: PeerId, protocol: StreamProtocol, client_handler: ClientConnectionHandler, incoming_streams: SelectAll, } -impl ConnectionHandler - for BitswapConnectionHandler -{ +impl ConnectionHandler for ConnHandler { type ToBehaviour = ToBehaviourEvent; type FromBehaviour = ToHandlerEvent; type InboundProtocol = ReadyUpgrade;