Skip to content

Commit 73e0a01

Browse files
author
Antoine Riard
committed
Cache local_commitment_tx in OnchainTxHandler
As transaction generation and signature is headed to be moved inside OnchainTxHandler, cache local_commitment_tx signed by remote. If access to local commitment transaction is needed, we extend Onchain TxHandler API to do so.
1 parent 04a17b2 commit 73e0a01

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12441244
self.prev_local_signed_commitment_tx = self.current_local_signed_commitment_tx.take();
12451245
self.current_local_signed_commitment_tx = Some(LocalSignedTx {
12461246
txid: commitment_tx.txid(),
1247-
tx: commitment_tx,
1247+
tx: commitment_tx.clone(),
12481248
revocation_key: local_keys.revocation_key,
12491249
a_htlc_key: local_keys.a_htlc_key,
12501250
b_htlc_key: local_keys.b_htlc_key,
@@ -1253,6 +1253,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12531253
feerate_per_kw,
12541254
htlc_outputs,
12551255
});
1256+
self.onchain_tx_handler.provide_latest_local_tx(commitment_tx);
12561257
Ok(())
12571258
}
12581259

lightning/src/ln/onchaintx.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use secp256k1;
1515

1616
use ln::msgs::DecodeError;
1717
use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
18-
use ln::chan_utils::HTLCType;
18+
use ln::chan_utils::{HTLCType, LocalCommitmentTransaction};
1919
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
2020
use chain::keysinterface::ChannelKeys;
2121
use util::logger::Logger;
@@ -142,6 +142,8 @@ macro_rules! subtract_high_prio_fee {
142142
pub struct OnchainTxHandler<ChanSigner: ChannelKeys> {
143143
destination_script: Script,
144144
funding_redeemscript: Script,
145+
local_commitment: Option<LocalCommitmentTransaction>,
146+
prev_local_commitment: Option<LocalCommitmentTransaction>,
145147

146148
key_storage: ChanSigner,
147149

@@ -182,6 +184,8 @@ impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
182184
pub(crate) fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
183185
self.destination_script.write(writer)?;
184186
self.funding_redeemscript.write(writer)?;
187+
self.local_commitment.write(writer)?;
188+
self.prev_local_commitment.write(writer)?;
185189

186190
self.key_storage.write(writer)?;
187191

@@ -224,6 +228,8 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
224228
fn read<R: ::std::io::Read>(reader: &mut R, logger: Arc<Logger>) -> Result<Self, DecodeError> {
225229
let destination_script = Readable::read(reader)?;
226230
let funding_redeemscript = Readable::read(reader)?;
231+
let local_commitment = Readable::read(reader)?;
232+
let prev_local_commitment = Readable::read(reader)?;
227233

228234
let key_storage = Readable::read(reader)?;
229235

@@ -273,6 +279,8 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
273279
Ok(OnchainTxHandler {
274280
destination_script,
275281
funding_redeemscript,
282+
local_commitment,
283+
prev_local_commitment,
276284
key_storage,
277285
claimable_outpoints,
278286
pending_claim_requests,
@@ -291,6 +299,8 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
291299
OnchainTxHandler {
292300
destination_script,
293301
funding_redeemscript,
302+
local_commitment: None,
303+
prev_local_commitment: None,
294304
key_storage,
295305
pending_claim_requests: HashMap::new(),
296306
claimable_outpoints: HashMap::new(),
@@ -714,4 +724,9 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
714724
self.pending_claim_requests.remove(&req);
715725
}
716726
}
727+
728+
pub(super) fn provide_latest_local_tx(&mut self, tx: LocalCommitmentTransaction) {
729+
self.prev_local_commitment = self.local_commitment.take();
730+
self.local_commitment = Some(tx);
731+
}
717732
}

0 commit comments

Comments
 (0)