Skip to content

Commit 24351f5

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 561ddc0 commit 24351f5

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lightning/src/chain/channelmonitor.rs

+11-2
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
}
@@ -954,6 +956,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
954956

955957
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
956958
for ref entry in self.onchain_events_waiting_threshold_conf.iter() {
959+
entry.txid.write(writer)?;
957960
writer.write_all(&byte_utils::be32_to_array(entry.height))?;
958961
match entry.event {
959962
OnchainEvent::HTLCUpdate { ref htlc_update } => {
@@ -1665,6 +1668,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
16651668
}
16661669
});
16671670
let entry = OnchainEventEntry {
1671+
txid: *$txid,
16681672
height,
16691673
event: OnchainEvent::HTLCUpdate {
16701674
htlc_update: ((**source).clone(), htlc.payment_hash.clone())
@@ -1730,6 +1734,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
17301734
}
17311735
});
17321736
self.onchain_events_waiting_threshold_conf.push(OnchainEventEntry {
1737+
txid: *$txid,
17331738
height,
17341739
event: OnchainEvent::HTLCUpdate {
17351740
htlc_update: ((**source).clone(), htlc.payment_hash.clone())
@@ -1885,6 +1890,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
18851890
}
18861891
});
18871892
let entry = OnchainEventEntry {
1893+
txid: commitment_txid,
18881894
height,
18891895
event: OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash) },
18901896
};
@@ -2403,6 +2409,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
24032409
}
24042410
});
24052411
let entry = OnchainEventEntry {
2412+
txid: tx.txid(),
24062413
height,
24072414
event: OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash) },
24082415
};
@@ -2467,6 +2474,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
24672474
}
24682475
if let Some(spendable_output) = spendable_output {
24692476
let entry = OnchainEventEntry {
2477+
txid: tx.txid(),
24702478
height: height,
24712479
event: OnchainEvent::MaturingOutput { descriptor: spendable_output.clone() },
24722480
};
@@ -2739,6 +2747,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27392747
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
27402748
let mut onchain_events_waiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
27412749
for _ in 0..waiting_threshold_conf_len {
2750+
let txid = Readable::read(reader)?;
27422751
let height = Readable::read(reader)?;
27432752
let event = match <u8 as Readable>::read(reader)? {
27442753
0 => {
@@ -2756,7 +2765,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27562765
},
27572766
_ => return Err(DecodeError::InvalidValue),
27582767
};
2759-
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { height, event });
2768+
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { txid, height, event });
27602769
}
27612770

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

lightning/src/ln/onchaintx.rs

+8-2
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)