1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::io;
use std::borrow::Cow;
use network::address::Address;
use network::constants::{self, ServiceFlags};
use consensus::{Encodable, Decodable, ReadExt};
use consensus::encode;
use network::message::CommandString;
use hashes::sha256d;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct VersionMessage {
pub version: u32,
pub services: ServiceFlags,
pub timestamp: i64,
pub receiver: Address,
pub sender: Address,
pub nonce: u64,
pub user_agent: String,
pub start_height: i32,
pub relay: bool
}
impl VersionMessage {
pub fn new(
services: ServiceFlags,
timestamp: i64,
receiver: Address,
sender: Address,
nonce: u64,
user_agent: String,
start_height: i32,
) -> VersionMessage {
VersionMessage {
version: constants::PROTOCOL_VERSION,
services: services,
timestamp: timestamp,
receiver: receiver,
sender: sender,
nonce: nonce,
user_agent: user_agent,
start_height: start_height,
relay: false,
}
}
}
impl_consensus_encoding!(VersionMessage, version, services, timestamp,
receiver, sender, nonce,
user_agent, start_height, relay);
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum RejectReason {
Malformed = 0x01,
Invalid = 0x10,
Obsolete = 0x11,
Duplicate = 0x12,
NonStandard = 0x40,
Dust = 0x41,
Fee = 0x42,
Checkpoint = 0x43
}
impl Encodable for RejectReason {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
e.write_all(&[*self as u8])?;
Ok(1)
}
}
impl Decodable for RejectReason {
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
Ok(match d.read_u8()? {
0x01 => RejectReason::Malformed,
0x10 => RejectReason::Invalid,
0x11 => RejectReason::Obsolete,
0x12 => RejectReason::Duplicate,
0x40 => RejectReason::NonStandard,
0x41 => RejectReason::Dust,
0x42 => RejectReason::Fee,
0x43 => RejectReason::Checkpoint,
_ => return Err(encode::Error::ParseFailed("unknown reject code"))
})
}
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Reject {
pub message: CommandString,
pub ccode: RejectReason,
pub reason: Cow<'static, str>,
pub hash: sha256d::Hash
}
impl_consensus_encoding!(Reject, message, ccode, reason, hash);
#[cfg(test)]
mod tests {
use super::VersionMessage;
use hashes::hex::FromHex;
use network::constants::ServiceFlags;
use consensus::encode::{deserialize, serialize};
#[test]
fn version_message_test() {
let from_sat = Vec::from_hex("721101000100000000000000e6e0845300000000010000000000000000000000000000000000ffff0000000000000100000000000000fd87d87eeb4364f22cf54dca59412db7208d47d920cffce83ee8102f5361746f7368693a302e392e39392f2c9f040001").unwrap();
let decode: Result<VersionMessage, _> = deserialize(&from_sat);
assert!(decode.is_ok());
let real_decode = decode.unwrap();
assert_eq!(real_decode.version, 70002);
assert_eq!(real_decode.services, ServiceFlags::NETWORK);
assert_eq!(real_decode.timestamp, 1401217254);
assert_eq!(real_decode.nonce, 16735069437859780935);
assert_eq!(real_decode.user_agent, "/Satoshi:0.9.99/".to_string());
assert_eq!(real_decode.start_height, 302892);
assert_eq!(real_decode.relay, true);
assert_eq!(serialize(&real_decode), from_sat);
}
}