Skip to content

Commit 3705440

Browse files
author
Antoine Riard
committed
Cache remote HTLC inside OnchainTxHandler::JusticeTxCache
As we can't predict if any and which revoked commitment tx is going to appear onchain we have by design to cache all htlc information to regenerate htlc script if needed.
1 parent 2288fdb commit 3705440

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12111211
log_trace!(self, "New potential remote commitment transaction: {}", encode::serialize_hex(unsigned_commitment_tx));
12121212
self.onchain_detection.prev_remote_commitment_txid = self.onchain_detection.current_remote_commitment_txid.take();
12131213
self.onchain_detection.current_remote_commitment_txid = Some(new_txid);
1214-
self.remote_claimable_outpoints.insert(new_txid, htlc_outputs);
1214+
self.remote_claimable_outpoints.insert(new_txid, htlc_outputs.clone());
12151215
self.current_remote_commitment_number = commitment_number;
12161216
//TODO: Merge this into the other per-remote-transaction output storage stuff
12171217
match self.their_cur_revocation_points {
@@ -1232,6 +1232,13 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12321232
self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
12331233
}
12341234
}
1235+
let mut htlcs = Vec::with_capacity(htlc_outputs.len());
1236+
for htlc in htlc_outputs {
1237+
if htlc.0.transaction_output_index.is_some() {
1238+
htlcs.push(htlc.0);
1239+
}
1240+
}
1241+
self.onchain_tx_handler.provide_latest_remote_tx(new_txid, htlcs);
12351242
}
12361243

12371244
pub(super) fn provide_rescue_remote_commitment_tx_info(&mut self, their_revocation_point: PublicKey) {

lightning/src/ln/onchaintx.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ struct HTLCTxCache {
6262
/// Cache remote basepoint to compute any justice tx.
6363
struct JusticeTxCache {
6464
remote_delayed_payment_base_key: PublicKey,
65-
remote_htlc_base_key: PublicKey
65+
remote_htlc_base_key: PublicKey,
66+
per_htlc: HashMap<Sha256dHash, Vec<(HTLCOutputInCommitment)>>
6667
}
6768

6869
/// Higher-level cache structure needed to re-generate bumped claim txn if needed
@@ -244,6 +245,14 @@ impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
244245

245246
self.justice_tx_cache.remote_delayed_payment_base_key.write(writer)?;
246247
self.justice_tx_cache.remote_htlc_base_key.write(writer)?;
248+
writer.write_all(&byte_utils::be64_to_array(self.justice_tx_cache.per_htlc.len() as u64))?;
249+
for (ref txid, ref htlcs) in self.justice_tx_cache.per_htlc.iter() {
250+
writer.write_all(&txid[..])?;
251+
writer.write_all(&byte_utils::be64_to_array(htlcs.len() as u64))?;
252+
for &ref htlc in htlcs.iter() {
253+
htlc.write(writer)?;
254+
}
255+
}
247256
self.remote_csv.write(writer)?;
248257

249258
self.key_storage.write(writer)?;
@@ -335,9 +344,24 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
335344
let justice_tx_cache = {
336345
let remote_delayed_payment_base_key = Readable::read(reader)?;
337346
let remote_htlc_base_key = Readable::read(reader)?;
347+
let per_htlc_len: u64 = Readable::read(reader)?;
348+
let mut per_htlc = HashMap::with_capacity(cmp::min(per_htlc_len as usize, MAX_ALLOC_SIZE / 64));
349+
for _ in 0..per_htlc_len {
350+
let txid: Sha256dHash = Readable::read(reader)?;
351+
let htlcs_count: u64 = Readable::read(reader)?;
352+
let mut htlcs = Vec::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
353+
for _ in 0..htlcs_count {
354+
let htlc = Readable::read(reader)?;
355+
htlcs.push(htlc);
356+
}
357+
if let Some(_) = per_htlc.insert(txid, htlcs) {
358+
return Err(DecodeError::InvalidValue);
359+
}
360+
}
338361
JusticeTxCache {
339362
remote_delayed_payment_base_key,
340363
remote_htlc_base_key,
364+
per_htlc,
341365
}
342366
};
343367
let remote_csv = Readable::read(reader)?;
@@ -415,6 +439,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
415439
let justice_tx_cache = JusticeTxCache {
416440
remote_delayed_payment_base_key,
417441
remote_htlc_base_key,
442+
per_htlc: HashMap::new(),
418443
};
419444

420445
OnchainTxHandler {
@@ -916,6 +941,10 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
916941
});
917942
}
918943

944+
pub(super) fn provide_latest_remote_tx(&mut self, commitment_txid: Sha256dHash, htlcs: Vec<HTLCOutputInCommitment>) {
945+
self.justice_tx_cache.per_htlc.insert(commitment_txid, htlcs);
946+
}
947+
919948
pub(super) fn get_fully_signed_local_tx(&mut self, channel_value_satoshis: u64) -> Option<Transaction> {
920949
if self.local_commitment.is_some() {
921950
let mut tx = self.local_commitment.clone().unwrap();

0 commit comments

Comments
 (0)