Skip to content

Commit

Permalink
BIP152: Add Compact Blocks network messages
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose authored and 0xB10C committed Jul 12, 2022
1 parent 6ef1b59 commit b90cec7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/network/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::network::address::{Address, AddrV2Message};
use crate::network::{message_network, message_bloom};
use crate::network::message_blockdata;
use crate::network::message_filter;
use crate::network::message_compact_blocks;
use crate::consensus::encode::{CheckedData, Decodable, Encodable, VarInt};
use crate::consensus::{encode, serialize};
use crate::util::merkleblock::MerkleBlock;
Expand Down Expand Up @@ -192,6 +193,14 @@ pub enum NetworkMessage {
GetCFCheckpt(message_filter::GetCFCheckpt),
/// BIP157 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(Vec<u8>),
/// `reject`
Expand Down Expand Up @@ -248,6 +257,10 @@ impl NetworkMessage {
NetworkMessage::CFHeaders(_) => "cfheaders",
NetworkMessage::GetCFCheckpt(_) => "getcfcheckpt",
NetworkMessage::CFCheckpt(_) => "cfcheckpt",
NetworkMessage::SendCmpct(_) => "sendcmpct",
NetworkMessage::CmpctBlock(_) => "cmpctblock",
NetworkMessage::GetBlockTxn(_) => "getblocktxn",
NetworkMessage::BlockTxn(_) => "blocktxn",
NetworkMessage::Alert(_) => "alert",
NetworkMessage::Reject(_) => "reject",
NetworkMessage::FeeFilter(_) => "feefilter",
Expand Down Expand Up @@ -325,6 +338,10 @@ impl Encodable for RawNetworkMessage {
NetworkMessage::CFHeaders(ref dat) => serialize(dat),
NetworkMessage::GetCFCheckpt(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::Reject(ref dat) => serialize(dat),
NetworkMessage::FeeFilter(ref data) => serialize(data),
Expand Down Expand Up @@ -405,6 +422,10 @@ impl Decodable for RawNetworkMessage {
"reject" => NetworkMessage::Reject(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)?),
"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,
"addrv2" => NetworkMessage::AddrV2(Decodable::consensus_decode_from_finite_reader(&mut mem_d)?),
"sendaddrv2" => NetworkMessage::SendAddrV2,
Expand Down
4 changes: 4 additions & 0 deletions src/network/message_blockdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum Inventory {
Transaction(Txid),
/// Block
Block(BlockHash),
/// Compact Block
CompactBlock(BlockHash),
/// Witness Transaction by Wtxid
WTx(Wtxid),
/// Witness Transaction
Expand Down Expand Up @@ -64,6 +66,7 @@ impl Encodable for Inventory {
Inventory::Error => encode_inv!(0, sha256d::Hash::default()),
Inventory::Transaction(ref t) => encode_inv!(1, t),
Inventory::Block(ref b) => encode_inv!(2, b),
Inventory::CompactBlock(ref b) => encode_inv!(4, b),
Inventory::WTx(w) => encode_inv!(5, w),
Inventory::WitnessTransaction(ref t) => encode_inv!(0x40000001, t),
Inventory::WitnessBlock(ref b) => encode_inv!(0x40000002, b),
Expand All @@ -80,6 +83,7 @@ impl Decodable for Inventory {
0 => Inventory::Error,
1 => Inventory::Transaction(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)?),
0x40000001 => Inventory::WitnessTransaction(Decodable::consensus_decode(r)?),
0x40000002 => Inventory::WitnessBlock(Decodable::consensus_decode(r)?),
Expand Down
44 changes: 44 additions & 0 deletions src/network/message_compact_blocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//!
//! BIP152 Compact Blocks network messages
//!

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);
3 changes: 3 additions & 0 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub mod message_blockdata;
pub mod message_bloom;
#[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;
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
Expand Down

0 comments on commit b90cec7

Please sign in to comment.