Skip to content

Commit 0acdc55

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 5b3ca02 commit 0acdc55

File tree

5 files changed

+25
-23
lines changed

5 files changed

+25
-23
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/ln/functional_test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
150150
{
151151
let mut channel_monitors = HashMap::new();
152152
for monitor in deserialized_monitors.iter_mut() {
153-
channel_monitors.insert(monitor.get_funding_txo(), monitor);
153+
channel_monitors.insert(monitor.get_funding_txo().0, monitor);
154154
}
155155

156156
let mut w = test_utils::TestVecWriter(Vec::new());
@@ -169,7 +169,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
169169
let chain_watch = chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet);
170170
let channel_monitor = test_utils::TestChannelMonitor::new(&chain_watch, self.tx_broadcaster.clone(), &self.logger, &feeest);
171171
for deserialized_monitor in deserialized_monitors.drain(..) {
172-
if let Err(_) = channel_monitor.add_monitor(deserialized_monitor.get_funding_txo(), deserialized_monitor) {
172+
if let Err(_) = channel_monitor.add_monitor(deserialized_monitor.get_funding_txo().0, deserialized_monitor) {
173173
panic!();
174174
}
175175
}

lightning/src/ln/functional_tests.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3914,7 +3914,7 @@ fn test_no_txn_manager_serialize_deserialize() {
39143914
keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet);
39153915
let (_, nodes_0_deserialized_tmp) = {
39163916
let mut channel_monitors = HashMap::new();
3917-
channel_monitors.insert(chan_0_monitor.get_funding_txo(), &mut chan_0_monitor);
3917+
channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
39183918
<(BlockHash, ChannelManager<EnforcingChannelKeys, &test_utils::TestChannelMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
39193919
default_config: config,
39203920
keys_manager: &keys_manager,
@@ -3928,7 +3928,7 @@ fn test_no_txn_manager_serialize_deserialize() {
39283928
nodes_0_deserialized = nodes_0_deserialized_tmp;
39293929
assert!(nodes_0_read.is_empty());
39303930

3931-
assert!(nodes[0].chan_monitor.add_monitor(chan_0_monitor.get_funding_txo(), chan_0_monitor).is_ok());
3931+
assert!(nodes[0].chan_monitor.add_monitor(chan_0_monitor.get_funding_txo().0, chan_0_monitor).is_ok());
39323932
nodes[0].node = &nodes_0_deserialized;
39333933
nodes[0].block_notifier.register_listener(nodes[0].node);
39343934
assert_eq!(nodes[0].node.list_channels().len(), 1);
@@ -4022,7 +4022,7 @@ fn test_manager_serialize_deserialize_events() {
40224022
keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet);
40234023
let (_, nodes_0_deserialized_tmp) = {
40244024
let mut channel_monitors = HashMap::new();
4025-
channel_monitors.insert(chan_0_monitor.get_funding_txo(), &mut chan_0_monitor);
4025+
channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
40264026
<(BlockHash, ChannelManager<EnforcingChannelKeys, &test_utils::TestChannelMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
40274027
default_config: config,
40284028
keys_manager: &keys_manager,
@@ -4038,7 +4038,7 @@ fn test_manager_serialize_deserialize_events() {
40384038

40394039
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
40404040

4041-
assert!(nodes[0].chan_monitor.add_monitor(chan_0_monitor.get_funding_txo(), chan_0_monitor).is_ok());
4041+
assert!(nodes[0].chan_monitor.add_monitor(chan_0_monitor.get_funding_txo().0, chan_0_monitor).is_ok());
40424042
nodes[0].node = &nodes_0_deserialized;
40434043

40444044
// After deserializing, make sure the FundingBroadcastSafe event is still held by the channel manager
@@ -4112,7 +4112,7 @@ fn test_simple_manager_serialize_deserialize() {
41124112
keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet);
41134113
let (_, nodes_0_deserialized_tmp) = {
41144114
let mut channel_monitors = HashMap::new();
4115-
channel_monitors.insert(chan_0_monitor.get_funding_txo(), &mut chan_0_monitor);
4115+
channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
41164116
<(BlockHash, ChannelManager<EnforcingChannelKeys, &test_utils::TestChannelMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
41174117
default_config: UserConfig::default(),
41184118
keys_manager: &keys_manager,
@@ -4126,7 +4126,7 @@ fn test_simple_manager_serialize_deserialize() {
41264126
nodes_0_deserialized = nodes_0_deserialized_tmp;
41274127
assert!(nodes_0_read.is_empty());
41284128

4129-
assert!(nodes[0].chan_monitor.add_monitor(chan_0_monitor.get_funding_txo(), chan_0_monitor).is_ok());
4129+
assert!(nodes[0].chan_monitor.add_monitor(chan_0_monitor.get_funding_txo().0, chan_0_monitor).is_ok());
41304130
nodes[0].node = &nodes_0_deserialized;
41314131
check_added_monitors!(nodes[0], 1);
41324132

@@ -4210,7 +4210,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
42104210
monitor: nodes[0].chan_monitor,
42114211
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
42124212
logger: &logger,
4213-
channel_monitors: &mut node_0_stale_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo(), monitor) }).collect(),
4213+
channel_monitors: &mut node_0_stale_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
42144214
}) { } else {
42154215
panic!("If the monitor(s) are stale, this indicates a bug and we should get an Err return");
42164216
};
@@ -4224,7 +4224,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
42244224
monitor: nodes[0].chan_monitor,
42254225
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
42264226
logger: &logger,
4227-
channel_monitors: &mut node_0_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo(), monitor) }).collect(),
4227+
channel_monitors: &mut node_0_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
42284228
}).unwrap();
42294229
nodes_0_deserialized = nodes_0_deserialized_tmp;
42304230
assert!(nodes_0_read.is_empty());
@@ -4237,7 +4237,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
42374237
}
42384238

42394239
for monitor in node_0_monitors.drain(..) {
4240-
assert!(nodes[0].chan_monitor.add_monitor(monitor.get_funding_txo(), monitor).is_ok());
4240+
assert!(nodes[0].chan_monitor.add_monitor(monitor.get_funding_txo().0, monitor).is_ok());
42414241
check_added_monitors!(nodes[0], 1);
42424242
}
42434243
nodes[0].node = &nodes_0_deserialized;

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)