Skip to content

Commit 2fbe512

Browse files
optout21adi2011
authored andcommitted
Include base input fee in fee calculation
The base input fee was missing in calculate_our_funding_satoshis(), it is added now; also add unit test.
1 parent 7485880 commit 2fbe512

File tree

1 file changed

+85
-3
lines changed

1 file changed

+85
-3
lines changed

lightning/src/ln/channel.rs

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use crate::chain::transaction::{OutPoint, TransactionData};
5757
use crate::sign::ecdsa::EcdsaChannelSigner;
5858
use crate::sign::{EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
5959
use crate::events::{ClosureReason, Event};
60+
use crate::events::bump_transaction::BASE_INPUT_WEIGHT;
6061
use crate::routing::gossip::NodeId;
6162
use crate::util::ser::{Readable, ReadableArgs, TransactionU16LenLimited, Writeable, Writer};
6263
use crate::util::logger::{Logger, Record, WithContext};
@@ -4477,7 +4478,6 @@ pub(super) fn calculate_our_funding_satoshis(
44774478
holder_dust_limit_satoshis: u64,
44784479
) -> Result<u64, APIError> {
44794480
let mut total_input_satoshis = 0u64;
4480-
let mut our_contributed_weight = 0u64;
44814481

44824482
for (idx, input) in funding_inputs.iter().enumerate() {
44834483
if let Some(output) = input.1.as_transaction().output.get(input.0.previous_output.vout as usize) {
@@ -4488,6 +4488,9 @@ pub(super) fn calculate_our_funding_satoshis(
44884488
input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
44894489
}
44904490
}
4491+
// inputs:
4492+
let mut our_contributed_weight = (funding_inputs.len() as u64) * BASE_INPUT_WEIGHT;
4493+
// witnesses:
44914494
our_contributed_weight = our_contributed_weight.saturating_add(total_witness_weight.to_wu());
44924495

44934496
// If we are the initiator, we must pay for weight of all common fields in the funding transaction.
@@ -10466,7 +10469,7 @@ mod tests {
1046610469
use bitcoin::amount::Amount;
1046710470
use bitcoin::constants::ChainHash;
1046810471
use bitcoin::script::{ScriptBuf, Builder};
10469-
use bitcoin::transaction::{Transaction, TxOut, Version};
10472+
use bitcoin::transaction::{Transaction, TxIn, TxOut, Version};
1047010473
use bitcoin::opcodes;
1047110474
use bitcoin::network::Network;
1047210475
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
@@ -10488,7 +10491,7 @@ mod tests {
1048810491
use crate::routing::router::{Path, RouteHop};
1048910492
use crate::util::config::UserConfig;
1049010493
use crate::util::errors::APIError;
10491-
use crate::util::ser::{ReadableArgs, Writeable};
10494+
use crate::util::ser::{ReadableArgs, TransactionU16LenLimited, Writeable};
1049210495
use crate::util::test_utils;
1049310496
use crate::util::test_utils::{OnGetShutdownScriptpubkey, TestKeysInterface};
1049410497
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
@@ -12238,4 +12241,83 @@ mod tests {
1223812241
assert_eq!(node_a_chan.context.channel_state, ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY));
1223912242
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1224012243
}
12244+
12245+
fn funding_input_sats(input_value_sats: u64) -> (TxIn, TransactionU16LenLimited) {
12246+
let input_1_prev_out = TxOut { value: Amount::from_sat(input_value_sats), script_pubkey: ScriptBuf::default() };
12247+
let input_1_prev_tx = Transaction {
12248+
input: vec![], output: vec![input_1_prev_out],
12249+
version: Version::TWO, lock_time: bitcoin::absolute::LockTime::ZERO,
12250+
};
12251+
let input_1_txin = TxIn {
12252+
previous_output: bitcoin::OutPoint { txid: input_1_prev_tx.compute_txid(), vout: 0 },
12253+
..Default::default()
12254+
};
12255+
(input_1_txin, TransactionU16LenLimited::new(input_1_prev_tx).unwrap())
12256+
}
12257+
12258+
#[test]
12259+
fn test_calculate_our_funding_satoshis() {
12260+
use crate::ln::channel::calculate_our_funding_satoshis;
12261+
use bitcoin::Weight;
12262+
12263+
// normal use case, output is less than the available inputs
12264+
assert_eq!(
12265+
calculate_our_funding_satoshis(
12266+
true,
12267+
&[
12268+
funding_input_sats(200_000),
12269+
funding_input_sats(100_000),
12270+
],
12271+
Weight::from_wu(300),
12272+
2000,
12273+
1000,
12274+
).unwrap(),
12275+
298332
12276+
);
12277+
12278+
assert_eq!(
12279+
calculate_our_funding_satoshis(
12280+
true,
12281+
&[funding_input_sats(20_000)],
12282+
Weight::from_wu(300),
12283+
2000,
12284+
1000,
12285+
).unwrap(),
12286+
18652
12287+
);
12288+
12289+
assert_eq!(
12290+
calculate_our_funding_satoshis(
12291+
true,
12292+
&[funding_input_sats(20_000)],
12293+
Weight::from_wu(0),
12294+
2000,
12295+
1000,
12296+
).unwrap(),
12297+
19252
12298+
);
12299+
12300+
assert_eq!(
12301+
calculate_our_funding_satoshis(
12302+
false,
12303+
&[funding_input_sats(20_000)],
12304+
Weight::from_wu(0),
12305+
2000,
12306+
1000,
12307+
).unwrap(),
12308+
19680
12309+
);
12310+
12311+
// below dust limit
12312+
assert_eq!(
12313+
calculate_our_funding_satoshis(
12314+
true,
12315+
&[funding_input_sats(20_000)],
12316+
Weight::from_wu(300),
12317+
2000,
12318+
20_000,
12319+
).unwrap(),
12320+
0
12321+
);
12322+
}
1224112323
}

0 commit comments

Comments
 (0)