2019-09-12 03:36:50 -07:00
|
|
|
//! An address-with-metadata type used in Bitcoin networking.
|
|
|
|
|
2019-10-17 21:19:23 -07:00
|
|
|
use std::{
|
|
|
|
cmp::{Ord, Ordering},
|
|
|
|
io::{Read, Write},
|
|
|
|
net::SocketAddr,
|
|
|
|
};
|
2019-09-12 03:36:50 -07:00
|
|
|
|
2019-09-25 16:20:02 -07:00
|
|
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
|
|
|
use chrono::{DateTime, TimeZone, Utc};
|
|
|
|
|
|
|
|
use zebra_chain::serialization::{
|
|
|
|
ReadZcashExt, SerializationError, WriteZcashExt, ZcashDeserialize, ZcashSerialize,
|
|
|
|
};
|
|
|
|
|
2019-09-30 11:40:44 -07:00
|
|
|
use crate::protocol::types::PeerServices;
|
2019-09-12 03:36:50 -07:00
|
|
|
|
2021-03-25 01:34:52 -07:00
|
|
|
use PeerAddrState::*;
|
|
|
|
|
2021-02-17 17:18:32 -08:00
|
|
|
/// Peer connection state, based on our interactions with the peer.
|
|
|
|
///
|
|
|
|
/// Zebra also tracks how recently a peer has sent us messages, and derives peer
|
|
|
|
/// liveness based on the current time. This derived state is tracked using
|
|
|
|
/// [`AddressBook::maybe_connected_peers`] and
|
|
|
|
/// [`AddressBook::reconnection_peers`].
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub enum PeerAddrState {
|
|
|
|
/// The peer has sent us a valid message.
|
|
|
|
///
|
|
|
|
/// Peers remain in this state, even if they stop responding to requests.
|
|
|
|
/// (Peer liveness is derived from the `last_seen` timestamp, and the current
|
|
|
|
/// time.)
|
|
|
|
Responded,
|
|
|
|
|
|
|
|
/// The peer's address has just been fetched from a DNS seeder, or via peer
|
|
|
|
/// gossip, but we haven't attempted to connect to it yet.
|
|
|
|
NeverAttempted,
|
|
|
|
|
|
|
|
/// The peer's TCP connection failed, or the peer sent us an unexpected
|
|
|
|
/// Zcash protocol message, so we failed the connection.
|
|
|
|
Failed,
|
|
|
|
|
|
|
|
/// We just started a connection attempt to this peer.
|
|
|
|
AttemptPending,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PeerAddrState {
|
|
|
|
fn default() -> Self {
|
2021-03-25 01:34:52 -07:00
|
|
|
NeverAttempted
|
2021-02-17 17:18:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for PeerAddrState {
|
|
|
|
/// `PeerAddrState`s are sorted in approximate reconnection attempt
|
|
|
|
/// order, ignoring liveness.
|
|
|
|
///
|
|
|
|
/// See [`CandidateSet`] and [`MetaAddr::cmp`] for more details.
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
match (self, other) {
|
|
|
|
(Responded, Responded)
|
|
|
|
| (NeverAttempted, NeverAttempted)
|
|
|
|
| (Failed, Failed)
|
|
|
|
| (AttemptPending, AttemptPending) => Ordering::Equal,
|
|
|
|
// We reconnect to `Responded` peers that have stopped sending messages,
|
|
|
|
// then `NeverAttempted` peers, then `Failed` peers
|
|
|
|
(Responded, _) => Ordering::Less,
|
|
|
|
(_, Responded) => Ordering::Greater,
|
|
|
|
(NeverAttempted, _) => Ordering::Less,
|
|
|
|
(_, NeverAttempted) => Ordering::Greater,
|
|
|
|
(Failed, _) => Ordering::Less,
|
|
|
|
(_, Failed) => Ordering::Greater,
|
|
|
|
// AttemptPending is covered by the other cases
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for PeerAddrState {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 03:36:50 -07:00
|
|
|
/// An address with metadata on its advertised services and last-seen time.
|
2019-09-19 09:38:02 -07:00
|
|
|
///
|
2019-09-12 03:36:50 -07:00
|
|
|
/// [Bitcoin reference](https://en.bitcoin.it/wiki/Protocol_documentation#Network_address)
|
2019-09-15 12:11:07 -07:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
2019-09-12 03:36:50 -07:00
|
|
|
pub struct MetaAddr {
|
|
|
|
/// The peer's address.
|
2019-09-14 08:56:43 -07:00
|
|
|
pub addr: SocketAddr,
|
2021-02-17 17:18:32 -08:00
|
|
|
|
2019-09-14 08:56:43 -07:00
|
|
|
/// The services advertised by the peer.
|
2021-02-17 17:18:32 -08:00
|
|
|
///
|
|
|
|
/// The exact meaning depends on `last_connection_state`:
|
|
|
|
/// - `Responded`: the services advertised by this peer, the last time we
|
|
|
|
/// performed a handshake with it
|
|
|
|
/// - `NeverAttempted`: the unverified services provided by the remote peer
|
|
|
|
/// that sent us this address
|
|
|
|
/// - `Failed` or `AttemptPending`: unverified services via another peer,
|
|
|
|
/// or services advertised in a previous handshake
|
|
|
|
///
|
|
|
|
/// ## Security
|
|
|
|
///
|
|
|
|
/// `services` from `NeverAttempted` peers may be invalid due to outdated
|
|
|
|
/// records, older peer versions, or buggy or malicious peers.
|
2019-09-30 11:40:44 -07:00
|
|
|
pub services: PeerServices,
|
2021-02-17 17:18:32 -08:00
|
|
|
|
|
|
|
/// The last time we interacted with this peer.
|
|
|
|
///
|
|
|
|
/// The exact meaning depends on `last_connection_state`:
|
|
|
|
/// - `Responded`: the last time we processed a message from this peer
|
|
|
|
/// - `NeverAttempted`: the unverified time provided by the remote peer
|
|
|
|
/// that sent us this address
|
|
|
|
/// - `Failed`: the last time we marked the peer as failed
|
|
|
|
/// - `AttemptPending`: the last time we queued the peer for a reconnection
|
|
|
|
/// attempt
|
|
|
|
///
|
|
|
|
/// ## Security
|
|
|
|
///
|
|
|
|
/// `last_seen` times from `NeverAttempted` peers may be invalid due to
|
|
|
|
/// clock skew, or buggy or malicious peers.
|
2019-09-14 08:56:43 -07:00
|
|
|
pub last_seen: DateTime<Utc>,
|
2021-02-17 17:18:32 -08:00
|
|
|
|
|
|
|
/// The outcome of our most recent communication attempt with this peer.
|
|
|
|
pub last_connection_state: PeerAddrState,
|
2019-09-12 03:36:50 -07:00
|
|
|
}
|
2019-09-25 16:20:02 -07:00
|
|
|
|
2019-11-13 14:03:12 -08:00
|
|
|
impl MetaAddr {
|
2021-03-25 00:47:25 -07:00
|
|
|
/// Return a sanitized version of this `MetaAddr`, for sending to a remote peer.
|
|
|
|
pub fn sanitize(&self) -> MetaAddr {
|
2019-11-13 14:03:12 -08:00
|
|
|
let interval = crate::constants::TIMESTAMP_TRUNCATION_SECONDS;
|
|
|
|
let ts = self.last_seen.timestamp();
|
2021-03-25 00:47:25 -07:00
|
|
|
let last_seen = Utc.timestamp(ts - ts.rem_euclid(interval), 0);
|
|
|
|
MetaAddr {
|
|
|
|
addr: self.addr,
|
|
|
|
services: self.services,
|
|
|
|
last_seen,
|
|
|
|
// the state isn't sent to the remote peer, but sanitize it anyway
|
|
|
|
last_connection_state: Default::default(),
|
|
|
|
}
|
2019-11-13 14:03:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 21:19:23 -07:00
|
|
|
impl Ord for MetaAddr {
|
2021-02-17 17:18:32 -08:00
|
|
|
/// `MetaAddr`s are sorted in approximate reconnection attempt order, but
|
|
|
|
/// with `Responded` peers sorted first as a group.
|
|
|
|
///
|
|
|
|
/// This order should not be used for reconnection attempts: use
|
|
|
|
/// [`AddressBook::reconnection_peers`] instead.
|
|
|
|
///
|
|
|
|
/// See [`CandidateSet`] for more details.
|
2019-10-17 21:19:23 -07:00
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
2021-02-17 17:18:32 -08:00
|
|
|
use std::net::IpAddr::{V4, V6};
|
|
|
|
|
|
|
|
let oldest_first = self.last_seen.cmp(&other.last_seen);
|
|
|
|
let newest_first = oldest_first.reverse();
|
|
|
|
|
|
|
|
let connection_state = self.last_connection_state.cmp(&other.last_connection_state);
|
|
|
|
let reconnection_time = match self.last_connection_state {
|
|
|
|
Responded => oldest_first,
|
|
|
|
NeverAttempted => newest_first,
|
|
|
|
Failed => oldest_first,
|
|
|
|
AttemptPending => oldest_first,
|
|
|
|
};
|
|
|
|
let ip_numeric = match (self.addr.ip(), other.addr.ip()) {
|
|
|
|
(V4(a), V4(b)) => a.octets().cmp(&b.octets()),
|
|
|
|
(V6(a), V6(b)) => a.octets().cmp(&b.octets()),
|
|
|
|
(V4(_), V6(_)) => Ordering::Less,
|
|
|
|
(V6(_), V4(_)) => Ordering::Greater,
|
|
|
|
};
|
|
|
|
|
|
|
|
connection_state
|
|
|
|
.then(reconnection_time)
|
2019-10-17 21:19:23 -07:00
|
|
|
// The remainder is meaningless as an ordering, but required so that we
|
|
|
|
// have a total order on `MetaAddr` values: self and other must compare
|
|
|
|
// as Ordering::Equal iff they are equal.
|
2021-02-17 17:18:32 -08:00
|
|
|
.then(ip_numeric)
|
2019-10-17 21:19:23 -07:00
|
|
|
.then(self.addr.port().cmp(&other.addr.port()))
|
|
|
|
.then(self.services.bits().cmp(&other.services.bits()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for MetaAddr {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 16:20:02 -07:00
|
|
|
impl ZcashSerialize for MetaAddr {
|
2020-02-05 14:32:10 -08:00
|
|
|
fn zcash_serialize<W: Write>(&self, mut writer: W) -> Result<(), std::io::Error> {
|
2019-09-25 16:20:02 -07:00
|
|
|
writer.write_u32::<LittleEndian>(self.last_seen.timestamp() as u32)?;
|
2019-09-30 11:58:32 -07:00
|
|
|
writer.write_u64::<LittleEndian>(self.services.bits())?;
|
2019-09-25 16:20:02 -07:00
|
|
|
writer.write_socket_addr(self.addr)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ZcashDeserialize for MetaAddr {
|
|
|
|
fn zcash_deserialize<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
|
|
|
|
Ok(MetaAddr {
|
|
|
|
last_seen: Utc.timestamp(reader.read_u32::<LittleEndian>()? as i64, 0),
|
2019-09-30 11:58:32 -07:00
|
|
|
// Discard unknown service bits.
|
|
|
|
services: PeerServices::from_bits_truncate(reader.read_u64::<LittleEndian>()?),
|
2019-09-25 16:20:02 -07:00
|
|
|
addr: reader.read_socket_addr()?,
|
2021-02-17 17:18:32 -08:00
|
|
|
last_connection_state: Default::default(),
|
2019-09-25 16:20:02 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-11-13 14:03:12 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
// XXX remove this test and replace it with a proptest instance.
|
|
|
|
#[test]
|
|
|
|
fn sanitize_truncates_timestamps() {
|
2020-11-09 16:29:25 -08:00
|
|
|
zebra_test::init();
|
|
|
|
|
2021-02-17 17:18:32 -08:00
|
|
|
let services = PeerServices::default();
|
|
|
|
let addr = "127.0.0.1:8233".parse().unwrap();
|
|
|
|
|
2019-11-13 14:03:12 -08:00
|
|
|
let entry = MetaAddr {
|
2021-02-17 17:18:32 -08:00
|
|
|
services,
|
|
|
|
addr,
|
2020-05-26 18:00:58 -07:00
|
|
|
last_seen: Utc.timestamp(1_573_680_222, 0),
|
2021-03-25 01:34:52 -07:00
|
|
|
last_connection_state: Responded,
|
2019-11-13 14:03:12 -08:00
|
|
|
}
|
|
|
|
.sanitize();
|
2021-02-17 17:18:32 -08:00
|
|
|
|
2019-11-13 14:03:12 -08:00
|
|
|
// We want the sanitized timestamp to be a multiple of the truncation interval.
|
|
|
|
assert_eq!(
|
|
|
|
entry.last_seen.timestamp() % crate::constants::TIMESTAMP_TRUNCATION_SECONDS,
|
|
|
|
0
|
|
|
|
);
|
2021-02-17 17:18:32 -08:00
|
|
|
// We want the state to be the default
|
|
|
|
assert_eq!(entry.last_connection_state, Default::default());
|
|
|
|
// We want the other fields to be unmodified
|
|
|
|
assert_eq!(entry.addr, addr);
|
|
|
|
assert_eq!(entry.services, services);
|
2019-11-13 14:03:12 -08:00
|
|
|
}
|
|
|
|
}
|