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 committed Oct 10, 2019
1 parent 471c190 commit d00f814
Show file tree
Hide file tree
Showing 4 changed files with 70 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 @@ -27,6 +27,7 @@ use blockdata::transaction;
use network::address::Address;
use network::message_network;
use network::message_blockdata;
use network::message_compact_blocks;
use network::message_filter;
use consensus::encode::{CheckedData, Decodable, Encodable, VarInt};
use consensus::{encode, serialize};
Expand Down Expand Up @@ -130,6 +131,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 @@ -162,6 +171,10 @@ impl RawNetworkMessage {
NetworkMessage::CFHeaders(_) => "cfheaders",
NetworkMessage::GetCFCheckpt(_) => "getcfckpt",
NetworkMessage::CFCheckpt(_) => "cfcheckpt",
NetworkMessage::SendCmpct(_) => "sendcmpct",
NetworkMessage::CmpctBlock(_) => "cmpctblock",
NetworkMessage::GetBlockTxn(_) => "getblocktxn",
NetworkMessage::BlockTxn(_) => "blocktxn",
NetworkMessage::Alert(_) => "alert",
NetworkMessage::Reject(_) => "reject",
}.to_owned()
Expand Down Expand Up @@ -213,6 +226,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::Verack
Expand Down Expand Up @@ -280,6 +297,10 @@ impl Decodable for RawNetworkMessage {
"getcfckpt" => NetworkMessage::GetCFCheckpt(Decodable::consensus_decode(&mut mem_d)?),
"cfcheckpt" => NetworkMessage::CFCheckpt(Decodable::consensus_decode(&mut mem_d)?),
"reject" => NetworkMessage::Reject(Decodable::consensus_decode(&mut mem_d)?),
"sendcmpct" => NetworkMessage::SendCmpct(Decodable::consensus_decode(&mut mem_d)?),
"cmpctblock" => NetworkMessage::CmpctBlock(Decodable::consensus_decode(&mut mem_d)?),
"getblocktxn" => NetworkMessage::GetBlockTxn(Decodable::consensus_decode(&mut mem_d)?),
"blocktxn" => NetworkMessage::BlockTxn(Decodable::consensus_decode(&mut mem_d)?),
"alert" => NetworkMessage::Alert(Decodable::consensus_decode(&mut mem_d)?),
_ => return Err(encode::Error::UnrecognizedNetworkCommand(cmd)),
};
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 @@ -33,6 +33,8 @@ pub enum InvType {
Transaction,
/// Block
Block,
/// Compact Block
CompactBlock,
/// Witness Block
WitnessBlock,
/// Witness Transaction
Expand Down Expand Up @@ -112,6 +114,7 @@ impl Encodable for Inventory {
InvType::Error => 0u32,
InvType::Transaction => 1,
InvType::Block => 2,
InvType::CompactBlock => 4,
InvType::WitnessBlock => 0x40000002,
InvType::WitnessTransaction => 0x40000001
}.consensus_encode(&mut s)?;
Expand All @@ -128,6 +131,7 @@ impl Decodable for Inventory {
0 => InvType::Error,
1 => InvType::Transaction,
2 => InvType::Block,
4 => InvType::CompactBlock,
// TODO do not fail here
_ => { panic!("bad inventory type field") }
},
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 util::bip152;

/// sendcmpct message
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct SendCmpct {
/// Request to be send compact blocks.
pub send_compact: bool,
/// Compact Blocks protocol version number.
pub version: u32,
}
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)]
pub struct CmpctBlock {
/// The Compact Block.
pub compact_block: bip152::HeaderAndShortIds,
}
impl_consensus_encoding!(CmpctBlock, compact_block);

/// getblocktxn message
#[derive(PartialEq, Eq, Clone, Debug)]
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)]
pub struct BlockTxn {
/// The requested block transactions.
pub transactions: bip152::BlockTransactions,
}
impl_consensus_encoding!(BlockTxn, transactions);
1 change: 1 addition & 0 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod constants;
pub mod address;
pub mod message;
pub mod message_blockdata;
pub mod message_compact_blocks;
pub mod message_network;
pub mod message_filter;
pub mod stream_reader;
Expand Down

0 comments on commit d00f814

Please sign in to comment.