Skip to content

Commit b2b2dfb

Browse files
tankyleoTheBlueMatt
andcommitted
Set AnchorDescriptor output value to CSV and P2A anchor amounts
Co-authored-by: Matt Corallo <git@bluematt.me>
1 parent 5ae19b4 commit b2b2dfb

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4416,8 +4416,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
44164416
counterparty_node_id,
44174417
claim_id,
44184418
package_target_feerate_sat_per_1000_weight,
4419-
commitment_tx,
4420-
commitment_tx_fee_satoshis,
44214419
anchor_descriptor: AnchorDescriptor {
44224420
channel_derivation_parameters: ChannelDerivationParameters {
44234421
keys_id: self.channel_keys_id,
@@ -4428,8 +4426,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
44284426
txid: commitment_txid,
44294427
vout: anchor_output_idx,
44304428
},
4429+
value: commitment_tx.output[anchor_output_idx as usize].value,
44314430
},
44324431
pending_htlcs: pending_nondust_htlcs,
4432+
commitment_tx,
4433+
commitment_tx_fee_satoshis,
44334434
}));
44344435
},
44354436
ClaimEvent::BumpHTLC {

lightning/src/chain/onchaintx.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ use crate::chain::package::{PackageSolvingData, PackageTemplate};
3030
use crate::chain::transaction::MaybeSignedTransaction;
3131
use crate::chain::ClaimId;
3232
use crate::ln::chan_utils::{
33-
self, ChannelTransactionParameters, HTLCOutputInCommitment, HolderCommitmentTransaction,
33+
get_keyed_anchor_redeemscript, shared_anchor_script_pubkey, ChannelTransactionParameters,
34+
HTLCOutputInCommitment, HolderCommitmentTransaction,
3435
};
3536
use crate::ln::msgs::DecodeError;
3637
use crate::sign::{ecdsa::EcdsaChannelSigner, EntropySource, HTLCDescriptor, SignerProvider};
@@ -677,7 +678,16 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
677678
let channel_parameters = output.channel_parameters.as_ref()
678679
.unwrap_or(self.channel_parameters());
679680
let funding_pubkey = &channel_parameters.holder_pubkeys.funding_pubkey;
680-
match chan_utils::get_keyed_anchor_output(&tx, funding_pubkey) {
681+
let script_pubkey = if channel_parameters.channel_type_features.supports_anchors_zero_fee_htlc_tx() {
682+
get_keyed_anchor_redeemscript(funding_pubkey).to_p2wsh()
683+
} else {
684+
debug_assert!(channel_parameters.channel_type_features.supports_anchor_zero_fee_commitments());
685+
shared_anchor_script_pubkey()
686+
};
687+
let anchor_output = tx.output.iter().enumerate()
688+
.find(|(_, txout)| txout.script_pubkey == script_pubkey)
689+
.map(|(idx, txout)| (idx as u32, txout));
690+
match anchor_output {
681691
// An anchor output was found, so we should yield a funding event externally.
682692
Some((idx, _)) => {
683693
// TODO: Use a lower confirmation target when both our and the

lightning/src/events/bump_transaction/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use crate::ln::chan_utils::{
2626
shared_anchor_script_pubkey, HTLCOutputInCommitment, ANCHOR_INPUT_WITNESS_WEIGHT,
2727
HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT, HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT,
2828
};
29-
use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
3029
use crate::ln::types::ChannelId;
3130
use crate::prelude::*;
3231
use crate::sign::ecdsa::EcdsaChannelSigner;
@@ -64,6 +63,8 @@ pub struct AnchorDescriptor {
6463
/// The transaction input's outpoint corresponding to the commitment transaction's anchor
6564
/// output.
6665
pub outpoint: OutPoint,
66+
/// Zero-fee-commitment anchors have variable value, which is tracked here.
67+
pub value: Amount,
6768
}
6869

6970
impl AnchorDescriptor {
@@ -80,7 +81,7 @@ impl AnchorDescriptor {
8081
assert!(tx_params.channel_type_features.supports_anchor_zero_fee_commitments());
8182
shared_anchor_script_pubkey()
8283
};
83-
TxOut { script_pubkey, value: Amount::from_sat(ANCHOR_OUTPUT_VALUE_SATOSHI) }
84+
TxOut { script_pubkey, value: self.value }
8485
}
8586

8687
/// Returns the unsigned transaction input spending the anchor output in the commitment
@@ -1029,6 +1030,7 @@ mod tests {
10291030
};
10301031
use crate::io::Cursor;
10311032
use crate::ln::chan_utils::ChannelTransactionParameters;
1033+
use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
10321034
use crate::sign::KeysManager;
10331035
use crate::types::features::ChannelTypeFeatures;
10341036
use crate::util::ser::Readable;
@@ -1147,6 +1149,7 @@ mod tests {
11471149
transaction_parameters,
11481150
},
11491151
outpoint: OutPoint { txid: Txid::from_byte_array([42; 32]), vout: 0 },
1152+
value: Amount::from_sat(ANCHOR_OUTPUT_VALUE_SATOSHI),
11501153
},
11511154
pending_htlcs: Vec::new(),
11521155
});

lightning/src/ln/chan_utils.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ pub const ANCHOR_INPUT_WITNESS_WEIGHT: u64 = 114;
8989
#[cfg(not(feature = "grind_signatures"))]
9090
pub const ANCHOR_INPUT_WITNESS_WEIGHT: u64 = 115;
9191

92-
/// The P2A scriptpubkey
93-
pub const P2A_SCRIPT: &[u8] = &[0x51, 0x02, 0x4e, 0x73];
94-
9592
/// The maximum value of the P2A anchor
9693
pub const P2A_MAX_VALUE: u64 = 240;
9794

@@ -978,16 +975,6 @@ pub fn get_keyed_anchor_redeemscript(funding_pubkey: &PublicKey) -> ScriptBuf {
978975
.into_script()
979976
}
980977

981-
/// Locates the output with a keyed anchor (non-zero-fee-commitments) script paying to
982-
/// `funding_pubkey` within `commitment_tx`.
983-
#[rustfmt::skip]
984-
pub(crate) fn get_keyed_anchor_output<'a>(commitment_tx: &'a Transaction, funding_pubkey: &PublicKey) -> Option<(u32, &'a TxOut)> {
985-
let anchor_script = get_keyed_anchor_redeemscript(funding_pubkey).to_p2wsh();
986-
commitment_tx.output.iter().enumerate()
987-
.find(|(_, txout)| txout.script_pubkey == anchor_script)
988-
.map(|(idx, txout)| (idx as u32, txout))
989-
}
990-
991978
/// Returns the witness required to satisfy and spend a keyed anchor (non-zero-fee-commitments)
992979
/// input.
993980
pub fn build_keyed_anchor_input_witness(
@@ -1891,7 +1878,7 @@ impl CommitmentTransaction {
18911878
// These subtractions panic on underflow, but this should never happen
18921879
let trimmed_sum_sat = channel_value_satoshis - nondust_htlcs_value_sum_sat - to_broadcaster_value_sat - to_countersignatory_value_sat;
18931880
insert_non_htlc_output(TxOut {
1894-
script_pubkey: ScriptBuf::from_bytes(P2A_SCRIPT.to_vec()),
1881+
script_pubkey: shared_anchor_script_pubkey(),
18951882
value: cmp::min(Amount::from_sat(P2A_MAX_VALUE), trimmed_sum_sat),
18961883
});
18971884
}

0 commit comments

Comments
 (0)