Skip to content

Commit 19bf131

Browse files
lexnvdmitry-markin
andauthored
kad: Do not update memory store on incoming GetRecordSuccess (#190)
This PR ensures that the records discovered by the `GetRecord` query are not propagated to the memory store. This makes the litep2p behavior similar to the libp2p. Another approach could be to put the record into the memory store if the validation mode is `Automatic`. However, for networks with a large number of records, this will exhaust the limit of our memory store. Thanks @alexggh for catching this 🙏 Fix for: - #190 cc @paritytech/networking --------- Signed-off-by: Alexandru Vasile <[email protected]> Co-authored-by: Dmitry Markin <[email protected]>
1 parent 171e36a commit 19bf131

File tree

1 file changed

+4
-31
lines changed
  • src/protocol/libp2p/kademlia

1 file changed

+4
-31
lines changed

src/protocol/libp2p/kademlia/mod.rs

+4-31
Original file line numberDiff line numberDiff line change
@@ -656,31 +656,6 @@ impl Kademlia {
656656
Ok(())
657657
}
658658
QueryAction::GetRecordQueryDone { query_id, records } => {
659-
// Considering this gives a view of all peers and their records, some peers may have
660-
// outdated records. Store only the record which is backed by most
661-
// peers.
662-
let now = std::time::Instant::now();
663-
let rec = records
664-
.iter()
665-
.filter_map(|peer_record| {
666-
if peer_record.record.is_expired(now) {
667-
None
668-
} else {
669-
Some(&peer_record.record)
670-
}
671-
})
672-
.fold(HashMap::new(), |mut acc, rec| {
673-
*acc.entry(rec).or_insert(0) += 1;
674-
acc
675-
})
676-
.into_iter()
677-
.max_by_key(|(_, v)| *v)
678-
.map(|(k, _)| k);
679-
680-
if let Some(record) = rec {
681-
self.store.put(record.clone());
682-
}
683-
684659
let _ = self
685660
.event_tx
686661
.send(KademliaEvent::GetRecordSuccess {
@@ -976,9 +951,8 @@ mod tests {
976951
let action = QueryAction::GetRecordQueryDone { query_id, records };
977952
assert!(kademlia.on_query_action(action).await.is_ok());
978953

979-
// Check the local storage was updated.
980-
let record = kademlia.store.get(&key).unwrap();
981-
assert_eq!(record.value, vec![0x1]);
954+
// Check the local storage should not get updated.
955+
assert!(kademlia.store.get(&key).is_none());
982956
}
983957

984958
#[tokio::test]
@@ -1017,8 +991,7 @@ mod tests {
1017991
let action = QueryAction::GetRecordQueryDone { query_id, records };
1018992
assert!(kademlia.on_query_action(action).await.is_ok());
1019993

1020-
// Check the local storage was updated.
1021-
let record = kademlia.store.get(&key).unwrap();
1022-
assert_eq!(record.value, vec![0x2]);
994+
// Check the local storage should not get updated.
995+
assert!(kademlia.store.get(&key).is_none());
1023996
}
1024997
}

0 commit comments

Comments
 (0)