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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use std::io;
use bytes::Bytes;
use ser::{
	Serializable, Stream,
	Deserializable, Reader, Error as ReaderError,
};
use common::{NetAddress, Services};
use {Payload, MessageResult};
use serialization::deserialize_payload;

#[derive(Debug, PartialEq, Clone)]
pub enum Version {
	V0(V0),
	V106(V0, V106),
	V70001(V0, V106, V70001),
}

impl Default for Version {
	fn default() -> Version {
		Version::V0(V0::default())
	}
}

impl Payload for Version {
	fn version() -> u32 {
		0
	}

	fn command() -> &'static str {
		"version"
	}

	// version package is an serialization excpetion
	fn deserialize_payload<T>(reader: &mut Reader<T>, _version: u32) -> MessageResult<Self> where T: io::Read {
		let simple: V0 = try!(reader.read());

		if simple.version < 106 {
			return Ok(Version::V0(simple));
		}

		let v106: V106 = try!(reader.read());
		if simple.version < 70001 {
			Ok(Version::V106(simple, v106))
		} else {
			let v70001: V70001 = try!(reader.read());
			Ok(Version::V70001(simple, v106, v70001))
		}
	}

	fn serialize_payload(&self, stream: &mut Stream, _version: u32) -> MessageResult<()> {
		match *self {
			Version::V0(ref simple) => {
				stream.append(simple);
			},
			Version::V106(ref simple, ref v106) => {
				stream
					.append(simple)
					.append(v106);
			},
			Version::V70001(ref simple, ref v106, ref v70001) => {
				stream
					.append(simple)
					.append(v106)
					.append(v70001);
			},
		}
		Ok(())
	}
}

impl Version {
	pub fn version(&self) -> u32 {
		match *self {
			Version::V0(ref s) |
			Version::V106(ref s, _) |
			Version::V70001(ref s, _, _) => s.version,
		}
	}

	pub fn nonce(&self) -> Option<u64> {
		match *self {
			Version::V0(_) => None,
			Version::V106(_, ref v) |
			Version::V70001(_, ref v, _) => Some(v.nonce),
		}
	}

	pub fn services(&self) -> Services {
		match *self {
			Version::V0(ref s) |
			Version::V106(ref s, _) |
			Version::V70001(ref s, _, _) => s.services,
		}
	}

	pub fn relay_transactions(&self) -> bool {
		match *self {
			Version::V0(_) => true,
			Version::V106(_, _) => true,
			Version::V70001(_, _, ref v) => v.relay,
		}
	}

	pub fn user_agent(&self) -> Option<String> {
		match *self {
			Version::V0(_) => None,
			Version::V106(_, ref v) |
			Version::V70001(_, ref v, _) => Some(v.user_agent.clone()),
		}
	}
}

#[derive(Debug, Default, PartialEq, Clone)]
pub struct V0 {
	pub version: u32,
	pub services: Services,
	pub timestamp: i64,
	pub receiver: NetAddress,
}

#[derive(Debug, PartialEq, Clone)]
pub struct V106 {
	pub from: NetAddress,
	pub nonce: u64,
	pub user_agent: String,
	pub start_height: i32,
}

#[derive(Debug, PartialEq, Clone)]
pub struct V70001 {
	pub relay: bool,
}

impl Serializable for V0 {
	fn serialize(&self, stream: &mut Stream) {
		stream
			.append(&self.version)
			.append(&self.services)
			.append(&self.timestamp)
			.append(&self.receiver);
	}
}

impl Deserializable for V0 {
	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, ReaderError> where T: io::Read {
		let result = V0 {
			version: try!(reader.read()),
			services: try!(reader.read()),
			timestamp: try!(reader.read()),
			receiver: try!(reader.read()),
		};

		Ok(result)
	}
}

impl Serializable for V106 {
	fn serialize(&self, stream: &mut Stream) {
		stream
			.append(&self.from)
			.append(&self.nonce)
			.append(&self.user_agent)
			.append(&self.start_height);
	}
}

impl Deserializable for V106 {
	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, ReaderError> where T: io::Read {
		let result = V106 {
			from: try!(reader.read()),
			nonce: try!(reader.read()),
			user_agent: try!(reader.read()),
			start_height: try!(reader.read()),
		};

		Ok(result)
	}
}

impl Serializable for V70001 {
	fn serialize(&self, stream: &mut Stream) {
		stream.append(&self.relay);
	}
}

impl Deserializable for V70001 {
	fn deserialize<T>(reader: &mut Reader<T>) -> Result<Self, ReaderError> where T: io::Read {
		let result = V70001 {
			relay: try!(reader.read()),
		};

		Ok(result)
	}
}

impl From<&'static str> for Version {
	fn from(s: &'static str) -> Self {
		let bytes: Bytes = s.into();
		deserialize_payload(&bytes, 0).unwrap()
	}
}

#[cfg(test)]
mod test {
	use bytes::Bytes;
	use serialization::{serialize_payload, deserialize_payload};
	use super::{Version, V0, V106};

	#[test]
	fn test_version_serialize() {
		let expected: Bytes = "9c7c00000100000000000000e615104d00000000010000000000000000000000000000000000ffff0a000001208d010000000000000000000000000000000000ffff0a000002208ddd9d202c3ab457130055810100".into();

		let version = Version::V106(V0 {
			version: 31900,
			services: 1u64.into(),
			timestamp: 0x4d1015e6,
			receiver: "010000000000000000000000000000000000ffff0a000001208d".into(),
		}, V106 {
			from: "010000000000000000000000000000000000ffff0a000002208d".into(),
			nonce: 0x1357b43a2c209ddd,
			user_agent: "".into(),
			start_height: 98645,
		});

		assert_eq!(serialize_payload(&version, 0), Ok(expected));
	}

	#[test]
	fn test_version_deserialize() {
		let raw: Bytes = "9c7c00000100000000000000e615104d00000000010000000000000000000000000000000000ffff0a000001208d010000000000000000000000000000000000ffff0a000002208ddd9d202c3ab457130055810100".into();

		let expected = Version::V106(V0 {
			version: 31900,
			services: 1u64.into(),
			timestamp: 0x4d1015e6,
			receiver: "010000000000000000000000000000000000ffff0a000001208d".into(),
		}, V106 {
			from: "010000000000000000000000000000000000ffff0a000002208d".into(),
			nonce: 0x1357b43a2c209ddd,
			user_agent: "".into(),
			start_height: 98645,
		});

		assert_eq!(expected, deserialize_payload(&raw, 0).unwrap());
	}
}