Make MetaAddr.last_seen into a private field

This commit is contained in:
teor 2021-03-25 18:36:06 +10:00
parent eae59de1e8
commit 6fe81d8992
4 changed files with 58 additions and 28 deletions

View File

@ -105,7 +105,7 @@ impl AddressBook {
); );
if let Some(prev) = self.get_by_addr(new.addr) { if let Some(prev) = self.get_by_addr(new.addr) {
if prev.last_seen > new.last_seen { if prev.get_last_seen() > new.get_last_seen() {
return; return;
} }
} }
@ -160,7 +160,7 @@ impl AddressBook {
// NeverAttempted, Failed, and AttemptPending peers should never be live // NeverAttempted, Failed, and AttemptPending peers should never be live
Some(peer) => { Some(peer) => {
peer.last_connection_state == PeerAddrState::Responded peer.last_connection_state == PeerAddrState::Responded
&& peer.last_seen > AddressBook::liveness_cutoff_time() && peer.get_last_seen() > AddressBook::liveness_cutoff_time()
} }
} }
} }

View File

@ -104,6 +104,42 @@ pub struct MetaAddr {
/// records, older peer versions, or buggy or malicious peers. /// records, older peer versions, or buggy or malicious peers.
pub services: PeerServices, pub services: PeerServices,
/// The last time we interacted with this peer.
///
/// See `get_last_seen` for details.
last_seen: DateTime<Utc>,
/// The outcome of our most recent communication attempt with this peer.
pub last_connection_state: PeerAddrState,
}
impl MetaAddr {
/// Create a new `MetaAddr` from the deserialized fields in an `Addr`
/// message.
pub fn new_gossiped(
addr: &SocketAddr,
services: &PeerServices,
last_seen: &DateTime<Utc>,
) -> MetaAddr {
MetaAddr {
addr: *addr,
services: *services,
last_seen: *last_seen,
// the state is Zebra-specific, it isn't part of the Zcash network protocol
last_connection_state: NeverAttempted,
}
}
/// Create a new `MetaAddr` for a peer that has just `Responded`.
pub fn new_responded(addr: &SocketAddr, services: &PeerServices) -> MetaAddr {
MetaAddr {
addr: *addr,
services: *services,
last_seen: Utc::now(),
last_connection_state: Responded,
}
}
/// The last time we interacted with this peer. /// The last time we interacted with this peer.
/// ///
/// The exact meaning depends on `last_connection_state`: /// The exact meaning depends on `last_connection_state`:
@ -118,17 +154,19 @@ pub struct MetaAddr {
/// ///
/// `last_seen` times from `NeverAttempted` peers may be invalid due to /// `last_seen` times from `NeverAttempted` peers may be invalid due to
/// clock skew, or buggy or malicious peers. /// clock skew, or buggy or malicious peers.
pub last_seen: DateTime<Utc>, pub fn get_last_seen(&self) -> DateTime<Utc> {
self.last_seen
}
/// The outcome of our most recent communication attempt with this peer. /// Update the last time we interacted with this peer to the current time.
pub last_connection_state: PeerAddrState, pub fn update_last_seen(&mut self) {
} self.last_seen = Utc::now();
}
impl MetaAddr {
/// Return a sanitized version of this `MetaAddr`, for sending to a remote peer. /// Return a sanitized version of this `MetaAddr`, for sending to a remote peer.
pub fn sanitize(&self) -> MetaAddr { pub fn sanitize(&self) -> MetaAddr {
let interval = crate::constants::TIMESTAMP_TRUNCATION_SECONDS; let interval = crate::constants::TIMESTAMP_TRUNCATION_SECONDS;
let ts = self.last_seen.timestamp(); let ts = self.get_last_seen().timestamp();
let last_seen = Utc.timestamp(ts - ts.rem_euclid(interval), 0); let last_seen = Utc.timestamp(ts - ts.rem_euclid(interval), 0);
MetaAddr { MetaAddr {
addr: self.addr, addr: self.addr,
@ -151,7 +189,7 @@ impl Ord for MetaAddr {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
use std::net::IpAddr::{V4, V6}; use std::net::IpAddr::{V4, V6};
let oldest_first = self.last_seen.cmp(&other.last_seen); let oldest_first = self.get_last_seen().cmp(&other.get_last_seen());
let newest_first = oldest_first.reverse(); let newest_first = oldest_first.reverse();
let connection_state = self.last_connection_state.cmp(&other.last_connection_state); let connection_state = self.last_connection_state.cmp(&other.last_connection_state);
@ -187,7 +225,7 @@ impl PartialOrd for MetaAddr {
impl ZcashSerialize for MetaAddr { impl ZcashSerialize for MetaAddr {
fn zcash_serialize<W: Write>(&self, mut writer: W) -> Result<(), std::io::Error> { fn zcash_serialize<W: Write>(&self, mut writer: W) -> Result<(), std::io::Error> {
writer.write_u32::<LittleEndian>(self.last_seen.timestamp() as u32)?; writer.write_u32::<LittleEndian>(self.get_last_seen().timestamp() as u32)?;
writer.write_u64::<LittleEndian>(self.services.bits())?; writer.write_u64::<LittleEndian>(self.services.bits())?;
writer.write_socket_addr(self.addr)?; writer.write_socket_addr(self.addr)?;
Ok(()) Ok(())
@ -196,13 +234,11 @@ impl ZcashSerialize for MetaAddr {
impl ZcashDeserialize for MetaAddr { impl ZcashDeserialize for MetaAddr {
fn zcash_deserialize<R: Read>(mut reader: R) -> Result<Self, SerializationError> { fn zcash_deserialize<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(MetaAddr { let last_seen = Utc.timestamp(reader.read_u32::<LittleEndian>()? as i64, 0);
last_seen: Utc.timestamp(reader.read_u32::<LittleEndian>()? as i64, 0), let services = PeerServices::from_bits_truncate(reader.read_u64::<LittleEndian>()?);
// Discard unknown service bits. let addr = reader.read_socket_addr()?;
services: PeerServices::from_bits_truncate(reader.read_u64::<LittleEndian>()?),
addr: reader.read_socket_addr()?, Ok(MetaAddr::new_gossiped(&addr, &services, &last_seen))
last_connection_state: Default::default(),
})
} }
} }
@ -227,7 +263,7 @@ mod tests {
// We want the sanitized timestamp to be a multiple of the truncation interval. // We want the sanitized timestamp to be a multiple of the truncation interval.
assert_eq!( assert_eq!(
entry.last_seen.timestamp() % crate::constants::TIMESTAMP_TRUNCATION_SECONDS, entry.get_last_seen().timestamp() % crate::constants::TIMESTAMP_TRUNCATION_SECONDS,
0 0
); );
// We want the state to be the default // We want the state to be the default

View File

@ -27,7 +27,7 @@ use crate::{
internal::{Request, Response}, internal::{Request, Response},
}, },
types::MetaAddr, types::MetaAddr,
BoxError, Config, PeerAddrState, BoxError, Config,
}; };
use super::{Client, Connection, ErrorSlot, HandshakeError, PeerError}; use super::{Client, Connection, ErrorSlot, HandshakeError, PeerError};
@ -390,12 +390,7 @@ where
); );
use futures::sink::SinkExt; use futures::sink::SinkExt;
let _ = timestamp_collector let _ = timestamp_collector
.send(MetaAddr { .send(MetaAddr::new_responded(&addr, &remote_services))
addr,
services: remote_services,
last_seen: Utc::now(),
last_connection_state: PeerAddrState::Responded,
})
.await; .await;
} }
msg msg

View File

@ -4,7 +4,6 @@ use std::{
time::Duration, time::Duration,
}; };
use chrono::Utc;
use futures::stream::{FuturesUnordered, StreamExt}; use futures::stream::{FuturesUnordered, StreamExt};
use tokio::time::{sleep, sleep_until, Sleep}; use tokio::time::{sleep, sleep_until, Sleep};
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
@ -221,7 +220,7 @@ where
// instead of yielding the next connection. // instead of yielding the next connection.
let mut reconnect = peer_set_guard.reconnection_peers().next()?; let mut reconnect = peer_set_guard.reconnection_peers().next()?;
reconnect.last_seen = Utc::now(); reconnect.update_last_seen();
reconnect.last_connection_state = PeerAddrState::AttemptPending; reconnect.last_connection_state = PeerAddrState::AttemptPending;
peer_set_guard.update(reconnect); peer_set_guard.update(reconnect);
reconnect reconnect
@ -235,7 +234,7 @@ where
/// Mark `addr` as a failed peer. /// Mark `addr` as a failed peer.
pub fn report_failed(&mut self, mut addr: MetaAddr) { pub fn report_failed(&mut self, mut addr: MetaAddr) {
addr.last_seen = Utc::now(); addr.update_last_seen();
addr.last_connection_state = PeerAddrState::Failed; addr.last_connection_state = PeerAddrState::Failed;
self.peer_set.lock().unwrap().update(addr); self.peer_set.lock().unwrap().update(addr);
} }