Skip to content

Commit d842e24

Browse files
committed
Remove caching of holder HTLC signatures
Since we want our holder HTLC signatures to be randomly generated and not reused, our existing caches are useless now, so we opt to remove them.
1 parent 496be8e commit d842e24

File tree

2 files changed

+7
-67
lines changed

2 files changed

+7
-67
lines changed

lightning/src/chain/channelmonitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3356,7 +3356,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33563356
continue;
33573357
}
33583358
} else { None };
3359-
if let Some(htlc_tx) = self.onchain_tx_handler.unsafe_get_fully_signed_htlc_tx(
3359+
if let Some(htlc_tx) = self.onchain_tx_handler.get_fully_signed_htlc_tx(
33603360
&::bitcoin::OutPoint { txid, vout }, &preimage) {
33613361
holder_transactions.push(htlc_tx);
33623362
}

lightning/src/chain/onchaintx.rs

+6-66
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,7 @@ pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
220220
channel_keys_id: [u8; 32],
221221
destination_script: Script,
222222
holder_commitment: HolderCommitmentTransaction,
223-
// holder_htlc_sigs and prev_holder_htlc_sigs are in the order as they appear in the commitment
224-
// transaction outputs (hence the Option<>s inside the Vec). The first usize is the index in
225-
// the set of HTLCs in the HolderCommitmentTransaction.
226-
holder_htlc_sigs: Option<Vec<Option<(usize, Signature)>>>,
227223
prev_holder_commitment: Option<HolderCommitmentTransaction>,
228-
prev_holder_htlc_sigs: Option<Vec<Option<(usize, Signature)>>>,
229224

230225
pub(super) signer: ChannelSigner,
231226
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
@@ -283,9 +278,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> PartialEq for OnchainTxHandler<
283278
self.channel_keys_id == other.channel_keys_id &&
284279
self.destination_script == other.destination_script &&
285280
self.holder_commitment == other.holder_commitment &&
286-
self.holder_htlc_sigs == other.holder_htlc_sigs &&
287281
self.prev_holder_commitment == other.prev_holder_commitment &&
288-
self.prev_holder_htlc_sigs == other.prev_holder_htlc_sigs &&
289282
self.channel_transaction_parameters == other.channel_transaction_parameters &&
290283
self.pending_claim_requests == other.pending_claim_requests &&
291284
self.claimable_outpoints == other.claimable_outpoints &&
@@ -303,9 +296,9 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
303296

304297
self.destination_script.write(writer)?;
305298
self.holder_commitment.write(writer)?;
306-
self.holder_htlc_sigs.write(writer)?;
299+
None::<Option<Vec<Option<(usize, Signature)>>>>.write(writer)?; // holder_htlc_sigs
307300
self.prev_holder_commitment.write(writer)?;
308-
self.prev_holder_htlc_sigs.write(writer)?;
301+
None::<Option<Vec<Option<(usize, Signature)>>>>.write(writer)?; // prev_holder_htlc_sigs
309302

310303
self.channel_transaction_parameters.write(writer)?;
311304

@@ -360,9 +353,9 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
360353
let destination_script = Readable::read(reader)?;
361354

362355
let holder_commitment = Readable::read(reader)?;
363-
let holder_htlc_sigs = Readable::read(reader)?;
356+
let _holder_htlc_sigs: Option<Vec<Option<(usize, Signature)>>> = Readable::read(reader)?;
364357
let prev_holder_commitment = Readable::read(reader)?;
365-
let prev_holder_htlc_sigs = Readable::read(reader)?;
358+
let _prev_holder_htlc_sigs: Option<Vec<Option<(usize, Signature)>>> = Readable::read(reader)?;
366359

367360
let channel_parameters = Readable::read(reader)?;
368361

@@ -427,9 +420,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
427420
channel_keys_id,
428421
destination_script,
429422
holder_commitment,
430-
holder_htlc_sigs,
431423
prev_holder_commitment,
432-
prev_holder_htlc_sigs,
433424
signer,
434425
channel_transaction_parameters: channel_parameters,
435426
claimable_outpoints,
@@ -453,9 +444,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
453444
channel_keys_id,
454445
destination_script,
455446
holder_commitment,
456-
holder_htlc_sigs: None,
457447
prev_holder_commitment: None,
458-
prev_holder_htlc_sigs: None,
459448
signer,
460449
channel_transaction_parameters: channel_parameters,
461450
pending_claim_requests: HashMap::new(),
@@ -1101,39 +1090,6 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
11011090

11021091
pub(crate) fn provide_latest_holder_tx(&mut self, tx: HolderCommitmentTransaction) {
11031092
self.prev_holder_commitment = Some(replace(&mut self.holder_commitment, tx));
1104-
self.holder_htlc_sigs = None;
1105-
}
1106-
1107-
// Normally holder HTLCs are signed at the same time as the holder commitment tx. However,
1108-
// in some configurations, the holder commitment tx has been signed and broadcast by a
1109-
// ChannelMonitor replica, so we handle that case here.
1110-
fn sign_latest_holder_htlcs(&mut self) {
1111-
if self.holder_htlc_sigs.is_none() {
1112-
let (_sig, sigs) = self.signer.sign_holder_commitment_and_htlcs(&self.holder_commitment, &self.secp_ctx).expect("sign holder commitment");
1113-
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(&self.holder_commitment, sigs));
1114-
}
1115-
}
1116-
1117-
// Normally only the latest commitment tx and HTLCs need to be signed. However, in some
1118-
// configurations we may have updated our holder commitment but a replica of the ChannelMonitor
1119-
// broadcast the previous one before we sync with it. We handle that case here.
1120-
fn sign_prev_holder_htlcs(&mut self) {
1121-
if self.prev_holder_htlc_sigs.is_none() {
1122-
if let Some(ref holder_commitment) = self.prev_holder_commitment {
1123-
let (_sig, sigs) = self.signer.sign_holder_commitment_and_htlcs(holder_commitment, &self.secp_ctx).expect("sign previous holder commitment");
1124-
self.prev_holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, sigs));
1125-
}
1126-
}
1127-
}
1128-
1129-
fn extract_holder_sigs(holder_commitment: &HolderCommitmentTransaction, sigs: Vec<Signature>) -> Vec<Option<(usize, Signature)>> {
1130-
let mut ret = Vec::new();
1131-
for (htlc_idx, (holder_sig, htlc)) in sigs.iter().zip(holder_commitment.htlcs().iter()).enumerate() {
1132-
let tx_idx = htlc.transaction_output_index.unwrap();
1133-
if ret.len() <= tx_idx as usize { ret.resize(tx_idx as usize + 1, None); }
1134-
ret[tx_idx as usize] = Some((htlc_idx, holder_sig.clone()));
1135-
}
1136-
ret
11371093
}
11381094

