Skip to content

Commit 9a59c35

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 834fe63 commit 9a59c35

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
}
@@ -1675,8 +1679,10 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
16751679
/// as long as we examine both the current counterparty commitment transaction and, if it hasn't
16761680
/// been revoked yet, the previous one, we we will never "forget" to resolve an HTLC.
16771681
macro_rules! fail_unbroadcast_htlcs {
1678-
($self: expr, $commitment_tx_type: expr, $commitment_txid_confirmed: expr,
1682+
($self: expr, $commitment_tx_type: expr, $commitment_txid_confirmed: expr, $commitment_tx_confirmed: expr,
16791683
$commitment_tx_conf_height: expr, $confirmed_htlcs_list: expr, $logger: expr) => { {
1684+
debug_assert_eq!($commitment_tx_confirmed.txid(), $commitment_txid_confirmed);
1685+
16801686
macro_rules! check_htlc_fails {
16811687
($txid: expr, $commitment_tx: expr) => {
16821688
if let Some(ref latest_outpoints) = $self.counterparty_claimable_outpoints.get($txid) {
@@ -1716,6 +1722,7 @@ macro_rules! fail_unbroadcast_htlcs {
17161722
});
17171723
let entry = OnchainEventEntry {
17181724
txid: $commitment_txid_confirmed,
1725+
transaction: Some($commitment_tx_confirmed.clone()),
17191726
height: $commitment_tx_conf_height,
17201727
event: OnchainEvent::HTLCUpdate {
17211728
source: (**source).clone(),
@@ -2147,13 +2154,13 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21472154
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
21482155

21492156
if let Some(per_commitment_data) = per_commitment_option {
2150-
fail_unbroadcast_htlcs!(self, "revoked_counterparty", commitment_txid, height,
2157+
fail_unbroadcast_htlcs!(self, "revoked_counterparty", commitment_txid, tx, height,
21512158
per_commitment_data.iter().map(|(htlc, htlc_source)|
21522159
(htlc, htlc_source.as_ref().map(|htlc_source| htlc_source.as_ref()))
21532160
), logger);
21542161
} else {
21552162
debug_assert!(false, "We should have per-commitment option for any recognized old commitment txn");
2156-
fail_unbroadcast_htlcs!(self, "revoked counterparty", commitment_txid, height,
2163+
fail_unbroadcast_htlcs!(self, "revoked counterparty", commitment_txid, tx, height,
21572164
[].iter().map(|reference| *reference), logger);
21582165
}
21592166
}
@@ -2171,7 +2178,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21712178
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
21722179

21732180
log_info!(logger, "Got broadcast of non-revoked counterparty commitment transaction {}", commitment_txid);
2174-
fail_unbroadcast_htlcs!(self, "counterparty", commitment_txid, height,
2181+
fail_unbroadcast_htlcs!(self, "counterparty", commitment_txid, tx, height,
21752182
per_commitment_data.iter().map(|(htlc, htlc_source)|
21762183
(htlc, htlc_source.as_ref().map(|htlc_source| htlc_source.as_ref()))
21772184
), logger);
@@ -2330,7 +2337,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23302337
let res = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, height);
23312338
let mut to_watch = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, tx);
23322339
append_onchain_update!(res, to_watch);
2333-
fail_unbroadcast_htlcs!(self, "latest holder", commitment_txid, height,
2340+
fail_unbroadcast_htlcs!(self, "latest holder", commitment_txid, tx, height,
23342341
self.current_holder_commitment_tx.htlc_outputs.iter()
23352342
.map(|(htlc, _, htlc_source)| (htlc, htlc_source.as_ref())), logger);
23362343
} else if let &Some(ref holder_tx) = &self.prev_holder_signed_commitment_tx {
@@ -2340,7 +2347,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23402347
let res = self.get_broadcasted_holder_claims(holder_tx, height);
23412348
let mut to_watch = self.get_broadcasted_holder_watch_outputs(holder_tx, tx);
23422349
append_onchain_update!(res, to_watch);
2343-
fail_unbroadcast_htlcs!(self, "previous holder", commitment_txid, height,
2350+
fail_unbroadcast_htlcs!(self, "previous holder", commitment_txid, tx, height,
23442351
holder_tx.htlc_outputs.iter().map(|(htlc, _, htlc_source)| (htlc, htlc_source.as_ref())),
23452352
logger);
23462353
}
@@ -2506,6 +2513,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
25062513
let txid = tx.txid();
25072514
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
25082515
txid,
2516+
transaction: Some((*tx).clone()),
25092517
height: height,
25102518
event: OnchainEvent::FundingSpendConfirmation {
25112519
on_local_output_csv: balance_spendable_csv,
@@ -2924,7 +2932,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29242932
let outbound_htlc = $holder_tx == htlc_output.offered;
29252933
if !outbound_htlc || revocation_sig_claim {
29262934
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
2927-
txid: tx.txid(), height,
2935+
txid: tx.txid(), height, transaction: Some(tx.clone()),
29282936
event: OnchainEvent::HTLCSpendConfirmation {
29292937
commitment_tx_output_idx: input.previous_output.vout,
29302938
preimage: if accepted_preimage_claim || offered_preimage_claim {
@@ -2976,6 +2984,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29762984
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
29772985
txid: tx.txid(),
29782986
height,
2987+
transaction: Some(tx.clone()),
29792988
event: OnchainEvent::HTLCSpendConfirmation {
29802989
commitment_tx_output_idx: input.previous_output.vout,
29812990
preimage: Some(payment_preimage),
@@ -2996,6 +3005,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29963005
} else { false }) {
29973006
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {
29983007
txid: tx.txid(),
3008+
transaction: Some(tx.clone()),
29993009
height,
30003010
event: OnchainEvent::HTLCSpendConfirmation {
30013011
commitment_tx_output_idx: input.previous_output.vout,
@@ -3022,6 +3032,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30223032
});
30233033
let entry = OnchainEventEntry {
30243034
txid: tx.txid(),
3035+
transaction: Some(tx.clone()),
30253036
height,
30263037
event: OnchainEvent::HTLCUpdate {
30273038
source, payment_hash,
@@ -3095,6 +3106,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30953106
if let Some(spendable_output) = spendable_output {
30963107
let entry = OnchainEventEntry {
30973108
txid: tx.txid(),
3109+
transaction: Some(tx.clone()),
30983110
height: height,
30993111
event: OnchainEvent::MaturingOutput { descriptor: spendable_output.clone() },
31003112
};

0 commit comments

Comments
 (0)