Skip to content

Commit a4df59b

Browse files
authored
Merge pull request #1904 from TheBlueMatt/2022-12-1825-followups
Trivial Followups to #1825
2 parents 4a0010d + 67f9f01 commit a4df59b

File tree

4 files changed

+29
-54
lines changed

4 files changed

+29
-54
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,10 +3032,10 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
30323032
if let Some(new_outputs) = new_outputs_option {
30333033
watch_outputs.push(new_outputs);
30343034
}
3035-
// Since there may be multiple HTLCs (all from the same commitment) being
3036-
// claimed by the counterparty within the same transaction, and
3037-
// `check_spend_counterparty_htlc` already checks for all of them, we can
3038-
// safely break from our loop.
3035+
// Since there may be multiple HTLCs for this channel (all spending the
3036+
// same commitment tx) being claimed by the counterparty within the same
3037+
// transaction, and `check_spend_counterparty_htlc` already checks all the
3038+
// ones relevant to this channel, we can safely break from our loop.
30393039
break;
30403040
}
30413041
}

lightning/src/chain/onchaintx.rs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ enum OnchainEvent {
7979
/// Outpoint under claim process by our own tx, once this one get enough confirmations, we remove it from
8080
/// bump-txn candidate buffer.
8181
Claim {
82-
claim_request: Txid,
82+
package_id: PackageID,
8383
},
8484
/// Claim tx aggregate multiple claimable outpoints. One of the outpoint may be claimed by a counterparty party tx.
8585
/// In this case, we need to drop the outpoint and regenerate a new claim tx. By safety, we keep tracking
@@ -123,7 +123,7 @@ impl MaybeReadable for OnchainEventEntry {
123123

124124
impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
125125
(0, Claim) => {
126-
(0, claim_request, required),
126+
(0, package_id, required),
127127
},
128128
(1, ContentiousOutpoint) => {
129129
(0, package, required),
@@ -480,8 +480,8 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
480480
// We check for outpoint spends within claims individually rather than as a set
481481
// since requests can have outpoints split off.
482482
if !self.onchain_events_awaiting_threshold_conf.iter()
483-
.any(|event_entry| if let OnchainEvent::Claim { claim_request } = event_entry.event {
484-
first_claim_txid_height.0 == claim_request.into_inner()
483+
.any(|event_entry| if let OnchainEvent::Claim { package_id } = event_entry.event {
484+
first_claim_txid_height.0 == package_id
485485
} else {
486486
// The onchain event is not a claim, keep seeking until we find one.
487487
false
@@ -733,31 +733,13 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
733733
// outpoints to know if transaction is the original claim or a bumped one issued
734734
// by us.
735735
let mut are_sets_equal = true;
736-
if !request.requires_external_funding() || !request.is_malleable() {
737-
// If the claim does not require external funds to be allocated through
738-
// additional inputs we can simply check the inputs in order as they
739-
// cannot change under us.
740-
if request.outpoints().len() != tx.input.len() {
736+
let mut tx_inputs = tx.input.iter().map(|input| &input.previous_output).collect::<Vec<_>>();
737+
tx_inputs.sort_unstable();
738+
for request_input in request.outpoints() {
739+
if tx_inputs.binary_search(&request_input).is_err() {
741740
are_sets_equal = false;
742-
} else {
743-
for (claim_inp, tx_inp) in request.outpoints().iter().zip(tx.input.iter()) {
744-
if **claim_inp != tx_inp.previous_output {
745-
are_sets_equal = false;
746-
}
747-
}
748-
}
749-
} else {
750-
// Otherwise, we'll do a linear search for each input (we don't expect
751-
// large input sets to exist) to ensure the request's input set is fully
752-
// spent to be resilient against the external claim reordering inputs.
753-
let mut spends_all_inputs = true;
754-
for request_input in request.outpoints() {
755-
if tx.input.iter().find(|input| input.previous_output == *request_input).is_none() {
756-
spends_all_inputs = false;
757-
break;
758-
}
741+
break;
759742
}
760-
are_sets_equal = spends_all_inputs;
761743
}
762744

763745
macro_rules! clean_claim_request_after_safety_delay {
@@ -766,7 +748,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
766748
txid: tx.txid(),
767749
height: conf_height,
768750
block_hash: Some(conf_hash),
769-
event: OnchainEvent::Claim { claim_request: Txid::from_inner(first_claim_txid_height.0) }
751+
event: OnchainEvent::Claim { package_id: first_claim_txid_height.0 }
770752
};
771753
if !self.onchain_events_awaiting_threshold_conf.contains(&entry) {
772754
self.onchain_events_awaiting_threshold_conf.push(entry);
@@ -821,13 +803,13 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
821803
for entry in onchain_events_awaiting_threshold_conf {
822804
if entry.has_reached_confirmation_threshold(cur_height) {
823805
match entry.event {
824-
OnchainEvent::Claim { claim_request } => {
825-
let package_id = claim_request.into_inner();
806+
OnchainEvent::Claim { package_id } => {
826807
// We may remove a whole set of claim outpoints here, as these one may have
827808
// been aggregated in a single tx and claimed so atomically
828809
if let Some(request) = self.pending_claim_requests.remove(&package_id) {
829810
for outpoint in request.outpoints() {
830-
log_debug!(logger, "Removing claim tracking for {} due to maturation of claim tx {}.", outpoint, claim_request);
811+
log_debug!(logger, "Removing claim tracking for {} due to maturation of claim package {}.",
812+
outpoint, log_bytes!(package_id));
831813
self.claimable_outpoints.remove(&outpoint);
832814
#[cfg(anchors)]
833815
self.pending_claim_events.remove(&package_id);
@@ -1065,7 +1047,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
10651047

10661048
#[cfg(anchors)]
10671049
pub(crate) fn generate_external_htlc_claim(
1068-
&mut self, outp: &::bitcoin::OutPoint, preimage: &Option<PaymentPreimage>
1050+
&self, outp: &::bitcoin::OutPoint, preimage: &Option<PaymentPreimage>
10691051
) -> Option<ExternalHTLCClaim> {
10701052
let find_htlc = |holder_commitment: &HolderCommitmentTransaction| -> Option<ExternalHTLCClaim> {
10711053
let trusted_tx = holder_commitment.trust();

lightning/src/ln/chan_utils.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -739,17 +739,12 @@ pub fn build_htlc_input_witness(
739739
} else {
740740
EcdsaSighashType::All
741741
};
742-
let mut remote_sig = remote_sig.serialize_der().to_vec();
743-
remote_sig.push(remote_sighash_type as u8);
744-
745-
let mut local_sig = local_sig.serialize_der().to_vec();
746-
local_sig.push(EcdsaSighashType::All as u8);
747742

748743
let mut witness = Witness::new();
749744
// First push the multisig dummy, note that due to BIP147 (NULLDUMMY) it must be a zero-length element.
750745
witness.push(vec![]);
751-
witness.push(remote_sig);
752-
witness.push(local_sig);
746+
witness.push_bitcoin_signature(&remote_sig.serialize_der(), remote_sighash_type);
747+
witness.push_bitcoin_signature(&local_sig.serialize_der(), EcdsaSighashType::All);
753748
if let Some(preimage) = preimage {
754749
witness.push(preimage.0.to_vec());
755750
} else {
@@ -801,9 +796,10 @@ pub(crate) fn get_anchor_output<'a>(commitment_tx: &'a Transaction, funding_pubk
801796
/// Returns the witness required to satisfy and spend an anchor input.
802797
pub fn build_anchor_input_witness(funding_key: &PublicKey, funding_sig: &Signature) -> Witness {
803798
let anchor_redeem_script = chan_utils::get_anchor_redeemscript(funding_key);
804-
let mut funding_sig = funding_sig.serialize_der().to_vec();
805-
funding_sig.push(EcdsaSighashType::All as u8);
806-
Witness::from_vec(vec![funding_sig, anchor_redeem_script.to_bytes()])
799+
let mut ret = Witness::new();
800+
ret.push_bitcoin_signature(&funding_sig.serialize_der(), EcdsaSighashType::All);
801+
ret.push(anchor_redeem_script.as_bytes());
802+
ret
807803
}
808804

809805
/// Per-channel data used to build transactions in conjunction with the per-commitment data (CommitmentTransaction).
@@ -1037,17 +1033,13 @@ impl HolderCommitmentTransaction {
10371033
// First push the multisig dummy, note that due to BIP147 (NULLDUMMY) it must be a zero-length element.
10381034
let mut tx = self.inner.built.transaction.clone();
10391035
tx.input[0].witness.push(Vec::new());
1040-
let mut ser_holder_sig = holder_sig.serialize_der().to_vec();
1041-
ser_holder_sig.push(EcdsaSighashType::All as u8);
1042-
let mut ser_cp_sig = self.counterparty_sig.serialize_der().to_vec();
1043-
ser_cp_sig.push(EcdsaSighashType::All as u8);
10441036

10451037
if self.holder_sig_first {
1046-
tx.input[0].witness.push(ser_holder_sig);
1047-
tx.input[0].witness.push(ser_cp_sig);
1038+
tx.input[0].witness.push_bitcoin_signature(&holder_sig.serialize_der(), EcdsaSighashType::All);
1039+
tx.input[0].witness.push_bitcoin_signature(&self.counterparty_sig.serialize_der(), EcdsaSighashType::All);
10481040
} else {
1049-
tx.input[0].witness.push(ser_cp_sig);
1050-
tx.input[0].witness.push(ser_holder_sig);
1041+
tx.input[0].witness.push_bitcoin_signature(&self.counterparty_sig.serialize_der(), EcdsaSighashType::All);
1042+
tx.input[0].witness.push_bitcoin_signature(&holder_sig.serialize_der(), EcdsaSighashType::All);
10511043
}
10521044

10531045
tx.input[0].witness.push(funding_redeemscript.as_bytes().to_vec());

lightning/src/util/events.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,7 @@ impl Writeable for Event {
11251125
BumpTransactionEvent::ChannelClose { .. } => {}
11261126
BumpTransactionEvent::HTLCResolution { .. } => {}
11271127
}
1128+
write_tlv_fields!(writer, {}); // Write a length field for forwards compat
11281129
}
11291130
&Event::ChannelReady { ref channel_id, ref user_channel_id, ref counterparty_node_id, ref channel_type } => {
11301131
29u8.write(writer)?;

0 commit comments

Comments
 (0)