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
use std::io;
use chain::BlockHeader;
use ser::{Stream, Reader, Serializable, Deserializable, CompactInteger, Error as ReaderError};
use {Payload, MessageResult};

pub const HEADERS_MAX_HEADERS_LEN: usize = 2000;

#[derive(Debug, PartialEq)]
pub struct Headers {
	pub headers: Vec<BlockHeader>,
}

impl Headers {
	pub fn with_headers(headers: Vec<BlockHeader>) -> Self {
		Headers {
			headers: headers,
		}
	}
}

#[derive(Debug, PartialEq)]
struct HeaderWithTxnCount {
	header: BlockHeader,
}

impl From<HeaderWithTxnCount> for BlockHeader {
	fn from(header: HeaderWithTxnCount) -> BlockHeader {
		header.header
	}
}

#[derive(Debug, PartialEq)]
struct HeaderWithTxnCountRef<'a> {
	header: &'a BlockHeader,
}

impl<'a> From<&'a BlockHeader> for HeaderWithTxnCountRef<'a> {
	fn from(header: &'a BlockHeader) -> Self {
		HeaderWithTxnCountRef {
			header: header,
		}
	}
}

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

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

	fn deserialize_payload<T>(reader: &mut Reader<T>, _version: u32) -> MessageResult<Self> where T: io::Read {
		let headers_with_txn_count: Vec<HeaderWithTxnCount> = try!(reader.read_list());
		let headers = Headers {
			headers: headers_with_txn_count.into_iter().map(Into::into).collect(),
		};

		Ok(headers)
	}

	fn serialize_payload(&self, stream: &mut Stream, _version: u32) -> MessageResult<()> {
		let headers_with_txn_count: Vec<HeaderWithTxnCountRef> = self.headers.iter().map(Into::into).collect();
		stream.append_list(&headers_with_txn_count);
		Ok(())
	}
}

impl<'a> Serializable for HeaderWithTxnCountRef<'a> {
	fn serialize(&self, stream: &mut Stream) {
		stream
			.append(self.header)
			.append(&CompactInteger::from(0u32));
	}
}

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

		let txn_count: CompactInteger = try!(reader.read());
		if txn_count != 0u32.into() {
			return Err(ReaderError::MalformedData);
		}

		Ok(header)
	}
}