Skip to content

Commit

Permalink
chore(kad): remove deprecated record
Browse files Browse the repository at this point in the history
Deprecated in libp2p#3738

Deprecation released with `v0.51.3`.

Follow up to libp2p#3896

Part of libp2p#3647
  • Loading branch information
mxinden committed Jun 6, 2023
1 parent 09d04fa commit 7f33cb0
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 72 deletions.
64 changes: 32 additions & 32 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<QueryId, store::Error> {
pub fn start_providing(&mut self, key: record::Key) -> Result<QueryId, store::Error> {
// 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.
Expand Down Expand Up @@ -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());
}
Expand All @@ -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)
Expand Down Expand Up @@ -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<KadPeer> {
fn provider_peers(&mut self, key: &record::Key, source: &PeerId) -> Vec<KadPeer> {
let kbuckets = &mut self.kbuckets;
let connected = &mut self.connected_peers;
let listen_addresses = &self.listen_addresses;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<PeerId>,
},
#[error("the quorum failed; needed {quorum} peers")]
QuorumFailed {
key: record_priv::Key,
key: record::Key,
records: Vec<PeerRecord>,
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,
Expand All @@ -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,
Expand All @@ -2786,22 +2786,22 @@ pub type PutRecordResult = Result<PutRecordOk, PutRecordError>;
/// 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`].
#[derive(Debug, Clone, Error)]
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<PeerId>,
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<PeerId>,
quorum: NonZeroUsize,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -2888,7 +2888,7 @@ pub type GetProvidersResult = Result<GetProvidersOk, GetProvidersError>;
#[derive(Debug, Clone)]
pub enum GetProvidersOk {
FoundProviders {
key: record_priv::Key,
key: record::Key,
/// The new set of providers discovered.
providers: HashSet<PeerId>,
},
Expand All @@ -2902,22 +2902,22 @@ pub enum GetProvidersOk {
pub enum GetProvidersError {
#[error("the request timed out")]
Timeout {
key: record_priv::Key,
key: record::Key,
closest_peers: Vec<PeerId>,
},
}

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,
}
}

/// 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,
}
Expand All @@ -2930,26 +2930,26 @@ pub type AddProviderResult = Result<AddProviderOk, AddProviderError>;
/// 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,
}
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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?
Expand Down
10 changes: 5 additions & 5 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -446,7 +446,7 @@ fn get_record_not_found() {
.map(|(_addr, swarm)| swarm)
.collect::<Vec<_>>();

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| {
Expand Down Expand Up @@ -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<record_priv::Key>, seed: Seed) {
fn prop(keys: Vec<record::Key>, 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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1433,7 +1433,7 @@ fn get_providers_single() {
}

fn get_providers_limit<const N: usize>() {
fn prop<const N: usize>(key: record_priv::Key) {
fn prop<const N: usize>(key: record::Key) {
let mut swarms = build_nodes(3);

// Let first peer know of second peer and second peer know of third peer.
Expand Down
18 changes: 9 additions & 9 deletions protocols/kad/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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,
},
Expand All @@ -266,15 +266,15 @@ 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,
},

/// 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,
},
Expand All @@ -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<u8>,
/// The user data passed to the `PutValue`.
Expand Down Expand Up @@ -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,
},
Expand All @@ -414,15 +414,15 @@ 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,
},

/// 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,
},
Expand All @@ -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<u8>,
/// Identifier of the request that was made by the remote.
Expand Down
Loading

0 comments on commit 7f33cb0

Please sign in to comment.