From c21c6a879137db8eabc052b757b85dfb7e941206 Mon Sep 17 00:00:00 2001 From: Davidson Souza Date: Mon, 6 Nov 2023 16:18:04 -0300 Subject: [PATCH] Move block filters to a dedicated module --- Cargo.lock | 10 +++++ Cargo.toml | 1 + crates/floresta-compact-filters/Cargo.toml | 17 +++++++++ .../src/lib.rs} | 37 +++++-------------- crates/floresta-watch-only/src/lib.rs | 1 - 5 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 crates/floresta-compact-filters/Cargo.toml rename crates/{floresta-watch-only/src/block_filter.rs => floresta-compact-filters/src/lib.rs} (92%) diff --git a/Cargo.lock b/Cargo.lock index e3f67db6..a1200e3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -747,6 +747,16 @@ dependencies = [ "sha2", ] +[[package]] +name = "floresta-compact-filters" +version = "0.1.0" +dependencies = [ + "bitcoin 0.29.2", + "bitcoin_hashes 0.11.0", + "floresta-common", + "kv", +] + [[package]] name = "floresta-electrum" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index ac07897f..bd3b61f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "crates/floresta-chain", "crates/floresta-cli", "crates/floresta-common", + "crates/floresta-compact-filters", "crates/floresta-electrum", "crates/floresta-watch-only", "crates/floresta-wire", diff --git a/crates/floresta-compact-filters/Cargo.toml b/crates/floresta-compact-filters/Cargo.toml new file mode 100644 index 00000000..224e01eb --- /dev/null +++ b/crates/floresta-compact-filters/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "floresta-compact-filters" +version = "0.1.0" +authors = ["Davidson Sousa "] +edition = "2018" +homepage = "https://github.com/davidson-souza/floresta" +repository = "https://github.com/davidson-souza/floresta" +description = """ + BIP158 compact filters for easy rescan of the blockchain in + light clients. + """ + +[dependencies] +bitcoin_hashes = "0.11.0" +bitcoin = "0.29" +floresta-common = { path = "../floresta-common" } +kv = "0.24.0" diff --git a/crates/floresta-watch-only/src/block_filter.rs b/crates/floresta-compact-filters/src/lib.rs similarity index 92% rename from crates/floresta-watch-only/src/block_filter.rs rename to crates/floresta-compact-filters/src/lib.rs index 6c67f68a..ccde8ea6 100644 --- a/crates/floresta-watch-only/src/block_filter.rs +++ b/crates/floresta-compact-filters/src/lib.rs @@ -1,22 +1,3 @@ -// SPDX-License-Identifier: MIT - -//! A module that uses BIP-158 block filters to sync our wallet - -//! BIP-158 client-side filters are a special probabilistic filter that can -//! quasi-succinctly represent a set of elements, determine whether an element -//! is present in the original set by querying the filter itself. -//! -//! As a probabilistic filter, it has false-positives, i.e., elements that aren't in the set returning -//! true. The false-positive rate can be tweaked and made manageable. -//! -//! This module doesn't use BIP-157 to download block filters from peers; instead, we build -//! them locally, using the actual blocks. This gives us protection against malicious peers -//! and control over what is indexed, reducing disk and CPU usage as required by our user. -//! -//! You can build a filter by calling `filter_block`, and query our blocks by calling -//! `match_any`. The latter method returns a list of block heights where our filter queries -//! returned true; you should then download those blocks and check if there's something we are interested in. - use std::io::Write; use bitcoin::{ @@ -84,7 +65,7 @@ pub struct BlockFilterBackend { k0: u64, /// The second half of the siphash key k1: u64, - /// A block hash used to salt the siphash, we use a random hash instead of + /// A block hash used to salt the siphash, we use a random hash instead of /// an actual block hash key: BlockHash, } @@ -111,7 +92,7 @@ impl BlockFilterBackend { pub fn new(storage: Box, key: BlockHash) -> BlockFilterBackend { let mut k0 = [0_u8; 8]; let mut k1 = [0_u8; 8]; - + k0.copy_from_slice(&key[0..8]); k1.copy_from_slice(&key[8..16]); @@ -213,7 +194,7 @@ pub struct FilterBackendBuilder { whitelisted_outputs: u8, index_input: bool, index_txids: bool, - key: [u8; 32] + key: [u8; 32], } impl FilterBackendBuilder { @@ -256,7 +237,7 @@ impl FilterBackendBuilder { } /// A key used by siphash /// - /// BIP-158 uses the block hash, but we use a fixed by here, so we don't + /// BIP-158 uses the block hash, but we use a fixed by here, so we don't /// need to access chaindata on query pub fn key_hash(&mut self, key: [u8; 32]) -> &mut Self { self.key = key; @@ -269,10 +250,10 @@ impl FilterBackendBuilder { pub fn build(self) -> BlockFilterBackend { let mut k0 = [0_u8; 8]; let mut k1 = [0_u8; 8]; - + k0.copy_from_slice(&self.key[0..8]); k1.copy_from_slice(&self.key[8..16]); - + BlockFilterBackend { key: BlockHash::from_inner(self.key), whitelisted_outputs: self.whitelisted_outputs, @@ -338,10 +319,10 @@ mod tests { consensus::deserialize, hashes::{hex::FromHex, Hash}, util::bip158, - Block, BlockHash, OutPoint, Txid, Script, + Block, BlockHash, OutPoint, Script, Txid, }; - use crate::block_filter::QueryType; + use crate::QueryType; use super::{BlockFilterBackend, FilterBuilder, MemoryBlockFilterStorage}; #[test] @@ -395,7 +376,7 @@ mod tests { } .into(), ); - // One spk from this block + // One spk from this block let spck = Script::from_hex("0014fabea557d8541249533fe281aac45c37b2dbf342").unwrap(); let spck = QueryType::ScriptHash(floresta_common::get_spk_hash(&spck).into_inner()); diff --git a/crates/floresta-watch-only/src/lib.rs b/crates/floresta-watch-only/src/lib.rs index c724f94b..62ad5ebb 100644 --- a/crates/floresta-watch-only/src/lib.rs +++ b/crates/floresta-watch-only/src/lib.rs @@ -3,7 +3,6 @@ use core::{cmp::Ordering, fmt::Debug}; use floresta_common::{get_spk_hash, parse_descriptors}; -pub mod block_filter; pub mod kv_database; #[cfg(any(test, feature = "memory-database"))] pub mod memory_database;