8000 add BIP157 (Client Side Block Filtering) Messages by tamasblummer · Pull Request #225 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add BIP157 (Client Side Block Filtering) Messages #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/network/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use blockdata::transaction;
use network::address::Address;
use network::message_network;
use network::message_blockdata;
use network::message_filter;
use consensus::encode::{Decodable, Encodable};
use consensus::encode::CheckedData;
use consensus::encode::{self, serialize, Encoder, Decoder};
Expand Down Expand Up @@ -117,7 +118,18 @@ pub enum NetworkMessage {
Pong(u64),
// TODO: reject,
// TODO: bloom filtering
// TODO: alert
/// BIP157 getcfilters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe add message names between commas, same as other messages (for documentation underlying)

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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving these to a separate enum, say... network::message::ClientFilter?

/// `alert`
Alert(Vec<u8>)
}
Expand All @@ -141,6 +153,12 @@ impl RawNetworkMessage {
NetworkMessage::GetAddr => "getaddr",
NetworkMessage::Ping(_) => "ping",
NetworkMessage::Pong(_) => "pong",
NetworkMessage::GetCFilters(_) => "getcfilters",
NetworkMessage::CFilter(_) => "cfilter",
NetworkMessage::GetCFHeaders(_) => "getcfheaders",
NetworkMessage::CFHeaders(_) => "cfheaders",
NetworkMessage::GetCFCheckpt(_) => "getcfckpt",
NetworkMessage::CFCheckpt(_) => "cfcheckpt",
NetworkMessage::Alert(_) => "alert",
}.to_owned()
}
Expand All @@ -163,6 +181,12 @@ impl<S: Encoder> Encodable<S> for RawNetworkMessage {
NetworkMessage::Headers(ref dat) => serialize(dat),
NetworkMessage::Ping(ref dat) => serialize(dat),
NetworkMessage::Pong(ref dat) => serialize(dat),
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),
NetworkMessage::Alert(ref dat) => serialize(dat),
NetworkMessage::Verack
| NetworkMessage::MemPool
Expand Down Expand Up @@ -194,6 +218,12 @@ impl<D: Decoder> Decodable<D> for RawNetworkMessage {
"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)?),
"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)?),
"getcfckpt" => NetworkMessage::GetCFCheckpt(Decodable::consensus_decode(&mut mem_d)?),
"cfcheckpt" => NetworkMessage::CFCheckpt(Decodable::consensus_decode(&mut mem_d)?),
"alert" => NetworkMessage::Alert(Decodable::consensus_decode(&mut mem_d)?),
_ => return Err(encode::Error::UnrecognizedNetworkCommand(cmd)),
};
Expand Down
76 changes: 76 additions & 0 deletions src/network/message_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//!
//! BIP157 Client Side Block Filtering network messages
//!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe add a comment a la message_blockdata: "This module describes network messages which are used for passing block filters and filter headers inside Bitcoin P2P layer"

use bitcoin_hashes::sha256d;

#[derive(PartialEq, Eq, Clone, Debug)]
/// getcfilters message
pub struct GetCFilters {
/// Filter type for which headers are requested
pub filter_type: u8,
/// The height of the first block in the requested range
pub start_height: u32,
/// The hash of the last block in the requested range
pub stop_hash: sha256d::Hash,
}
impl_consensus_encoding!(GetCFilters, filter_type, start_height, stop_hash);

#[derive(PartialEq, Eq, Clone, Debug)]
/// cfilter message
pub struct CFilter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, on cfilter message, reading the BIP, "The FilterType SHOULD match the field in the getcfilters request, and BlockHash must correspond to a block that is an ancestor of StopHash with height greater than or equal to StartHeight.". A nit, but I think there the BIP is ambiguous, BlockHash could be also a block hash that is StopHash itself so not only its ancestor ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all comments are copy-paste from BIP157 tables, not my language.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one isn't an issue in PR but one in the BIP itself, I was curious to have your advice on it ?

/// Byte identifying the type of filter being returned
pub filter_type: u8,
/// Block hash of the Bitcoin block for which the filter is being returned
pub block_hash: sha256d::Hash,
/// The serialized compact filter for this block
pub filter: Vec<u8>,
}
impl_consensus_encoding!(CFilter, filter_type, block_hash, filter);

#[derive(PartialEq, Eq, Clone, Debug)]
/// getcfheaders message
pub struct GetCFHeaders {
/// Byte identifying the type of filter being returned
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Filter type for which headers are requested"

pub filter_type: u8,
/// The height of the first block in the requested range
pub start_height: u32,
/// The hash of the last block in the requested range
pub stop_hash: sha256d::Hash,
}
impl_consensus_encoding!(GetCFHeaders, filter_type, start_height, stop_hash);

#[derive(PartialEq, Eq, Clone, Debug)]
/// cfheaders message
pub struct CFHeaders {
/// Filter type for which headers are requested
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Filter type for which hashes are requested"

pub filter_type: u8,
/// The hash of the last block in the requested range
pub stop_hash: sha256d::Hash,
/// The filter header preceding the first block in the requested range
pub previous_filter: sha256d::Hash,
/// The filter hashes for each block in the requested range
pub filter_hashes: Vec<sha256d::Hash>,
}
impl_consensus_encoding!(CFHeaders, filter_type, stop_hash, previous_filter, filter_hashes);

#[derive(PartialEq, Eq, Clone, Debug)]
/// getcfcheckpt message
pub struct GetCFCheckpt {
/// Filter type for which headers are requested
pub filter_type: u8,
/// The hash of the last block in the requested range
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The hash of the last block in the chain that headers are requested for"

pub stop_hash: sha256d::Hash,
}
impl_consensus_encoding!(GetCFCheckpt, filter_type, stop_hash);

#[derive(PartialEq, Eq, Clone, Debug)]
/// cfcheckpt message
pub struct CFCheckpt {
/// Filter type for which headers are requested
pub filter_type: u8,
/// The hash of the last block in the requested range
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The hash of the last block in the chain that headers are requested for"

pub stop_hash: sha256d::Hash,
/// The filter headers at intervals of 1,000
pub filter_headers: Vec<sha256d::Hash>,
}
impl_consensus_encoding!(CFCheckpt, filter_type, stop_hash, filter_headers);
1 change: 1 addition & 0 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub mod address;
pub mod message;
pub mod message_blockdata;
pub mod message_network;
pub mod message_filter;

/// Network error
#[derive(Debug)]
Expand Down
0