Introduce a `MetaAddr` type replacing `NetworkAddress`.

The `NetworkAddress` type was a `(Services, SocketAddr)` pair as used in the
`version` handshake message, described as the `net_addr` struct in the Bitcoin
wiki protocol documentation.  However, all of the other uses of the `net_addr`
struct are a `(Timestamp, Services, SocketAddr)` pair (where the timestamp is
the last-seen time of the peer), and the timestamp is omitted only during the
`version` messages, which are used only during the handshake, so it seems
better to include the timestamp field and omit it during serialization of
`version` packets.
This commit is contained in:
Henry de Valence 2019-09-12 03:36:50 -07:00 committed by Deirdre Connolly
parent 1d0517fe56
commit b9af047a09
4 changed files with 29 additions and 29 deletions

View File

@ -5,3 +5,4 @@
pub mod message;
pub mod types;
mod constants;
mod meta_addr;

View File

@ -1,9 +1,8 @@
//! Definitions of network messages.
use std::net::SocketAddr;
use chrono::{DateTime, Utc};
use crate::meta_addr::MetaAddr;
use crate::types::*;
/// A Bitcoin-like network message for the Zcash protocol.
@ -47,10 +46,16 @@ pub enum Message {
timestamp: DateTime<Utc>,
/// The network address of the node receiving this message.
address_receiving: NetworkAddress,
///
/// Note that the timestamp field of the [`MetaAddr`] is not included in
/// the serialization of `version` messages.
address_receiving: MetaAddr,
/// The network address of the node emitting this message.
address_from: NetworkAddress,
///
/// Note that the timestamp field of the [`MetaAddr`] is not included in
/// the serialization of `version` messages.
address_from: MetaAddr,
/// Node random nonce, randomly generated every time a version
/// packet is sent. This nonce is used to detect connections
@ -113,19 +118,7 @@ pub enum Message {
/// An `addr` message.
///
/// [Bitcoin reference](https://en.bitcoin.it/wiki/Protocol_documentation#addr)
Addr {
/// Number of address entries (max: 1000)
count: u16,
/// Address of other nodes on the network, preceeded by a timestamp.
// Starting version 31402, addresses are prefixed with a
// timestamp. If no timestamp is present, the addresses should
// not be relayed to other peers, unless it is indeed
// confirmed they are up.
//
// XXX: I don't know how this serializes.
address_list: (DateTime<Utc>, Vec<NetworkAddress>),
},
Addr(Vec<MetaAddr>),
/// A `getaddr` message.
///
@ -242,7 +235,7 @@ pub enum Message {
// Q: how do we want to implement serialization, exactly? do we want to have
// something generic over stdlib Read and Write traits, or over async versions
// of those traits?
//
//
// Note: because of the way the message structure is defined (checksum comes
// first) we can't write the message headers before collecting the whole body
// into a buffer

View File

@ -0,0 +1,16 @@
//! An address-with-metadata type used in Bitcoin networking.
use chrono::{DateTime, Utc};
use std::net::SocketAddr;
use crate::types::Services;
/// An address with metadata on its advertised services and last-seen time.
///
/// [Bitcoin reference](https://en.bitcoin.it/wiki/Protocol_documentation#Network_address)
pub struct MetaAddr {
/// The peer's address.
addr: SocketAddr,
services: Services,
last_seen: DateTime<Utc>,
}

View File

@ -11,14 +11,4 @@ pub struct Version(pub u32);
pub struct Services(pub u64);
/// A nonce used in the networking layer to identify messages.
pub struct Nonce(pub u64);
/// A network address but with some extra flavor.
///
/// When a network address is needed somewhere, this structure is
/// used. Network addresses are not prefixed with a timestamp in the
/// version message.
///
/// [Bitcoin reference](https://en.bitcoin.it/wiki/Protocol_documentation#Network_address)
// XXX this doesn't quite fit here
pub struct NetworkAddress(pub Services, pub SocketAddr);
pub struct Nonce(pub u64);