Skip to content

Commit 58cd2a0

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 f2d7a09 commit 58cd2a0

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ pub(crate) struct ClaimRequest {
468468
/// TODO: Write docs
469469
#[derive(PartialEq)]
470470
struct OnchainEventEntry {
471+
txid: Txid,
471472
height: u32,
472473
event: OnchainEvent,
473474
}
@@ -954,6 +955,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
954955

955956
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
956957
for ref entry in self.onchain_events_waiting_threshold_conf.iter() {
958+
entry.txid.write(writer)?;
957959
writer.write_all(&byte_utils::be32_to_array(entry.height))?;
958960
match entry.event {
959961
OnchainEvent::HTLCUpdate { ref htlc_update } => {
@@ -1665,6 +1667,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
16651667
}
16661668
});
16671669
let entry = OnchainEventEntry {
1670+
txid: *$txid,
16681671
height,
16691672
event: OnchainEvent::HTLCUpdate {
16701673
htlc_update: ((**source).clone(), htlc.payment_hash.clone())
@@ -1730,6 +1733,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
17301733
}
17311734
});
17321735
self.onchain_events_waiting_threshold_conf.push(OnchainEventEntry {
1736+
txid: *$txid,
17331737
height,
17341738
event: OnchainEvent::HTLCUpdate {
17351739
htlc_update: ((**source).clone(), htlc.payment_hash.clone())
@@ -1885,6 +1889,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
18851889
}
18861890
});
18871891
let entry = OnchainEventEntry {
1892+
txid: commitment_txid,
18881893
height,
18891894
event: OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash) },
18901895
};
@@ -2368,6 +2373,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23682373
}
23692374
});
23702375
let entry = OnchainEventEntry {
2376+
txid: tx.txid(),
23712377
height,
23722378
event: OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash) },
23732379
};
@@ -2432,6 +2438,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
24322438
}
24332439
if let Some(spendable_output) = spendable_output {
24342440
let entry = OnchainEventEntry {
2441+
txid: tx.txid(),
24352442
height: height,
24362443
event: OnchainEvent::MaturingOutput { descriptor: spendable_output.clone() },
24372444
};
@@ -2704,6 +2711,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27042711
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
27052712
let mut onchain_events_waiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
27062713
for _ in 0..waiting_threshold_conf_len {
2714+
let txid = Readable::read(reader)?;
27072715
let height = Readable::read(reader)?;
27082716
let event = match <u8 as Readable>::read(reader)? {
27092717
0 => {
@@ -2721,7 +2729,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27212729
},
27222730
_ => return Err(DecodeError::InvalidValue),
27232731
};
2724-
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { height, event });
2732+
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { txid, height, event });
27252733
}
27262734

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

lightning/src/ln/onchaintx.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
4242
/// TODO: Write docs
4343
#[derive(PartialEq)]
4444
struct OnchainEventEntry {
45+
txid: Txid,
4546
height: u32,
4647
event: OnchainEvent,
4748
}
@@ -336,6 +337,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
336337

337338
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
338339
for ref entry in self.onchain_events_waiting_threshold_conf.iter() {
340+
entry.txid.write(writer)?;
339341
writer.write_all(&byte_utils::be32_to_array(entry.height))?;
340342
match entry.event {
341343
OnchainEvent::Claim { ref claim_request } => {
@@ -393,6 +395,7 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
393395
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
394396
let mut onchain_events_waiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
395397
for _ in 0..waiting_threshold_conf_len {
398+
let txid = Readable::read(reader)?;
396399
let height = Readable::read(reader)?;
397400
let event = match <u8 as Readable>::read(reader)? {
398401
0 => {
@@ -411,7 +414,7 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
411414
}
412415
_ => return Err(DecodeError::InvalidValue),
413416
};
414-
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { height, event });
417+
onchain_events_waiting_threshold_conf.push(OnchainEventEntry { txid, height, event });
415418
}
416419
let latest_height = Readable::read(reader)?;
417420

@@ -766,6 +769,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
766769
macro_rules! clean_claim_request_after_safety_delay {
767770
() => {
768771
let entry = OnchainEventEntry {
772+
txid: tx.txid(),
769773
height,
770774
event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() }
771775
};
@@ -805,6 +809,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
805809
}
806810
for (outpoint, input_material) in claimed_outputs_material.drain(..) {
807811
let entry = OnchainEventEntry {
812+
txid: tx.txid(),
808813
height,
809814
event: OnchainEvent::ContentiousOutpoint { outpoint, input_material },
810815
};

0 commit comments

Comments
 (0)