BIP152: Add Compact Blocks network messages

This commit is contained in:
Steven Roose 2019-03-24 15:21:44 +00:00 committed by 0xb10c
parent f2fcdc86e6
commit d4d92a838e
No known key found for this signature in database
GPG Key ID: E2FFD5B1D88CA97D
4 changed files with 73 additions and 0 deletions

View File

@ -19,6 +19,7 @@ use crate::network::address::{Address, AddrV2Message};
use crate::network::{message_network, message_bloom}; use crate::network::{message_network, message_bloom};
use crate::network::message_blockdata; use crate::network::message_blockdata;
use crate::network::message_filter; use crate::network::message_filter;
use crate::network::message_compact_blocks;
use crate::consensus::encode::{CheckedData, Decodable, Encodable, VarInt}; use crate::consensus::encode::{CheckedData, Decodable, Encodable, VarInt};
use crate::consensus::{encode, serialize}; use crate::consensus::{encode, serialize};
use crate::util::merkleblock::MerkleBlock; use crate::util::merkleblock::MerkleBlock;
@ -181,6 +182,14 @@ pub enum NetworkMessage {
GetCFCheckpt(message_filter::GetCFCheckpt), GetCFCheckpt(message_filter::GetCFCheckpt),
/// BIP157 cfcheckpt /// BIP157 cfcheckpt
CFCheckpt(message_filter::CFCheckpt), CFCheckpt(message_filter::CFCheckpt),
/// BIP152 sendcmpct
SendCmpct(message_compact_blocks::SendCmpct),
/// BIP152 cmpctblock
CmpctBlock(message_compact_blocks::CmpctBlock),
/// BIP152 getblocktxn
GetBlockTxn(message_compact_blocks::GetBlockTxn),
/// BIP152 blocktxn
BlockTxn(message_compact_blocks::BlockTxn),
/// `alert` /// `alert`
Alert(Vec<u8>), Alert(Vec<u8>),
/// `reject` /// `reject`
@ -237,6 +246,10 @@ impl NetworkMessage {
NetworkMessage::CFHeaders(_) => "cfheaders", NetworkMessage::CFHeaders(_) => "cfheaders",
NetworkMessage::GetCFCheckpt(_) => "getcfcheckpt", NetworkMessage::GetCFCheckpt(_) => "getcfcheckpt",
NetworkMessage::CFCheckpt(_) => "cfcheckpt", NetworkMessage::CFCheckpt(_) => "cfcheckpt",
NetworkMessage::SendCmpct(_) => "sendcmpct",
NetworkMessage::CmpctBlock(_) => "cmpctblock",
NetworkMessage::GetBlockTxn(_) => "getblocktxn",
NetworkMessage::BlockTxn(_) => "blocktxn",
NetworkMessage::Alert(_) => "alert", NetworkMessage::Alert(_) => "alert",
NetworkMessage::Reject(_) => "reject", NetworkMessage::Reject(_) => "reject",
NetworkMessage::FeeFilter(_) => "feefilter", NetworkMessage::FeeFilter(_) => "feefilter",
@ -314,6 +327,10 @@ impl Encodable for RawNetworkMessage {
NetworkMessage::CFHeaders(ref dat) => serialize(dat), NetworkMessage::CFHeaders(ref dat) => serialize(dat),
NetworkMessage::GetCFCheckpt(ref dat) => serialize(dat), NetworkMessage::GetCFCheckpt(ref dat) => serialize(dat),
NetworkMessage::CFCheckpt(ref dat) => serialize(dat), NetworkMessage::CFCheckpt(ref dat) => serialize(dat),
NetworkMessage::SendCmpct(ref dat) => serialize(dat),
NetworkMessage::CmpctBlock(ref dat) => serialize(dat),
NetworkMessage::GetBlockTxn(ref dat) => serialize(dat),
NetworkMessage::BlockTxn(ref dat) => serialize(dat),
NetworkMessage::Alert(ref dat) => serialize(dat), NetworkMessage::Alert(ref dat) => serialize(dat),
NetworkMessage::Reject(ref dat) => serialize(dat), NetworkMessage::Reject(ref dat) => serialize(dat),
NetworkMessage::FeeFilter(ref data) => serialize(data), NetworkMessage::FeeFilter(ref data) => serialize(data),
@ -394,6 +411,10 @@ impl Decodable for RawNetworkMessage {
"reject" => NetworkMessage::Reject(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?), "reject" => NetworkMessage::Reject(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?),
"alert" => NetworkMessage::Alert(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?), "alert" => NetworkMessage::Alert(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?),
"feefilter" => NetworkMessage::FeeFilter(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?), "feefilter" => NetworkMessage::FeeFilter(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?),
"sendcmpct" => NetworkMessage::SendCmpct(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?),
"cmpctblock" => NetworkMessage::CmpctBlock(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?),
"getblocktxn" => NetworkMessage::GetBlockTxn(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?),
"blocktxn" => NetworkMessage::BlockTxn(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?),
"wtxidrelay" => NetworkMessage::WtxidRelay, "wtxidrelay" => NetworkMessage::WtxidRelay,
"addrv2" => NetworkMessage::AddrV2(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?), "addrv2" => NetworkMessage::AddrV2(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?),
"sendaddrv2" => NetworkMessage::SendAddrV2, "sendaddrv2" => NetworkMessage::SendAddrV2,

View File

@ -27,6 +27,8 @@ pub enum Inventory {
Transaction(Txid), Transaction(Txid),
/// Block /// Block
Block(BlockHash), Block(BlockHash),
/// Compact Block
CompactBlock(BlockHash),
/// Witness Transaction by Wtxid /// Witness Transaction by Wtxid
WTx(Wtxid), WTx(Wtxid),
/// Witness Transaction /// Witness Transaction
@ -54,6 +56,7 @@ impl Encodable for Inventory {
Inventory::Error => encode_inv!(0, sha256d::Hash::all_zeros()), Inventory::Error => encode_inv!(0, sha256d::Hash::all_zeros()),
Inventory::Transaction(ref t) => encode_inv!(1, t), Inventory::Transaction(ref t) => encode_inv!(1, t),
Inventory::Block(ref b) => encode_inv!(2, b), Inventory::Block(ref b) => encode_inv!(2, b),
Inventory::CompactBlock(ref b) => encode_inv!(4, b),
Inventory::WTx(w) => encode_inv!(5, w), Inventory::WTx(w) => encode_inv!(5, w),
Inventory::WitnessTransaction(ref t) => encode_inv!(0x40000001, t), Inventory::WitnessTransaction(ref t) => encode_inv!(0x40000001, t),
Inventory::WitnessBlock(ref b) => encode_inv!(0x40000002, b), Inventory::WitnessBlock(ref b) => encode_inv!(0x40000002, b),
@ -70,6 +73,7 @@ impl Decodable for Inventory {
0 => Inventory::Error, 0 => Inventory::Error,
1 => Inventory::Transaction(Decodable::consensus_decode(r)?), 1 => Inventory::Transaction(Decodable::consensus_decode(r)?),
2 => Inventory::Block(Decodable::consensus_decode(r)?), 2 => Inventory::Block(Decodable::consensus_decode(r)?),
4 => Inventory::CompactBlock(Decodable::consensus_decode(r)?),
5 => Inventory::WTx(Decodable::consensus_decode(r)?), 5 => Inventory::WTx(Decodable::consensus_decode(r)?),
0x40000001 => Inventory::WitnessTransaction(Decodable::consensus_decode(r)?), 0x40000001 => Inventory::WitnessTransaction(Decodable::consensus_decode(r)?),
0x40000002 => Inventory::WitnessBlock(Decodable::consensus_decode(r)?), 0x40000002 => Inventory::WitnessBlock(Decodable::consensus_decode(r)?),

View File

@ -0,0 +1,45 @@
//!
//! BIP152 Compact Blocks network messages
//!
use crate::internal_macros::impl_consensus_encoding;
use crate::util::bip152;
/// sendcmpct message
#[derive(PartialEq, Eq, Clone, Debug, Copy, PartialOrd, Ord, Hash)]
pub struct SendCmpct {
/// Request to be send compact blocks.
pub send_compact: bool,
/// Compact Blocks protocol version number.
pub version: u64,
}
impl_consensus_encoding!(SendCmpct, send_compact, version);
/// cmpctblock message
///
/// Note that the rules for validation before relaying compact blocks is
/// different from headers and regular block messages. Thus, you shouldn't use
/// compact blocks when relying on an upstream full node to have validated data
/// being forwarded to you.
#[derive(PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)]
pub struct CmpctBlock {
/// The Compact Block.
pub compact_block: bip152::HeaderAndShortIds,
}
impl_consensus_encoding!(CmpctBlock, compact_block);
/// getblocktxn message
#[derive(PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)]
pub struct GetBlockTxn {
/// The block transactions request.
pub txs_request: bip152::BlockTransactionsRequest,
}
impl_consensus_encoding!(GetBlockTxn, txs_request);
/// blocktxn message
#[derive(PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)]
pub struct BlockTxn {
/// The requested block transactions.
pub transactions: bip152::BlockTransactions,
}
impl_consensus_encoding!(BlockTxn, transactions);

View File

@ -26,6 +26,9 @@ pub mod message_blockdata;
pub mod message_bloom; pub mod message_bloom;
#[cfg(feature = "std")] #[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub mod message_compact_blocks;
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub mod message_network; pub mod message_network;
#[cfg(feature = "std")] #[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] #[cfg_attr(docsrs, doc(cfg(feature = "std")))]