Skip to content

Commit

Permalink
litep2p/peerstore: Fix bump last updated time (#4971)
Browse files Browse the repository at this point in the history
This PR bumps the last time of a reputation update of a peer.
Doing so ensures the peer remains in the peerstore for longer than 1
hour.

Libp2p updates the `last_updated` field as well.

Small summary for the peerstore:
- A: when peers are reported the `last_updated` time is set to current
time (not done before this PR)
- B: peers that were not updated for 1 hour are removed from the
peerstore
- the reputation of the peers is decaying to zero over time
- peers are reported with a reputation change (positive or negative
depending on the behavior)

Because, (A) was not updating the `last_updated` time, we might lose the
reputation of peers that are constantly updated after 1hour because of
(B).

cc @paritytech/networking

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv authored Jul 8, 2024
1 parent 7290042 commit d4657f8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion substrate/client/network/src/litep2p/peerstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ impl PeerInfo {
self.reputation < BANNED_THRESHOLD
}

fn add_reputation(&mut self, increment: i32) {
self.reputation = self.reputation.saturating_add(increment);
self.bump_last_updated();
}

fn decay_reputation(&mut self, seconds_passed: u64) {
// Note that decaying the reputation value happens "on its own",
// so we don't do `bump_last_updated()`.
Expand All @@ -103,6 +108,10 @@ impl PeerInfo {
}
}
}

fn bump_last_updated(&mut self) {
self.last_updated = Instant::now();
}
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -169,7 +178,7 @@ impl PeerStoreProvider for PeerstoreHandle {

match lock.peers.get_mut(&peer) {
Some(info) => {
info.reputation = info.reputation.saturating_add(reputation_change.value);
info.add_reputation(reputation_change.value);
},
None => {
lock.peers.insert(
Expand Down

0 comments on commit d4657f8

Please sign in to comment.