Skip to content

Commit 2d03c7b

Browse files
committed
Add txid to on-chain event tracking
When using Electrum, transactions are individually unconfirmed during a reorg rather than by block. Store the txid of the transaction creating the on-chain event so that it can be used to determine which events need to be removed when a transaction is unconfirmed.
1 parent b71b320 commit 2d03c7b

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,13 @@ pub(crate) struct ClaimRequest {
465465
pub(crate) witness_data: InputMaterial
466466
}
467467

468-
/// An entry for an [`OnchainEvent`], stating the block height when the event was observed.
468+
/// An entry for an [`OnchainEvent`], stating the block height when the event was observed and the
469+
/// transaction causing it.
469470
///
470471
/// Used to determine when the on-chain event can be considered safe from a chain reorganization.
471472
#[derive(PartialEq)]
472473
struct OnchainEventEntry {
474+
txid: Txid,
473475
height: u32,
474476
event: OnchainEvent,
475477
}
@@ -956,6 +958,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
956958

957959
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
958960
for ref entry in self.onchain_events_waiting_threshold_conf.iter() {
961+
entry.txid.write(writer)?;
959962
writer.write_all(&byte_utils::be32_to_array(entry.height))?;
960963
match entry.event {
961964
OnchainEvent::HTLCUpdate { ref htlc_update } => {
@@ -1667,6 +1670,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
16671670
}
16681671
});
16691672
let entry = OnchainEventEntry {
1673+
txid: *$txid,
16701674
height,
16711675
event: OnchainEvent::HTLCUpdate {
16721676
htlc_update: ((**source).clone(), htlc.payment_hash.clone())
@@ -1732,6 +1736,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
17321736
}
17331737
});
17341738
self.onchain_events_waiting_threshold_conf.push(OnchainEventEntry {
1739+
txid: *$txid,
17351740
height,
17361741
event: OnchainEvent::HTLCUpdate {
17371742
htlc_update: ((**source).clone(), htlc.payment_hash.clone())
@@ -1887,6 +1892,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
18871892
}
18881893
});
18891894
let entry = OnchainEventEntry {
1895+
txid: commitment_txid,
18901896
height,
18911897
event: OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash) },
18921898
};
@@ -2395,6 +2401,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23952401
}
23962402
});
23972403
let entry = OnchainEventEntry {
2404+
txid: tx.txid(),
23982405
height,
23992406
event: OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash) },
24002407
};
@@ -2459,6 +2466,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
24592466
}
24602467
if let Some(spendable_output) = spendable_output {
24612468
let entry = OnchainEventEntry {
2469+
txid: tx.txid(),
24622470
height: height,
24632471
event: OnchainEvent::MaturingOutput { descriptor: spendable_output.clone() },
24642472
};
@@ -2731,6 +2739,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27312739
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
27322740
let mut onchain_events_waiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
27332741
for _ in 0..waiting_threshold_conf_len {
2742+
let txid = Readable::read(reader)?;
27342743
let height = Readable::read(reader)?;
27352744
let event = match <u8 as Readable>::read(reader)? {
27362745
0 => {
@@ -2748,7 +2757,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27482757
},
27492758
_ => return Err(DecodeError::InvalidValue),
27502759
};
2751-
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { height, event });
2760+
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { txid, height, event });
27522761
}
27532762

27542763
let outputs_to_watch_len: u64 = Readable::read(reader)?;

lightning/src/ln/onchaintx.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ use std::mem::replace;
3939

4040
const MAX_ALLOC_SIZE: usize = 64*1024;
4141

42-
/// An entry for an [`OnchainEvent`], stating the block height when the event was observed.
42+
/// An entry for an [`OnchainEvent`], stating the block height when the event was observed and the
43+
/// transaction causing it.
4344
///
4445
/// Used to determine when the on-chain event can be considered safe from a chain reorganization.
4546
#[derive(PartialEq)]
4647
struct OnchainEventEntry {
48+
txid: Txid,
4749
height: u32,
4850
event: OnchainEvent,
4951
}
@@ -338,6 +340,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
338340

339341
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
340342
for ref entry in self.onchain_events_waiting_threshold_conf.iter() {
343+
entry.txid.write(writer)?;
341344
writer.write_all(&byte_utils::be32_to_array(entry.height))?;
342345
match entry.event {
343346
OnchainEvent::Claim { ref claim_request } => {
@@ -395,6 +398,7 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
395398
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
396399
let mut onchain_events_waiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
397400
for _ in 0..waiting_threshold_conf_len {
401+
let txid = Readable::read(reader)?;
398402
let height = Readable::read(reader)?;
399403
let event = match <u8 as Readable>::read(reader)? {
400404
0 => {
@@ -413,7 +417,7 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
413417
}
414418
_ => return Err(DecodeError::InvalidValue),
415419
};
416-
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { height, event });
420+
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { txid, height, event });
417421
}
418422
let latest_height = Readable::read(reader)?;
419423

@@ -768,6 +772,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
768772
macro_rules! clean_claim_request_after_safety_delay {
769773
() => {
770774
let entry = OnchainEventEntry {
775+
txid: tx.txid(),
771776
height,
772777
event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
773778
};
@@ -807,6 +812,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
807812
}
808813
for (outpoint, input_material) in claimed_outputs_material.drain(..) {
809814
let entry = OnchainEventEntry {
815+
txid: tx.txid(),
810816
height,
811817
event: OnchainEvent::ContentiousOutpoint { outpoint, input_material },
812818
};

0 commit comments

Comments
 (0)