Skip to content

Commit b14e4ee

Browse files
committed
Merge #756: Remove genesis_block lazy initialization
e6f2d02 Remove genesis_block lazy initialization (Shobit Beltangdy) Pull request description: ### Description This commit contains a change to address issue #752 cargo test runs successfully. ### Notes to the reviewers Hi, newbie here learning Rust and BDK! I've removed the lazy_static block in this commit, and when learning about lazy_static also came across something called [once_cell](https://doc.rust-lang.org/std/cell/struct.OnceCell.html), [soon to be available in stdlib](rust-lang/rust#74465). It's like lazy_static but faster and is not a macro. Shall I keep the lazy_static and create an issue to switch to once_cell in the future, or remove the lazy_static (as I've done) and create an issue anyway? #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing ACKs for top commit: notmandatory: ACK e6f2d02 Tree-SHA512: 528f5fdfb0d7d1f7a83869b7a0de1b25dfcfafae2671c9229cdb4e5d80d11e5578d9325b3d95555e59f5dfb4ed6304c0c112a01a56b6a596e50dd62e310c516e
2 parents bbf524b + e6f2d02 commit b14e4ee

File tree

3 files changed

+5
-29
lines changed

3 files changed

+5
-29
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ async-trait = { version = "0.1", optional = true }
3232
rocksdb = { version = "0.14", default-features = false, features = ["snappy"], optional = true }
3333
cc = { version = ">=1.0.64", optional = true }
3434
socks = { version = "0.3", optional = true }
35-
lazy_static = { version = "1.4", optional = true }
3635
hwi = { version = "0.2.2", optional = true }
3736

3837
bip39 = { version = "1.0.1", optional = true }
@@ -57,7 +56,7 @@ verify = ["bitcoinconsensus"]
5756
default = ["key-value-db", "electrum"]
5857
sqlite = ["rusqlite", "ahash"]
5958
sqlite-bundled = ["sqlite", "rusqlite/bundled"]
60-
compact_filters = ["rocksdb", "socks", "lazy_static", "cc"]
59+
compact_filters = ["rocksdb", "socks", "cc"]
6160
key-value-db = ["sled"]
6261
all-keys = ["keys-bip39"]
6362
keys-bip39 = ["bip39"]

src/blockchain/compact_filters/store.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::convert::TryInto;
1313
use std::fmt;
1414
use std::io::{Read, Write};
1515
use std::marker::PhantomData;
16-
use std::ops::Deref;
1716
use std::sync::Arc;
1817
use std::sync::RwLock;
1918

@@ -22,9 +21,9 @@ use rand::{thread_rng, Rng};
2221

2322
use rocksdb::{Direction, IteratorMode, ReadOptions, WriteBatch, DB};
2423

24+
use bitcoin::blockdata::constants::genesis_block;
2525
use bitcoin::consensus::{deserialize, encode::VarInt, serialize, Decodable, Encodable};
2626
use bitcoin::hash_types::{FilterHash, FilterHeader};
27-
use bitcoin::hashes::hex::FromHex;
2827
use bitcoin::hashes::Hash;
2928
use bitcoin::util::bip158::BlockFilter;
3029
use bitcoin::util::uint::Uint256;
@@ -33,17 +32,8 @@ use bitcoin::BlockHash;
3332
use bitcoin::BlockHeader;
3433
use bitcoin::Network;
3534

36-
use lazy_static::lazy_static;
37-
3835
use super::CompactFiltersError;
3936

40-
lazy_static! {
41-
static ref MAINNET_GENESIS: Block = deserialize(&Vec::<u8>::from_hex("0100000000000000000000000000000000000000000000000000000000000000000000003BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A29AB5F49FFFF001D1DAC2B7C0101000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000").unwrap()).unwrap();
42-
static ref TESTNET_GENESIS: Block = deserialize(&Vec::<u8>::from_hex("0100000000000000000000000000000000000000000000000000000000000000000000003BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4ADAE5494DFFFF001D1AA4AE180101000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000").unwrap()).unwrap();
43-
static ref REGTEST_GENESIS: Block = deserialize(&Vec::<u8>::from_hex("0100000000000000000000000000000000000000000000000000000000000000000000003BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4ADAE5494DFFFF7F20020000000101000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000").unwrap()).unwrap();
44-
static ref SIGNET_GENESIS: Block = deserialize(&Vec::<u8>::from_hex("0100000000000000000000000000000000000000000000000000000000000000000000003BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A008F4D5FAE77031E8AD222030101000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000").unwrap()).unwrap();
45-
}
46-
4737
pub trait StoreType: Default + fmt::Debug {}
4838

4939
#[derive(Default, Debug)]
@@ -224,12 +214,7 @@ pub struct ChainStore<T: StoreType> {
224214

225215
impl ChainStore<Full> {
226216
pub fn new(store: DB, network: Network) -> Result<Self, CompactFiltersError> {
227-
let genesis = match network {
228-
Network::Bitcoin => MAINNET_GENESIS.deref(),
229-
Network::Testnet => TESTNET_GENESIS.deref(),
230-
Network::Regtest => REGTEST_GENESIS.deref(),
231-
Network::Signet => SIGNET_GENESIS.deref(),
232-
};
217+
let genesis = genesis_block(network);
233218

234219
let cf_name = "default".to_string();
235220
let cf_handle = store.cf_handle(&cf_name).unwrap();
@@ -647,14 +632,9 @@ impl CfStore {
647632
filter_type,
648633
};
649634

650-
let genesis = match headers_store.network {
651-
Network::Bitcoin => MAINNET_GENESIS.deref(),
652-
Network::Testnet => TESTNET_GENESIS.deref(),
653-
Network::Regtest => REGTEST_GENESIS.deref(),
654-
Network::Signet => SIGNET_GENESIS.deref(),
655-
};
635+
let genesis = genesis_block(headers_store.network);
656636

657-
let filter = BlockFilter::new_script_filter(genesis, |utxo| {
637+
let filter = BlockFilter::new_script_filter(&genesis, |utxo| {
658638
Err(bitcoin::util::bip158::Error::UtxoMissing(*utxo))
659639
})?;
660640
let first_key = StoreEntry::CFilterTable((filter_type, Some(0))).get_key();

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,6 @@ extern crate async_trait;
231231
#[macro_use]
232232
extern crate bdk_macros;
233233

234-
#[cfg(feature = "compact_filters")]
235-
extern crate lazy_static;
236-
237234
#[cfg(feature = "rpc")]
238235
pub extern crate bitcoincore_rpc;
239236

0 commit comments

Comments
 (0)