Use ServiceFlags type in existing API

This commit is contained in:
Steven Roose 2019-11-22 11:00:48 +00:00
parent fdf4b2f74e
commit de18e926c1
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
6 changed files with 17 additions and 38 deletions

View File

@ -22,12 +22,14 @@ use std::io;
use std::fmt; use std::fmt;
use std::net::{SocketAddr, Ipv6Addr, SocketAddrV4, SocketAddrV6}; use std::net::{SocketAddr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
use network::constants::ServiceFlags;
use consensus::encode::{self, Decodable, Encodable}; use consensus::encode::{self, Decodable, Encodable};
/// A message which can be sent on the Bitcoin network /// A message which can be sent on the Bitcoin network
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Address { pub struct Address {
/// Services provided by the peer whose address this is /// Services provided by the peer whose address this is
pub services: u64, pub services: ServiceFlags,
/// Network byte-order ipv6 address, or ipv4-mapped ipv6 address /// Network byte-order ipv6 address, or ipv4-mapped ipv6 address
pub address: [u16; 8], pub address: [u16; 8],
/// Network port /// Network port
@ -38,7 +40,7 @@ const ONION : [u16; 3] = [0xFD87, 0xD87E, 0xEB43];
impl Address { impl Address {
/// Create an address message for a socket /// Create an address message for a socket
pub fn new (socket :&SocketAddr, services: u64) -> Address { pub fn new (socket :&SocketAddr, services: ServiceFlags) -> Address {
let (address, port) = match socket { let (address, port) = match socket {
&SocketAddr::V4(ref addr) => (addr.ip().to_ipv6_mapped().segments(), addr.port()), &SocketAddr::V4(ref addr) => (addr.ip().to_ipv6_mapped().segments(), addr.port()),
&SocketAddr::V6(ref addr) => (addr.ip().segments(), addr.port()) &SocketAddr::V6(ref addr) => (addr.ip().segments(), addr.port())
@ -103,26 +105,6 @@ impl fmt::Debug for Address {
} }
} }
impl Clone for Address {
fn clone(&self) -> Address {
Address {
services: self.services,
address: self.address,
port: self.port,
}
}
}
impl PartialEq for Address {
fn eq(&self, other: &Address) -> bool {
self.services == other.services &&
&self.address[..] == &other.address[..] &&
self.port == other.port
}
}
impl Eq for Address {}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::str::FromStr; use std::str::FromStr;
@ -134,7 +116,7 @@ mod test {
#[test] #[test]
fn serialize_address_test() { fn serialize_address_test() {
assert_eq!(serialize(&Address { assert_eq!(serialize(&Address {
services: 1, services: 1.into(),
address: [0, 0, 0, 0, 0, 0xffff, 0x0a00, 0x0001], address: [0, 0, 0, 0, 0, 0xffff, 0x0a00, 0x0001],
port: 8333 port: 8333
}), }),
@ -154,7 +136,7 @@ mod test {
_ => false _ => false
} }
); );
assert_eq!(full.services, 1); assert_eq!(full.services, 1.into());
assert_eq!(full.address, [0, 0, 0, 0, 0, 0xffff, 0x0a00, 0x0001]); assert_eq!(full.address, [0, 0, 0, 0, 0, 0xffff, 0x0a00, 0x0001]);
assert_eq!(full.port, 8333); assert_eq!(full.port, 8333);
@ -166,11 +148,11 @@ mod test {
#[test] #[test]
fn test_socket_addr () { fn test_socket_addr () {
let s4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(111,222,123,4)), 5555); let s4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(111,222,123,4)), 5555);
let a4 = Address::new(&s4, 9); let a4 = Address::new(&s4, 9.into());
assert_eq!(a4.socket_addr().unwrap(), s4); assert_eq!(a4.socket_addr().unwrap(), s4);
let s6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0x1111, 0x2222, 0x3333, 0x4444, let s6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0x1111, 0x2222, 0x3333, 0x4444,
0x5555, 0x6666, 0x7777, 0x8888)), 9999); 0x5555, 0x6666, 0x7777, 0x8888)), 9999);
let a6 = Address::new(&s6, 9); let a6 = Address::new(&s6, 9.into());
assert_eq!(a6.socket_addr().unwrap(), s6); assert_eq!(a6.socket_addr().unwrap(), s6);
} }
@ -179,7 +161,7 @@ mod test {
let onionaddr = SocketAddr::new( let onionaddr = SocketAddr::new(
IpAddr::V6( IpAddr::V6(
Ipv6Addr::from_str("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").unwrap()), 1111); Ipv6Addr::from_str("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").unwrap()), 1111);
let addr = Address::new(&onionaddr, 0); let addr = Address::new(&onionaddr, 0.into());
assert!(addr.socket_addr().is_err()); assert!(addr.socket_addr().is_err());
} }
} }

View File

