-
Notifications
You must be signed in to change notification settings - Fork 831
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
@@ -117,7 +118,18 @@ pub enum NetworkMessage { | |
Pong(u64), | ||
// TODO: reject, | ||
// TODO: bloom filtering | ||
// TODO: alert | ||
/// 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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about moving these to a separate enum, say... |
||
/// `alert` | ||
Alert(Vec<u8>) | ||
} | ||
|
@@ -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() | ||
} | ||
|
@@ -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 | ||
|
@@ -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)), | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//! | ||
//! BIP157 Client Side Block Filtering network messages | ||
//! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all comments are copy-paste from BIP157 tables, not my language. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); |
There was a problem hiding this comment.
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)