Skip to content

Commit 587af43

Browse files
author
Antoine Riard
committed
Implement block_disconnect for pruning of waiting-conf HTLC updates
Modify ChainListener API by adding height field to block_disconnect
1 parent 72c5423 commit 587af43

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ impl<'a> MoneyLossDetector<'a> {
209209
if self.height > 0 && (self.max_height < 6 || self.height >= self.max_height - 6) {
210210
self.height -= 1;
211211
let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
212-
self.manager.block_disconnected(&header);
213-
self.monitor.block_disconnected(&header);
212+
self.manager.block_disconnected(&header, self.height as u32);
213+
self.monitor.block_disconnected(&header, self.height as u32);
214214
let removal_height = self.height;
215215
self.txids_confirmed.retain(|_, height| {
216216
removal_height != *height

src/chain/chaininterface.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ pub trait ChainListener: Sync + Send {
7878
fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]);
7979
/// Notifies a listener that a block was disconnected.
8080
/// Unlike block_connected, this *must* never be called twice for the same disconnect event.
81-
fn block_disconnected(&self, header: &BlockHeader);
81+
/// Height must be the one of the block which was disconnected (not new height of the best chain)
82+
fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32);
8283
}
8384

8485
/// An enum that represents the speed at which we want a transaction to confirm used for feerate
@@ -279,11 +280,11 @@ impl ChainWatchInterfaceUtil {
279280
}
280281

281282
/// Notify listeners that a block was disconnected.
282-
pub fn block_disconnected(&self, header: &BlockHeader) {
283+
pub fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
283284
let listeners = self.listeners.lock().unwrap().clone();
284285
for listener in listeners.iter() {
285286
match listener.upgrade() {
286-
Some(arc) => arc.block_disconnected(header),
287+
Some(arc) => arc.block_disconnected(&header, disconnected_height),
287288
None => ()
288289
}
289290
}

src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2474,7 +2474,7 @@ impl ChainListener for ChannelManager {
24742474
}
24752475

24762476
/// We force-close the channel without letting our counterparty participate in the shutdown
2477-
fn block_disconnected(&self, header: &BlockHeader) {
2477+
fn block_disconnected(&self, header: &BlockHeader, _: u32) {
24782478
let _ = self.total_consistency_lock.read().unwrap();
24792479
let mut failed_channels = Vec::new();
24802480
{

src/ln/channelmonitor.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,13 @@ impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonit
201201
pending_events.append(&mut new_events);
202202
}
203203

204-
fn block_disconnected(&self, _: &BlockHeader) { }
204+
fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
205+
let block_hash = header.bitcoin_hash();
206+
let mut monitors = self.monitors.lock().unwrap();
207+
for monitor in monitors.values_mut() {
208+
monitor.block_disconnected(disconnected_height, &block_hash);
209+
}
210+
}
205211
}
206212

207213
impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key> {
@@ -1912,6 +1918,13 @@ impl ChannelMonitor {
19121918
(watch_outputs, spendable_outputs, htlc_updated)
19131919
}
19141920

1921+
fn block_disconnected(&mut self, height: u32, block_hash: &Sha256dHash) {
1922+
if let Some(_) = self.htlc_updated_waiting_threshold_conf.remove(&(height + HTLC_FAIL_ANTI_REORG_DELAY - 1)) {
1923+
//We discard htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
1924+
}
1925+
self.last_block_hash = block_hash.clone();
1926+
}
1927+
19151928
pub(super) fn would_broadcast_at_height(&self, height: u32) -> bool {
19161929
// We need to consider all HTLCs which are:
19171930
// * in any unrevoked remote commitment transaction, as they could broadcast said

src/ln/functional_tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2674,8 +2674,10 @@ fn test_unconf_chan() {
26742674
header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
26752675
headers.push(header.clone());
26762676
}
2677+
let mut height = 99;
26772678
while !headers.is_empty() {
2678-
nodes[0].node.block_disconnected(&headers.pop().unwrap());
2679+
nodes[0].node.block_disconnected(&headers.pop().unwrap(), height);
2680+
height -= 1;
26792681
}
26802682
check_closed_broadcast!(nodes[0]);
26812683
let channel_state = nodes[0].node.channel_state.lock().unwrap();

0 commit comments

Comments
 (0)