@ -43,10 +43,6 @@ use consensus::encode::{self, Encodable, Decodable};
/// Version of the protocol as appearing in network message headers /// Version of the protocol as appearing in network message headers
pub const PROTOCOL_VERSION: u32 = 70001; pub const PROTOCOL_VERSION: u32 = 70001;
/// Bitfield of services provided by this node
pub const SERVICES: u64 = 0;
/// User agent as it appears in the version message
pub const USER_AGENT: &'static str = "bitcoin-rust v0.1";
user_enum! { user_enum! {
/// The cryptocurrency to act on /// The cryptocurrency to act on

View File

@ -383,7 +383,7 @@ mod test {
assert_eq!(msg.magic, 0xd9b4bef9); assert_eq!(msg.magic, 0xd9b4bef9);
if let NetworkMessage::Version(version_msg) = msg.payload { if let NetworkMessage::Version(version_msg) = msg.payload {
assert_eq!(version_msg.version, 70015); assert_eq!(version_msg.version, 70015);
assert_eq!(version_msg.services, 1037); assert_eq!(version_msg.services, 1037.into());
assert_eq!(version_msg.timestamp, 1548554224); assert_eq!(version_msg.timestamp, 1548554224);
assert_eq!(version_msg.nonce, 13952548347456104954); assert_eq!(version_msg.nonce, 13952548347456104954);
assert_eq!(version_msg.user_agent, "/Satoshi:0.17.1/"); assert_eq!(version_msg.user_agent, "/Satoshi:0.17.1/");
@ -420,7 +420,7 @@ mod test {
assert_eq!(msg.magic, 0xd9b4bef9); assert_eq!(msg.magic, 0xd9b4bef9);
if let NetworkMessage::Version(version_msg) = msg.payload { if let NetworkMessage::Version(version_msg) = msg.payload {
assert_eq!(version_msg.version, 70015); assert_eq!(version_msg.version, 70015);
assert_eq!(version_msg.services, 1037); assert_eq!(version_msg.services, 1037.into());
assert_eq!(version_msg.timestamp, 1548554224); assert_eq!(version_msg.timestamp, 1548554224);
assert_eq!(version_msg.nonce, 13952548347456104954); assert_eq!(version_msg.nonce, 13952548347456104954);
assert_eq!(version_msg.user_agent, "/Satoshi:0.17.1/"); assert_eq!(version_msg.user_agent, "/Satoshi:0.17.1/");

View File

@ -19,7 +19,7 @@
//! //!
use network::address::Address; use network::address::Address;
use network::constants; use network::constants::{self, ServiceFlags};
use consensus::{Encodable, Decodable, ReadExt}; use consensus::{Encodable, Decodable, ReadExt};
use consensus::encode; use consensus::encode;
use std::io; use std::io;
@ -35,7 +35,7 @@ pub struct VersionMessage {
/// The P2P network protocol version /// The P2P network protocol version
pub version: u32, pub version: u32,
/// A bitmask describing the services supported by this node /// A bitmask describing the services supported by this node
pub services: u64, pub services: ServiceFlags,
/// The time at which the `version` message was sent /// The time at which the `version` message was sent
pub timestamp: i64, pub timestamp: i64,
/// The network address of the peer receiving the message /// The network address of the peer receiving the message
@ -58,7 +58,7 @@ impl VersionMessage {
// TODO: we have fixed services and relay to 0 // TODO: we have fixed services and relay to 0
/// Constructs a new `version` message /// Constructs a new `version` message
pub fn new( pub fn new(
services: u64, services: ServiceFlags,
timestamp: i64, timestamp: i64,
receiver: Address, receiver: Address,
sender: Address, sender: Address,
@ -160,7 +160,7 @@ mod tests {
assert!(decode.is_ok()); assert!(decode.is_ok());
let real_decode = decode.unwrap(); let real_decode = decode.unwrap();
assert_eq!(real_decode.version, 70002); assert_eq!(real_decode.version, 70002);
assert_eq!(real_decode.services, 1); assert_eq!(real_decode.services, 1.into());
assert_eq!(real_decode.timestamp, 1401217254); assert_eq!(real_decode.timestamp, 1401217254);
// address decodes should be covered by Address tests // address decodes should be covered by Address tests
assert_eq!(real_decode.nonce, 16735069437859780935); assert_eq!(real_decode.nonce, 16735069437859780935);

View File

@ -25,6 +25,7 @@ use std::error;
pub mod constants; pub mod constants;
pub mod address; pub mod address;
pub use self::address::Address;
pub mod message; pub mod message;
pub mod message_blockdata; pub mod message_blockdata;
pub mod message_network; pub mod message_network;

View File

@ -156,7 +156,7 @@ mod test {
assert_eq!(msg.magic, 0xd9b4bef9); assert_eq!(msg.magic, 0xd9b4bef9);
if let NetworkMessage::Version(ref version_msg) = msg.payload { if let NetworkMessage::Version(ref version_msg) = msg.payload {
assert_eq!(version_msg.version, 70015); assert_eq!(version_msg.version, 70015);
assert_eq!(version_msg.services, 1037); assert_eq!(version_msg.services, 1037.into());
assert_eq!(version_msg.timestamp, 1548554224); assert_eq!(version_msg.timestamp, 1548554224);
assert_eq!(version_msg.nonce, 13952548347456104954); assert_eq!(version_msg.nonce, 13952548347456104954);
assert_eq!(version_msg.user_agent, "/Satoshi:0.17.1/"); assert_eq!(version_msg.user_agent, "/Satoshi:0.17.1/");