diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index d50f7319af8..81ae5ee5649 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,3 +1,6 @@ + Add new `new_anonymous` function to enable putting Records without a publisher. + See [PR 6176](https://github.com/libp2p/rust-libp2p/pull/6176) + ## 0.49.0 - Remove no longer constructed GetRecordError::QuorumFailed. diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index f5f44baec74..460fd29a775 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -870,7 +870,10 @@ where mut record: Record, quorum: Quorum, ) -> Result { - record.publisher = Some(*self.kbuckets.local_key().preimage()); + record + .publisher + .and(Some(*self.kbuckets.local_key().preimage())); + self.store.put(record.clone())?; record.expires = record .expires diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 3819276d350..6c419e4d28c 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -557,7 +557,7 @@ fn get_record_not_found() { })) } -/// A node joining a fully connected network via three (ALPHA_VALUE) bootnodes +/// A node joining a fully connected network via three (`ALPHA_VALUE`) bootnodes /// should be able to put a record to the X closest nodes of the network where X /// is equal to the configured replication factor. #[test] @@ -609,6 +609,8 @@ fn put_record() { #[allow(clippy::mutable_key_type)] // False positive, we never modify `Bytes`. let records = records .into_iter() + // Exclude records without a publisher. + .filter(|r| r.publisher.is_some()) .take(num_total) .map(|mut r| { // We don't want records to expire prematurely, as they would diff --git a/protocols/kad/src/record.rs b/protocols/kad/src/record.rs index fea17f826a4..f0a99db22ef 100644 --- a/protocols/kad/src/record.rs +++ b/protocols/kad/src/record.rs @@ -94,6 +94,16 @@ impl Record { where K: Into, { + Record { + key: key.into(), + value, + publisher: Some(PeerId::random()), + expires: None, + } + } + + /// Creates a new record for insertion into the DHT. + pub fn new_anonymous(key: impl Into, value: Vec) -> Self { Record { key: key.into(), value,