Skip to content

Commit 1c276f7

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 75353cf commit 1c276f7

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
}
@@ -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
};
@@ -2393,6 +2399,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23932399
}
23942400
});
23952401
let entry = OnchainEventEntry {
2402+
txid: tx.txid(),
23962403
height,
23972404
event: OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash) },
23982405
};
@@ -2457,6 +2464,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
24572464
}
24582465
if let Some(spendable_output) = spendable_output {
24592466
let entry = OnchainEventEntry {
2467+
txid: tx.txid(),
24602468
height: height,
24612469
event: OnchainEvent::MaturingOutput { descriptor: spendable_output.clone() },
24622470
};
@@ -2729,6 +2737,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27292737
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
27302738
let mut onchain_events_waiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
27312739
for _ in 0..waiting_threshold_conf_len {
2740+
let txid = Readable::read(reader)?;
27322741
let height = Readable::read(reader)?;
27332742
let event = match <u8 as Readable>::read(reader)? {
27342743
0 => {
@@ -2746,7 +2755,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27462755
},
27472756
_ => return Err(DecodeError::InvalidValue),
27482757
};
2749-
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { height, event });
2758+
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { txid, height, event });
27502759
}
27512760

27522761
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)