Skip to content

Commit 578cce6

Browse files
author
Antoine Riard
committed
Cache remote HTLC inside OnchainTxHandler::RemoteTxCache
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 d282bbf commit 578cce6

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
@@ -63,7 +63,8 @@ struct HTLCTxCache {
6363
/// remote outputs, either justice or preimage/timeout transactions.
6464
struct RemoteTxCache {
6565
remote_delayed_payment_base_key: PublicKey,
66-
remote_htlc_base_key: PublicKey
66+
remote_htlc_base_key: PublicKey,
67+
per_htlc: HashMap<Sha256dHash, Vec<(HTLCOutputInCommitment)>>
6768
}
6869

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

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

250259
self.key_storage.write(writer)?;
@@ -336,9 +345,24 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
336345
let remote_tx_cache = {
337346
let remote_delayed_payment_base_key = Readable::read(reader)?;
338347
let remote_htlc_base_key = Readable::read(reader)?;
348+
let per_htlc_len: u64 = Readable::read(reader)?;
349+
let mut per_htlc = HashMap::with_capacity(cmp::min(per_htlc_len as usize, MAX_ALLOC_SIZE / 64));
350+
for _ in 0..per_htlc_len {
351+
let txid: Sha256dHash = Readable::read(reader)?;
352+
let htlcs_count: u64 = Readable::read(reader)?;
353+
let mut htlcs = Vec::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
354+
for _ in 0..htlcs_count {
355+
let htlc = Readable::read(reader)?;
356+
htlcs.push(htlc);
357+
}
358+
if let Some(_) = per_htlc.insert(txid, htlcs) {
359+
return Err(DecodeError::InvalidValue);
360+
}
361+
}
339362
RemoteTxCache {
340363
remote_delayed_payment_base_key,
341364
remote_htlc_base_key,
365+
per_htlc,
342366
}
343367
};
344368
let remote_csv = Readable::read(reader)?;
@@ -416,6 +440,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
416440
let remote_tx_cache = RemoteTxCache {
417441
remote_delayed_payment_base_key,
418442
remote_htlc_base_key,
443+
per_htlc: HashMap::new(),
419444
};
420445

421446
OnchainTxHandler {
@@ -917,6 +942,10 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
917942
});
918943
}
919944

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

0 commit comments

Comments
 (0)