node: refactor announcement timestamp handling
Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
4c1aa65f90
commit
45ea9cc97d
|
|
@ -621,9 +621,7 @@ where
|
||||||
AnnouncementMessage::Inventory(message) => {
|
AnnouncementMessage::Inventory(message) => {
|
||||||
// Discard inventory messages we've already seen, otherwise update
|
// Discard inventory messages we've already seen, otherwise update
|
||||||
// out last seen time.
|
// out last seen time.
|
||||||
if timestamp > peer.last_inventory {
|
if !peer.inventory_announced(timestamp) {
|
||||||
peer.last_inventory = timestamp;
|
|
||||||
} else {
|
|
||||||
debug!("Ignoring stale inventory announcement from {node}");
|
debug!("Ignoring stale inventory announcement from {node}");
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
@ -647,24 +645,12 @@ where
|
||||||
AnnouncementMessage::Refs(message) => {
|
AnnouncementMessage::Refs(message) => {
|
||||||
// TODO: Buffer/throttle fetches.
|
// TODO: Buffer/throttle fetches.
|
||||||
// TODO: Check that we're tracking this user as well.
|
// TODO: Check that we're tracking this user as well.
|
||||||
// FIXME: Discard old messages.
|
|
||||||
if self.config.is_tracking(&message.id) {
|
if self.config.is_tracking(&message.id) {
|
||||||
// Discard inventory messages we've already seen, otherwise update
|
// Discard inventory messages we've already seen, otherwise update
|
||||||
// out last seen time.
|
// out last seen time.
|
||||||
match peer.last_refs.entry(message.id) {
|
if !peer.refs_announced(message.id, timestamp) {
|
||||||
Entry::Vacant(e) => {
|
debug!("Ignoring stale refs announcement from {node}");
|
||||||
e.insert(timestamp);
|
return Ok(false);
|
||||||
}
|
|
||||||
Entry::Occupied(mut e) => {
|
|
||||||
let last = e.get_mut();
|
|
||||||
|
|
||||||
if timestamp > *last {
|
|
||||||
*last = timestamp;
|
|
||||||
} else {
|
|
||||||
debug!("Ignoring stale refs announcement from {node}");
|
|
||||||
return Ok(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// TODO: Check refs to see if we should try to fetch or not.
|
// TODO: Check refs to see if we should try to fetch or not.
|
||||||
// FIXME: This code is wrong: we shouldn't be fetching from the connected peer,
|
// FIXME: This code is wrong: we shouldn't be fetching from the connected peer,
|
||||||
|
|
@ -691,9 +677,7 @@ where
|
||||||
}) => {
|
}) => {
|
||||||
// Discard node messages we've already seen, otherwise update
|
// Discard node messages we've already seen, otherwise update
|
||||||
// out last seen time.
|
// out last seen time.
|
||||||
if timestamp > peer.last_node {
|
if !peer.node_announced(timestamp) {
|
||||||
peer.last_node = timestamp;
|
|
||||||
} else {
|
|
||||||
debug!("Ignoring stale node announcement from {node}");
|
debug!("Ignoring stale node announcement from {node}");
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
@ -1023,6 +1007,48 @@ pub struct Node {
|
||||||
pub last_node: Timestamp,
|
pub last_node: Timestamp,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Node {
|
||||||
|
/// Process a refs announcement for the given node.
|
||||||
|
/// Returns `true` if the timestamp was updated.
|
||||||
|
pub fn refs_announced(&mut self, id: Id, t: Timestamp) -> bool {
|
||||||
|
match self.last_refs.entry(id) {
|
||||||
|
Entry::Vacant(e) => {
|
||||||
|
e.insert(t);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Entry::Occupied(mut e) => {
|
||||||
|
let last = e.get_mut();
|
||||||
|
|
||||||
|
if t > *last {
|
||||||
|
*last = t;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Process an inventory announcement for the given node.
|
||||||
|
/// Returns `true` if the timestamp was updated.
|
||||||
|
pub fn inventory_announced(&mut self, t: Timestamp) -> bool {
|
||||||
|
if t > self.last_inventory {
|
||||||
|
self.last_inventory = t;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Process a node announcement for the given node.
|
||||||
|
/// Returns `true` if the timestamp was updated.
|
||||||
|
pub fn node_announced(&mut self, t: Timestamp) -> bool {
|
||||||
|
if t > self.last_node {
|
||||||
|
self.last_node = t;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
/// Holds currently (or recently) connected peers.
|
/// Holds currently (or recently) connected peers.
|
||||||
pub struct Sessions(AddressBook<IpAddr, Session>);
|
pub struct Sessions(AddressBook<IpAddr, Session>);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue