Skip to content

Commit

Permalink
Add ChannelSigner::get_holder_anchor_input_witness_weight
Browse files Browse the repository at this point in the history
  • Loading branch information
tankyleo committed Jan 6, 2025
1 parent deb5267 commit 250a1f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lightning/src/events/bump_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::io_extras::sink;
use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
use crate::ln::types::ChannelId;
use crate::ln::chan_utils;
use crate::ln::chan_utils::{ANCHOR_INPUT_WITNESS_WEIGHT, HTLCOutputInCommitment};
use crate::ln::chan_utils::HTLCOutputInCommitment;
use crate::prelude::*;
use crate::sign::{
ChannelDerivationParameters, ChannelSigner, HTLCDescriptor, SignerProvider, P2WPKH_WITNESS_WEIGHT,
Expand Down Expand Up @@ -608,14 +608,17 @@ where
&self, claim_id: ClaimId, package_target_feerate_sat_per_1000_weight: u32,
commitment_tx: &Transaction, commitment_tx_fee_sat: u64, anchor_descriptor: &AnchorDescriptor,
) -> Result<(), ()> {
let signer = anchor_descriptor.derive_channel_signer(&self.signer_provider);
let anchor_input_witness_weight = signer.get_holder_anchor_input_witness_weight();

// Our commitment transaction already has fees allocated to it, so we should take them into
// account. We do so by pretending the commitment transaction's fee and weight are part of
// the anchor input.
let mut anchor_utxo = anchor_descriptor.previous_utxo();
let commitment_tx_fee_sat = Amount::from_sat(commitment_tx_fee_sat);
anchor_utxo.value += commitment_tx_fee_sat;
let starting_package_and_fixed_input_satisfaction_weight =
commitment_tx.weight().to_wu() + ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT;
commitment_tx.weight().to_wu() + anchor_input_witness_weight + EMPTY_SCRIPT_SIG_WEIGHT;
let mut package_and_fixed_input_satisfaction_weight =
starting_package_and_fixed_input_satisfaction_weight;

Expand All @@ -640,7 +643,7 @@ where
output: vec![],
};

let total_satisfaction_weight = ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT +
let total_satisfaction_weight = anchor_input_witness_weight + EMPTY_SCRIPT_SIG_WEIGHT +
coin_selection.confirmed_utxos.iter().map(|utxo| utxo.satisfaction_weight).sum::<u64>();
let total_input_amount = must_spend_amount +
coin_selection.confirmed_utxos.iter().map(|utxo| utxo.output.value).sum();
Expand Down Expand Up @@ -686,7 +689,6 @@ where
log_debug!(self.logger, "Signing anchor transaction {}", anchor_txid);
anchor_tx = self.utxo_source.sign_psbt(anchor_psbt)?;

let signer = anchor_descriptor.derive_channel_signer(&self.signer_provider);
anchor_tx = signer.spend_holder_anchor_input(&anchor_tx, 0, &self.secp)?;

#[cfg(debug_assertions)] {
Expand Down Expand Up @@ -857,7 +859,7 @@ mod tests {
use super::*;

use crate::io::Cursor;
use crate::ln::chan_utils::ChannelTransactionParameters;
use crate::ln::chan_utils::{ANCHOR_INPUT_WITNESS_WEIGHT, ChannelTransactionParameters};
use crate::util::ser::Readable;
use crate::util::test_utils::{TestBroadcaster, TestLogger};
use crate::sign::KeysManager;
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::ln::chan_utils::{
get_anchor_redeemscript, get_counterparty_payment_script, get_revokeable_redeemscript,
make_funding_redeemscript, ChannelPublicKeys, ChannelTransactionParameters, ClosingTransaction,
CommitmentTransaction, HTLCOutputInCommitment, HolderCommitmentTransaction,
ANCHOR_INPUT_WITNESS_WEIGHT,
};
use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
use crate::ln::channel_keys::{
Expand Down Expand Up @@ -969,6 +970,9 @@ pub trait ChannelSigner {
fn spend_holder_anchor_input(
&self, anchor_tx: &Transaction, input_idx: usize, secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Transaction, ()>;

/// Get the weight of the witness to spend the holder anchor input
fn get_holder_anchor_input_witness_weight(&self) -> u64;
}

/// Specifies the recipient of an invoice.
Expand Down Expand Up @@ -1908,6 +1912,10 @@ impl ChannelSigner for InMemorySigner {
tx.input[0].witness = witness;
Ok(tx)
}

fn get_holder_anchor_input_witness_weight(&self) -> u64 {
ANCHOR_INPUT_WITNESS_WEIGHT
}
}

const MISSING_PARAMS_ERR: &'static str =
Expand Down
6 changes: 5 additions & 1 deletion lightning/src/util/test_channel_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// licenses.

use crate::ln::channel::{ANCHOR_OUTPUT_VALUE_SATOSHI, MIN_CHAN_DUST_LIMIT_SATOSHIS};
use crate::ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction};
use crate::ln::chan_utils::{ANCHOR_INPUT_WITNESS_WEIGHT, HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction};
use crate::ln::channel_keys::{HtlcKey};
use crate::ln::msgs;
use crate::types::payment::PaymentPreimage;
Expand Down Expand Up @@ -371,6 +371,10 @@ impl ChannelSigner for TestChannelSigner {
) -> Result<Transaction, ()> {
self.inner.spend_holder_anchor_input(anchor_tx, input_idx, secp_ctx)
}

fn get_holder_anchor_input_witness_weight(&self) -> u64 {
ANCHOR_INPUT_WITNESS_WEIGHT
}
}

impl EcdsaChannelSigner for TestChannelSigner {
Expand Down

0 comments on commit 250a1f6

Please sign in to comment.