@@ -57,6 +57,7 @@ use crate::chain::transaction::{OutPoint, TransactionData};
57
57
use crate::sign::ecdsa::EcdsaChannelSigner;
58
58
use crate::sign::{EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
59
59
use crate::events::{ClosureReason, Event};
60
+ use crate::events::bump_transaction::BASE_INPUT_WEIGHT;
60
61
use crate::routing::gossip::NodeId;
61
62
use crate::util::ser::{Readable, ReadableArgs, TransactionU16LenLimited, Writeable, Writer};
62
63
use crate::util::logger::{Logger, Record, WithContext};
@@ -4475,7 +4476,6 @@ pub(super) fn calculate_our_funding_satoshis(
4475
4476
holder_dust_limit_satoshis: u64,
4476
4477
) -> Result<u64, APIError> {
4477
4478
let mut total_input_satoshis = 0u64;
4478
- let mut our_contributed_weight = 0u64;
4479
4479
4480
4480
for (idx, input) in funding_inputs.iter().enumerate() {
4481
4481
if let Some(output) = input.1.as_transaction().output.get(input.0.previous_output.vout as usize) {
@@ -4486,6 +4486,9 @@ pub(super) fn calculate_our_funding_satoshis(
4486
4486
input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
4487
4487
}
4488
4488
}
4489
+ // inputs:
4490
+ let mut our_contributed_weight = (funding_inputs.len() as u64) * BASE_INPUT_WEIGHT;
4491
+ // witnesses:
4489
4492
our_contributed_weight = our_contributed_weight.saturating_add(total_witness_weight.to_wu());
4490
4493
4491
4494
// If we are the initiator, we must pay for weight of all common fields in the funding transaction.
@@ -10464,7 +10467,7 @@ mod tests {
10464
10467
use bitcoin::amount::Amount;
10465
10468
use bitcoin::constants::ChainHash;
10466
10469
use bitcoin::script::{ScriptBuf, Builder};
10467
- use bitcoin::transaction::{Transaction, TxOut, Version};
10470
+ use bitcoin::transaction::{Transaction, TxIn, TxOut, Version};
10468
10471
use bitcoin::opcodes;
10469
10472
use bitcoin::network::Network;
10470
10473
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
@@ -10486,7 +10489,7 @@ mod tests {
10486
10489
use crate::routing::router::{Path, RouteHop};
10487
10490
use crate::util::config::UserConfig;
10488
10491
use crate::util::errors::APIError;
10489
- use crate::util::ser::{ReadableArgs, Writeable};
10492
+ use crate::util::ser::{ReadableArgs, TransactionU16LenLimited, Writeable};
10490
10493
use crate::util::test_utils;
10491
10494
use crate::util::test_utils::{OnGetShutdownScriptpubkey, TestKeysInterface};
10492
10495
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
@@ -12236,4 +12239,83 @@ mod tests {
12236
12239
assert_eq!(node_a_chan.context.channel_state, ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY));
12237
12240
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
12238
12241
}
12242
+
12243
+ fn funding_input_sats(input_value_sats: u64) -> (TxIn, TransactionU16LenLimited) {
12244
+ let input_1_prev_out = TxOut { value: Amount::from_sat(input_value_sats), script_pubkey: ScriptBuf::default() };
12245
+ let input_1_prev_tx = Transaction {
12246
+ input: vec![], output: vec![input_1_prev_out],
12247
+ version: Version::TWO, lock_time: bitcoin::absolute::LockTime::ZERO,
12248
+ };
12249
+ let input_1_txin = TxIn {
12250
+ previous_output: bitcoin::OutPoint { txid: input_1_prev_tx.compute_txid(), vout: 0 },
12251
+ ..Default::default()
12252
+ };
12253
+ (input_1_txin, TransactionU16LenLimited::new(input_1_prev_tx).unwrap())
12254
+ }
12255
+
12256
+ #[test]
12257
+ fn test_calculate_our_funding_satoshis() {
12258
+ use crate::ln::channel::calculate_our_funding_satoshis;
12259
+ use bitcoin::Weight;
12260
+
12261
+ // normal use case, output is less than the available inputs
12262
+ assert_eq!(
12263
+ calculate_our_funding_satoshis(
12264
+ true,
12265
+ &[
12266
+ funding_input_sats(200_000),
12267
+ funding_input_sats(100_000),
12268
+ ],
12269
+ Weight::from_wu(300),
12270
+ 2000,
12271
+ 1000,
12272
+ ).unwrap(),
12273
+ 298332
12274
+ );
12275
+
12276
+ assert_eq!(
12277
+ calculate_our_funding_satoshis(
12278
+ true,
12279
+ &[funding_input_sats(20_000)],
12280
+ Weight::from_wu(300),
12281
+ 2000,
12282
+ 1000,
12283
+ ).unwrap(),
12284
+ 18652
12285
+ );
12286
+
12287
+ assert_eq!(
12288
+ calculate_our_funding_satoshis(
12289
+ true,
12290
+ &[funding_input_sats(20_000)],
12291
+ Weight::from_wu(0),
12292
+ 2000,
12293
+ 1000,
12294
+ ).unwrap(),
12295
+ 19252
12296
+ );
12297
+
12298
+ assert_eq!(
12299
+ calculate_our_funding_satoshis(
12300
+ false,
12301
+ &[funding_input_sats(20_000)],
12302
+ Weight::from_wu(0),
12303
+ 2000,
12304
+ 1000,
12305
+ ).unwrap(),
12306
+ 19680
12307
+ );
12308
+
12309
+ // below dust limit
12310
+ assert_eq!(
12311
+ calculate_our_funding_satoshis(
12312
+ true,
12313
+ &[funding_input_sats(20_000)],
12314
+ Weight::from_wu(300),
12315
+ 2000,
12316
+ 20_000,
12317
+ ).unwrap(),
12318
+ 0
12319
+ );
12320
+ }
12239
12321
}
0 commit comments