2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
2015-04-07 01:51:11 +00:00
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
2014-07-18 13:56:17 +00:00
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2018-08-08 21:38:50 +00:00
|
|
|
//! Network message
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
|
|
|
//! This module defines the `Message` traits which are used
|
|
|
|
//! for (de)serializing Bitcoin objects for transmission on the network. It
|
|
|
|
//! also defines (de)serialization routines for many primitives.
|
|
|
|
//!
|
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
use prelude::*;
|
|
|
|
|
2021-06-09 10:40:41 +00:00
|
|
|
use core::{mem, fmt, iter};
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2021-06-09 10:40:41 +00:00
|
|
|
use io;
|
2014-07-18 13:56:17 +00:00
|
|
|
use blockdata::block;
|
2014-08-23 23:37:01 +00:00
|
|
|
use blockdata::transaction;
|
2020-10-11 18:40:29 +00:00
|
|
|
use network::address::{Address, AddrV2Message};
|
2014-07-18 13:56:17 +00:00
|
|
|
use network::message_network;
|
|
|
|
use network::message_blockdata;
|
2019-02-08 12:00:51 +00:00
|
|
|
use network::message_filter;
|
2021-04-20 11:59:37 +00:00
|
|
|
use consensus::encode::{CheckedData, Decodable, Encodable, VarInt, MAX_VEC_SIZE};
|
2019-07-11 17:06:42 +00:00
|
|
|
use consensus::{encode, serialize};
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2021-05-03 09:41:58 +00:00
|
|
|
/// The maximum number of [super::message_blockdata::Inventory] items in an `inv` message.
|
2020-11-09 22:22:59 +00:00
|
|
|
///
|
|
|
|
/// This limit is not currently enforced by this implementation.
|
|
|
|
pub const MAX_INV_SIZE: usize = 50_000;
|
|
|
|
|
2014-07-18 13:56:17 +00:00
|
|
|
/// Serializer for command string
|
2015-03-26 15:35:31 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2019-12-03 22:44:42 +00:00
|
|
|
pub struct CommandString(Cow<'static, str>);
|
2019-12-03 21:22:19 +00:00
|
|
|
|
2020-10-09 15:03:43 +00:00
|
|
|
impl CommandString {
|
|
|
|
/// Convert from various string types into a [CommandString].
|
|
|
|
///
|
|
|
|
/// Supported types are:
|
|
|
|
/// - `&'static str`
|
|
|
|
/// - `String`
|
|
|
|
///
|
|
|
|
/// Returns an empty error if and only if the string is
|
|
|
|
/// larger than 12 characters in length.
|
|
|
|
pub fn try_from<S: Into<Cow<'static, str>>>(s: S) -> Result<CommandString, ()> {
|
|
|
|
let cow = s.into();
|
|
|
|
if cow.as_ref().len() > 12 {
|
|
|
|
Err(())
|
|
|
|
} else {
|
|
|
|
Ok(CommandString(cow))
|
|
|
|
}
|
2019-12-03 22:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 15:03:43 +00:00
|
|
|
impl fmt::Display for CommandString {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.write_str(self.0.as_ref())
|
2019-12-03 22:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:44:42 +00:00
|
|
|
impl AsRef<str> for CommandString {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
self.0.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:56:37 +00:00
|
|
|
impl Encodable for CommandString {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
2019-07-11 14:56:37 +00:00
|
|
|
fn consensus_encode<S: io::Write>(
|
|
|
|
&self,
|
|
|
|
s: S,
|
2020-10-09 15:04:10 +00:00
|
|
|
) -> Result<usize, io::Error> {
|
2015-12-18 04:45:11 +00:00
|
|
|
let mut rawbytes = [0u8; 12];
|
2019-12-03 22:39:04 +00:00
|
|
|
let strbytes = self.0.as_bytes();
|
2020-10-09 15:03:43 +00:00
|
|
|
debug_assert!(strbytes.len() <= 12);
|
2019-08-05 19:41:07 +00:00
|
|
|
rawbytes[..strbytes.len()].clone_from_slice(&strbytes[..]);
|
2015-04-07 01:51:11 +00:00
|
|
|
rawbytes.consensus_encode(s)
|
|
|
|
}
|
2014-08-01 16:01:39 +00:00
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2019-07-11 17:06:42 +00:00
|
|
|
impl Decodable for CommandString {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
2019-07-11 17:06:42 +00:00
|
|
|
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, encode::Error> {
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
let rawbytes: [u8; 12] = Decodable::consensus_decode(d)?;
|
2019-07-11 17:06:42 +00:00
|
|
|
let rv = iter::FromIterator::from_iter(
|
|
|
|
rawbytes
|
|
|
|
.iter()
|
|
|
|
.filter_map(|&u| if u > 0 { Some(u as char) } else { None })
|
|
|
|
);
|
2015-04-07 01:51:11 +00:00
|
|
|
Ok(CommandString(rv))
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:21:29 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2014-07-18 13:56:17 +00:00
|
|
|
/// A Network message
|
|
|
|
pub struct RawNetworkMessage {
|
2015-04-07 01:51:11 +00:00
|
|
|
/// Magic bytes to identify the network these messages are meant for
|
|
|
|
pub magic: u32,
|
|
|
|
/// The actual message data
|
|
|
|
pub payload: NetworkMessage
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 14:35:27 +00:00
|
|
|
/// A Network message payload. Proper documentation is available on at
|
|
|
|
/// [Bitcoin Wiki: Protocol Specification](https://en.bitcoin.it/wiki/Protocol_specification)
|
2020-12-21 11:59:25 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2014-09-10 12:15:48 +00:00
|
|
|
pub enum NetworkMessage {
|
2015-04-07 01:51:11 +00:00
|
|
|
/// `version`
|
|
|
|
Version(message_network::VersionMessage),
|
|
|
|
/// `verack`
|
|
|
|
Verack,
|
|
|
|
/// `addr`
|
|
|
|
Addr(Vec<(u32, Address)>),
|
|
|
|
/// `inv`
|
|
|
|
Inv(Vec<message_blockdata::Inventory>),
|
|
|
|
/// `getdata`
|
|
|
|
GetData(Vec<message_blockdata::Inventory>),
|
|
|
|
/// `notfound`
|
|
|
|
NotFound(Vec<message_blockdata::Inventory>),
|
|
|
|
/// `getblocks`
|
|
|
|
GetBlocks(message_blockdata::GetBlocksMessage),
|
|
|
|
/// `getheaders`
|
|
|
|
GetHeaders(message_blockdata::GetHeadersMessage),
|
2015-11-22 23:28:49 +00:00
|
|
|
/// `mempool`
|
|
|
|
MemPool,
|
2015-04-07 01:51:11 +00:00
|
|
|
/// tx
|
|
|
|
Tx(transaction::Transaction),
|
|
|
|
/// `block`
|
|
|
|
Block(block::Block),
|
|
|
|
/// `headers`
|
2019-05-17 21:53:38 +00:00
|
|
|
Headers(Vec<block::BlockHeader>),
|
2019-05-17 18:09:27 +00:00
|
|
|
/// `sendheaders`
|
|
|
|
SendHeaders,
|
2016-10-10 15:14:12 +00:00
|
|
|
/// `getaddr`
|
|
|
|
GetAddr,
|
2015-04-07 01:51:11 +00:00
|
|
|
// TODO: checkorder,
|
|
|
|
// TODO: submitorder,
|
|
|
|
// TODO: reply,
|
|
|
|
/// `ping`
|
|
|
|
Ping(u64),
|
|
|
|
/// `pong`
|
2018-02-10 15:47:46 +00:00
|
|
|
Pong(u64),
|
2015-04-07 01:51:11 +00:00
|
|
|
// TODO: bloom filtering
|
2019-02-08 12:00:51 +00:00
|
|
|
/// BIP157 getcfilters
|
|
|
|
GetCFilters(message_filter::GetCFilters),
|
|
|
|
/// BIP157 cfilter
|
|
|
|
CFilter(message_filter::CFilter),
|
|
|
|
/// BIP157 getcfheaders
|
|
|
|
GetCFHeaders(message_filter::GetCFHeaders),
|
|
|
|
/// BIP157 cfheaders
|
|
|
|
CFHeaders(message_filter::CFHeaders),
|
|
|
|
/// BIP157 getcfcheckpt
|
|
|
|
GetCFCheckpt(message_filter::GetCFCheckpt),
|
|
|
|
/// BIP157 cfcheckpt
|
|
|
|
CFCheckpt(message_filter::CFCheckpt),
|
2018-02-10 15:47:46 +00:00
|
|
|
/// `alert`
|
2019-08-27 11:01:21 +00:00
|
|
|
Alert(Vec<u8>),
|
|
|
|
/// `reject`
|
2020-07-30 16:25:21 +00:00
|
|
|
Reject(message_network::Reject),
|
|
|
|
/// `feefilter`
|
|
|
|
FeeFilter(i64),
|
2020-07-22 22:00:04 +00:00
|
|
|
/// `wtxidrelay`
|
|
|
|
WtxidRelay,
|
2020-10-11 18:40:29 +00:00
|
|
|
/// `addrv2`
|
|
|
|
AddrV2(Vec<AddrV2Message>),
|
|
|
|
/// `sendaddrv2`
|
|
|
|
SendAddrV2,
|
2020-12-21 11:59:25 +00:00
|
|
|
|
|
|
|
/// Any other message.
|
|
|
|
Unknown {
|
|
|
|
/// The command of this message.
|
|
|
|
command: CommandString,
|
|
|
|
/// The payload of this message.
|
|
|
|
payload: Vec<u8>,
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 21:19:44 +00:00
|
|
|
impl NetworkMessage {
|
2020-12-21 11:59:25 +00:00
|
|
|
/// Return the message command as a static string reference.
|
|
|
|
///
|
|
|
|
/// This returns `"unknown"` for [NetworkMessage::Unknown],
|
|
|
|
/// regardless of the actual command in the unknown message.
|
2021-05-03 09:41:58 +00:00
|
|
|
/// Use the [Self::command] method to get the command for unknown messages.
|
2019-12-03 22:27:19 +00:00
|
|
|
pub fn cmd(&self) -> &'static str {
|
2019-12-03 21:19:44 +00:00
|
|
|
match *self {
|
2015-04-07 01:51:11 +00:00
|
|
|
NetworkMessage::Version(_) => "version",
|
|
|
|
NetworkMessage::Verack => "verack",
|
|
|
|
NetworkMessage::Addr(_) => "addr",
|
|
|
|
NetworkMessage::Inv(_) => "inv",
|
|
|
|
NetworkMessage::GetData(_) => "getdata",
|
|
|
|
NetworkMessage::NotFound(_) => "notfound",
|
|
|
|
NetworkMessage::GetBlocks(_) => "getblocks",
|
|
|
|
NetworkMessage::GetHeaders(_) => "getheaders",
|
2015-11-22 23:28:49 +00:00
|
|
|
NetworkMessage::MemPool => "mempool",
|
2015-04-07 01:51:11 +00:00
|
|
|
NetworkMessage::Tx(_) => "tx",
|
|
|
|
NetworkMessage::Block(_) => "block",
|
|
|
|
NetworkMessage::Headers(_) => "headers",
|
2019-05-17 18:09:27 +00:00
|
|
|
NetworkMessage::SendHeaders => "sendheaders",
|
2016-10-10 15:14:12 +00:00
|
|
|
NetworkMessage::GetAddr => "getaddr",
|
2015-04-07 01:51:11 +00:00
|
|
|
NetworkMessage::Ping(_) => "ping",
|
|
|
|
NetworkMessage::Pong(_) => "pong",
|
2019-02-08 12:00:51 +00:00
|
|
|
NetworkMessage::GetCFilters(_) => "getcfilters",
|
|
|
|
NetworkMessage::CFilter(_) => "cfilter",
|
|
|
|
NetworkMessage::GetCFHeaders(_) => "getcfheaders",
|
|
|
|
NetworkMessage::CFHeaders(_) => "cfheaders",
|
2019-12-20 22:47:40 +00:00
|
|
|
NetworkMessage::GetCFCheckpt(_) => "getcfcheckpt",
|
2019-02-08 12:00:51 +00:00
|
|
|
NetworkMessage::CFCheckpt(_) => "cfcheckpt",
|
2018-02-10 15:47:46 +00:00
|
|
|
NetworkMessage::Alert(_) => "alert",
|
2019-08-27 11:01:21 +00:00
|
|
|
NetworkMessage::Reject(_) => "reject",
|
2020-07-30 16:25:21 +00:00
|
|
|
NetworkMessage::FeeFilter(_) => "feefilter",
|
2020-07-22 22:00:04 +00:00
|
|
|
NetworkMessage::WtxidRelay => "wtxidrelay",
|
2020-10-11 18:40:29 +00:00
|
|
|
NetworkMessage::AddrV2(_) => "addrv2",
|
|
|
|
NetworkMessage::SendAddrV2 => "sendaddrv2",
|
2020-12-21 11:59:25 +00:00
|
|
|
NetworkMessage::Unknown { .. } => "unknown",
|
2019-12-03 21:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-03 22:27:19 +00:00
|
|
|
|
|
|
|
/// Return the CommandString for the message command.
|
|
|
|
pub fn command(&self) -> CommandString {
|
2020-12-21 11:59:25 +00:00
|
|
|
match *self {
|
|
|
|
NetworkMessage::Unknown { command: ref c, .. } => c.clone(),
|
|
|
|
_ => CommandString::try_from(self.cmd()).expect("cmd returns valid commands")
|
|
|
|
}
|
2019-12-03 22:27:19 +00:00
|
|
|
}
|
2019-12-03 21:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RawNetworkMessage {
|
2020-12-21 11:59:25 +00:00
|
|
|
/// Return the message command as a static string reference.
|
|
|
|
///
|
|
|
|
/// This returns `"unknown"` for [NetworkMessage::Unknown],
|
|
|
|
/// regardless of the actual command in the unknown message.
|
2021-05-03 09:41:58 +00:00
|
|
|
/// Use the [Self::command] method to get the command for unknown messages.
|
2019-12-03 22:27:19 +00:00
|
|
|
pub fn cmd(&self) -> &'static str {
|
|
|
|
self.payload.cmd()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the CommandString for the message command.
|
|
|
|
pub fn command(&self) -> CommandString {
|
2019-12-03 21:19:44 +00:00
|
|
|
self.payload.command()
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 21:53:38 +00:00
|
|
|
struct HeaderSerializationWrapper<'a>(&'a Vec<block::BlockHeader>);
|
2019-07-11 14:56:37 +00:00
|
|
|
|
|
|
|
impl<'a> Encodable for HeaderSerializationWrapper<'a> {
|
2019-05-17 21:53:38 +00:00
|
|
|
#[inline]
|
2019-07-11 14:56:37 +00:00
|
|
|
fn consensus_encode<S: io::Write>(
|
|
|
|
&self,
|
|
|
|
mut s: S,
|
2020-10-09 15:04:10 +00:00
|
|
|
) -> Result<usize, io::Error> {
|
2019-05-23 20:28:10 +00:00
|
|
|
let mut len = 0;
|
2019-07-11 14:56:37 +00:00
|
|
|
len += VarInt(self.0.len() as u64).consensus_encode(&mut s)?;
|
2019-05-17 21:53:38 +00:00
|
|
|
for header in self.0.iter() {
|
2019-07-11 14:56:37 +00:00
|
|
|
len += header.consensus_encode(&mut s)?;
|
|
|
|
len += 0u8.consensus_encode(&mut s)?;
|
2019-05-17 21:53:38 +00:00
|
|
|
}
|
2019-05-23 20:28:10 +00:00
|
|
|
Ok(len)
|
2019-05-17 21:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:56:37 +00:00
|
|
|
impl Encodable for RawNetworkMessage {
|
|
|
|
fn consensus_encode<S: io::Write>(
|
|
|
|
&self,
|
|
|
|
mut s: S,
|
2020-10-09 15:04:10 +00:00
|
|
|
) -> Result<usize, io::Error> {
|
2019-05-23 20:28:10 +00:00
|
|
|
let mut len = 0;
|
2019-07-11 14:56:37 +00:00
|
|
|
len += self.magic.consensus_encode(&mut s)?;
|
2019-12-03 22:27:19 +00:00
|
|
|
len += self.command().consensus_encode(&mut s)?;
|
2019-05-23 20:28:10 +00:00
|
|
|
len += CheckedData(match self.payload {
|
2015-04-07 01:51:11 +00:00
|
|
|
NetworkMessage::Version(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::Addr(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::Inv(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::GetData(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::NotFound(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::GetBlocks(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::GetHeaders(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::Tx(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::Block(ref dat) => serialize(dat),
|
2019-05-17 21:53:38 +00:00
|
|
|
NetworkMessage::Headers(ref dat) => serialize(&HeaderSerializationWrapper(dat)),
|
2015-04-07 01:51:11 +00:00
|
|
|
NetworkMessage::Ping(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::Pong(ref dat) => serialize(dat),
|
2019-02-08 12:00:51 +00:00
|
|
|
NetworkMessage::GetCFilters(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::CFilter(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::GetCFHeaders(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::CFHeaders(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::GetCFCheckpt(ref dat) => serialize(dat),
|
|
|
|
NetworkMessage::CFCheckpt(ref dat) => serialize(dat),
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
NetworkMessage::Alert(ref dat) => serialize(dat),
|
2019-08-27 11:01:21 +00:00
|
|
|
NetworkMessage::Reject(ref dat) => serialize(dat),
|
2020-07-30 16:25:21 +00:00
|
|
|
NetworkMessage::FeeFilter(ref data) => serialize(data),
|
2020-10-11 18:40:29 +00:00
|
|
|
NetworkMessage::AddrV2(ref dat) => serialize(dat),
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
NetworkMessage::Verack
|
2019-05-17 18:09:27 +00:00
|
|
|
| NetworkMessage::SendHeaders
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
| NetworkMessage::MemPool
|
2020-07-22 22:00:04 +00:00
|
|
|
| NetworkMessage::GetAddr
|
2020-12-21 11:59:25 +00:00
|
|
|
| NetworkMessage::WtxidRelay
|
2020-10-11 18:40:29 +00:00
|
|
|
| NetworkMessage::SendAddrV2 => vec![],
|
2020-12-21 11:59:25 +00:00
|
|
|
NetworkMessage::Unknown { payload: ref data, .. } => serialize(data),
|
2019-07-11 14:56:37 +00:00
|
|
|
}).consensus_encode(&mut s)?;
|
2019-05-23 20:28:10 +00:00
|
|
|
Ok(len)
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
2014-08-01 16:01:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 21:53:38 +00:00
|
|
|
struct HeaderDeserializationWrapper(Vec<block::BlockHeader>);
|
2019-07-11 17:06:42 +00:00
|
|
|
|
|
|
|
impl Decodable for HeaderDeserializationWrapper {
|
2019-05-17 21:53:38 +00:00
|
|
|
#[inline]
|
2019-07-11 17:06:42 +00:00
|
|
|
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
|
|
|
|
let len = VarInt::consensus_decode(&mut d)?.0;
|
2019-05-17 21:53:38 +00:00
|
|
|
let byte_size = (len as usize)
|
|
|
|
.checked_mul(mem::size_of::<block::BlockHeader>())
|
|
|
|
.ok_or(encode::Error::ParseFailed("Invalid length"))?;
|
|
|
|
if byte_size > MAX_VEC_SIZE {
|
|
|
|
return Err(encode::Error::OversizedVectorAllocation { requested: byte_size, max: MAX_VEC_SIZE })
|
|
|
|
}
|
|
|
|
let mut ret = Vec::with_capacity(len as usize);
|
|
|
|
for _ in 0..len {
|
2019-07-11 17:06:42 +00:00
|
|
|
ret.push(Decodable::consensus_decode(&mut d)?);
|
|
|
|
if u8::consensus_decode(&mut d)? != 0u8 {
|
2019-05-17 21:53:38 +00:00
|
|
|
return Err(encode::Error::ParseFailed("Headers message should not contain transactions"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(HeaderDeserializationWrapper(ret))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 17:06:42 +00:00
|
|
|
impl Decodable for RawNetworkMessage {
|
|
|
|
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
|
|
|
|
let magic = Decodable::consensus_decode(&mut d)?;
|
2020-12-21 11:59:25 +00:00
|
|
|
let cmd = CommandString::consensus_decode(&mut d)?;
|
2019-07-11 17:06:42 +00:00
|
|
|
let raw_payload = CheckedData::consensus_decode(&mut d)?.0;
|
2015-04-07 01:51:11 +00:00
|
|
|
|
2021-06-09 10:40:41 +00:00
|
|
|
let mut mem_d = io::Cursor::new(raw_payload);
|
2020-12-21 11:59:25 +00:00
|
|
|
let payload = match &cmd.0[..] {
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
"version" => NetworkMessage::Version(Decodable::consensus_decode(&mut mem_d)?),
|
2015-04-07 01:51:11 +00:00
|
|
|
"verack" => NetworkMessage::Verack,
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
"addr" => NetworkMessage::Addr(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"inv" => NetworkMessage::Inv(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"getdata" => NetworkMessage::GetData(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"notfound" => NetworkMessage::NotFound(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"getblocks" => NetworkMessage::GetBlocks(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"getheaders" => NetworkMessage::GetHeaders(Decodable::consensus_decode(&mut mem_d)?),
|
2015-11-22 23:28:49 +00:00
|
|
|
"mempool" => NetworkMessage::MemPool,
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
"block" => NetworkMessage::Block(Decodable::consensus_decode(&mut mem_d)?),
|
2019-07-11 17:06:42 +00:00
|
|
|
"headers" => NetworkMessage::Headers(
|
|
|
|
HeaderDeserializationWrapper::consensus_decode(&mut mem_d)?.0
|
|
|
|
),
|
2019-05-17 18:09:27 +00:00
|
|
|
"sendheaders" => NetworkMessage::SendHeaders,
|
2016-10-10 15:14:12 +00:00
|
|
|
"getaddr" => NetworkMessage::GetAddr,
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
"ping" => NetworkMessage::Ping(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"pong" => NetworkMessage::Pong(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"tx" => NetworkMessage::Tx(Decodable::consensus_decode(&mut mem_d)?),
|
2019-02-08 12:00:51 +00:00
|
|
|
"getcfilters" => NetworkMessage::GetCFilters(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"cfilter" => NetworkMessage::CFilter(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"getcfheaders" => NetworkMessage::GetCFHeaders(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"cfheaders" => NetworkMessage::CFHeaders(Decodable::consensus_decode(&mut mem_d)?),
|
2019-12-20 22:47:40 +00:00
|
|
|
"getcfcheckpt" => NetworkMessage::GetCFCheckpt(Decodable::consensus_decode(&mut mem_d)?),
|
2019-02-08 12:00:51 +00:00
|
|
|
"cfcheckpt" => NetworkMessage::CFCheckpt(Decodable::consensus_decode(&mut mem_d)?),
|
2019-09-03 17:59:48 +00:00
|
|
|
"reject" => NetworkMessage::Reject(Decodable::consensus_decode(&mut mem_d)?),
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
"alert" => NetworkMessage::Alert(Decodable::consensus_decode(&mut mem_d)?),
|
2020-07-30 16:25:21 +00:00
|
|
|
"feefilter" => NetworkMessage::FeeFilter(Decodable::consensus_decode(&mut mem_d)?),
|
2020-07-22 22:00:04 +00:00
|
|
|
"wtxidrelay" => NetworkMessage::WtxidRelay,
|
2020-10-11 18:40:29 +00:00
|
|
|
"addrv2" => NetworkMessage::AddrV2(Decodable::consensus_decode(&mut mem_d)?),
|
|
|
|
"sendaddrv2" => NetworkMessage::SendAddrV2,
|
2020-12-21 11:59:25 +00:00
|
|
|
_ => NetworkMessage::Unknown {
|
|
|
|
command: cmd,
|
|
|
|
payload: mem_d.into_inner(),
|
|
|
|
}
|
2015-04-07 01:51:11 +00:00
|
|
|
};
|
|
|
|
Ok(RawNetworkMessage {
|
|
|
|
magic: magic,
|
|
|
|
payload: payload
|
|
|
|
})
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-10-11 18:40:29 +00:00
|
|
|
use std::net::Ipv4Addr;
|
2015-04-07 01:51:11 +00:00
|
|
|
use super::{RawNetworkMessage, NetworkMessage, CommandString};
|
2019-12-11 07:32:47 +00:00
|
|
|
use network::constants::ServiceFlags;
|
2020-10-09 15:03:43 +00:00
|
|
|
use consensus::encode::{deserialize, deserialize_partial, serialize};
|
2020-01-08 17:02:30 +00:00
|
|
|
use hashes::hex::FromHex;
|
2019-12-22 12:00:31 +00:00
|
|
|
use hashes::sha256d::Hash;
|
|
|
|
use hashes::Hash as HashTrait;
|
2020-10-11 18:40:29 +00:00
|
|
|
use network::address::{Address, AddrV2, AddrV2Message};
|
2019-12-22 12:00:31 +00:00
|
|
|
use super::message_network::{Reject, RejectReason, VersionMessage};
|
2019-12-18 11:40:46 +00:00
|
|
|
use network::message_blockdata::{Inventory, GetBlocksMessage, GetHeadersMessage};
|
2019-12-22 12:00:31 +00:00
|
|
|
use blockdata::block::{Block, BlockHeader};
|
|
|
|
use network::message_filter::{GetCFilters, CFilter, GetCFHeaders, CFHeaders, GetCFCheckpt, CFCheckpt};
|
|
|
|
use blockdata::transaction::Transaction;
|
|
|
|
|
|
|
|
fn hash(slice: [u8;32]) -> Hash {
|
|
|
|
Hash::from_slice(&slice).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn full_round_ser_der_raw_network_message_test() {
|
|
|
|
// TODO: Impl Rand traits here to easily generate random values.
|
2020-01-08 17:02:30 +00:00
|
|
|
let version_msg: VersionMessage = deserialize(&Vec::from_hex("721101000100000000000000e6e0845300000000010000000000000000000000000000000000ffff0000000000000100000000000000fd87d87eeb4364f22cf54dca59412db7208d47d920cffce83ee8102f5361746f7368693a302e392e39392f2c9f040001").unwrap()).unwrap();
|
|
|
|
let tx: Transaction = deserialize(&Vec::from_hex("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap()).unwrap();
|
|
|
|
let block: Block = deserialize(&Vec::from_hex("000000202aa2f2ca794ccbd40c16e2f3333f6b8b683f9e7179b2c4d7490600000000000010bc26e70a2f672ad420a6153dd0c28b40a6002c55531bfc99bf8994a8e8f67e5503bd5750d4061a4ed90a700f010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff3603da1b0e00045503bd5704c7dd8a0d0ced13bb5785010800000000000a636b706f6f6c122f4e696e6a61506f6f6c2f5345475749542fffffffff02b4e5a212000000001976a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac0000000000000000266a24aa21a9edf91c46b49eb8a29089980f02ee6b57e7d63d33b18b4fddac2bcd7db2a3983704012000000000000000000000000000000000000000000000000000000000000000000000000001000000017e4f81175332a733e26d4ba4e29f53f67b7a5d7c2adebb276e447ca71d130b55000000006b483045022100cac809cd1a3d9ad5d5e31a84e2e1d8ec5542841e4d14c6b52e8b38cbe1ff1728022064470b7fb0c2efeccb2e84bfa36ec5f9e434c84b1101c00f7ee32f726371b7410121020e62280798b6b8c37f068df0915b0865b63fabc401c2457cbc3ef96887dd3647ffffffff02ca2f780c000000001976a914c6b5545b3592cb477d709896fa705592c9b6113a88ac663b2a06000000001976a914e7c1345fc8f87c68170b3aa798a956c2fe6a9eff88ac0000000001000000011e99f5a785e677e017d36b50aa4fd10010ffd039f38f42f447ca8895250e121f01000000d90047304402200d3d296ad641a281dd5c0d68b9ab0d1ad5f7052bec148c1fb81fb1ba69181ec502201a372bb16fb8e054ee9bef41e300d292153830f841a4db0ab7f7407f6581b9bc01473044022002584f313ae990236b6bebb82fbbb006a2b02a448dd5c93434428991eae960d60220491d67d2660c4dde19025cf86e5164a559e2c79c3b98b40e146fab974acd24690147522102632178d046673c9729d828cfee388e121f497707f810c131e0d3fc0fe0bd66d62103a0951ec7d3a9da9de171617026442fcd30f34d66100fab539853b43f508787d452aeffffffff0240420f000000000017a9140ffdcf96700455074292a821c74922e8652993998788997bc60000000017a9148ce5408cfeaddb7ccb2545ded41ef478109454848700000000010000000113100b09e6a78d63ec4850654ab0f68806de29710b09172eddfef730652b155501000000da00473044022015389408e3446a3f36a05060e0e4a3c8b92ff3901ba2511aa944ec91a537a1cb022045a33b6ec47605b1718ed2e753263e54918edbf6126508ff039621fb928d28a001483045022100bb952fde81f216f7063575c0bb2bedc050ce08c96d9b437ea922f5eb98c882da02201b7cbf3a2f94ea4c5eb7f0df3af2ebcafa8705af7f410ab5d3d4bac13d6bc6120147522102632178d046673c9729d828cfee388e121f497707f810c131e0d3fc0fe0bd66d62103a0951ec7d3a9da9de171617026442fcd30f34d66100fab539853b43f508787d452aeffffffff0240420f000000000017a914d3db9a20312c3ab896a316eb108dbd01e47e17d687e0ba7ac60000000017a9148ce5408cfeaddb7ccb2545ded41ef47810945484870000000001000000016e3cca1599cde54878e2f27f434df69df0afd1f313cb6e38c08d3ffb57f97a6c01000000da0048304502210095623b70ec3194fa4037a1c1106c2580caedc390e25e5b330bbeb3111e8184bc02205ae973c4a4454be2a3a03beb66297143c1044a3c4743742c5cdd1d516a1ad3040147304402202f3d6d89996f5b42773dd6ebaf367f1af1f3a95c7c7b487ec040131c40f4a4a30220524ffbb0b563f37b3eb1341228f792e8f84111b7c4a9f49cdd998e052ee42efa0147522102632178d046673c9729d828cfee388e121f497707f810c131e0d3fc0fe0bd66d62103a0951ec7d3a9da9de171617026442fcd30f34d66100fab539853b43f508787d452aeffffffff0240420f000000000017a9141ade6b95896dde8ec4dee9e59af8849d3797348e8728af7ac60000000017a9148ce5408cfeaddb7ccb2545ded41ef47810945484870000000001000000011d9dc3a5df9b5b2eeb2bd11a2db243be9e8cc23e2f180bf317d32a499904c15501000000db00483045022100ebbd1c9a8ce626edbb1a7881df81e872ef8c6424feda36faa8a5745157400c6a02206eb463bc8acd5ea06a289e86115e1daae0c2cf10d9cbbd199e1311170d5543ef01483045022100809411a917dc8cf4f3a777f0388fdea6de06243ef7691e500c60abd1c7f19ae602205255d2b1191d8adedb77b814ccb66471eb8486cb4ff8727824254ee5589f176b0147522102632178d046673c9729d828cfee388e121f497707f810c131e0d3fc0fe0bd66d62103a0951ec7d3a9da9de171617026442fcd30f34d66100fab539853b43f508787d452aeffffffff0240420f000000000017a914759a49c772347be81c49517f9e1e6def6a88d4dd87800b85c60000000017a9148ce5408cfeaddb7ccb2545ded41ef47810945484870000000001000000018c51902affd8e5247dfcc2e5d0528a3815f53c8b6d2c200ff290b2b2b486d7704f0000006a47304402201be0d485f6a3ce871be80064c593c5327b3fd7e450f05ab7fae38385bc40cfbe02206e2a6c9970b5d1d10207892376733757486634fce4f352e772149c486857612101210350c33bc9a790c9495195761577b34912a949b73d5bc5ae5343f5ba08b33220ccffffffff01102700000000
|
|
|
|
let header: BlockHeader = deserialize(&Vec::from_hex("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b").unwrap()).unwrap();
|
2019-12-22 12:00:31 +00:00
|
|
|
|
|
|
|
let msgs = vec![
|
|
|
|
NetworkMessage::Version(version_msg),
|
|
|
|
NetworkMessage::Verack,
|
|
|
|
NetworkMessage::Addr(vec![(45, Address::new(&([123,255,000,100], 833).into(), ServiceFlags::NETWORK))]),
|
2019-12-18 11:40:46 +00:00
|
|
|
NetworkMessage::Inv(vec![Inventory::Block(hash([8u8; 32]).into())]),
|
|
|
|
NetworkMessage::GetData(vec![Inventory::Transaction(hash([45u8; 32]).into())]),
|
|
|
|
NetworkMessage::NotFound(vec![Inventory::Error]),
|
2019-12-06 09:01:15 +00:00
|
|
|
NetworkMessage::GetBlocks(GetBlocksMessage::new(vec![hash([1u8; 32]).into(), hash([4u8; 32]).into()], hash([5u8; 32]).into())),
|
|
|
|
NetworkMessage::GetHeaders(GetHeadersMessage::new(vec![hash([10u8; 32]).into(), hash([40u8; 32]).into()], hash([50u8; 32]).into())),
|
2019-12-22 12:00:31 +00:00
|
|
|
NetworkMessage::MemPool,
|
|
|
|
NetworkMessage::Tx(tx),
|
|
|
|
NetworkMessage::Block(block),
|
|
|
|
NetworkMessage::Headers(vec![header]),
|
|
|
|
NetworkMessage::SendHeaders,
|
|
|
|
NetworkMessage::GetAddr,
|
|
|
|
NetworkMessage::Ping(15),
|
|
|
|
NetworkMessage::Pong(23),
|
2019-12-09 22:43:33 +00:00
|
|
|
NetworkMessage::GetCFilters(GetCFilters{filter_type: 2, start_height: 52, stop_hash: hash([42u8; 32]).into()}),
|
|
|
|
NetworkMessage::CFilter(CFilter{filter_type: 7, block_hash: hash([25u8; 32]).into(), filter: vec![1,2,3]}),
|
2019-12-06 09:01:15 +00:00
|
|
|
NetworkMessage::GetCFHeaders(GetCFHeaders{filter_type: 4, start_height: 102, stop_hash: hash([47u8; 32]).into()}),
|
2020-08-07 12:23:35 +00:00
|
|
|
NetworkMessage::CFHeaders(CFHeaders{filter_type: 13, stop_hash: hash([53u8; 32]).into(), previous_filter_header: hash([12u8; 32]).into(), filter_hashes: vec![hash([4u8; 32]).into(), hash([12u8; 32]).into()]}),
|
2019-12-06 09:01:15 +00:00
|
|
|
NetworkMessage::GetCFCheckpt(GetCFCheckpt{filter_type: 17, stop_hash: hash([25u8; 32]).into()}),
|
|
|
|
NetworkMessage::CFCheckpt(CFCheckpt{filter_type: 27, stop_hash: hash([77u8; 32]).into(), filter_headers: vec![hash([3u8; 32]).into(), hash([99u8; 32]).into()]}),
|
2019-12-22 12:00:31 +00:00
|
|
|
NetworkMessage::Alert(vec![45,66,3,2,6,8,9,12,3,130]),
|
2020-10-09 15:03:43 +00:00
|
|
|
NetworkMessage::Reject(Reject{message: CommandString::try_from("Test reject").unwrap(), ccode: RejectReason::Duplicate, reason: "Cause".into(), hash: hash([255u8; 32])}),
|
2020-07-30 16:25:21 +00:00
|
|
|
NetworkMessage::FeeFilter(1000),
|
2020-07-22 22:00:04 +00:00
|
|
|
NetworkMessage::WtxidRelay,
|
2020-10-11 18:40:29 +00:00
|
|
|
NetworkMessage::AddrV2(vec![AddrV2Message{ addr: AddrV2::Ipv4(Ipv4Addr::new(127, 0, 0, 1)), port: 0, services: ServiceFlags::NONE, time: 0 }]),
|
|
|
|
NetworkMessage::SendAddrV2,
|
2019-12-22 12:00:31 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for msg in msgs {
|
|
|
|
let raw_msg = RawNetworkMessage {magic: 57, payload: msg};
|
|
|
|
assert_eq!(deserialize::<RawNetworkMessage>(&serialize(&raw_msg)).unwrap(), raw_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-04-07 01:51:11 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-10-09 15:03:43 +00:00
|
|
|
fn commandstring_test() {
|
|
|
|
// Test converting.
|
|
|
|
assert_eq!(CommandString::try_from("AndrewAndrew").unwrap().as_ref(), "AndrewAndrew");
|
|
|
|
assert!(CommandString::try_from("AndrewAndrewA").is_err());
|
|
|
|
|
|
|
|
// Test serializing.
|
2019-12-03 21:22:19 +00:00
|
|
|
let cs = CommandString("Andrew".into());
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
assert_eq!(serialize(&cs), vec![0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0, 0]);
|
2019-12-10 17:22:55 +00:00
|
|
|
|
2020-10-09 15:03:43 +00:00
|
|
|
// Test deserializing
|
2015-04-08 22:23:45 +00:00
|
|
|
let cs: Result<CommandString, _> = deserialize(&[0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0, 0]);
|
2015-04-07 01:51:11 +00:00
|
|
|
assert!(cs.is_ok());
|
2019-12-03 21:22:19 +00:00
|
|
|
assert_eq!(cs.as_ref().unwrap().to_string(), "Andrew".to_owned());
|
2020-10-09 15:03:43 +00:00
|
|
|
assert_eq!(cs.unwrap(), CommandString::try_from("Andrew").unwrap());
|
2015-04-07 01:51:11 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
let short_cs: Result<CommandString, _> = deserialize(&[0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0]);
|
2015-04-07 01:51:11 +00:00
|
|
|
assert!(short_cs.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_verack_test() {
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
assert_eq!(serialize(&RawNetworkMessage { magic: 0xd9b4bef9, payload: NetworkMessage::Verack }),
|
|
|
|
vec![0xf9, 0xbe, 0xb4, 0xd9, 0x76, 0x65, 0x72, 0x61,
|
|
|
|
0x63, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x5d, 0xf6, 0xe0, 0xe2]);
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_ping_test() {
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
assert_eq!(serialize(&RawNetworkMessage { magic: 0xd9b4bef9, payload: NetworkMessage::Ping(100) }),
|
|
|
|
vec![0xf9, 0xbe, 0xb4, 0xd9, 0x70, 0x69, 0x6e, 0x67,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x08, 0x00, 0x00, 0x00, 0x24, 0x67, 0xf1, 0x1d,
|
|
|
|
0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
2015-11-22 23:28:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_mempool_test() {
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
assert_eq!(serialize(&RawNetworkMessage { magic: 0xd9b4bef9, payload: NetworkMessage::MemPool }),
|
|
|
|
vec![0xf9, 0xbe, 0xb4, 0xd9, 0x6d, 0x65, 0x6d, 0x70,
|
|
|
|
0x6f, 0x6f, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x5d, 0xf6, 0xe0, 0xe2]);
|
2015-11-22 23:28:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 15:14:12 +00:00
|
|
|
#[test]
|
|
|
|
fn serialize_getaddr_test() {
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
assert_eq!(serialize(&RawNetworkMessage { magic: 0xd9b4bef9, payload: NetworkMessage::GetAddr }),
|
|
|
|
vec![0xf9, 0xbe, 0xb4, 0xd9, 0x67, 0x65, 0x74, 0x61,
|
|
|
|
0x64, 0x64, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x5d, 0xf6, 0xe0, 0xe2]);
|
2016-10-10 15:14:12 +00:00
|
|
|
}
|
|
|
|
|
Better RawNewtorkMessage deserealization from IO stream (#231)
Follow-up to https://github.com/rust-bitcoin/rust-bitcoin/pull/229
While working with remote peers over the network it is required to deserealize RawNetworkMessage from `TCPStream` to read the incoming messages. These messages can be partial – or one TCP packet can contain few of them. To make the library usable for such use cases, I have implemented the required functionality and covered it with unit tests.
Sample usage:
```rust
fn run() -> Result<(), Error> {
// Opening stream to the remote bitcoind peer
let mut stream = TcpStream::connect(SocketAddr::from(([37, 187, 0, 47], 8333));
let start = SystemTime::now();
// Constructing and sending `version` message to get some messages back from the remote peer
let since_the_epoch = start.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let version_msg = message::RawNetworkMessage {
magic: constants::Network::Bitcoin.magic(),
payload: message::NetworkMessage::Version(message_network::VersionMessage::new(
0,
since_the_epoch.as_secs() as i64,
address::Address::new(receiver, 0),
address::Address::new(receiver, 0),
0,
String::from("macx0r"),
0
))
};
stream.write(encode::serialize(&version_msg).as_slice())?;
// Receiving incoming messages
let mut buffer = vec![];
loop {
let result = StreamReader::new(&mut stream, None).read_messages();
if let Err(err) = result {
stream.shutdown(Shutdown::Both)?;
return Err(Error::DataError(err))
}
for msg in result.unwrap() {
println!("Received message: {:?}", msg.payload);
}
}
}
```
Sample output is the following:
```
Received message: Version(VersionMessage { version: 70015, services: 1037, timestamp: 1548637162, receiver: Address {services: 0, address: [0, 0, 0, 0, 0, 65535, 23536, 35968], port: 33716}, sender: Address {services: 1037, address: [0, 0, 0, 0, 0, 0, 0, 0], port: 0}, nonce: 1370726880972892633, user_agent: "/Satoshi:0.17.99/", start_height: 560412, relay: true })
Received message: Verack
Received message: Alert([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 127, 0, 0, 0, 0, 255, 255, 255, 127, 254, 255, 255, 127, 1, 255, 255, 255, 127, 0, 0, 0, 0, 255, 255, 255, 127, 0, 255, 255, 255, 127, 0, 47, 85, 82, 71, 69, 78, 84, 58, 32, 65, 108, 101, 114, 116, 32, 107, 101, 121, 32, 99, 111, 109, 112, 114, 111, 109, 105, 115, 101, 100, 44, 32, 117, 112, 103, 114, 97, 100, 101, 32, 114, 101, 113, 117, 105, 114, 101, 100, 0])
```
Working sample code can be found here: https://github.com/dr-orlovsky/bitcoinbigdata-netlistener
2019-02-27 21:41:28 +00:00
|
|
|
#[test]
|
|
|
|
fn deserialize_getaddr_test() {
|
|
|
|
let msg = deserialize(
|
|
|
|
&[0xf9, 0xbe, 0xb4, 0xd9, 0x67, 0x65, 0x74, 0x61,
|
|
|
|
0x64, 0x64, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x5d, 0xf6, 0xe0, 0xe2]);
|
|
|
|
let preimage = RawNetworkMessage { magic: 0xd9b4bef9, payload: NetworkMessage::GetAddr };
|
|
|
|
assert!(msg.is_ok());
|
|
|
|
let msg : RawNetworkMessage = msg.unwrap();
|
|
|
|
assert_eq!(preimage.magic, msg.magic);
|
|
|
|
assert_eq!(preimage.payload, msg.payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_version_test() {
|
|
|
|
let msg = deserialize::<RawNetworkMessage>(
|
|
|
|
&[ 0xf9, 0xbe, 0xb4, 0xd9, 0x76, 0x65, 0x72, 0x73,
|
|
|
|
0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x66, 0x00, 0x00, 0x00, 0xbe, 0x61, 0xb8, 0x27,
|
|
|
|
0x7f, 0x11, 0x01, 0x00, 0x0d, 0x04, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0xf0, 0x0f, 0x4d, 0x5c,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
|
|
|
|
0x5b, 0xf0, 0x8c, 0x80, 0xb4, 0xbd, 0x0d, 0x04,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0xfa, 0xa9, 0x95, 0x59, 0xcc, 0x68, 0xa1, 0xc1,
|
|
|
|
0x10, 0x2f, 0x53, 0x61, 0x74, 0x6f, 0x73, 0x68,
|
|
|
|
0x69, 0x3a, 0x30, 0x2e, 0x31, 0x37, 0x2e, 0x31,
|
|
|
|
0x2f, 0x93, 0x8c, 0x08, 0x00, 0x01 ]);
|
|
|
|
|
|
|
|
assert!(msg.is_ok());
|
|
|
|
let msg = msg.unwrap();
|
|
|
|
assert_eq!(msg.magic, 0xd9b4bef9);
|
|
|
|
if let NetworkMessage::Version(version_msg) = msg.payload {
|
|
|
|
assert_eq!(version_msg.version, 70015);
|
2019-12-11 07:32:47 +00:00
|
|
|
assert_eq!(version_msg.services, ServiceFlags::NETWORK | ServiceFlags::BLOOM | ServiceFlags::WITNESS | ServiceFlags::NETWORK_LIMITED);
|
Better RawNewtorkMessage deserealization from IO stream (#231)
Follow-up to https://github.com/rust-bitcoin/rust-bitcoin/pull/229
While working with remote peers over the network it is required to deserealize RawNetworkMessage from `TCPStream` to read the incoming messages. These messages can be partial – or one TCP packet can contain few of them. To make the library usable for such use cases, I have implemented the required functionality and covered it with unit tests.
Sample usage:
```rust
fn run() -> Result<(), Error> {
// Opening stream to the remote bitcoind peer
let mut stream = TcpStream::connect(SocketAddr::from(([37, 187, 0, 47], 8333));
let start = SystemTime::now();
// Constructing and sending `version` message to get some messages back from the remote peer
let since_the_epoch = start.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let version_msg = message::RawNetworkMessage {
magic: constants::Network::Bitcoin.magic(),
payload: message::NetworkMessage::Version(message_network::VersionMessage::new(
0,
since_the_epoch.as_secs() as i64,
address::Address::new(receiver, 0),
address::Address::new(receiver, 0),
0,
String::from("macx0r"),
0
))
};
stream.write(encode::serialize(&version_msg).as_slice())?;
// Receiving incoming messages
let mut buffer = vec![];
loop {
let result = StreamReader::new(&mut stream, None).read_messages();
if let Err(err) = result {
stream.shutdown(Shutdown::Both)?;
return Err(Error::DataError(err))
}
for msg in result.unwrap() {
println!("Received message: {:?}", msg.payload);
}
}
}
```
Sample output is the following:
```
Received message: Version(VersionMessage { version: 70015, services: 1037, timestamp: 1548637162, receiver: Address {services: 0, address: [0, 0, 0, 0, 0, 65535, 23536, 35968], port: 33716}, sender: Address {services: 1037, address: [0, 0, 0, 0, 0, 0, 0, 0], port: 0}, nonce: 1370726880972892633, user_agent: "/Satoshi:0.17.99/", start_height: 560412, relay: true })
Received message: Verack
Received message: Alert([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 127, 0, 0, 0, 0, 255, 255, 255, 127, 254, 255, 255, 127, 1, 255, 255, 255, 127, 0, 0, 0, 0, 255, 255, 255, 127, 0, 255, 255, 255, 127, 0, 47, 85, 82, 71, 69, 78, 84, 58, 32, 65, 108, 101, 114, 116, 32, 107, 101, 121, 32, 99, 111, 109, 112, 114, 111, 109, 105, 115, 101, 100, 44, 32, 117, 112, 103, 114, 97, 100, 101, 32, 114, 101, 113, 117, 105, 114, 101, 100, 0])
```
Working sample code can be found here: https://github.com/dr-orlovsky/bitcoinbigdata-netlistener
2019-02-27 21:41:28 +00:00
|
|
|
assert_eq!(version_msg.timestamp, 1548554224);
|
|
|
|
assert_eq!(version_msg.nonce, 13952548347456104954);
|
|
|
|
assert_eq!(version_msg.user_agent, "/Satoshi:0.17.1/");
|
|
|
|
assert_eq!(version_msg.start_height, 560275);
|
|
|
|
assert_eq!(version_msg.relay, true);
|
|
|
|
} else {
|
|
|
|
panic!("Wrong message type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_partial_message_test() {
|
|
|
|
let data = [ 0xf9, 0xbe, 0xb4, 0xd9, 0x76, 0x65, 0x72, 0x73,
|
|
|
|
0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x66, 0x00, 0x00, 0x00, 0xbe, 0x61, 0xb8, 0x27,
|
|
|
|
0x7f, 0x11, 0x01, 0x00, 0x0d, 0x04, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0xf0, 0x0f, 0x4d, 0x5c,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
|
|
|
|
0x5b, 0xf0, 0x8c, 0x80, 0xb4, 0xbd, 0x0d, 0x04,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0xfa, 0xa9, 0x95, 0x59, 0xcc, 0x68, 0xa1, 0xc1,
|
|
|
|
0x10, 0x2f, 0x53, 0x61, 0x74, 0x6f, 0x73, 0x68,
|
|
|
|
0x69, 0x3a, 0x30, 0x2e, 0x31, 0x37, 0x2e, 0x31,
|
|
|
|
0x2f, 0x93, 0x8c, 0x08, 0x00, 0x01, 0, 0 ];
|
|
|
|
let msg = deserialize_partial::<RawNetworkMessage>(&data);
|
|
|
|
assert!(msg.is_ok());
|
|
|
|
|
|
|
|
let (msg, consumed) = msg.unwrap();
|
|
|
|
assert_eq!(consumed, data.to_vec().len() - 2);
|
|
|
|
assert_eq!(msg.magic, 0xd9b4bef9);
|
|
|
|
if let NetworkMessage::Version(version_msg) = msg.payload {
|
|
|
|
assert_eq!(version_msg.version, 70015);
|
2019-12-11 07:32:47 +00:00
|
|
|
assert_eq!(version_msg.services, ServiceFlags::NETWORK | ServiceFlags::BLOOM | ServiceFlags::WITNESS | ServiceFlags::NETWORK_LIMITED);
|
Better RawNewtorkMessage deserealization from IO stream (#231)
Follow-up to https://github.com/rust-bitcoin/rust-bitcoin/pull/229
While working with remote peers over the network it is required to deserealize RawNetworkMessage from `TCPStream` to read the incoming messages. These messages can be partial – or one TCP packet can contain few of them. To make the library usable for such use cases, I have implemented the required functionality and covered it with unit tests.
Sample usage:
```rust
fn run() -> Result<(), Error> {
// Opening stream to the remote bitcoind peer
let mut stream = TcpStream::connect(SocketAddr::from(([37, 187, 0, 47], 8333));
let start = SystemTime::now();
// Constructing and sending `version` message to get some messages back from the remote peer
let since_the_epoch = start.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let version_msg = message::RawNetworkMessage {
magic: constants::Network::Bitcoin.magic(),
payload: message::NetworkMessage::Version(message_network::VersionMessage::new(
0,
since_the_epoch.as_secs() as i64,
address::Address::new(receiver, 0),
address::Address::new(receiver, 0),
0,
String::from("macx0r"),
0
))
};
stream.write(encode::serialize(&version_msg).as_slice())?;
// Receiving incoming messages
let mut buffer = vec![];
loop {
let result = StreamReader::new(&mut stream, None).read_messages();
if let Err(err) = result {
stream.shutdown(Shutdown::Both)?;
return Err(Error::DataError(err))
}
for msg in result.unwrap() {
println!("Received message: {:?}", msg.payload);
}
}
}
```
Sample output is the following:
```
Received message: Version(VersionMessage { version: 70015, services: 1037, timestamp: 1548637162, receiver: Address {services: 0, address: [0, 0, 0, 0, 0, 65535, 23536, 35968], port: 33716}, sender: Address {services: 1037, address: [0, 0, 0, 0, 0, 0, 0, 0], port: 0}, nonce: 1370726880972892633, user_agent: "/Satoshi:0.17.99/", start_height: 560412, relay: true })
Received message: Verack
Received message: Alert([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 127, 0, 0, 0, 0, 255, 255, 255, 127, 254, 255, 255, 127, 1, 255, 255, 255, 127, 0, 0, 0, 0, 255, 255, 255, 127, 0, 255, 255, 255, 127, 0, 47, 85, 82, 71, 69, 78, 84, 58, 32, 65, 108, 101, 114, 116, 32, 107, 101, 121, 32, 99, 111, 109, 112, 114, 111, 109, 105, 115, 101, 100, 44, 32, 117, 112, 103, 114, 97, 100, 101, 32, 114, 101, 113, 117, 105, 114, 101, 100, 0])
```
Working sample code can be found here: https://github.com/dr-orlovsky/bitcoinbigdata-netlistener
2019-02-27 21:41:28 +00:00
|
|
|
assert_eq!(version_msg.timestamp, 1548554224);
|
|
|
|
assert_eq!(version_msg.nonce, 13952548347456104954);
|
|
|
|
assert_eq!(version_msg.user_agent, "/Satoshi:0.17.1/");
|
|
|
|
assert_eq!(version_msg.start_height, 560275);
|
|
|
|
assert_eq!(version_msg.relay, true);
|
|
|
|
} else {
|
|
|
|
panic!("Wrong message type");
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|