Skip to content

Commit 5c154c6

Browse files
committed
fix errors in test_reorg_handling function
1 parent 4e5051b commit 5c154c6

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

crates/bitcoind_rpc/tests/test_filter_iter.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,20 @@ fn test_reorg_handling() -> anyhow::Result<()> {
169169
let env = testenv()?;
170170
let rpc = env.rpc_client();
171171

172+
// Make sure we have a chain with sufficient height
173+
while rpc.get_block_count()? < 99 {
174+
env.mine_blocks(1, None)?;
175+
}
176+
172177
// Mine initial chain: 100:A, 101:B
173178
let block_a_hash = env.mine_blocks(1, None)?[0];
174179
let block_b_hash = env.mine_blocks(1, None)?[0];
175180

181+
// Create SPK to test with
182+
let dummy_spk = ScriptBuf::new();
183+
176184
let mut iter = FilterIter::new_with_height(rpc, 100);
177-
iter.add_spks(vec![ScriptBuf::new()]); // Dummy SPK
185+
iter.add_spks(vec![dummy_spk.clone()]);
178186

179187
// Process block 100:A
180188
assert!(matches!(
@@ -185,27 +193,38 @@ fn test_reorg_handling() -> anyhow::Result<()> {
185193
// Reorg to 100:A', 101:B'
186194
// 1. Invalidate existing blocks
187195
rpc.invalidate_block(&block_a_hash)?;
188-
rpc.invalidate_block(&block_b_hash)?;
189196

190-
// 2. Mine new chain starting from parent of block_a
191-
let block_a_prime = env.mine_blocks(1, None)?[0]; // Builds on new tip
197+
// 2. Mine new chain with transaction matching our SPK
198+
// Create a transaction that matches our dummy SPK for block A'
199+
let address = Address::from_script(&dummy_spk, Network::Regtest)?;
200+
rpc.send_to_address(
201+
&address,
202+
Amount::from_sat(1000),
203+
None,
204+
None,
205+
None,
206+
None,
207+
None,
208+
None,
209+
)?;
210+
211+
// Mine new blocks
212+
let block_a_prime = env.mine_blocks(1, None)?[0];
192213
let block_b_prime = env.mine_blocks(1, None)?[0];
193214

194-
// Process new blocks
215+
// Process new blocks - should detect reorg and match the transaction
195216
match iter.next().transpose()? {
196217
Some(Event::Block(inner)) => {
197218
assert_eq!(inner.height, 100);
198219
assert_eq!(inner.block.block_hash(), block_a_prime);
199220
}
200-
_ => panic!("Expected block 100:A'"),
221+
other => panic!("Expected block 100:A', got {:?}", other),
201222
}
202223

224+
// The second block should be a NoMatch since we don't have a matching transaction
203225
match iter.next().transpose()? {
204-
Some(Event::Block(inner)) => {
205-
assert_eq!(inner.height, 101);
206-
assert_eq!(inner.block.block_hash(), block_b_prime);
207-
}
208-
_ => panic!("Expected block 101:B'"),
226+
Some(Event::NoMatch(101)) => { /* Expected */ }
227+
other => panic!("Expected NoMatch(101), got {:?}", other),
209228
}
210229

211230
Ok(())

0 commit comments

Comments
 (0)