Skip to content

Commit 9115f66

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 4005724 commit 9115f66

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 = Txid::all_zeros();
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
}
@@ -1683,8 +1687,10 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
16831687
/// as long as we examine both the current counterparty commitment transaction and, if it hasn't
16841688
/// been revoked yet, the previous one, we we will never "forget" to resolve an HTLC.
16851689
macro_rules! fail_unbroadcast_htlcs {
1686-
($self: expr, $commitment_tx_type: expr, $commitment_txid_confirmed: expr,
1690+
($self: expr, $commitment_tx_type: expr, $commitment_txid_confirmed: expr, $commitment_tx_confirmed: expr,
16871691
$commitment_tx_conf_height: expr, $confirmed_htlcs_list: expr, $logger: expr) => { {
1692+
debug_assert_eq!($commitment_tx_confirmed.txid(), $commitment_txid_confirmed);
1693+
16881694
macro_rules! check_htlc_fails {
16891695
($txid: expr, $commitment_tx: expr) => {
16901696
if let Some(ref latest_outpoints) = $self.counterparty_claimable_outpoints.get($txid) {
@@ -1724,6 +1730,7 @@ macro_rules! fail_unbroadcast_htlcs {
17241730
});
17251731
let entry = OnchainEventEntry {
17261732
txid: $commitment_txid_confirmed,
1733+
transaction: Some($commitment_tx_confirmed.clone()),
17271734
height: $commitment_tx_conf_height,
17281735
event: OnchainEvent::HTLCUpdate {
17291736
source: (**source).clone(),
@@ -2155,13 +2162,13 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21552162
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
21562163

21572164
if let Some(per_commitment_data) = per_commitment_option {
2158-
fail_unbroadcast_htlcs!(self, "revoked_counterparty", commitment_txid, height,
2165+
fail_unbroadcast_htlcs!(self, "revoked_counterparty", commitment_txid, tx, height,
21592166
per_commitment_data.iter().map(|(htlc, htlc_source)|
21602167
(htlc, htlc_source.as_ref().map(|htlc_source| htlc_source.as_ref()))
21612168
), logger);
21622169
} else {
21632170
debug_assert!(false, "We should have per-commitment option for any recognized old commitment txn");
2164-
fail_unbroadcast_htlcs!(self, "revoked counterparty", commitment_txid, height,
2171+
fail_unbroadcast_htlcs!(self, "revoked counterparty", commitment_txid, tx, height,
21652172
[].iter().map(|reference| *reference), logger);
21662173
}
21672174
}
@@ -2179,7 +2186,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21792186
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
21802187

21812188
log_info!(logger, "Got broadcast of non-revoked counterparty commitment transaction {}", commitment_txid);
2182-
fail_unbroadcast_htlcs!(self, "counterparty", commitment_txid, height,
2189+
fail_unbroadcast_htlcs!(self, "counterparty", commitment_txid, tx, height,
21832190
per_commitment_data.iter().map(|(htlc, htlc_source)|
21842191
(htlc, htlc_source.as_ref().map(|htlc_source| htlc_source.as_ref()))
21852192
), logger);
@@ -2338,7 +2345,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23382345
let res = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, height);
23392346
let mut to_watch = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, tx);
23402347
append_onchain_update!(res, to_watch);
2341-
fail_unbroadcast_htlcs!(self, "latest holder", commitment_txid, height,
2348+
fail_unbroadcast_htlcs!(self, "latest holder", commitment_txid, tx, height,
23422349
self.current_holder_commitment_tx.htlc_outputs.iter()
23432350
.map(|(htlc, _, htlc_source)| (htlc, htlc_source.as_ref())), logger);
23442351
} else if let &Some(ref holder_tx) = &self.prev_holder_signed_commitment_tx {
@@ -2348,7 +2355,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23482355
let res = self.get_broadcasted_holder_claims(holder_tx, height);
23492356
let mut to_watch = self.get_broadcasted_holder_watch_outputs(holder_tx, tx);
23502357
append_onchain_update!(res, to_watch);
2351-
fail_unbroadcast_htlcs!(self, "previous holder", commitment_txid, height,
2358+
fail_unbroadcast_htlcs!(self, "previous holder", commitment_txid, tx, height,
23522359
holder_tx.htlc_outputs.iter().map(|(htlc, _, htlc_source)| (htlc, htlc_source.as_ref())),
23532360
logger);
23542361
}
@@ -2514,6 +2521,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
25142521
let txid = tx.txid();
25152522
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
25162523
txid,
2524+
transaction: Some((*tx).clone()),
25172525
height: height,
25182526
event: OnchainEvent::FundingSpendConfirmation {
25192527
on_local_output_csv: balance_spendable_csv,
@@ -2932,7 +2940,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29322940
let outbound_htlc = $holder_tx == htlc_output.offered;
29332941
if !outbound_htlc || revocation_sig_claim {
29342942
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
2935-
txid: tx.txid(), height,
2943+
txid: tx.txid(), height, transaction: Some(tx.clone()),
29362944
event: OnchainEvent::HTLCSpendConfirmation {
29372945
commitment_tx_output_idx: input.previous_output.vout,
29382946
preimage: if accepted_preimage_claim || offered_preimage_claim {
@@ -2984,6 +2992,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29842992
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
29852993
txid: tx.txid(),
29862994
height,
2995+
transaction: Some(tx.clone()),
29872996
event: OnchainEvent::HTLCSpendConfirmation {
29882997
commitment_tx_output_idx: input.previous_output.vout,
29892998
preimage: Some(payment_preimage),
@@ -3004,6 +3013,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30043013
} else { false }) {
30053014
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
30063015
txid: tx.txid(),
3016+
transaction: Some(tx.clone()),
30073017
height,
30083018
event: OnchainEvent::HTLCSpendConfirmation {
30093019
commitment_tx_output_idx: input.previous_output.vout,
@@ -3030,6 +3040,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30303040
});
30313041
let entry = OnchainEventEntry {
30323042
txid: tx.txid(),
3043+
transaction: Some(tx.clone()),
30333044
height,
30343045
event: OnchainEvent::HTLCUpdate {
30353046
source, payment_hash,
@@ -3103,6 +3114,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
31033114
if let Some(spendable_output) = spendable_output {
31043115
let entry = OnchainEventEntry {
31053116
txid: tx.txid(),
3117+
transaction: Some(tx.clone()),
31063118
height: height,
31073119
event: OnchainEvent::MaturingOutput { descriptor: spendable_output.clone() },
31083120
};

0 commit comments

Comments
 (0)