From b9af047a09744fbe1a31de29e1f321793091ff20 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Thu, 12 Sep 2019 03:36:50 -0700 Subject: [PATCH] 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. --- zebra-network/src/lib.rs | 1 + zebra-network/src/message.rs | 29 +++++++++++------------------ zebra-network/src/meta_addr.rs | 16 ++++++++++++++++ zebra-network/src/types.rs | 12 +----------- 4 files changed, 29 insertions(+), 29 deletions(-) create mode 100644 zebra-network/src/meta_addr.rs diff --git a/zebra-network/src/lib.rs b/zebra-network/src/lib.rs index cb9c8baf..8fa2ce77 100644 --- a/zebra-network/src/lib.rs +++ b/zebra-network/src/lib.rs @@ -5,3 +5,4 @@ pub mod message; pub mod types; mod constants; +mod meta_addr; \ No newline at end of file diff --git a/zebra-network/src/message.rs b/zebra-network/src/message.rs index 46f5e560..9f92ea8d 100644 --- a/zebra-network/src/message.rs +++ b/zebra-network/src/message.rs @@ -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, /// 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, Vec), - }, + Addr(Vec), /// 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 diff --git a/zebra-network/src/meta_addr.rs b/zebra-network/src/meta_addr.rs new file mode 100644 index 00000000..49ece026 --- /dev/null +++ b/zebra-network/src/meta_addr.rs @@ -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, +} diff --git a/zebra-network/src/types.rs b/zebra-network/src/types.rs index 855c173e..6325a44a 100644 --- a/zebra-network/src/types.rs +++ b/zebra-network/src/types.rs @@ -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); \ No newline at end of file