11391095
pub(crate) fn get_unsigned_holder_commitment_tx(&self) -> &Transaction {
@@ -1145,15 +1101,13 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
11451101
// before providing a initial commitment transaction. For outbound channel, init ChannelMonitor at Channel::funding_signed, there is nothing
11461102
// to monitor before.
11471103
pub(crate) fn get_fully_signed_holder_tx(&mut self, funding_redeemscript: &Script) -> Transaction {
1148-
let (sig, htlc_sigs) = self.signer.sign_holder_commitment_and_htlcs(&self.holder_commitment, &self.secp_ctx).expect("signing holder commitment");
1149-
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(&self.holder_commitment, htlc_sigs));
1104+
let (sig, _) = self.signer.sign_holder_commitment_and_htlcs(&self.holder_commitment, &self.secp_ctx).expect("signing holder commitment");
11501105
self.holder_commitment.add_holder_sig(funding_redeemscript, sig)
11511106
}
11521107

11531108
#[cfg(any(test, feature="unsafe_revoked_tx_signing"))]
11541109
pub(crate) fn get_fully_signed_copy_holder_tx(&mut self, funding_redeemscript: &Script) -> Transaction {
1155-
let (sig, htlc_sigs) = self.signer.unsafe_sign_holder_commitment_and_htlcs(&self.holder_commitment, &self.secp_ctx).expect("sign holder commitment");
1156-
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(&self.holder_commitment, htlc_sigs));
1110+
let (sig, _) = self.signer.unsafe_sign_holder_commitment_and_htlcs(&self.holder_commitment, &self.secp_ctx).expect("sign holder commitment");
11571111
self.holder_commitment.add_holder_sig(funding_redeemscript, sig)
11581112
}
11591113

@@ -1238,18 +1192,4 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
12381192
pub(crate) fn channel_type_features(&self) -> &ChannelTypeFeatures {
12391193
&self.channel_transaction_parameters.channel_type_features
12401194
}
1241-
1242-
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
1243-
pub(crate) fn unsafe_get_fully_signed_htlc_tx(&mut self, outp: &::bitcoin::OutPoint, preimage: &Option<PaymentPreimage>) -> Option<Transaction> {
1244-
let latest_had_sigs = self.holder_htlc_sigs.is_some();
1245-
let prev_had_sigs = self.prev_holder_htlc_sigs.is_some();
1246-
let ret = self.get_fully_signed_htlc_tx(outp, preimage);
1247-
if !latest_had_sigs {
1248-
self.holder_htlc_sigs = None;
1249-
}
1250-
if !prev_had_sigs {
1251-
self.prev_holder_htlc_sigs = None;
1252-
}
1253-
ret
1254-
}
12551195
}

0 commit comments

Comments
 (0)