From 7f33cb05d026cfccd7b88321b32665e669cdafef Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 6 Jun 2023 13:53:18 +0900 Subject: [PATCH] chore(kad): remove deprecated record Deprecated in https://github.com/libp2p/rust-libp2p/pull/3738 Deprecation released with `v0.51.3`. Follow up to https://github.com/libp2p/rust-libp2p/pull/3896 Part of https://github.com/libp2p/rust-libp2p/issues/3647 --- protocols/kad/src/behaviour.rs | 64 +++++++++---------- protocols/kad/src/behaviour/test.rs | 10 +-- protocols/kad/src/handler.rs | 18 +++--- protocols/kad/src/jobs.rs | 8 +-- protocols/kad/src/kbucket/key.rs | 6 +- protocols/kad/src/lib.rs | 11 +--- protocols/kad/src/protocol.rs | 20 +++--- .../kad/src/{record_priv.rs => record.rs} | 0 .../kad/src/{record_priv => record}/store.rs | 0 .../{record_priv => record}/store/memory.rs | 0 10 files changed, 65 insertions(+), 72 deletions(-) rename protocols/kad/src/{record_priv.rs => record.rs} (100%) rename protocols/kad/src/{record_priv => record}/store.rs (100%) rename protocols/kad/src/{record_priv => record}/store/memory.rs (100%) diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 5d4b84c65f6..9b37ed60961 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -28,7 +28,7 @@ use crate::jobs::*; use crate::kbucket::{self, Distance, KBucketsTable, NodeStatus}; use crate::protocol::{KadConnectionType, KadPeer, KademliaProtocolConfig}; use crate::query::{Query, QueryConfig, QueryId, QueryPool, QueryPoolState}; -use crate::record_priv::{ +use crate::record::{ self, store::{self, RecordStore}, ProviderRecord, Record, @@ -149,7 +149,7 @@ pub enum KademliaBucketInserts { /// This can be used for e.g. signature verification or validating /// the accompanying [`Key`]. /// -/// [`Key`]: crate::record_priv::Key +/// [`Key`]: crate::record::Key #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum KademliaStoreInserts { /// Whenever a (provider) record is received, @@ -687,7 +687,7 @@ where /// /// The result of this operation is delivered in a /// [`KademliaEvent::OutboundQueryCompleted{QueryResult::GetRecord}`]. - pub fn get_record(&mut self, key: record_priv::Key) -> QueryId { + pub fn get_record(&mut self, key: record::Key) -> QueryId { let record = if let Some(record) = self.store.get(&key) { if record.is_expired(Instant::now()) { self.store.remove(&key); @@ -838,7 +838,7 @@ where /// This is a _local_ operation. However, it also has the effect that /// the record will no longer be periodically re-published, allowing the /// record to eventually expire throughout the DHT. - pub fn remove_record(&mut self, key: &record_priv::Key) { + pub fn remove_record(&mut self, key: &record::Key) { if let Some(r) = self.store.get(key) { if r.publisher.as_ref() == Some(self.kbuckets.local_key().preimage()) { self.store.remove(key) @@ -908,7 +908,7 @@ where /// /// The results of the (repeated) provider announcements sent by this node are /// reported via [`KademliaEvent::OutboundQueryCompleted{QueryResult::StartProviding}`]. - pub fn start_providing(&mut self, key: record_priv::Key) -> Result { + pub fn start_providing(&mut self, key: record::Key) -> Result { // Note: We store our own provider records locally without local addresses // to avoid redundant storage and outdated addresses. Instead these are // acquired on demand when returning a `ProviderRecord` for the local node. @@ -936,7 +936,7 @@ where /// /// This is a local operation. The local node will still be considered as a /// provider for the key by other nodes until these provider records expire. - pub fn stop_providing(&mut self, key: &record_priv::Key) { + pub fn stop_providing(&mut self, key: &record::Key) { self.store .remove_provider(key, self.kbuckets.local_key().preimage()); } @@ -945,7 +945,7 @@ where /// /// The result of this operation is delivered in a /// reported via [`KademliaEvent::OutboundQueryCompleted{QueryResult::GetProviders}`]. - pub fn get_providers(&mut self, key: record_priv::Key) -> QueryId { + pub fn get_providers(&mut self, key: record::Key) -> QueryId { let providers: HashSet<_> = self .store .providers(&key) @@ -1034,7 +1034,7 @@ where } /// Collects all peers who are known to be providers of the value for a given `Multihash`. - fn provider_peers(&mut self, key: &record_priv::Key, source: &PeerId) -> Vec { + fn provider_peers(&mut self, key: &record::Key, source: &PeerId) -> Vec { let kbuckets = &mut self.kbuckets; let connected = &mut self.connected_peers; let listen_addresses = &self.listen_addresses; @@ -1091,7 +1091,7 @@ where } /// Starts an iterative `ADD_PROVIDER` query for the given key. - fn start_add_provider(&mut self, key: record_priv::Key, context: AddProviderContext) { + fn start_add_provider(&mut self, key: record::Key, context: AddProviderContext) { let info = QueryInfo::AddProvider { context, key: key.clone(), @@ -1429,7 +1429,7 @@ where get_closest_peers_stats, }, } => { - let mk_result = |key: record_priv::Key| { + let mk_result = |key: record::Key| { if success.len() >= quorum.get() { Ok(PutRecordOk { key }) } else { @@ -1728,7 +1728,7 @@ where } /// Processes a provider record received from a peer. - fn provider_received(&mut self, key: record_priv::Key, provider: KadPeer) { + fn provider_received(&mut self, key: record::Key, provider: KadPeer) { if &provider.node_id != self.kbuckets.local_key().preimage() { let record = ProviderRecord { key, @@ -2746,22 +2746,22 @@ pub enum GetRecordOk { pub enum GetRecordError { #[error("the record was not found")] NotFound { - key: record_priv::Key, + key: record::Key, closest_peers: Vec, }, #[error("the quorum failed; needed {quorum} peers")] QuorumFailed { - key: record_priv::Key, + key: record::Key, records: Vec, quorum: NonZeroUsize, }, #[error("the request timed out")] - Timeout { key: record_priv::Key }, + Timeout { key: record::Key }, } impl GetRecordError { /// Gets the key of the record for which the operation failed. - pub fn key(&self) -> &record_priv::Key { + pub fn key(&self) -> &record::Key { match self { GetRecordError::QuorumFailed { key, .. } => key, GetRecordError::Timeout { key, .. } => key, @@ -2771,7 +2771,7 @@ impl GetRecordError { /// Extracts the key of the record for which the operation failed, /// consuming the error. - pub fn into_key(self) -> record_priv::Key { + pub fn into_key(self) -> record::Key { match self { GetRecordError::QuorumFailed { key, .. } => key, GetRecordError::Timeout { key, .. } => key, @@ -2786,7 +2786,7 @@ pub type PutRecordResult = Result; /// The successful result of [`Kademlia::put_record`]. #[derive(Debug, Clone)] pub struct PutRecordOk { - pub key: record_priv::Key, + pub key: record::Key, } /// The error result of [`Kademlia::put_record`]. @@ -2794,14 +2794,14 @@ pub struct PutRecordOk { pub enum PutRecordError { #[error("the quorum failed; needed {quorum} peers")] QuorumFailed { - key: record_priv::Key, + key: record::Key, /// [`PeerId`]s of the peers the record was successfully stored on. success: Vec, quorum: NonZeroUsize, }, #[error("the request timed out")] Timeout { - key: record_priv::Key, + key: record::Key, /// [`PeerId`]s of the peers the record was successfully stored on. success: Vec, quorum: NonZeroUsize, @@ -2810,7 +2810,7 @@ pub enum PutRecordError { impl PutRecordError { /// Gets the key of the record for which the operation failed. - pub fn key(&self) -> &record_priv::Key { + pub fn key(&self) -> &record::Key { match self { PutRecordError::QuorumFailed { key, .. } => key, PutRecordError::Timeout { key, .. } => key, @@ -2819,7 +2819,7 @@ impl PutRecordError { /// Extracts the key of the record for which the operation failed, /// consuming the error. - pub fn into_key(self) -> record_priv::Key { + pub fn into_key(self) -> record::Key { match self { PutRecordError::QuorumFailed { key, .. } => key, PutRecordError::Timeout { key, .. } => key, @@ -2888,7 +2888,7 @@ pub type GetProvidersResult = Result; #[derive(Debug, Clone)] pub enum GetProvidersOk { FoundProviders { - key: record_priv::Key, + key: record::Key, /// The new set of providers discovered. providers: HashSet, }, @@ -2902,14 +2902,14 @@ pub enum GetProvidersOk { pub enum GetProvidersError { #[error("the request timed out")] Timeout { - key: record_priv::Key, + key: record::Key, closest_peers: Vec, }, } impl GetProvidersError { /// Gets the key for which the operation failed. - pub fn key(&self) -> &record_priv::Key { + pub fn key(&self) -> &record::Key { match self { GetProvidersError::Timeout { key, .. } => key, } @@ -2917,7 +2917,7 @@ impl GetProvidersError { /// Extracts the key for which the operation failed, /// consuming the error. - pub fn into_key(self) -> record_priv::Key { + pub fn into_key(self) -> record::Key { match self { GetProvidersError::Timeout { key, .. } => key, } @@ -2930,26 +2930,26 @@ pub type AddProviderResult = Result; /// The successful result of publishing a provider record. #[derive(Debug, Clone)] pub struct AddProviderOk { - pub key: record_priv::Key, + pub key: record::Key, } /// The possible errors when publishing a provider record. #[derive(Debug, Clone, Error)] pub enum AddProviderError { #[error("the request timed out")] - Timeout { key: record_priv::Key }, + Timeout { key: record::Key }, } impl AddProviderError { /// Gets the key for which the operation failed. - pub fn key(&self) -> &record_priv::Key { + pub fn key(&self) -> &record::Key { match self { AddProviderError::Timeout { key, .. } => key, } } /// Extracts the key for which the operation failed, - pub fn into_key(self) -> record_priv::Key { + pub fn into_key(self) -> record::Key { match self { AddProviderError::Timeout { key, .. } => key, } @@ -3048,7 +3048,7 @@ pub enum QueryInfo { /// A (repeated) query initiated by [`Kademlia::get_providers`]. GetProviders { /// The key for which to search for providers. - key: record_priv::Key, + key: record::Key, /// The number of providers found so far. providers_found: usize, /// Current index of events. @@ -3058,7 +3058,7 @@ pub enum QueryInfo { /// A (repeated) query initiated by [`Kademlia::start_providing`]. AddProvider { /// The record key. - key: record_priv::Key, + key: record::Key, /// The current phase of the query. phase: AddProviderPhase, /// The execution context of the query. @@ -3079,7 +3079,7 @@ pub enum QueryInfo { /// A (repeated) query initiated by [`Kademlia::get_record`]. GetRecord { /// The key to look for. - key: record_priv::Key, + key: record::Key, /// Current index of events. step: ProgressStep, /// Did we find at least one record? diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index ce7712b9e8e..dad1e89c260 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -23,7 +23,7 @@ use super::*; use crate::kbucket::Distance; -use crate::record_priv::{store::MemoryStore, Key}; +use crate::record::{store::MemoryStore, Key}; use crate::{K_VALUE, SHA_256_MH}; use futures::{executor::block_on, future::poll_fn, prelude::*}; use futures_timer::Delay; @@ -446,7 +446,7 @@ fn get_record_not_found() { .map(|(_addr, swarm)| swarm) .collect::>(); - let target_key = record_priv::Key::from(random_multihash()); + let target_key = record::Key::from(random_multihash()); let qid = swarms[0].behaviour_mut().get_record(target_key.clone()); block_on(poll_fn(move |ctx| { @@ -863,7 +863,7 @@ fn get_record_many() { /// network where X is equal to the configured replication factor. #[test] fn add_provider() { - fn prop(keys: Vec, seed: Seed) { + fn prop(keys: Vec, seed: Seed) { let mut rng = StdRng::from_seed(seed.0); let replication_factor = NonZeroUsize::new(rng.gen_range(1..(K_VALUE.get() / 2) + 1)).unwrap(); @@ -1380,7 +1380,7 @@ fn network_behaviour_on_address_change() { #[test] fn get_providers_single() { - fn prop(key: record_priv::Key) { + fn prop(key: record::Key) { let (_, mut single_swarm) = build_node(); single_swarm .behaviour_mut() @@ -1433,7 +1433,7 @@ fn get_providers_single() { } fn get_providers_limit() { - fn prop(key: record_priv::Key) { + fn prop(key: record::Key) { let mut swarms = build_nodes(3); // Let first peer know of second peer and second peer know of third peer. diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index 948f14f30eb..78797b9bb06 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -23,7 +23,7 @@ use crate::protocol::{ KadInStreamSink, KadOutStreamSink, KadPeer, KadRequestMsg, KadResponseMsg, KademliaProtocolConfig, }; -use crate::record_priv::{self, Record}; +use crate::record::{self, Record}; use crate::QueryId; use either::Either; use futures::prelude::*; @@ -240,7 +240,7 @@ pub enum KademliaHandlerEvent { /// this key. GetProvidersReq { /// The key for which providers are requested. - key: record_priv::Key, + key: record::Key, /// Identifier of the request. Needs to be passed back when answering. request_id: KademliaRequestId, }, @@ -266,7 +266,7 @@ pub enum KademliaHandlerEvent { /// The peer announced itself as a provider of a key. AddProvider { /// The key for which the peer is a provider of the associated value. - key: record_priv::Key, + key: record::Key, /// The peer that is the provider of the value for `key`. provider: KadPeer, }, @@ -274,7 +274,7 @@ pub enum KademliaHandlerEvent { /// Request to get a value from the dht records GetRecord { /// Key for which we should look in the dht - key: record_priv::Key, + key: record::Key, /// Identifier of the request. Needs to be passed back when answering. request_id: KademliaRequestId, }, @@ -299,7 +299,7 @@ pub enum KademliaHandlerEvent { /// Response to a request to store a record. PutRecordRes { /// The key of the stored record. - key: record_priv::Key, + key: record::Key, /// The value of the stored record. value: Vec, /// The user data passed to the `PutValue`. @@ -391,7 +391,7 @@ pub enum KademliaHandlerIn { /// this key. GetProvidersReq { /// Identifier being searched. - key: record_priv::Key, + key: record::Key, /// Custom user data. Passed back in the out event when the results arrive. query_id: QueryId, }, @@ -414,7 +414,7 @@ pub enum KademliaHandlerIn { /// succeeded. AddProvider { /// Key for which we should add providers. - key: record_priv::Key, + key: record::Key, /// Known provider for this key. provider: KadPeer, }, @@ -422,7 +422,7 @@ pub enum KademliaHandlerIn { /// Request to retrieve a record from the DHT. GetRecord { /// The key of the record. - key: record_priv::Key, + key: record::Key, /// Custom data. Passed back in the out event when the results arrive. query_id: QueryId, }, @@ -447,7 +447,7 @@ pub enum KademliaHandlerIn { /// Response to a `PutRecord`. PutRecordRes { /// Key of the value that was put. - key: record_priv::Key, + key: record::Key, /// Value that was put. value: Vec, /// Identifier of the request that was made by the remote. diff --git a/protocols/kad/src/jobs.rs b/protocols/kad/src/jobs.rs index cfc4f92941b..6588e42f7bb 100644 --- a/protocols/kad/src/jobs.rs +++ b/protocols/kad/src/jobs.rs @@ -61,7 +61,7 @@ //! > to the size of all stored records. As a job runs, the records are moved //! > out of the job to the consumer, where they can be dropped after being sent. -use crate::record_priv::{self, store::RecordStore, ProviderRecord, Record}; +use crate::record::{self, store::RecordStore, ProviderRecord, Record}; use futures::prelude::*; use futures_timer::Delay; use instant::Instant; @@ -132,7 +132,7 @@ pub(crate) struct PutRecordJob { next_publish: Option, publish_interval: Option, record_ttl: Option, - skipped: HashSet, + skipped: HashSet, inner: PeriodicJob>, } @@ -164,7 +164,7 @@ impl PutRecordJob { /// Adds the key of a record that is ignored on the current or /// next run of the job. - pub(crate) fn skip(&mut self, key: record_priv::Key) { + pub(crate) fn skip(&mut self, key: record::Key) { self.skipped.insert(key); } @@ -330,7 +330,7 @@ impl AddProviderJob { #[cfg(test)] mod tests { use super::*; - use crate::record_priv::store::MemoryStore; + use crate::record::store::MemoryStore; use futures::{executor::block_on, future::poll_fn}; use quickcheck::*; use rand::Rng; diff --git a/protocols/kad/src/kbucket/key.rs b/protocols/kad/src/kbucket/key.rs index 1c48184078a..af2999ebea5 100644 --- a/protocols/kad/src/kbucket/key.rs +++ b/protocols/kad/src/kbucket/key.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::record_priv; +use crate::record; use libp2p_core::multihash::Multihash; use libp2p_identity::PeerId; use sha2::digest::generic_array::{typenum::U32, GenericArray}; @@ -113,8 +113,8 @@ impl From> for Key> { } } -impl From for Key { - fn from(k: record_priv::Key) -> Self { +impl From for Key { + fn from(k: record::Key) -> Self { Key::new(k) } } diff --git a/protocols/kad/src/lib.rs b/protocols/kad/src/lib.rs index 7e51f8b9908..c64ef678d74 100644 --- a/protocols/kad/src/lib.rs +++ b/protocols/kad/src/lib.rs @@ -38,14 +38,6 @@ #![allow(dead_code)] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] -mod record_priv; -#[deprecated( - note = "The `record` module will be made private in the future and should not be depended on." -)] -pub mod record { - pub use super::record_priv::*; -} - mod addresses; mod behaviour; mod handler; @@ -53,6 +45,7 @@ mod jobs; mod kbucket; mod protocol; mod query; +mod record; mod proto { #![allow(unreachable_pub)] @@ -79,7 +72,7 @@ pub use behaviour::{ pub use kbucket::{EntryView, KBucketRef, Key as KBucketKey}; pub use protocol::KadConnectionType; pub use query::QueryId; -pub use record_priv::{store, Key as RecordKey, ProviderRecord, Record}; +pub use record::{store, Key as RecordKey, ProviderRecord, Record}; use libp2p_swarm::StreamProtocol; use std::num::NonZeroUsize; diff --git a/protocols/kad/src/protocol.rs b/protocols/kad/src/protocol.rs index d960e6508d1..3f31f5adb24 100644 --- a/protocols/kad/src/protocol.rs +++ b/protocols/kad/src/protocol.rs @@ -27,7 +27,7 @@ //! is used to send messages to remote peers. use crate::proto; -use crate::record_priv::{self, Record}; +use crate::record::{self, Record}; use asynchronous_codec::Framed; use bytes::BytesMut; use codec::UviBytes; @@ -288,13 +288,13 @@ pub enum KadRequestMsg { /// this key. GetProviders { /// Identifier being searched. - key: record_priv::Key, + key: record::Key, }, /// Indicates that this list of providers is known for this key. AddProvider { /// Key for which we should add providers. - key: record_priv::Key, + key: record::Key, /// Known provider for this key. provider: KadPeer, }, @@ -302,7 +302,7 @@ pub enum KadRequestMsg { /// Request to get a value from the dht records. GetValue { /// The key we are searching for. - key: record_priv::Key, + key: record::Key, }, /// Request to put a value into the dht records. @@ -340,7 +340,7 @@ pub enum KadResponseMsg { /// Response to a `PutValue`. PutValue { /// The key of the record. - key: record_priv::Key, + key: record::Key, /// Value of the record. value: Vec, }, @@ -444,11 +444,11 @@ fn proto_to_req_msg(message: proto::Message) -> Result Ok(KadRequestMsg::PutValue { record }) } proto::MessageType::GET_VALUE => Ok(KadRequestMsg::GetValue { - key: record_priv::Key::from(message.key), + key: record::Key::from(message.key), }), proto::MessageType::FIND_NODE => Ok(KadRequestMsg::FindNode { key: message.key }), proto::MessageType::GET_PROVIDERS => Ok(KadRequestMsg::GetProviders { - key: record_priv::Key::from(message.key), + key: record::Key::from(message.key), }), proto::MessageType::ADD_PROVIDER => { // TODO: for now we don't parse the peer properly, so it is possible that we get @@ -460,7 +460,7 @@ fn proto_to_req_msg(message: proto::Message) -> Result .find_map(|peer| KadPeer::try_from(peer).ok()); if let Some(provider) = provider { - let key = record_priv::Key::from(message.key); + let key = record::Key::from(message.key); Ok(KadRequestMsg::AddProvider { key, provider }) } else { Err(invalid_data("AddProvider message with no valid peer.")) @@ -524,7 +524,7 @@ fn proto_to_resp_msg(message: proto::Message) -> Result { - let key = record_priv::Key::from(message.key); + let key = record::Key::from(message.key); let rec = message .record .ok_or_else(|| invalid_data("received PutValue message with no record"))?; @@ -542,7 +542,7 @@ fn proto_to_resp_msg(message: proto::Message) -> Result Result { - let key = record_priv::Key::from(record.key); + let key = record::Key::from(record.key); let value = record.value; let publisher = if !record.publisher.is_empty() { diff --git a/protocols/kad/src/record_priv.rs b/protocols/kad/src/record.rs similarity index 100% rename from protocols/kad/src/record_priv.rs rename to protocols/kad/src/record.rs diff --git a/protocols/kad/src/record_priv/store.rs b/protocols/kad/src/record/store.rs similarity index 100% rename from protocols/kad/src/record_priv/store.rs rename to protocols/kad/src/record/store.rs diff --git a/protocols/kad/src/record_priv/store/memory.rs b/protocols/kad/src/record/store/memory.rs similarity index 100% rename from protocols/kad/src/record_priv/store/memory.rs rename to protocols/kad/src/record/store/memory.rs