Merge rust-bitcoin/rust-bitcoin#580: Add Bloom filter network messages

894f0f09b6 Add Bloom filter network messages (Ben Carman)

Pull request description:

  Adds initial support for parsing the `merkleblock`, `filterload`, `filteradd` and `filterclear` network messages.

  https://developer.bitcoin.org/reference/p2p_networking.html#filteradd

  https://developer.bitcoin.org/reference/p2p_networking.html#filterclear

ACKs for top commit:
  dr-orlovsky:
    ACK 894f0f09b6
  apoelstra:
    ACK 894f0f09b6

Tree-SHA512: 6483457faf4e63fad0c9ac52b473c37fb04ad28ec60c92e5c3b5be7472d02fd873cbccfe387799002d1862b88bcdc2ded3a7b03349f3ac8fd591942dae005e6d
This commit is contained in:
Andrew Poelstra 2021-09-13 21:36:12 +00:00
commit 57d7baf05b
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 97 additions and 2 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,64 @@
//!
//! BIP37 Connection Bloom filtering network messages
//!
use consensus::encode;
use consensus::{Decodable, Encodable, ReadExt};
use std::io;
/// `filterload` message sets the current bloom filter
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FilterLoad {
/// The filter itself
pub filter: Vec<u8>,
/// The number of hash functions to use
pub hash_funcs: u32,
/// A random value
pub tweak: u32,
/// Controls how matched items are added to the filter
pub flags: BloomFlags,
}
impl_consensus_encoding!(FilterLoad, filter, hash_funcs, tweak, flags);
/// Bloom filter update flags
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum BloomFlags {
/// Never update the filter with outpoints.
None,
/// Always update the filter with outpoints.
All,
/// Only update the filter with outpoints if it is P2PK or P2MS
PubkeyOnly,
}
impl Encodable for BloomFlags {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
e.write_all(&[match self {
BloomFlags::None => 0,
BloomFlags::All => 1,
BloomFlags::PubkeyOnly => 2,
}])?;
Ok(1)
}
}
impl Decodable for BloomFlags {
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
Ok(match d.read_u8()? {
0 => BloomFlags::None,
1 => BloomFlags::All,
2 => BloomFlags::PubkeyOnly,
_ => return Err(encode::Error::ParseFailed("unknown bloom flag")),
})
}
}
/// `filteradd` message updates the current filter with new data
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FilterAdd {
/// The data element to add to the current filter.
pub data: Vec<u8>,
}
impl_consensus_encoding!(FilterAdd, data);

View File

@ -28,6 +28,7 @@ pub mod constants;
#[cfg(feature = "std")] pub use self::address::Address;
#[cfg(feature = "std")] pub mod message;
#[cfg(feature = "std")] pub mod message_blockdata;
#[cfg(feature = "std")] pub mod message_bloom;
#[cfg(feature = "std")] pub mod message_network;
#[cfg(feature = "std")] pub mod message_filter;
#[cfg(feature = "std")] pub mod stream_reader;