Skip to content

Commit 3caa38f

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 3244e41 commit 3caa38f

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12051205
log_trace!(self, "New potential remote commitment transaction: {}", encode::serialize_hex(unsigned_commitment_tx));
12061206
self.prev_remote_commitment_txid = self.current_remote_commitment_txid.take();
12071207
self.current_remote_commitment_txid = Some(new_txid);
1208-
self.remote_claimable_outpoints.insert(new_txid, htlc_outputs);
1208+
self.remote_claimable_outpoints.insert(new_txid, htlc_outputs.clone());
12091209
self.current_remote_commitment_number = commitment_number;
12101210
//TODO: Merge this into the other per-remote-transaction output storage stuff
12111211
match self.their_cur_revocation_points {
@@ -1226,6 +1226,13 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12261226
self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
12271227
}
12281228
}
1229+
let mut htlcs = Vec::with_capacity(htlc_outputs.len());
1230+
for htlc in htlc_outputs {
1231+
if htlc.0.transaction_output_index.is_some() {
1232+
htlcs.push(htlc.0);
1233+
}
1234+
}
1235+
self.onchain_tx_handler.provide_latest_remote_tx(new_txid, htlcs);
12291236
}
12301237

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

lightning/src/ln/onchaintx.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use secp256k1;
1717
use ln::msgs::DecodeError;
1818
use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
1919
use ln::channelmanager::PaymentPreimage;
20-
use ln::chan_utils::{HTLCType, LocalCommitmentTransaction};
20+
use ln::chan_utils::{HTLCType, LocalCommitmentTransaction, HTLCOutputInCommitment};
2121
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
2222
use chain::keysinterface::ChannelKeys;
2323
use util::logger::Logger;
@@ -53,7 +53,8 @@ enum OnchainEvent {
5353
/// remote outputs, either justice or preimage/timeout transactions.
5454
struct RemoteTxCache {
5555
remote_delayed_payment_base_key: PublicKey,
56-
remote_htlc_base_key: PublicKey
56+
remote_htlc_base_key: PublicKey,
57+
per_htlc: HashMap<Sha256dHash, Vec<(HTLCOutputInCommitment)>>
5758
}
5859

5960
/// Higher-level cache structure needed to re-generate bumped claim txn if needed
@@ -253,6 +254,14 @@ impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
253254

254255
self.remote_tx_cache.remote_delayed_payment_base_key.write(writer)?;
255256
self.remote_tx_cache.remote_htlc_base_key.write(writer)?;
257+
writer.write_all(&byte_utils::be64_to_array(self.remote_tx_cache.per_htlc.len() as u64))?;
258+
for (ref txid, ref htlcs) in self.remote_tx_cache.per_htlc.iter() {
259+
writer.write_all(&txid[..])?;
260+
writer.write_all(&byte_utils::be64_to_array(htlcs.len() as u64))?;
261+
for &ref htlc in htlcs.iter() {
262+
htlc.write(writer)?;
263+
}
264+
}
256265
self.remote_csv.write(writer)?;
257266

258267
self.key_storage.write(writer)?;
@@ -306,9 +315,24 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
306315
let remote_tx_cache = {
307316
let remote_delayed_payment_base_key = Readable::read(reader)?;
308317
let remote_htlc_base_key = Readable::read(reader)?;
318+
let per_htlc_len: u64 = Readable::read(reader)?;
319+
let mut per_htlc = HashMap::with_capacity(cmp::min(per_htlc_len as usize, MAX_ALLOC_SIZE / 64));
320+
for _ in 0..per_htlc_len {
321+
let txid: Sha256dHash = Readable::read(reader)?;
322+
let htlcs_count: u64 = Readable::read(reader)?;
323+
let mut htlcs = Vec::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
324+
for _ in 0..htlcs_count {
325+
let htlc = Readable::read(reader)?;
326+
htlcs.push(htlc);
327+
}
328+
if let Some(_) = per_htlc.insert(txid, htlcs) {
329+
return Err(DecodeError::InvalidValue);
330+
}
331+
}
309332
RemoteTxCache {
310333
remote_delayed_payment_base_key,
311334
remote_htlc_base_key,
335+
per_htlc,
312336
}
313337
};
314338
let remote_csv = Readable::read(reader)?;
@@ -385,6 +409,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
385409
let remote_tx_cache = RemoteTxCache {
386410
remote_delayed_payment_base_key,
387411
remote_htlc_base_key,
412+
per_htlc: HashMap::new(),
388413
};
389414

390415
OnchainTxHandler {
@@ -903,6 +928,10 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
903928
}
904929
}
905930

931+
pub(super) fn provide_latest_remote_tx(&mut self, commitment_txid: Sha256dHash, htlcs: Vec<HTLCOutputInCommitment>) {
932+
self.remote_tx_cache.per_htlc.insert(commitment_txid, htlcs);
933+
}
934+
906935
#[cfg(test)]
907936
pub(super) fn get_fully_signed_copy_local_tx(&mut self, funding_redeemscript: &Script) -> Option<Transaction> {
908937
if let Some(ref mut local_commitment) = self.local_commitment {

0 commit comments

Comments
 (0)