Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions lightning/src/ln/chan_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,27 @@ pub const HTLC_SUCCESS_INPUT_P2A_ANCHOR_WITNESS_WEIGHT: u64 = 324;
/// The size of the 2-of-2 multisig script
const MULTISIG_SCRIPT_SIZE: u64 = 1 + // OP_2
1 + // data len
33 + // pubkey1
crate::sign::COMPRESSED_PUBLIC_KEY_SIZE as u64 + // pubkey1
1 + // data len
33 + // pubkey2
crate::sign::COMPRESSED_PUBLIC_KEY_SIZE as u64 + // pubkey2
1 + // OP_2
1; // OP_CHECKMULTISIG
/// The weight of a funding transaction input (2-of-2 P2WSH)
/// See https://github.com/lightning/bolts/blob/master/03-transactions.md#expected-weight-of-the-commitment-transaction

/// The weight of a funding transaction input (2-of-2 P2WSH).
///
/// Unlike in the [spec], 72 WU is used for the max signature size since 73 WU signatures are
/// non-standard.
///
/// Note: If you have the `grind_signatures` feature enabled, this will be at least 1 byte
/// shorter.
///
/// [spec]: https://github.com/lightning/bolts/blob/master/03-transactions.md#expected-weight-of-the-commitment-transaction
pub const FUNDING_TRANSACTION_WITNESS_WEIGHT: u64 = 1 + // number_of_witness_elements
1 + // nil_len
1 + // sig len
73 + // sig1
crate::sign::MAX_STANDARD_SIGNATURE_SIZE as u64 + // sig1
1 + // sig len
73 + // sig2
crate::sign::MAX_STANDARD_SIGNATURE_SIZE as u64 + // sig2
1 + // witness_script_length
MULTISIG_SCRIPT_SIZE;

Expand Down
102 changes: 58 additions & 44 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6547,6 +6547,11 @@ fn estimate_v2_funding_transaction_fee(
.saturating_add(BASE_INPUT_WEIGHT)
.saturating_add(EMPTY_SCRIPT_SIG_WEIGHT)
.saturating_add(FUNDING_TRANSACTION_WITNESS_WEIGHT);
#[cfg(feature = "grind_signatures")]
{
// Guarantees a low R signature
weight -= 1;
}
}
}

Expand Down Expand Up @@ -17397,19 +17402,19 @@ mod tests {
// 2 inputs, initiator, 2000 sat/kw feerate
assert_eq!(
estimate_v2_funding_transaction_fee(&two_inputs, &[], true, false, 2000),
1520,
if cfg!(feature = "grind_signatures") { 1512 } else { 1516 },
);

// higher feerate
assert_eq!(
estimate_v2_funding_transaction_fee(&two_inputs, &[], true, false, 3000),
2280,
if cfg!(feature = "grind_signatures") { 2268 } else { 2274 },
);

// only 1 input
assert_eq!(
estimate_v2_funding_transaction_fee(&one_input, &[], true, false, 2000),
974,
if cfg!(feature = "grind_signatures") { 970 } else { 972 },
);

// 0 inputs
Expand All @@ -17427,13 +17432,13 @@ mod tests {
// splice initiator
assert_eq!(
estimate_v2_funding_transaction_fee(&one_input, &[], true, true, 2000),
1746,
if cfg!(feature = "grind_signatures") { 1736 } else { 1740 },
);

// splice acceptor
assert_eq!(
estimate_v2_funding_transaction_fee(&one_input, &[], false, true, 2000),
546,
if cfg!(feature = "grind_signatures") { 542 } else { 544 },
);
}

Expand All @@ -17457,40 +17462,46 @@ mod tests {
use crate::ln::channel::check_v2_funding_inputs_sufficient;

// positive case, inputs well over intended contribution
assert_eq!(
check_v2_funding_inputs_sufficient(
220_000,
&[
funding_input_sats(200_000),
funding_input_sats(100_000),
],
true,
true,
2000,
).unwrap(),
2292,
);
{
let expected_fee = if cfg!(feature = "grind_signatures") { 2278 } else { 2284 };
assert_eq!(
check_v2_funding_inputs_sufficient(
220_000,
&[
funding_input_sats(200_000),
funding_input_sats(100_000),
],
true,
true,
2000,
).unwrap(),
expected_fee,
);
}

// negative case, inputs clearly insufficient
{
let res = check_v2_funding_inputs_sufficient(
220_000,
&[
funding_input_sats(100_000),
],
true,
true,
2000,
);
let expected_fee = if cfg!(feature = "grind_signatures") { 1736 } else { 1740 };
assert_eq!(
res.err().unwrap(),
"Total input amount 100000 is lower than needed for contribution 220000, considering fees of 1746. Need more inputs.",
check_v2_funding_inputs_sufficient(
220_000,
&[
funding_input_sats(100_000),
],
true,
true,
2000,
),
Err(format!(
"Total input amount 100000 is lower than needed for contribution 220000, considering fees of {}. Need more inputs.",
expected_fee,
)),
);
}

// barely covers
{
let expected_fee: u64 = 2292;
let expected_fee = if cfg!(feature = "grind_signatures") { 2278 } else { 2284 };
assert_eq!(
check_v2_funding_inputs_sufficient(
(300_000 - expected_fee - 20) as i64,
Expand All @@ -17508,25 +17519,28 @@ mod tests {

// higher fee rate, does not cover
{
let res = check_v2_funding_inputs_sufficient(
298032,
&[
funding_input_sats(200_000),
funding_input_sats(100_000),
],
true,
true,
2200,
);
let expected_fee = if cfg!(feature = "grind_signatures") { 2506 } else { 2513 };
assert_eq!(
res.err().unwrap(),
"Total input amount 300000 is lower than needed for contribution 298032, considering fees of 2522. Need more inputs.",
check_v2_funding_inputs_sufficient(
298032,
&[
funding_input_sats(200_000),
funding_input_sats(100_000),
],
true,
true,
2200,
),
Err(format!(
"Total input amount 300000 is lower than needed for contribution 298032, considering fees of {}. Need more inputs.",
expected_fee
)),
);
}

// barely covers, less fees (no extra weight, no init)
// barely covers, less fees (no extra weight, not initiator)
{
let expected_fee: u64 = 1092;
let expected_fee = if cfg!(feature = "grind_signatures") { 1084 } else { 1088 };
assert_eq!(
check_v2_funding_inputs_sufficient(
(300_000 - expected_fee - 20) as i64,
Expand Down
13 changes: 12 additions & 1 deletion lightning/src/ln/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ impl FundingTxInput {
/// [`TxIn::sequence`]: bitcoin::TxIn::sequence
/// [`set_sequence`]: Self::set_sequence
pub fn new_p2wpkh(prevtx: Transaction, vout: u32) -> Result<Self, ()> {
let witness_weight = Weight::from_wu(P2WPKH_WITNESS_WEIGHT);
let witness_weight = Weight::from_wu(P2WPKH_WITNESS_WEIGHT)
- if cfg!(feature = "grind_signatures") {
// Guarantees a low R signature
Weight::from_wu(1)
} else {
Weight::ZERO
};
FundingTxInput::new(prevtx, vout, witness_weight, Script::is_p2wpkh)
}

Expand Down Expand Up @@ -224,4 +230,9 @@ impl FundingTxInput {
pub fn set_sequence(&mut self, sequence: Sequence) {
self.sequence = sequence;
}

/// Converts the [`FundingTxInput`] into a [`Utxo`] for coin selection.
pub fn into_utxo(self) -> Utxo {
self.utxo
}
}
Loading