Skip to content

Commit db65d96

Browse files
committed
test(rpc): add test filter_iter_returns_matched_blocks
1 parent 4e1ecd3 commit db65d96

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

crates/bitcoind_rpc/tests/test_filter_iter.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bitcoin::{constants, Network};
1+
use bitcoin::{constants, Address, Amount, Network, ScriptBuf};
22

33
use bdk_bitcoind_rpc::bip158::FilterIter;
44
use bdk_core::{BlockId, CheckPoint};
@@ -109,6 +109,44 @@ fn get_tip_and_chain_update() -> anyhow::Result<()> {
109109
Ok(())
110110
}
111111

112+
#[test]
113+
fn filter_iter_returns_matched_blocks() -> anyhow::Result<()> {
114+
use bdk_bitcoind_rpc::bip158::{Event, EventInner};
115+
let env = testenv()?;
116+
let rpc = env.rpc_client();
117+
let miner = rpc.get_new_address(None, None)?.assume_checked();
118+
while rpc.get_block_count()? < 101 {
119+
let _ = env.mine_blocks(1, Some(miner.clone()))?;
120+
}
121+
122+
// send tx
123+
let spk = ScriptBuf::from_hex("0014446906a6560d8ad760db3156706e72e171f3a2aa")?;
124+
let txid = env.send(
125+
&Address::from_script(&spk, Network::Regtest)?,
126+
Amount::from_btc(0.42)?,
127+
)?;
128+
let _ = env.mine_blocks(1, Some(miner));
129+
130+
// match blocks
131+
let mut iter = FilterIter::new_with_height(rpc, 1);
132+
iter.add_spk(spk);
133+
assert_eq!(iter.get_tip()?.unwrap().height, 102);
134+
135+
for res in iter {
136+
let event = res?;
137+
match event {
138+
event if event.height() <= 101 => assert!(!event.is_match()),
139+
Event::Block(EventInner { height, block }) => {
140+
assert_eq!(height, 102);
141+
assert!(block.txdata.iter().any(|tx| tx.compute_txid() == txid));
142+
}
143+
Event::NoMatch(_) => panic!("expected to match block 102"),
144+
}
145+
}
146+
147+
Ok(())
148+
}
149+
112150
#[test]
113151
fn filter_iter_error_no_scripts() -> anyhow::Result<()> {
114152
use bdk_bitcoind_rpc::bip158::Error;

0 commit comments

Comments
 (0)