Skip to content

Commit 41f8ebc

Browse files
committed
fix(bip158)!: get_tip now errors due to wrong network
1 parent d7639da commit 41f8ebc

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

crates/bitcoind_rpc/src/bip158.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ impl<C: RpcApi> FilterIter<'_, C> {
204204
let height = cp.height();
205205
let fetched_hash = match self.blocks.get(&height) {
206206
Some(hash) => *hash,
207-
None if height == 0 => cp.hash(),
208-
_ => self.client.get_block_hash(height as _)?,
207+
None => self.client.get_block_hash(height as u64)?,
209208
};
210209
if cp.hash() == fetched_hash {
211210
// ensure this block also exists in self
@@ -214,7 +213,7 @@ impl<C: RpcApi> FilterIter<'_, C> {
214213
}
215214
// remember conflicts
216215
self.blocks.insert(height, fetched_hash);
217-
cp = cp.prev().expect("must break before genesis");
216+
cp = cp.prev().ok_or(Error::ReorgDepthExceeded)?;
218217
}
219218
}
220219

@@ -245,6 +244,8 @@ pub enum Error {
245244
NoScripts,
246245
/// `bitcoincore_rpc` error
247246
Rpc(bitcoincore_rpc::Error),
247+
/// `MAX_REORG_DEPTH` exceeded
248+
ReorgDepthExceeded,
248249
}
249250

250251
impl From<bitcoincore_rpc::Error> for Error {
@@ -259,6 +260,7 @@ impl fmt::Display for Error {
259260
Self::Bip158(e) => e.fmt(f),
260261
Self::NoScripts => write!(f, "no script pubkeys were provided to match with"),
261262
Self::Rpc(e) => e.fmt(f),
263+
Self::ReorgDepthExceeded => write!(f, "maximum reorg depth exceeded"),
262264
}
263265
}
264266
}

crates/bitcoind_rpc/tests/test_filter_iter.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use bitcoin::{constants, Address, Amount, Network, ScriptBuf};
2-
3-
use bdk_bitcoind_rpc::bip158::FilterIter;
1+
use bdk_bitcoind_rpc::bip158::{Error, Event, EventInner, FilterIter};
42
use bdk_core::{BlockId, CheckPoint};
53
use bdk_testenv::{anyhow, bitcoind, block_id, TestEnv};
4+
use bitcoin::{constants, Address, Amount, Network, ScriptBuf};
65
use bitcoincore_rpc::RpcApi;
76

87
fn testenv() -> anyhow::Result<TestEnv> {
@@ -111,7 +110,6 @@ fn get_tip_and_chain_update() -> anyhow::Result<()> {
111110

112111
#[test]
113112
fn filter_iter_returns_matched_blocks() -> anyhow::Result<()> {
114-
use bdk_bitcoind_rpc::bip158::{Event, EventInner};
115113
let env = testenv()?;
116114
let rpc = env.rpc_client();
117115
while rpc.get_block_count()? < 101 {
@@ -148,7 +146,6 @@ fn filter_iter_returns_matched_blocks() -> anyhow::Result<()> {
148146

149147
#[test]
150148
fn filter_iter_error_no_scripts() -> anyhow::Result<()> {
151-
use bdk_bitcoind_rpc::bip158::Error;
152149
let env = testenv()?;
153150
let _ = env.mine_blocks(2, None)?;
154151

@@ -163,3 +160,24 @@ fn filter_iter_error_no_scripts() -> anyhow::Result<()> {
163160

164161
Ok(())
165162
}
163+
164+
#[test]
165+
fn filter_iter_error_wrong_network() -> anyhow::Result<()> {
166+
let env = testenv()?;
167+
let rpc = env.rpc_client();
168+
let _ = env.mine_blocks(10, None)?;
169+
170+
// Try to initialize FilterIter with a CP on the wrong network
171+
let block_id = BlockId {
172+
height: 0,
173+
hash: bitcoin::hashes::Hash::hash(b"wrong-hash"),
174+
};
175+
let cp = CheckPoint::new(block_id);
176+
let mut iter = FilterIter::new_with_checkpoint(rpc, cp);
177+
let err = iter
178+
.get_tip()
179+
.expect_err("`get_tip` should fail to find PoA");
180+
assert!(matches!(err, Error::ReorgDepthExceeded));
181+
182+
Ok(())
183+
}

0 commit comments

Comments
 (0)