Skip to content

Commit 62e0aa2

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 5c06d1d commit 62e0aa2

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>,
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
}
@@ -1667,8 +1671,10 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
16671671
/// as long as we examine both the current counterparty commitment transaction and, if it hasn't
16681672
/// been revoked yet, the previous one, we we will never "forget" to resolve an HTLC.
16691673
macro_rules! fail_unbroadcast_htlcs {
1670-
($self: expr, $commitment_tx_type: expr, $commitment_txid_confirmed: expr,
1674+
($self: expr, $commitment_tx_type: expr, $commitment_txid_confirmed: expr, $commitment_tx_confirmed: expr,
16711675
$commitment_tx_conf_height: expr, $confirmed_htlcs_list: expr, $logger: expr) => { {
1676+
debug_assert_eq!($commitment_tx_confirmed.txid(), $commitment_txid_confirmed);
1677+
16721678
macro_rules! check_htlc_fails {
16731679
($txid: expr, $commitment_tx: expr) => {
16741680
if let Some(ref latest_outpoints) = $self.counterparty_claimable_outpoints.get($txid) {
@@ -1708,6 +1714,7 @@ macro_rules! fail_unbroadcast_htlcs {
17081714
});
17091715
let entry = OnchainEventEntry {
17101716
txid: $commitment_txid_confirmed,
1717+
transaction: Some($commitment_tx_confirmed.clone()),
17111718
height: $commitment_tx_conf_height,
17121719
event: OnchainEvent::HTLCUpdate {
17131720
source: (**source).clone(),
@@ -2136,13 +2143,13 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21362143
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
21372144

21382145
if let Some(per_commitment_data) = per_commitment_option {
2139-
fail_unbroadcast_htlcs!(self, "revoked_counterparty", commitment_txid, height,
2146+
fail_unbroadcast_htlcs!(self, "revoked_counterparty", commitment_txid, tx, height,
21402147
per_commitment_data.iter().map(|(htlc, htlc_source)|
21412148
(htlc, htlc_source.as_ref().map(|htlc_source| htlc_source.as_ref()))
21422149
), logger);
21432150
} else {
21442151
debug_assert!(false, "We should have per-commitment option for any recognized old commitment txn");
2145-
fail_unbroadcast_htlcs!(self, "revoked counterparty", commitment_txid, height,
2152+
fail_unbroadcast_htlcs!(self, "revoked counterparty", commitment_txid, tx, height,
21462153
[].iter().map(|reference| *reference), logger);
21472154
}
21482155
}
@@ -2160,7 +2167,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21602167
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
21612168

21622169
log_info!(logger, "Got broadcast of non-revoked counterparty commitment transaction {}", commitment_txid);
2163-
fail_unbroadcast_htlcs!(self, "counterparty", commitment_txid, height,
2170+
fail_unbroadcast_htlcs!(self, "counterparty", commitment_txid, tx, height,
21642171
per_commitment_data.iter().map(|(htlc, htlc_source)|
21652172
(htlc, htlc_source.as_ref().map(|htlc_source| htlc_source.as_ref()))
21662173
), logger);
@@ -2319,7 +2326,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23192326
let res = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, height);
23202327
let mut to_watch = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, tx);
23212328
append_onchain_update!(res, to_watch);
2322-
fail_unbroadcast_htlcs!(self, "latest holder", commitment_txid, height,
2329+
fail_unbroadcast_htlcs!(self, "latest holder", commitment_txid, tx, height,
23232330
self.current_holder_commitment_tx.htlc_outputs.iter()
23242331
.map(|(htlc, _, htlc_source)| (htlc, htlc_source.as_ref())), logger);
23252332
} else if let &Some(ref holder_tx) = &self.prev_holder_signed_commitment_tx {
@@ -2329,7 +2336,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23292336
let res = self.get_broadcasted_holder_claims(holder_tx, height);
23302337
let mut to_watch = self.get_broadcasted_holder_watch_outputs(holder_tx, tx);
23312338
append_onchain_update!(res, to_watch);
2332-
fail_unbroadcast_htlcs!(self, "previous holder", commitment_txid, height,
2339+
fail_unbroadcast_htlcs!(self, "previous holder", commitment_txid, tx, height,
23332340
holder_tx.htlc_outputs.iter().map(|(htlc, _, htlc_source)| (htlc, htlc_source.as_ref())),
23342341
logger);
23352342
}
@@ -2494,6 +2501,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
24942501
let txid = tx.txid();
24952502
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
24962503
txid,
2504+
transaction: Some((*tx).clone()),
24972505
height: height,
24982506
event: OnchainEvent::FundingSpendConfirmation {
24992507
on_local_output_csv: balance_spendable_csv,
@@ -2911,7 +2919,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29112919
let outbound_htlc = $holder_tx == htlc_output.offered;
29122920
if !outbound_htlc || revocation_sig_claim {
29132921
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
2914-
txid: tx.txid(), height,
2922+
txid: tx.txid(), height, transaction: Some(tx.clone()),
29152923
event: OnchainEvent::HTLCSpendConfirmation {
29162924
commitment_tx_output_idx: input.previous_output.vout,
29172925
preimage: if accepted_preimage_claim || offered_preimage_claim {
@@ -2963,6 +2971,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29632971
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
29642972
txid: tx.txid(),
29652973
height,
2974+
transaction: Some(tx.clone()),
29662975
event: OnchainEvent::HTLCSpendConfirmation {
29672976
commitment_tx_output_idx: input.previous_output.vout,
29682977
preimage: Some(payment_preimage),
@@ -2983,6 +2992,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29832992
} else { false }) {
29842993
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
29852994
txid: tx.txid(),
2995+
transaction: Some(tx.clone()),
29862996
height,
29872997
event: OnchainEvent::HTLCSpendConfirmation {
29882998
commitment_tx_output_idx: input.previous_output.vout,
@@ -3009,6 +3019,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30093019
});
30103020
let entry = OnchainEventEntry {
30113021
txid: tx.txid(),
3022+
transaction: Some(tx.clone()),
30123023
height,
30133024
event: OnchainEvent::HTLCUpdate {
30143025
source, payment_hash,
@@ -3082,6 +3093,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30823093
if let Some(spendable_output) = spendable_output {
30833094
let entry = OnchainEventEntry {
30843095
txid: tx.txid(),
3096+
transaction: Some(tx.clone()),
30853097
height: height,
30863098
event: OnchainEvent::MaturingOutput { descriptor: spendable_output.clone() },
30873099
};

0 commit comments

Comments
 (0)