Skip to content

Commit 2760586

Browse files
author
Antoine Riard
committed
Cache per-HTLC data in OnchainTxHandler::HTLCTxCache
Splitting further parsing from transaction generation, we cache transaction elements needed for local HTLC transaction inside OnchainTxHandler. Duplicated data will be removed from ChannelMonitor in future commits.
1 parent 8a3992d commit 2760586

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,10 +1275,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12751275
delayed_payment_key: local_keys.a_delayed_payment_key,
12761276
per_commitment_point: local_keys.per_commitment_point,
12771277
feerate_per_kw,
1278-
htlc_outputs,
1278+
htlc_outputs: htlc_outputs.clone(),
12791279
});
12801280
self.current_local_commitment_number = 0xffff_ffff_ffff - ((((commitment_tx.without_valid_witness().input[0].sequence as u64 & 0xffffff) << 3*8) | (commitment_tx.without_valid_witness().lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor);
1281-
self.onchain_tx_handler.provide_latest_local_tx(commitment_tx, local_keys, feerate_per_kw);
1281+
self.onchain_tx_handler.provide_latest_local_tx(commitment_tx, local_keys, feerate_per_kw, htlc_outputs);
12821282
Ok(())
12831283
}
12841284

lightning/src/ln/onchaintx.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ use bitcoin::util::bip143;
1010

1111
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1212

13-
use secp256k1::Secp256k1;
13+
use secp256k1::{Secp256k1, Signature};
1414
use secp256k1;
1515

1616
use ln::msgs::DecodeError;
1717
use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
18+
use ln::channelmanager::HTLCSource;
1819
use ln::chan_utils;
19-
use ln::chan_utils::{HTLCType, LocalCommitmentTransaction, TxCreationKeys};
20+
use ln::chan_utils::{HTLCType, LocalCommitmentTransaction, TxCreationKeys, HTLCOutputInCommitment};
2021
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
2122
use chain::keysinterface::ChannelKeys;
2223
use util::logger::Logger;
@@ -54,6 +55,7 @@ enum OnchainEvent {
5455
struct HTLCTxCache {
5556
local_keys: TxCreationKeys,
5657
feerate_per_kw: u64,
58+
per_htlc: HashMap<u32, (HTLCOutputInCommitment, Option<Signature>)>
5759
}
5860

5961
/// Higher-level cache structure needed to re-generate bumped claim txn if needed
@@ -202,6 +204,16 @@ impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
202204
($cache: expr) => {
203205
$cache.local_keys.write(writer)?;
204206
$cache.feerate_per_kw.write(writer)?;
207+
writer.write_all(&byte_utils::be64_to_array($cache.per_htlc.len() as u64))?;
208+
for (_, &(ref htlc, ref sig)) in $cache.per_htlc.iter() {
209+
htlc.write(writer)?;
210+
if let &Some(ref their_sig) = sig {
211+
1u8.write(writer)?;
212+
writer.write_all(&their_sig.serialize_compact())?;
213+
} else {
214+
0u8.write(writer)?;
215+
}
216+
}
205217
}
206218
}
207219

@@ -268,9 +280,21 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
268280
{
269281
let local_keys = Readable::read(reader)?;
270282
let feerate_per_kw = Readable::read(reader)?;
283+
let htlcs_count: u64 = Readable::read(reader)?;
284+
let mut per_htlc = HashMap::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
285+
for _ in 0..htlcs_count {
286+
let htlc: HTLCOutputInCommitment = Readable::read(reader)?;
287+
let sigs = match <u8 as Readable>::read(reader)? {
288+
0 => None,
289+
1 => Some(Readable::read(reader)?),
290+
_ => return Err(DecodeError::InvalidValue),
291+
};
292+
per_htlc.insert(htlc.transaction_output_index.unwrap(), (htlc, sigs));
293+
}
271294
HTLCTxCache {
272295
local_keys,
273296
feerate_per_kw,
297+
per_htlc
274298
}
275299
}
276300
}
@@ -821,13 +845,20 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
821845
}
822846
}
823847

824-
pub(super) fn provide_latest_local_tx(&mut self, tx: LocalCommitmentTransaction, local_keys: chan_utils::TxCreationKeys, feerate_per_kw: u64) {
848+
pub(super) fn provide_latest_local_tx(&mut self, tx: LocalCommitmentTransaction, local_keys: chan_utils::TxCreationKeys, feerate_per_kw: u64, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) {
825849
self.prev_local_commitment = self.local_commitment.take();
826850
self.local_commitment = Some(tx);
827851
self.prev_htlc_cache = self.current_htlc_cache.take();
852+
let mut per_htlc = HashMap::with_capacity(htlc_outputs.len());
853+
for htlc in htlc_outputs {
854+
if htlc.0.transaction_output_index.is_some() { // Discard dust HTLC as we will never have to generate onchain tx for them
855+
per_htlc.insert(htlc.0.transaction_output_index.unwrap(), (htlc.0, htlc.1));
856+
}
857+
}
828858
self.current_htlc_cache = Some(HTLCTxCache {
829859
local_keys,
830860
feerate_per_kw,
861+
per_htlc
831862
});
832863
}
833864

0 commit comments

Comments
 (0)