Skip to content

Commit d18fc17

Browse files
committed
Align ChannelMonitor interface with ChainListener
ChannelMonitor has block_connected and block_disconnected methods called by <SimpleManyChannelMonitor as ChainListener>. Use similar parameters in ChannelMonitor such that transformations are not needed and the interface is more closely aligned with ChainListener.
1 parent f22b926 commit d18fc17

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,12 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + Sync
187187
let mut reentered = true;
188188
while reentered {
189189
let matched_indexes = self.chain_monitor.filter_block(header, txdata);
190-
let matched_txn: Vec<_> = matched_indexes.iter().map(|index| txdata[*index].1).collect();
190+
let matched_txn: Vec<_> = matched_indexes.iter().map(|index| txdata[*index]).collect();
191191
let last_seen = self.chain_monitor.reentered();
192-
let block_hash = header.bitcoin_hash();
193192
{
194193
let mut monitors = self.monitors.lock().unwrap();
195194
for monitor in monitors.values_mut() {
196-
let txn_outputs = monitor.block_connected(&matched_txn, height, &block_hash, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
195+
let txn_outputs = monitor.block_connected(header, &matched_txn, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
197196

198197
for (ref txid, ref outputs) in txn_outputs {
199198
for (idx, output) in outputs.iter().enumerate() {
@@ -207,10 +206,9 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + Sync
207206
}
208207

209208
fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
210-
let block_hash = header.bitcoin_hash();
211209
let mut monitors = self.monitors.lock().unwrap();
212210
for monitor in monitors.values_mut() {
213-
monitor.block_disconnected(disconnected_height, &block_hash, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
211+
monitor.block_disconnected(header, disconnected_height, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
214212
}
215213
}
216214
}
@@ -1877,12 +1875,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18771875
/// Eventually this should be pub and, roughly, implement ChainListener, however this requires
18781876
/// &mut self, as well as returns new spendable outputs and outpoints to watch for spending of
18791877
/// on-chain.
1880-
fn block_connected<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], height: u32, block_hash: &BlockHash, broadcaster: B, fee_estimator: F, logger: L)-> Vec<(Txid, Vec<TxOut>)>
1878+
fn block_connected<B: Deref, F: Deref, L: Deref>(&mut self, header: &BlockHeader, txn_matched: &[(usize, &Transaction)], height: u32, broadcaster: B, fee_estimator: F, logger: L)-> Vec<(Txid, Vec<TxOut>)>
18811879
where B::Target: BroadcasterInterface,
18821880
F::Target: FeeEstimator,
18831881
L::Target: Logger,
18841882
{
1885-
for tx in txn_matched {
1883+
for &(_, tx) in txn_matched {
18861884
let mut output_val = 0;
18871885
for out in tx.output.iter() {
18881886
if out.value > 21_000_000_0000_0000 { panic!("Value-overflowing transaction provided to block connected"); }
@@ -1891,10 +1889,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18911889
}
18921890
}
18931891

1892+
let block_hash = header.bitcoin_hash();
18941893
log_trace!(logger, "Block {} at height {} connected with {} txn matched", block_hash, height, txn_matched.len());
1894+
18951895
let mut watch_outputs = Vec::new();
18961896
let mut claimable_outpoints = Vec::new();
1897-
for tx in txn_matched {
1897+
for &(_, tx) in txn_matched {
18981898
if tx.input.len() == 1 {
18991899
// Assuming our keys were not leaked (in which case we're screwed no matter what),
19001900
// commitment transactions and HTLC transactions will all only ever have one input,
@@ -1968,20 +1968,22 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
19681968
}
19691969
self.onchain_tx_handler.block_connected(txn_matched, claimable_outpoints, height, &*broadcaster, &*fee_estimator, &*logger);
19701970

1971-
self.last_block_hash = block_hash.clone();
1971+
self.last_block_hash = block_hash;
19721972
for &(ref txid, ref output_scripts) in watch_outputs.iter() {
19731973
self.outputs_to_watch.insert(txid.clone(), output_scripts.iter().map(|o| o.script_pubkey.clone()).collect());
19741974
}
19751975

19761976
watch_outputs
19771977
}
19781978

1979-
fn block_disconnected<B: Deref, F: Deref, L: Deref>(&mut self, height: u32, block_hash: &BlockHash, broadcaster: B, fee_estimator: F, logger: L)
1979+
fn block_disconnected<B: Deref, F: Deref, L: Deref>(&mut self, header: &BlockHeader, height: u32, broadcaster: B, fee_estimator: F, logger: L)
19801980
where B::Target: BroadcasterInterface,
19811981
F::Target: FeeEstimator,
19821982
L::Target: Logger,
19831983
{
1984+
let block_hash = header.bitcoin_hash();
19841985
log_trace!(logger, "Block {} at height {} disconnected", block_hash, height);
1986+
19851987
if let Some(_) = self.onchain_events_waiting_threshold_conf.remove(&(height + ANTI_REORG_DELAY - 1)) {
19861988
//We may discard:
19871989
//- htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
@@ -1990,7 +1992,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
19901992

19911993
self.onchain_tx_handler.block_disconnected(height, broadcaster, fee_estimator, logger);
19921994

1993-
self.last_block_hash = block_hash.clone();
1995+
self.last_block_hash = block_hash;
19941996
}
19951997

19961998
pub(super) fn would_broadcast_at_height<L: Deref>(&self, height: u32, logger: &L) -> bool where L::Target: Logger {

lightning/src/ln/onchaintx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
648648
None
649649
}
650650

651-
pub(super) fn block_connected<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], claimable_outpoints: Vec<ClaimRequest>, height: u32, broadcaster: B, fee_estimator: F, logger: L)
651+
pub(super) fn block_connected<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[(usize, &Transaction)], claimable_outpoints: Vec<ClaimRequest>, height: u32, broadcaster: B, fee_estimator: F, logger: L)
652652
where B::Target: BroadcasterInterface,
653653
F::Target: FeeEstimator,
654654
L::Target: Logger,
@@ -697,7 +697,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
697697
}
698698

699699
let mut bump_candidates = HashMap::new();
700-
for tx in txn_matched {
700+
for &(_, tx) in txn_matched {
701701
// Scan all input to verify is one of the outpoint spent is of interest for us
702702
let mut claimed_outputs_material = Vec::new();
703703
for inp in &tx.input {

0 commit comments

Comments
 (0)