diff --git a/zebra-network/src/protocol/external/types.rs b/zebra-network/src/protocol/external/types.rs index 022cbdbe4..fd81f3b79 100644 --- a/zebra-network/src/protocol/external/types.rs +++ b/zebra-network/src/protocol/external/types.rs @@ -1,5 +1,10 @@ use hex; -use std::fmt; +use std::{ + fmt, + io::{Read, Write}, +}; + +use zebra_chain::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}; /// A magic number identifying the network. #[derive(Copy, Clone, Eq, PartialEq)] @@ -42,6 +47,41 @@ impl Default for Nonce { } } +/// A random value to add to the seed value in a hash function. +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] +pub struct Tweak(pub u32); + +impl Default for Tweak { + fn default() -> Self { + use rand::{thread_rng, Rng}; + Self(thread_rng().gen()) + } +} + +/// A Bloom filter consisting of a bit field of arbitrary byte-aligned +/// size, maximum size is 36,000 bytes. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct Filter(pub Vec); + +impl ZcashSerialize for Filter { + fn zcash_serialize(&self, mut writer: W) -> Result<(), SerializationError> { + writer.write_all(&self.0)?; + Ok(()) + } +} + +impl ZcashDeserialize for Filter { + fn zcash_deserialize(reader: R) -> Result { + let mut bytes = Vec::new(); + + // Maximum size of a filter is 36,000 bytes. + let mut handle = reader.take(36000); + + handle.read(&mut bytes)?; + Ok(Self(bytes)) + } +} + #[cfg(test)] mod tests {