From b5f7c1ea74a738590060f67b94dd48f2b243d711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ci=C4=99=C5=BCarkiewicz?= Date: Tue, 20 Aug 2024 22:41:58 -0700 Subject: [PATCH] chore: use a hardcoded regtest genesis block hash Instead of assuming that any unknown gensis block hash is a regtest network, use the fact the regtest genesis block is hardcoded. This should protect us from any bugs that lead to invalid block hashes, like in #5813. --- fedimint-bitcoind/src/electrum.rs | 8 ++++---- fedimint-bitcoind/src/esplora.rs | 8 ++++---- fedimint-bitcoind/src/lib.rs | 4 ++++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/fedimint-bitcoind/src/electrum.rs b/fedimint-bitcoind/src/electrum.rs index 19592121531..ed2469606b0 100644 --- a/fedimint-bitcoind/src/electrum.rs +++ b/fedimint-bitcoind/src/electrum.rs @@ -1,6 +1,6 @@ use std::fmt; -use anyhow::anyhow as format_err; +use anyhow::{anyhow as format_err, bail}; use bitcoin::{BlockHash, Network, ScriptBuf, Transaction, Txid}; use electrum_client::ElectrumApi; use electrum_client::Error::Protocol; @@ -11,7 +11,7 @@ use fedimint_core::util::SafeUrl; use fedimint_core::{apply, async_trait_maybe_send, Feerate}; use hex::ToHex; use serde_json::{Map, Value}; -use tracing::{info, warn}; +use tracing::info; use crate::{DynBitcoindRpc, IBitcoindRpc, IBitcoindRpcFactory, RetryClient}; @@ -50,9 +50,9 @@ impl IBitcoindRpc for ElectrumClient { crate::MAINNET_GENESIS_BLOCK_HASH => Network::Bitcoin, crate::TESTNET_GENESIS_BLOCK_HASH => Network::Testnet, crate::SIGNET_GENESIS_BLOCK_HASH => Network::Signet, + crate::REGTEST_GENESIS_BLOCK_HASH => Network::Regtest, hash => { - warn!("Unknown genesis hash {hash} - assuming regtest"); - Network::Regtest + bail!("Unknown genesis hash {hash}"); } }) } diff --git a/fedimint-bitcoind/src/esplora.rs b/fedimint-bitcoind/src/esplora.rs index 9dcf3bebf2d..ac373d05cd7 100644 --- a/fedimint-bitcoind/src/esplora.rs +++ b/fedimint-bitcoind/src/esplora.rs @@ -1,12 +1,12 @@ use std::collections::HashMap; -use anyhow::format_err; +use anyhow::{bail, format_err}; use bitcoin::{BlockHash, Network, ScriptBuf, Transaction, Txid}; use fedimint_core::task::TaskHandle; use fedimint_core::txoproof::TxOutProof; use fedimint_core::util::SafeUrl; use fedimint_core::{apply, async_trait_maybe_send, Feerate}; -use tracing::{info, warn}; +use tracing::info; use crate::{DynBitcoindRpc, IBitcoindRpc, IBitcoindRpcFactory, RetryClient}; @@ -47,9 +47,9 @@ impl IBitcoindRpc for EsploraClient { crate::MAINNET_GENESIS_BLOCK_HASH => Network::Bitcoin, crate::TESTNET_GENESIS_BLOCK_HASH => Network::Testnet, crate::SIGNET_GENESIS_BLOCK_HASH => Network::Signet, + crate::REGTEST_GENESIS_BLOCK_HASH => Network::Regtest, hash => { - warn!("Unknown genesis hash {hash} - assuming regtest"); - Network::Regtest + bail!("Unknown genesis hash {hash}"); } }; diff --git a/fedimint-bitcoind/src/lib.rs b/fedimint-bitcoind/src/lib.rs index cbd34ac7f0d..4bdec9b09a3 100644 --- a/fedimint-bitcoind/src/lib.rs +++ b/fedimint-bitcoind/src/lib.rs @@ -45,6 +45,10 @@ const TESTNET_GENESIS_BLOCK_HASH: &str = // const SIGNET_GENESIS_BLOCK_HASH: &str = "00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6"; +// See +// +const REGTEST_GENESIS_BLOCK_HASH: &str = + "0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"; /// Global factories for creating bitcoin RPCs static BITCOIN_RPC_REGISTRY: LazyLock>> =