sapio_bitcoin/network/
message_bloom.rs

1//! Bitcoin Connection Bloom filtering network messages.
2//!
3//! This module describes BIP37 Connection Bloom filtering network messages.
4//!
5
6use consensus::encode;
7use consensus::{Decodable, Encodable, ReadExt};
8use std::io;
9
10/// `filterload` message sets the current bloom filter
11#[derive(Clone, PartialEq, Eq, Debug)]
12pub struct FilterLoad {
13    /// The filter itself
14    pub filter: Vec<u8>,
15    /// The number of hash functions to use
16    pub hash_funcs: u32,
17    /// A random value
18    pub tweak: u32,
19    /// Controls how matched items are added to the filter
20    pub flags: BloomFlags,
21}
22
23impl_consensus_encoding!(FilterLoad, filter, hash_funcs, tweak, flags);
24
25/// Bloom filter update flags
26#[derive(Debug, Copy, Clone, Eq, PartialEq)]
27pub enum BloomFlags {
28    /// Never update the filter with outpoints.
29    None,
30    /// Always update the filter with outpoints.
31    All,
32    /// Only update the filter with outpoints if it is P2PK or P2MS
33    PubkeyOnly,
34}
35
36impl Encodable for BloomFlags {
37    fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
38        e.write_all(&[match self {
39            BloomFlags::None => 0,
40            BloomFlags::All => 1,
41            BloomFlags::PubkeyOnly => 2,
42        }])?;
43        Ok(1)
44    }
45}
46
47impl Decodable for BloomFlags {
48    fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
49        Ok(match d.read_u8()? {
50            0 => BloomFlags::None,
51            1 => BloomFlags::All,
52            2 => BloomFlags::PubkeyOnly,
53            _ => return Err(encode::Error::ParseFailed("unknown bloom flag")),
54        })
55    }
56}
57
58/// `filteradd` message updates the current filter with new data
59#[derive(Clone, PartialEq, Eq, Debug)]
60pub struct FilterAdd {
61    /// The data element to add to the current filter.
62    pub data: Vec<u8>,
63}
64
65impl_consensus_encoding!(FilterAdd, data);