2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2016-10-24 00:02:02 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Message types that transit over the network and related serialization code.
|
|
|
|
|
2018-05-29 05:45:31 +03:00
|
|
|
use num::FromPrimitive;
|
2018-02-02 05:03:12 +03:00
|
|
|
use std::io::{self, Read, Write};
|
2018-03-04 03:19:54 +03:00
|
|
|
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, TcpStream};
|
2018-06-14 15:16:14 +03:00
|
|
|
use std::{thread, time};
|
2016-10-29 16:51:24 +03:00
|
|
|
|
2018-08-12 23:02:30 +03:00
|
|
|
use core::consensus;
|
2017-02-08 00:52:17 +03:00
|
|
|
use core::core::hash::Hash;
|
2018-08-12 23:02:30 +03:00
|
|
|
use core::core::BlockHeader;
|
2018-09-19 01:12:57 +03:00
|
|
|
use core::pow::Difficulty;
|
2018-11-13 12:30:02 +03:00
|
|
|
use core::ser::{self, FixedLength, Readable, Reader, Writeable, Writer};
|
2016-10-26 08:06:13 +03:00
|
|
|
|
2018-06-14 15:16:14 +03:00
|
|
|
use types::{Capabilities, Error, ReasonForBan, MAX_BLOCK_HEADERS, MAX_LOCATORS, MAX_PEER_ADDRS};
|
2016-10-29 22:36:45 +03:00
|
|
|
|
2016-10-26 08:06:13 +03:00
|
|
|
/// Current latest version of the protocol
|
|
|
|
pub const PROTOCOL_VERSION: u32 = 1;
|
2017-02-10 07:16:34 +03:00
|
|
|
|
2018-03-27 15:52:59 +03:00
|
|
|
/// Grin's user agent with current version
|
|
|
|
pub const USER_AGENT: &'static str = concat!("MW/Grin ", env!("CARGO_PKG_VERSION"));
|
2016-10-24 00:02:02 +03:00
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
/// Magic number expected in the header of every message
|
2018-10-17 22:36:12 +03:00
|
|
|
const MAGIC: [u8; 2] = [0x54, 0x34];
|
2016-10-25 07:35:10 +03:00
|
|
|
|
2018-08-12 23:02:30 +03:00
|
|
|
/// Max theoretical size of a block filled with outputs.
|
|
|
|
const MAX_BLOCK_SIZE: u64 =
|
|
|
|
(consensus::MAX_BLOCK_WEIGHT / consensus::BLOCK_OUTPUT_WEIGHT * 708) as u64;
|
|
|
|
|
2018-05-30 00:57:11 +03:00
|
|
|
/// Types of messages.
|
|
|
|
/// Note: Values here are *important* so we should only add new values at the
|
|
|
|
/// end.
|
2016-10-29 16:51:24 +03:00
|
|
|
enum_from_primitive! {
|
2018-01-31 23:39:55 +03:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum Type {
|
2018-05-30 00:57:11 +03:00
|
|
|
Error = 0,
|
|
|
|
Hand = 1,
|
|
|
|
Shake = 2,
|
|
|
|
Ping = 3,
|
|
|
|
Pong = 4,
|
|
|
|
GetPeerAddrs = 5,
|
|
|
|
PeerAddrs = 6,
|
|
|
|
GetHeaders = 7,
|
|
|
|
Header = 8,
|
|
|
|
Headers = 9,
|
|
|
|
GetBlock = 10,
|
|
|
|
Block = 11,
|
|
|
|
GetCompactBlock = 12,
|
|
|
|
CompactBlock = 13,
|
|
|
|
StemTransaction = 14,
|
|
|
|
Transaction = 15,
|
|
|
|
TxHashSetRequest = 16,
|
|
|
|
TxHashSetArchive = 17,
|
|
|
|
BanReason = 18,
|
2018-11-07 12:28:17 +03:00
|
|
|
GetTransaction = 19,
|
|
|
|
TransactionKernel = 20,
|
2018-01-31 23:39:55 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 00:57:11 +03:00
|
|
|
// Max msg size for each msg type.
|
|
|
|
fn max_msg_size(msg_type: Type) -> u64 {
|
|
|
|
match msg_type {
|
|
|
|
Type::Error => 0,
|
|
|
|
Type::Hand => 128,
|
|
|
|
Type::Shake => 88,
|
|
|
|
Type::Ping => 16,
|
|
|
|
Type::Pong => 16,
|
|
|
|
Type::GetPeerAddrs => 4,
|
|
|
|
Type::PeerAddrs => 4 + (1 + 16 + 2) * MAX_PEER_ADDRS as u64,
|
|
|
|
Type::GetHeaders => 1 + 32 * MAX_LOCATORS as u64,
|
|
|
|
Type::Header => 365,
|
|
|
|
Type::Headers => 2 + 365 * MAX_BLOCK_HEADERS as u64,
|
|
|
|
Type::GetBlock => 32,
|
2018-08-12 23:02:30 +03:00
|
|
|
Type::Block => MAX_BLOCK_SIZE,
|
2018-05-30 00:57:11 +03:00
|
|
|
Type::GetCompactBlock => 32,
|
2018-08-12 23:02:30 +03:00
|
|
|
Type::CompactBlock => MAX_BLOCK_SIZE / 10,
|
|
|
|
Type::StemTransaction => MAX_BLOCK_SIZE,
|
|
|
|
Type::Transaction => MAX_BLOCK_SIZE,
|
2018-05-30 00:57:11 +03:00
|
|
|
Type::TxHashSetRequest => 40,
|
|
|
|
Type::TxHashSetArchive => 64,
|
|
|
|
Type::BanReason => 64,
|
2018-11-07 12:28:17 +03:00
|
|
|
Type::GetTransaction => 32,
|
|
|
|
Type::TransactionKernel => 32,
|
2018-05-30 00:57:11 +03:00
|
|
|
}
|
|
|
|
}
|
2018-04-03 23:21:13 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
/// The default implementation of read_exact is useless with async TcpStream as
|
2018-02-05 22:52:11 +03:00
|
|
|
/// it will return as soon as something has been read, regardless of
|
|
|
|
/// whether the buffer has been filled (and then errors). This implementation
|
|
|
|
/// will block until it has read exactly `len` bytes and returns them as a
|
|
|
|
/// `vec<u8>`. Except for a timeout, this implementation will never return a
|
|
|
|
/// partially filled buffer.
|
|
|
|
///
|
|
|
|
/// The timeout in milliseconds aborts the read when it's met. Note that the
|
|
|
|
/// time is not guaranteed to be exact. To support cases where we want to poll
|
|
|
|
/// instead of blocking, a `block_on_empty` boolean, when false, ensures
|
|
|
|
/// `read_exact` returns early with a `io::ErrorKind::WouldBlock` if nothing
|
2018-03-04 03:19:54 +03:00
|
|
|
/// has been read from the socket.
|
2018-02-05 22:52:11 +03:00
|
|
|
pub fn read_exact(
|
|
|
|
conn: &mut TcpStream,
|
|
|
|
mut buf: &mut [u8],
|
2018-08-31 20:20:59 +03:00
|
|
|
timeout: time::Duration,
|
2018-02-05 22:52:11 +03:00
|
|
|
block_on_empty: bool,
|
|
|
|
) -> io::Result<()> {
|
2018-08-31 20:20:59 +03:00
|
|
|
let sleep_time = time::Duration::from_micros(10);
|
|
|
|
let mut count = time::Duration::new(0, 0);
|
2018-02-02 05:03:12 +03:00
|
|
|
|
2018-02-05 22:52:11 +03:00
|
|
|
let mut read = 0;
|
2018-02-02 05:03:12 +03:00
|
|
|
loop {
|
|
|
|
match conn.read(buf) {
|
2018-09-11 14:14:48 +03:00
|
|
|
Ok(0) => {
|
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::ConnectionAborted,
|
|
|
|
"read_exact",
|
|
|
|
));
|
|
|
|
}
|
2018-02-05 22:52:11 +03:00
|
|
|
Ok(n) => {
|
|
|
|
let tmp = buf;
|
|
|
|
buf = &mut tmp[n..];
|
|
|
|
read += n;
|
|
|
|
}
|
2018-02-02 05:03:12 +03:00
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
|
2018-02-05 22:52:11 +03:00
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
|
|
|
|
if read == 0 && !block_on_empty {
|
|
|
|
return Err(io::Error::new(io::ErrorKind::WouldBlock, "read_exact"));
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 05:03:12 +03:00
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
if !buf.is_empty() {
|
|
|
|
thread::sleep(sleep_time);
|
2018-08-31 20:20:59 +03:00
|
|
|
count += sleep_time;
|
2018-02-02 05:03:12 +03:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if count > timeout {
|
2018-03-04 03:19:54 +03:00
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::TimedOut,
|
|
|
|
"reading from tcp stream",
|
|
|
|
));
|
2018-02-02 05:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
/// Same as `read_exact` but for writing.
|
2018-08-31 20:20:59 +03:00
|
|
|
pub fn write_all(conn: &mut Write, mut buf: &[u8], timeout: time::Duration) -> io::Result<()> {
|
|
|
|
let sleep_time = time::Duration::from_micros(10);
|
|
|
|
let mut count = time::Duration::new(0, 0);
|
2018-02-10 01:32:16 +03:00
|
|
|
|
|
|
|
while !buf.is_empty() {
|
|
|
|
match conn.write(buf) {
|
2018-03-04 03:19:54 +03:00
|
|
|
Ok(0) => {
|
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::WriteZero,
|
|
|
|
"failed to write whole buffer",
|
|
|
|
))
|
|
|
|
}
|
2018-02-10 01:32:16 +03:00
|
|
|
Ok(n) => buf = &buf[n..],
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
if !buf.is_empty() {
|
|
|
|
thread::sleep(sleep_time);
|
2018-08-31 20:20:59 +03:00
|
|
|
count += sleep_time;
|
2018-02-10 01:32:16 +03:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if count > timeout {
|
2018-03-04 03:19:54 +03:00
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::TimedOut,
|
|
|
|
"reading from tcp stream",
|
|
|
|
));
|
2018-02-10 01:32:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
/// Read a header from the provided connection without blocking if the
|
|
|
|
/// underlying stream is async. Typically headers will be polled for, so
|
|
|
|
/// we do not want to block.
|
2018-08-30 10:21:13 +03:00
|
|
|
pub fn read_header(conn: &mut TcpStream, msg_type: Option<Type>) -> Result<MsgHeader, Error> {
|
2018-11-13 12:30:02 +03:00
|
|
|
let mut head = vec![0u8; MsgHeader::LEN];
|
2018-08-30 10:21:13 +03:00
|
|
|
if Some(Type::Hand) == msg_type {
|
2018-08-31 20:20:59 +03:00
|
|
|
read_exact(conn, &mut head, time::Duration::from_millis(10), true)?;
|
2018-08-30 10:21:13 +03:00
|
|
|
} else {
|
2018-08-31 20:20:59 +03:00
|
|
|
read_exact(conn, &mut head, time::Duration::from_secs(10), false)?;
|
2018-08-30 10:21:13 +03:00
|
|
|
}
|
2018-02-02 05:03:12 +03:00
|
|
|
let header = ser::deserialize::<MsgHeader>(&mut &head[..])?;
|
2018-05-30 00:57:11 +03:00
|
|
|
let max_len = max_msg_size(header.msg_type);
|
2018-04-03 23:21:13 +03:00
|
|
|
// TODO 4x the limits for now to leave ourselves space to change things
|
|
|
|
if header.msg_len > max_len * 4 {
|
|
|
|
error!(
|
2018-10-21 23:30:56 +03:00
|
|
|
"Too large read {}, had {}, wanted {}.",
|
|
|
|
header.msg_type as u8, max_len, header.msg_len
|
2018-04-03 23:21:13 +03:00
|
|
|
);
|
2018-02-02 05:03:12 +03:00
|
|
|
return Err(Error::Serialization(ser::Error::TooLargeReadErr));
|
|
|
|
}
|
|
|
|
Ok(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read a message body from the provided connection, always blocking
|
|
|
|
/// until we have a result (or timeout).
|
|
|
|
pub fn read_body<T>(h: &MsgHeader, conn: &mut TcpStream) -> Result<T, Error>
|
2017-10-26 20:48:51 +03:00
|
|
|
where
|
2018-02-02 05:03:12 +03:00
|
|
|
T: Readable,
|
2016-12-11 06:11:49 +03:00
|
|
|
{
|
2018-02-02 05:03:12 +03:00
|
|
|
let mut body = vec![0u8; h.msg_len as usize];
|
2018-08-31 20:20:59 +03:00
|
|
|
read_exact(conn, &mut body, time::Duration::from_secs(20), true)?;
|
2018-02-02 05:03:12 +03:00
|
|
|
ser::deserialize(&mut &body[..]).map_err(From::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads a full message from the underlying connection.
|
|
|
|
pub fn read_message<T>(conn: &mut TcpStream, msg_type: Type) -> Result<T, Error>
|
|
|
|
where
|
|
|
|
T: Readable,
|
|
|
|
{
|
2018-08-30 10:21:13 +03:00
|
|
|
let header = read_header(conn, Some(msg_type))?;
|
2018-02-02 05:03:12 +03:00
|
|
|
if header.msg_type != msg_type {
|
|
|
|
return Err(Error::BadMessage);
|
|
|
|
}
|
|
|
|
read_body(&header, conn)
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
pub fn write_to_buf<T>(msg: T, msg_type: Type) -> Vec<u8>
|
2018-02-02 05:03:12 +03:00
|
|
|
where
|
|
|
|
T: Writeable,
|
|
|
|
{
|
|
|
|
// prepare the body first so we know its serialized length
|
|
|
|
let mut body_buf = vec![];
|
|
|
|
ser::serialize(&mut body_buf, &msg).unwrap();
|
|
|
|
|
|
|
|
// build and serialize the header using the body size
|
2018-02-06 02:09:57 +03:00
|
|
|
let mut msg_buf = vec![];
|
2018-02-02 05:03:12 +03:00
|
|
|
let blen = body_buf.len() as u64;
|
2018-02-06 02:09:57 +03:00
|
|
|
ser::serialize(&mut msg_buf, &MsgHeader::new(msg_type, blen)).unwrap();
|
|
|
|
msg_buf.append(&mut body_buf);
|
2018-02-02 05:03:12 +03:00
|
|
|
|
2018-02-06 02:09:57 +03:00
|
|
|
msg_buf
|
2016-12-11 06:11:49 +03:00
|
|
|
}
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
pub fn write_message<T>(conn: &mut TcpStream, msg: T, msg_type: Type) -> Result<(), Error>
|
2017-10-26 20:48:51 +03:00
|
|
|
where
|
|
|
|
T: Writeable + 'static,
|
2016-12-11 06:11:49 +03:00
|
|
|
{
|
2018-02-06 02:09:57 +03:00
|
|
|
let buf = write_to_buf(msg, msg_type);
|
2018-02-02 05:03:12 +03:00
|
|
|
// send the whole thing
|
2018-02-06 02:09:57 +03:00
|
|
|
conn.write_all(&buf[..])?;
|
2018-02-02 05:03:12 +03:00
|
|
|
Ok(())
|
2016-12-11 06:11:49 +03:00
|
|
|
}
|
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
/// Header of any protocol message, used to identify incoming messages.
|
|
|
|
pub struct MsgHeader {
|
2017-06-01 01:47:52 +03:00
|
|
|
magic: [u8; 2],
|
2017-02-03 02:51:48 +03:00
|
|
|
/// Type of the message.
|
2016-10-31 04:23:52 +03:00
|
|
|
pub msg_type: Type,
|
2017-11-20 20:35:52 +03:00
|
|
|
/// Total length of the message in bytes.
|
2016-12-11 06:11:49 +03:00
|
|
|
pub msg_len: u64,
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MsgHeader {
|
2017-02-03 02:51:48 +03:00
|
|
|
/// Creates a new message header.
|
2016-12-11 06:11:49 +03:00
|
|
|
pub fn new(msg_type: Type, len: u64) -> MsgHeader {
|
2016-10-31 04:23:52 +03:00
|
|
|
MsgHeader {
|
|
|
|
magic: MAGIC,
|
|
|
|
msg_type: msg_type,
|
2016-12-11 06:11:49 +03:00
|
|
|
msg_len: len,
|
2016-10-31 04:23:52 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-13 12:30:02 +03:00
|
|
|
}
|
2016-10-31 04:23:52 +03:00
|
|
|
|
2018-11-13 12:30:02 +03:00
|
|
|
impl FixedLength for MsgHeader {
|
|
|
|
const LEN: usize = 1 + 1 + 1 + 8;
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for MsgHeader {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2017-09-29 21:44:25 +03:00
|
|
|
ser_multiwrite!(
|
|
|
|
writer,
|
|
|
|
[write_u8, self.magic[0]],
|
|
|
|
[write_u8, self.magic[1]],
|
|
|
|
[write_u8, self.msg_type as u8],
|
|
|
|
[write_u64, self.msg_len]
|
|
|
|
);
|
2016-11-01 05:03:12 +03:00
|
|
|
Ok(())
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for MsgHeader {
|
2016-10-25 07:35:10 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<MsgHeader, ser::Error> {
|
2018-06-04 19:05:49 +03:00
|
|
|
reader.expect_u8(MAGIC[0])?;
|
|
|
|
reader.expect_u8(MAGIC[1])?;
|
2016-12-11 06:11:49 +03:00
|
|
|
let (t, len) = ser_multiread!(reader, read_u8, read_u64);
|
2016-11-01 20:42:33 +03:00
|
|
|
match Type::from_u8(t) {
|
2017-11-01 02:32:33 +03:00
|
|
|
Some(ty) => Ok(MsgHeader {
|
|
|
|
magic: MAGIC,
|
|
|
|
msg_type: ty,
|
|
|
|
msg_len: len,
|
|
|
|
}),
|
2016-11-01 20:42:33 +03:00
|
|
|
None => Err(ser::Error::CorruptedData),
|
2016-10-28 00:28:02 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// First part of a handshake, sender advertises its version and
|
|
|
|
/// characteristics.
|
2016-10-24 00:02:02 +03:00
|
|
|
pub struct Hand {
|
2016-10-26 08:06:13 +03:00
|
|
|
/// protocol version of the sender
|
|
|
|
pub version: u32,
|
|
|
|
/// capabilities of the sender
|
|
|
|
pub capabilities: Capabilities,
|
|
|
|
/// randomly generated for each handshake, helps detect self
|
|
|
|
pub nonce: u64,
|
2017-11-20 20:35:52 +03:00
|
|
|
/// genesis block of our chain, only connect to peers on the same chain
|
|
|
|
pub genesis: Hash,
|
2017-02-08 00:52:17 +03:00
|
|
|
/// total difficulty accumulated by the sender, used to check whether sync
|
2017-11-02 01:56:59 +03:00
|
|
|
/// may be needed
|
2017-02-08 00:52:17 +03:00
|
|
|
pub total_difficulty: Difficulty,
|
2016-10-26 08:06:13 +03:00
|
|
|
/// network address of the sender
|
|
|
|
pub sender_addr: SockAddr,
|
|
|
|
/// network address of the receiver
|
|
|
|
pub receiver_addr: SockAddr,
|
|
|
|
/// name of version of the software
|
|
|
|
pub user_agent: String,
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
impl Writeable for Hand {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2017-09-29 21:44:25 +03:00
|
|
|
ser_multiwrite!(
|
|
|
|
writer,
|
|
|
|
[write_u32, self.version],
|
|
|
|
[write_u32, self.capabilities.bits()],
|
|
|
|
[write_u64, self.nonce]
|
|
|
|
);
|
2017-08-10 03:54:10 +03:00
|
|
|
self.total_difficulty.write(writer).unwrap();
|
|
|
|
self.sender_addr.write(writer).unwrap();
|
|
|
|
self.receiver_addr.write(writer).unwrap();
|
2017-11-20 20:35:52 +03:00
|
|
|
writer.write_bytes(&self.user_agent).unwrap();
|
|
|
|
self.genesis.write(writer).unwrap();
|
|
|
|
Ok(())
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for Hand {
|
2016-10-25 07:35:10 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<Hand, ser::Error> {
|
2017-02-08 00:52:17 +03:00
|
|
|
let (version, capab, nonce) = ser_multiread!(reader, read_u32, read_u32, read_u64);
|
2018-11-07 14:15:37 +03:00
|
|
|
let capabilities = Capabilities::from_bits_truncate(capab);
|
2018-06-04 19:05:49 +03:00
|
|
|
let total_diff = Difficulty::read(reader)?;
|
|
|
|
let sender_addr = SockAddr::read(reader)?;
|
|
|
|
let receiver_addr = SockAddr::read(reader)?;
|
2018-11-13 12:30:40 +03:00
|
|
|
let ua = reader.read_bytes_len_prefix()?;
|
2018-06-04 19:05:49 +03:00
|
|
|
let user_agent = String::from_utf8(ua).map_err(|_| ser::Error::CorruptedData)?;
|
|
|
|
let genesis = Hash::read(reader)?;
|
2016-10-26 08:06:13 +03:00
|
|
|
Ok(Hand {
|
2016-10-25 07:35:10 +03:00
|
|
|
version: version,
|
2016-10-26 08:06:13 +03:00
|
|
|
capabilities: capabilities,
|
2016-10-28 00:28:02 +03:00
|
|
|
nonce: nonce,
|
2017-11-20 20:35:52 +03:00
|
|
|
genesis: genesis,
|
2017-02-08 00:52:17 +03:00
|
|
|
total_difficulty: total_diff,
|
2016-10-26 08:06:13 +03:00
|
|
|
sender_addr: sender_addr,
|
2016-10-25 07:35:10 +03:00
|
|
|
receiver_addr: receiver_addr,
|
|
|
|
user_agent: user_agent,
|
2016-10-26 08:06:13 +03:00
|
|
|
})
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Second part of a handshake, receiver of the first part replies with its own
|
|
|
|
/// version and characteristics.
|
2016-10-24 00:02:02 +03:00
|
|
|
pub struct Shake {
|
2016-10-26 08:06:13 +03:00
|
|
|
/// sender version
|
|
|
|
pub version: u32,
|
|
|
|
/// sender capabilities
|
|
|
|
pub capabilities: Capabilities,
|
2017-11-20 20:35:52 +03:00
|
|
|
/// genesis block of our chain, only connect to peers on the same chain
|
|
|
|
pub genesis: Hash,
|
2017-02-08 00:52:17 +03:00
|
|
|
/// total difficulty accumulated by the sender, used to check whether sync
|
2017-11-02 01:56:59 +03:00
|
|
|
/// may be needed
|
2017-02-08 00:52:17 +03:00
|
|
|
pub total_difficulty: Difficulty,
|
2016-10-26 08:06:13 +03:00
|
|
|
/// name of version of the software
|
|
|
|
pub user_agent: String,
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|
|
|
|
|
2016-10-26 08:06:13 +03:00
|
|
|
impl Writeable for Shake {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2017-09-29 21:44:25 +03:00
|
|
|
ser_multiwrite!(
|
|
|
|
writer,
|
|
|
|
[write_u32, self.version],
|
|
|
|
[write_u32, self.capabilities.bits()]
|
|
|
|
);
|
2017-08-10 03:54:10 +03:00
|
|
|
self.total_difficulty.write(writer).unwrap();
|
|
|
|
writer.write_bytes(&self.user_agent).unwrap();
|
2017-11-20 20:35:52 +03:00
|
|
|
self.genesis.write(writer).unwrap();
|
2016-11-01 05:03:12 +03:00
|
|
|
Ok(())
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for Shake {
|
2016-10-25 07:35:10 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<Shake, ser::Error> {
|
2017-02-08 00:52:17 +03:00
|
|
|
let (version, capab) = ser_multiread!(reader, read_u32, read_u32);
|
2018-11-07 14:15:37 +03:00
|
|
|
let capabilities = Capabilities::from_bits_truncate(capab);
|
2018-06-04 19:05:49 +03:00
|
|
|
let total_diff = Difficulty::read(reader)?;
|
2018-11-13 12:30:40 +03:00
|
|
|
let ua = reader.read_bytes_len_prefix()?;
|
2018-06-04 19:05:49 +03:00
|
|
|
let user_agent = String::from_utf8(ua).map_err(|_| ser::Error::CorruptedData)?;
|
|
|
|
let genesis = Hash::read(reader)?;
|
2016-10-26 08:06:13 +03:00
|
|
|
Ok(Shake {
|
2016-10-25 07:35:10 +03:00
|
|
|
version: version,
|
2016-10-26 08:06:13 +03:00
|
|
|
capabilities: capabilities,
|
2017-11-20 20:35:52 +03:00
|
|
|
genesis: genesis,
|
2017-02-08 00:52:17 +03:00
|
|
|
total_difficulty: total_diff,
|
2016-10-25 07:35:10 +03:00
|
|
|
user_agent: user_agent,
|
2016-10-26 08:06:13 +03:00
|
|
|
})
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
/// Ask for other peers addresses, required for network discovery.
|
|
|
|
pub struct GetPeerAddrs {
|
|
|
|
/// Filters on the capabilities we'd like the peers to have
|
|
|
|
pub capabilities: Capabilities,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for GetPeerAddrs {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2016-10-29 22:36:45 +03:00
|
|
|
writer.write_u32(self.capabilities.bits())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for GetPeerAddrs {
|
2016-10-29 22:36:45 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<GetPeerAddrs, ser::Error> {
|
2018-06-04 19:05:49 +03:00
|
|
|
let capab = reader.read_u32()?;
|
2018-11-07 14:15:37 +03:00
|
|
|
let capabilities = Capabilities::from_bits_truncate(capab);
|
2018-11-07 12:28:17 +03:00
|
|
|
Ok(GetPeerAddrs { capabilities })
|
2016-10-29 22:36:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Peer addresses we know of that are fresh enough, in response to
|
|
|
|
/// GetPeerAddrs.
|
2018-01-29 17:45:01 +03:00
|
|
|
#[derive(Debug)]
|
2016-10-29 22:36:45 +03:00
|
|
|
pub struct PeerAddrs {
|
|
|
|
pub peers: Vec<SockAddr>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for PeerAddrs {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2018-06-04 19:05:49 +03:00
|
|
|
writer.write_u32(self.peers.len() as u32)?;
|
2016-10-29 22:36:45 +03:00
|
|
|
for p in &self.peers {
|
2017-08-10 03:54:10 +03:00
|
|
|
p.write(writer).unwrap();
|
2016-10-29 22:36:45 +03:00
|
|
|
}
|
2016-11-01 20:42:33 +03:00
|
|
|
Ok(())
|
2016-10-29 22:36:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for PeerAddrs {
|
2016-10-29 22:36:45 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<PeerAddrs, ser::Error> {
|
2018-06-04 19:05:49 +03:00
|
|
|
let peer_count = reader.read_u32()?;
|
2017-02-19 05:42:34 +03:00
|
|
|
if peer_count > MAX_PEER_ADDRS {
|
2016-12-11 06:11:49 +03:00
|
|
|
return Err(ser::Error::TooLargeReadErr);
|
2017-02-19 05:42:34 +03:00
|
|
|
} else if peer_count == 0 {
|
|
|
|
return Ok(PeerAddrs { peers: vec![] });
|
|
|
|
}
|
|
|
|
// let peers = try_map_vec!([0..peer_count], |_| SockAddr::read(reader));
|
|
|
|
let mut peers = Vec::with_capacity(peer_count as usize);
|
|
|
|
for _ in 0..peer_count {
|
|
|
|
peers.push(SockAddr::read(reader)?);
|
2016-10-29 22:36:45 +03:00
|
|
|
}
|
|
|
|
Ok(PeerAddrs { peers: peers })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 07:35:10 +03:00
|
|
|
/// We found some issue in the communication, sending an error back, usually
|
|
|
|
/// followed by closing the connection.
|
2016-10-24 00:02:02 +03:00
|
|
|
pub struct PeerError {
|
2016-10-26 08:06:13 +03:00
|
|
|
/// error code
|
|
|
|
pub code: u32,
|
|
|
|
/// slightly more user friendly message
|
|
|
|
pub message: String,
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
|
|
|
|
impl Writeable for PeerError {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2016-11-01 20:42:33 +03:00
|
|
|
ser_multiwrite!(writer, [write_u32, self.code], [write_bytes, &self.message]);
|
2016-11-01 05:03:12 +03:00
|
|
|
Ok(())
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for PeerError {
|
2016-10-25 07:35:10 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<PeerError, ser::Error> {
|
2018-11-13 12:30:40 +03:00
|
|
|
let code = reader.read_u32()?;
|
|
|
|
let msg = reader.read_bytes_len_prefix()?;
|
2018-06-04 19:05:49 +03:00
|
|
|
let message = String::from_utf8(msg).map_err(|_| ser::Error::CorruptedData)?;
|
2016-10-26 08:06:13 +03:00
|
|
|
Ok(PeerError {
|
2016-10-25 07:35:10 +03:00
|
|
|
code: code,
|
|
|
|
message: message,
|
2016-10-26 08:06:13 +03:00
|
|
|
})
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 00:28:02 +03:00
|
|
|
/// Only necessary so we can implement Readable and Writeable. Rust disallows
|
|
|
|
/// implementing traits when both types are outside of this crate (which is the
|
|
|
|
/// case for SocketAddr and Readable/Writeable).
|
2018-01-29 17:45:01 +03:00
|
|
|
#[derive(Debug)]
|
2016-10-26 08:06:13 +03:00
|
|
|
pub struct SockAddr(pub SocketAddr);
|
|
|
|
|
|
|
|
impl Writeable for SockAddr {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2016-10-26 08:06:13 +03:00
|
|
|
match self.0 {
|
|
|
|
SocketAddr::V4(sav4) => {
|
2017-09-29 21:44:25 +03:00
|
|
|
ser_multiwrite!(
|
|
|
|
writer,
|
|
|
|
[write_u8, 0],
|
|
|
|
[write_fixed_bytes, &sav4.ip().octets().to_vec()],
|
|
|
|
[write_u16, sav4.port()]
|
|
|
|
);
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
2016-10-26 08:06:13 +03:00
|
|
|
SocketAddr::V6(sav6) => {
|
2018-06-04 19:05:49 +03:00
|
|
|
writer.write_u8(1)?;
|
2016-10-26 08:06:13 +03:00
|
|
|
for seg in &sav6.ip().segments() {
|
2018-06-04 19:05:49 +03:00
|
|
|
writer.write_u16(*seg)?;
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
2018-06-04 19:05:49 +03:00
|
|
|
writer.write_u16(sav6.port())?;
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-01 05:03:12 +03:00
|
|
|
Ok(())
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for SockAddr {
|
2016-10-26 08:06:13 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<SockAddr, ser::Error> {
|
2018-06-04 19:05:49 +03:00
|
|
|
let v4_or_v6 = reader.read_u8()?;
|
2016-10-25 07:35:10 +03:00
|
|
|
if v4_or_v6 == 0 {
|
2018-06-04 19:05:49 +03:00
|
|
|
let ip = reader.read_fixed_bytes(4)?;
|
|
|
|
let port = reader.read_u16()?;
|
2017-09-29 21:44:25 +03:00
|
|
|
Ok(SockAddr(SocketAddr::V4(SocketAddrV4::new(
|
|
|
|
Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]),
|
|
|
|
port,
|
|
|
|
))))
|
2016-10-25 07:35:10 +03:00
|
|
|
} else {
|
2018-11-06 07:38:41 +03:00
|
|
|
let ip = try_iter_map_vec!(0..8, |_| reader.read_u16());
|
2018-06-04 19:05:49 +03:00
|
|
|
let port = reader.read_u16()?;
|
2017-09-29 21:44:25 +03:00
|
|
|
Ok(SockAddr(SocketAddr::V6(SocketAddrV6::new(
|
2017-11-01 02:32:33 +03:00
|
|
|
Ipv6Addr::new(ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7]),
|
2017-09-29 21:44:25 +03:00
|
|
|
port,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
))))
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 06:11:49 +03:00
|
|
|
|
2017-02-08 00:52:17 +03:00
|
|
|
/// Serializable wrapper for the block locator.
|
2018-01-30 17:42:04 +03:00
|
|
|
#[derive(Debug)]
|
2017-02-08 00:52:17 +03:00
|
|
|
pub struct Locator {
|
|
|
|
pub hashes: Vec<Hash>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for Locator {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2017-02-08 00:52:17 +03:00
|
|
|
writer.write_u8(self.hashes.len() as u8)?;
|
|
|
|
for h in &self.hashes {
|
|
|
|
h.write(writer)?
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for Locator {
|
2017-02-08 00:52:17 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<Locator, ser::Error> {
|
|
|
|
let len = reader.read_u8()?;
|
2018-04-03 23:21:13 +03:00
|
|
|
if len > (MAX_LOCATORS as u8) {
|
|
|
|
return Err(ser::Error::TooLargeReadErr);
|
|
|
|
}
|
2017-02-08 00:52:17 +03:00
|
|
|
let mut hashes = Vec::with_capacity(len as usize);
|
|
|
|
for _ in 0..len {
|
|
|
|
hashes.push(Hash::read(reader)?);
|
|
|
|
}
|
|
|
|
Ok(Locator { hashes: hashes })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serializable wrapper for a list of block headers.
|
|
|
|
pub struct Headers {
|
|
|
|
pub headers: Vec<BlockHeader>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for Headers {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
2017-02-08 00:52:17 +03:00
|
|
|
writer.write_u16(self.headers.len() as u16)?;
|
|
|
|
for h in &self.headers {
|
|
|
|
h.write(writer)?
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
impl Readable for Headers {
|
2017-02-08 00:52:17 +03:00
|
|
|
fn read(reader: &mut Reader) -> Result<Headers, ser::Error> {
|
|
|
|
let len = reader.read_u16()?;
|
2018-04-03 23:21:13 +03:00
|
|
|
if (len as u32) > MAX_BLOCK_HEADERS + 1 {
|
|
|
|
return Err(ser::Error::TooLargeReadErr);
|
|
|
|
}
|
2018-08-17 05:30:05 +03:00
|
|
|
let mut headers: Vec<BlockHeader> = Vec::with_capacity(len as usize);
|
|
|
|
for n in 0..len as usize {
|
|
|
|
let header = BlockHeader::read(reader)?;
|
|
|
|
if n > 0 && header.height != headers[n - 1].height + 1 {
|
|
|
|
return Err(ser::Error::CorruptedData);
|
|
|
|
}
|
|
|
|
headers.push(header);
|
2017-02-08 00:52:17 +03:00
|
|
|
}
|
|
|
|
Ok(Headers { headers: headers })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:24:29 +03:00
|
|
|
pub struct Ping {
|
|
|
|
/// total difficulty accumulated by the sender, used to check whether sync
|
|
|
|
/// may be needed
|
|
|
|
pub total_difficulty: Difficulty,
|
2017-12-14 00:52:21 +03:00
|
|
|
/// total height
|
|
|
|
pub height: u64,
|
2017-11-21 17:24:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for Ping {
|
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
|
|
|
self.total_difficulty.write(writer).unwrap();
|
2017-12-14 00:52:21 +03:00
|
|
|
self.height.write(writer).unwrap();
|
2017-11-21 17:24:29 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 06:11:49 +03:00
|
|
|
|
2017-11-21 17:24:29 +03:00
|
|
|
impl Readable for Ping {
|
|
|
|
fn read(reader: &mut Reader) -> Result<Ping, ser::Error> {
|
2018-07-13 02:55:21 +03:00
|
|
|
let total_difficulty = Difficulty::read(reader)?;
|
|
|
|
let height = reader.read_u64()?;
|
2018-03-04 03:19:54 +03:00
|
|
|
Ok(Ping {
|
|
|
|
total_difficulty,
|
|
|
|
height,
|
|
|
|
})
|
2017-11-21 17:24:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Pong {
|
|
|
|
/// total difficulty accumulated by the sender, used to check whether sync
|
|
|
|
/// may be needed
|
|
|
|
pub total_difficulty: Difficulty,
|
2017-12-14 00:52:21 +03:00
|
|
|
/// height accumulated by sender
|
2018-03-04 03:19:54 +03:00
|
|
|
pub height: u64,
|
2017-11-21 17:24:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for Pong {
|
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
|
|
|
self.total_difficulty.write(writer).unwrap();
|
2017-12-14 00:52:21 +03:00
|
|
|
self.height.write(writer).unwrap();
|
2016-12-11 06:11:49 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:24:29 +03:00
|
|
|
impl Readable for Pong {
|
|
|
|
fn read(reader: &mut Reader) -> Result<Pong, ser::Error> {
|
2018-07-13 02:55:21 +03:00
|
|
|
let total_difficulty = Difficulty::read(reader)?;
|
|
|
|
let height = reader.read_u64()?;
|
2018-03-04 03:19:54 +03:00
|
|
|
Ok(Pong {
|
|
|
|
total_difficulty,
|
|
|
|
height,
|
|
|
|
})
|
2016-12-11 06:11:49 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2018-05-29 05:45:31 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BanReason {
|
|
|
|
/// the reason for the ban
|
|
|
|
pub ban_reason: ReasonForBan,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for BanReason {
|
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
|
|
|
let ban_reason_i32 = self.ban_reason as i32;
|
|
|
|
ban_reason_i32.write(writer).unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Readable for BanReason {
|
|
|
|
fn read(reader: &mut Reader) -> Result<BanReason, ser::Error> {
|
|
|
|
let ban_reason_i32 = match reader.read_i32() {
|
|
|
|
Ok(h) => h,
|
|
|
|
Err(_) => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let ban_reason = ReasonForBan::from_i32(ban_reason_i32).ok_or(ser::Error::CorruptedData)?;
|
|
|
|
|
|
|
|
Ok(BanReason { ban_reason })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
/// Request to get an archive of the full txhashset store, required to sync
|
2018-02-10 01:32:16 +03:00
|
|
|
/// a new node.
|
2018-03-05 22:33:44 +03:00
|
|
|
pub struct TxHashSetRequest {
|
|
|
|
/// Hash of the block for which the txhashset should be provided
|
2018-02-10 01:32:16 +03:00
|
|
|
pub hash: Hash,
|
2018-03-04 03:19:54 +03:00
|
|
|
/// Height of the corresponding block
|
|
|
|
pub height: u64,
|
2018-02-10 01:32:16 +03:00
|
|
|
}
|
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
impl Writeable for TxHashSetRequest {
|
2018-02-10 01:32:16 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
|
|
|
self.hash.write(writer)?;
|
|
|
|
writer.write_u64(self.height)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
impl Readable for TxHashSetRequest {
|
|
|
|
fn read(reader: &mut Reader) -> Result<TxHashSetRequest, ser::Error> {
|
|
|
|
Ok(TxHashSetRequest {
|
2018-02-10 01:32:16 +03:00
|
|
|
hash: Hash::read(reader)?,
|
|
|
|
height: reader.read_u64()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
/// Response to a txhashset archive request, must include a zip stream of the
|
2018-02-10 01:32:16 +03:00
|
|
|
/// archive after the message body.
|
2018-03-05 22:33:44 +03:00
|
|
|
pub struct TxHashSetArchive {
|
|
|
|
/// Hash of the block for which the txhashset are provided
|
2018-02-10 01:32:16 +03:00
|
|
|
pub hash: Hash,
|
2018-03-04 03:19:54 +03:00
|
|
|
/// Height of the corresponding block
|
2018-02-10 01:32:16 +03:00
|
|
|
pub height: u64,
|
|
|
|
/// Size in bytes of the archive
|
|
|
|
pub bytes: u64,
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
impl Writeable for TxHashSetArchive {
|
2018-02-10 01:32:16 +03:00
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
|
|
|
self.hash.write(writer)?;
|
2018-08-12 23:02:30 +03:00
|
|
|
ser_multiwrite!(writer, [write_u64, self.height], [write_u64, self.bytes]);
|
2018-02-10 01:32:16 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
impl Readable for TxHashSetArchive {
|
|
|
|
fn read(reader: &mut Reader) -> Result<TxHashSetArchive, ser::Error> {
|
2018-02-10 01:32:16 +03:00
|
|
|
let hash = Hash::read(reader)?;
|
2018-07-02 02:23:24 +03:00
|
|
|
let (height, bytes) = ser_multiread!(reader, read_u64, read_u64);
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
Ok(TxHashSetArchive {
|
2018-03-04 03:19:54 +03:00
|
|
|
hash,
|
|
|
|
height,
|
|
|
|
bytes,
|
|
|
|
})
|
2018-02-10 01:32:16 +03:00
|
|
|
}
|
|
|
|
}
|