Skip to content

Commit feeb550

Browse files
committed
Store the full event transaction in OnchainEvent structs
When we see a transaction which generates some `OnchainEvent`, its useful to have the full transaction around for later analysis. Specifically, it lets us check the list of outputs which were spent in the transaction, allowing us to look up, e.g. which HTLC outpoint was spent in a transaction. This will be used in a few commits to do exactly that - figure out which HTLC a given `OnchainEvent` corresponds with.
1 parent b84b53a commit feeb550

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ struct OnchainEventEntry {
315315
txid: Txid,
316316
height: u32,
317317
event: OnchainEvent,
318+
transaction: Option<Transaction>, // Added as optional, but always filled in, in LDK 0.0.110
318319
}
319320

320321
impl OnchainEventEntry {
@@ -395,6 +396,7 @@ impl Writeable for OnchainEventEntry {
395396
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
396397
write_tlv_fields!(writer, {
397398
(0, self.txid, required),
399+
(1, self.transaction, option),
398400
(2, self.height, required),
399401
(4, self.event, required),
400402
});
@@ -405,15 +407,17 @@ impl Writeable for OnchainEventEntry {
405407
impl MaybeReadable for OnchainEventEntry {
406408
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
407409
let mut txid = Default::default();
410+
let mut transaction = None;
408411
let mut height = 0;
409412
let mut event = None;
410413
read_tlv_fields!(reader, {
411414
(0, txid, required),
415+
(1, transaction, option),
412416
(2, height, required),
413417
(4, event, ignorable),
414418
});
415419
if let Some(ev) = event {
416-
Ok(Some(Self { txid, height, event: ev }))
420+
Ok(Some(Self { txid, transaction, height, event: ev }))
417421
} else {
418422
Ok(None)
419423
}
@@ -1679,8 +1683,10 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
16791683
/// as long as we examine both the current counterparty commitment transaction and, if it hasn't
16801684
/// been revoked yet, the previous one, we we will never "forget" to resolve an HTLC.
16811685
macro_rules! fail_unbroadcast_htlcs {
1682-
($self: expr, $commitment_tx_type: expr, $commitment_txid_confirmed: expr,
1686+
($self: expr, $commitment_tx_type: expr, $commitment_txid_confirmed: expr, $commitment_tx_confirmed: expr,
16831687
$commitment_tx_conf_height: expr, $confirmed_htlcs_list: expr, $logger: expr) => { {
1688+
debug_assert_eq!($commitment_tx_confirmed.txid(), $commitment_txid_confirmed);
1689+
16841690
macro_rules! check_htlc_fails {
16851691
($txid: expr, $commitment_tx: expr) => {
16861692
if let Some(ref latest_outpoints) = $self.counterparty_claimable_outpoints.get($txid) {
@@ -1720,6 +1726,7 @@ macro_rules! fail_unbroadcast_htlcs {
17201726
});
17211727
let entry = OnchainEventEntry {
17221728
txid: $commitment_txid_confirmed,
1729+
transaction: Some($commitment_tx_confirmed.clone()),
17231730
height: $commitment_tx_conf_height,
17241731
event: OnchainEvent::HTLCUpdate {
17251732
source: (**source).clone(),
@@ -2151,13 +2158,13 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21512158
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
21522159

21532160
if let Some(per_commitment_data) = per_commitment_option {
2154-
fail_unbroadcast_htlcs!(self, "revoked_counterparty", commitment_txid, height,
2161+
fail_unbroadcast_htlcs!(self, "revoked_counterparty", commitment_txid, tx, height,
21552162
per_commitment_data.iter().map(|(htlc, htlc_source)|
21562163
(htlc, htlc_source.as_ref().map(|htlc_source| htlc_source.as_ref()))
21572164
), logger);
21582165
} else {
21592166
debug_assert!(false, "We should have per-commitment option for any recognized old commitment txn");
2160-
fail_unbroadcast_htlcs!(self, "revoked counterparty", commitment_txid, height,
2167+
fail_unbroadcast_htlcs!(self, "revoked counterparty", commitment_txid, tx, height,
21612168
[].iter().map(|reference| *reference), logger);
21622169
}
21632170
}
@@ -2175,7 +2182,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21752182
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
21762183

21772184
log_info!(logger, "Got broadcast of non-revoked counterparty commitment transaction {}", commitment_txid);
2178-
fail_unbroadcast_htlcs!(self, "counterparty", commitment_txid, height,
2185+
fail_unbroadcast_htlcs!(self, "counterparty", commitment_txid, tx, height,
21792186
per_commitment_data.iter().map(|(htlc, htlc_source)|
21802187
(htlc, htlc_source.as_ref().map(|htlc_source| htlc_source.as_ref()))
21812188
), logger);
@@ -2334,7 +2341,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23342341
let res = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, height);
23352342
let mut to_watch = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, tx);
23362343
append_onchain_update!(res, to_watch);
2337-
fail_unbroadcast_htlcs!(self, "latest holder", commitment_txid, height,
2344+
fail_unbroadcast_htlcs!(self, "latest holder", commitment_txid, tx, height,
23382345
self.current_holder_commitment_tx.htlc_outputs.iter()
23392346
.map(|(htlc, _, htlc_source)| (htlc, htlc_source.as_ref())), logger);
23402347
} else if let &Some(ref holder_tx) = &self.prev_holder_signed_commitment_tx {
@@ -2344,7 +2351,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23442351
let res = self.get_broadcasted_holder_claims(holder_tx, height);
23452352
let mut to_watch = self.get_broadcasted_holder_watch_outputs(holder_tx, tx);
23462353
append_onchain_update!(res, to_watch);
2347-
fail_unbroadcast_htlcs!(self, "previous holder", commitment_txid, height,
2354+
fail_unbroadcast_htlcs!(self, "previous holder", commitment_txid, tx, height,
23482355
holder_tx.htlc_outputs.iter().map(|(htlc, _, htlc_source)| (htlc, htlc_source.as_ref())),
23492356
logger);
23502357
}
@@ -2510,6 +2517,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
25102517
let txid = tx.txid();
25112518
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
25122519
txid,
2520+
transaction: Some((*tx).clone()),
25132521
height: height,
25142522
event: OnchainEvent::FundingSpendConfirmation {
25152523
on_local_output_csv: balance_spendable_csv,
@@ -2928,7 +2936,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29282936
let outbound_htlc = $holder_tx == htlc_output.offered;
29292937
if !outbound_htlc || revocation_sig_claim {
29302938
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
2931-
txid: tx.txid(), height,
2939+
txid: tx.txid(), height, transaction: Some(tx.clone()),
29322940
event: OnchainEvent::HTLCSpendConfirmation {
29332941
commitment_tx_output_idx: input.previous_output.vout,
29342942
preimage: if accepted_preimage_claim || offered_preimage_claim {
@@ -2980,6 +2988,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29802988
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
29812989
txid: tx.txid(),
29822990
height,
2991+
transaction: Some(tx.clone()),
29832992
event: OnchainEvent::HTLCSpendConfirmation {
29842993
commitment_tx_output_idx: input.previous_output.vout,
29852994
preimage: Some(payment_preimage),
@@ -3000,6 +3009,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30003009
} else { false }) {
30013010
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
30023011
txid: tx.txid(),
3012+
transaction: Some(tx.clone()),
30033013
height,
30043014
event: OnchainEvent::HTLCSpendConfirmation {
30053015
commitment_tx_output_idx: input.previous_output.vout,
@@ -3026,6 +3036,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30263036
});
30273037
let entry = OnchainEventEntry {
30283038
txid: tx.txid(),
3039+
transaction: Some(tx.clone()),
30293040
height,
30303041
event: OnchainEvent::HTLCUpdate {
30313042
source, payment_hash,
@@ -3099,6 +3110,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30993110
if let Some(spendable_output) = spendable_output {
31003111
let entry = OnchainEventEntry {
31013112
txid: tx.txid(),
3113+
transaction: Some(tx.clone()),
31023114
height: height,
31033115
event: OnchainEvent::MaturingOutput { descriptor: spendable_output.clone() },
31043116
};

0 commit comments

Comments
 (0)