Skip to content

Commit 882db71

Browse files
committed
Make ChainWatchInterface::filter_block return only idxes, not refs
Instead of making the filter_block fn in the ChainWatchInterface trait return both a list of indexes of transaction positions within the block and references to the transactions themselves, return only the list of indexes and then build the reference list at the callsite. While this may be slightly less effecient from a memory locality perspective, it shouldn't be materially different. This should make it more practical to generate bindings for filter_block as it no longer needs to reference Rust Transaction objects that are contained in a Rust Block object (which we'd otherwise just pass over the FFI in fully-serialized form).
1 parent ee6dd7f commit 882db71

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

fuzz/src/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ impl ChainWatchInterface for DummyChainWatcher {
7676
fn install_watch_tx(&self, _txid: &Txid, _script_pub_key: &Script) { }
7777
fn install_watch_outpoint(&self, _outpoint: (Txid, u32), _out_script: &Script) { }
7878
fn watch_all_txn(&self) { }
79-
fn filter_block<'a>(&self, _block: &'a Block) -> (Vec<&'a Transaction>, Vec<u32>) {
80-
(Vec::new(), Vec::new())
79+
fn filter_block(&self, _block: &Block) -> Vec<u32> {
80+
Vec::new()
8181
}
8282
fn reentered(&self) -> usize { 0 }
8383

lightning/src/chain/chaininterface.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ pub trait ChainWatchInterface: Sync + Send {
5353
/// final two the output within the transaction.
5454
fn get_chain_utxo(&self, genesis_hash: BlockHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError>;
5555

56-
/// Gets the list of transactions and transaction indices that the ChainWatchInterface is
56+
/// Gets the list of transaction indices within a given block that the ChainWatchInterface is
5757
/// watching for.
58-
fn filter_block<'a>(&self, block: &'a Block) -> (Vec<&'a Transaction>, Vec<u32>);
58+
fn filter_block(&self, block: &Block) -> Vec<u32>;
5959

6060
/// Returns a usize that changes when the ChainWatchInterface's watched data is modified.
6161
/// Users of `filter_block` should pre-save a copy of `reentered`'s return value and use it to
@@ -277,8 +277,12 @@ impl<'a, CL: Deref<Target = ChainListener + 'a> + 'a, C: Deref> BlockNotifier<'a
277277
pub fn block_connected(&self, block: &Block, height: u32) {
278278
let mut reentered = true;
279279
while reentered {
280-
let (matched, matched_index) = self.chain_monitor.filter_block(block);
281-
reentered = self.block_connected_checked(&block.header, height, matched.as_slice(), matched_index.as_slice());
280+
let matched_indexes = self.chain_monitor.filter_block(block);
281+
let mut matched_txn = Vec::new();
282+
for index in matched_indexes.iter() {
283+
matched_txn.push(&block.txdata[*index as usize]);
284+
}
285+
reentered = self.block_connected_checked(&block.header, height, matched_txn.as_slice(), matched_indexes.as_slice());
282286
}
283287
}
284288

@@ -357,19 +361,17 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
357361
Err(ChainError::NotSupported)
358362
}
359363

360-
fn filter_block<'a>(&self, block: &'a Block) -> (Vec<&'a Transaction>, Vec<u32>) {
361-
let mut matched = Vec::new();
364+
fn filter_block(&self, block: &Block) -> Vec<u32> {
362365
let mut matched_index = Vec::new();
363366
{
364367
let watched = self.watched.lock().unwrap();
365368
for (index, transaction) in block.txdata.iter().enumerate() {
366369
if self.does_match_tx_unguarded(transaction, &watched) {
367-
matched.push(transaction);
368370
matched_index.push(index as u32);
369371
}
370372
}
371373
}
372-
(matched, matched_index)
374+
matched_index
373375
}
374376

375377
fn reentered(&self) -> usize {

lightning/src/util/test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ impl ChainWatchInterface for TestChainWatcher {
371371
fn install_watch_tx(&self, _txid: &Txid, _script_pub_key: &Script) { }
372372
fn install_watch_outpoint(&self, _outpoint: (Txid, u32), _out_script: &Script) { }
373373
fn watch_all_txn(&self) { }
374-
fn filter_block<'a>(&self, _block: &'a Block) -> (Vec<&'a Transaction>, Vec<u32>) {
375-
(Vec::new(), Vec::new())
374+
fn filter_block<'a>(&self, _block: &'a Block) -> Vec<u32> {
375+
Vec::new()
376376
}
377377
fn reentered(&self) -> usize { 0 }
378378

0 commit comments

Comments
 (0)