bitcoin/network/
message_network.rs

1// Rust Bitcoin Library
2// Written in 2014 by
3//     Andrew Poelstra <apoelstra@wpsoftware.net>
4//
5// To the extent possible under law, the author(s) have dedicated all
6// copyright and related and neighboring rights to this software to
7// the public domain worldwide. This software is distributed without
8// any warranty.
9//
10// You should have received a copy of the CC0 Public Domain Dedication
11// along with this software.
12// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13//
14
15//! Network-related network messages
16//!
17//! This module defines network messages which describe peers and their
18//! capabilities
19//!
20
21use network::address::Address;
22use network::constants;
23
24/// Some simple messages
25
26/// The `version` message
27#[derive(PartialEq, Eq, Clone, Debug)]
28pub struct VersionMessage {
29    /// The P2P network protocol version
30    pub version: u32,
31    /// A bitmask describing the services supported by this node
32    pub services: u64,
33    /// The time at which the `version` message was sent
34    pub timestamp: i64,
35    /// The network address of the peer receiving the message
36    pub receiver: Address,
37    /// The network address of the peer sending the message
38    pub sender: Address,
39    /// A random nonce used to detect loops in the network
40    pub nonce: u64,
41    /// A string describing the peer's software
42    pub user_agent: String,
43    /// The height of the maximum-work blockchain that the peer is aware of
44    pub start_height: i32,
45    /// Whether the receiving peer should relay messages to the sender; used
46    /// if the sender is bandwidth-limited and would like to support bloom
47    /// filtering. Defaults to true.
48    pub relay: bool
49}
50
51impl VersionMessage {
52    // TODO: we have fixed services and relay to 0
53    /// Constructs a new `version` message
54    pub fn new(
55        services: u64,
56        timestamp: i64,
57        receiver: Address,
58        sender: Address,
59        nonce: u64,
60        user_agent: String,
61        start_height: i32,
62    ) -> VersionMessage {
63        VersionMessage {
64            version: constants::PROTOCOL_VERSION,
65            services: services,
66            timestamp: timestamp,
67            receiver: receiver,
68            sender: sender,
69            nonce: nonce,
70            user_agent: user_agent,
71            start_height: start_height,
72            relay: false,
73        }
74    }
75}
76
77impl_consensus_encoding!(VersionMessage, version, services, timestamp,
78                         receiver, sender, nonce,
79                         user_agent, start_height, relay);
80
81#[cfg(test)]
82mod tests {
83    use super::VersionMessage;
84
85    use hex::decode as hex_decode;
86
87    use consensus::encode::{deserialize, serialize};
88
89    #[test]
90    fn version_message_test() {
91        // This message is from my satoshi node, morning of May 27 2014
92        let from_sat = hex_decode("721101000100000000000000e6e0845300000000010000000000000000000000000000000000ffff0000000000000100000000000000fd87d87eeb4364f22cf54dca59412db7208d47d920cffce83ee8102f5361746f7368693a302e392e39392f2c9f040001").unwrap();
93
94        let decode: Result<VersionMessage, _> = deserialize(&from_sat);
95        assert!(decode.is_ok());
96        let real_decode = decode.unwrap();
97        assert_eq!(real_decode.version, 70002);
98        assert_eq!(real_decode.services, 1);
99        assert_eq!(real_decode.timestamp, 1401217254);
100        // address decodes should be covered by Address tests
101        assert_eq!(real_decode.nonce, 16735069437859780935);
102        assert_eq!(real_decode.user_agent, "/Satoshi:0.9.99/".to_string());
103        assert_eq!(real_decode.start_height, 302892);
104        assert_eq!(real_decode.relay, true);
105
106        assert_eq!(serialize(&real_decode), from_sat);
107    }
108}