@@ -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};
@@ -4477,7 +4478,6 @@ pub(super) fn calculate_our_funding_satoshis(
4477
4478
holder_dust_limit_satoshis: u64,
4478
4479
) -> Result<u64, APIError> {
4479
4480
let mut total_input_satoshis = 0u64;
4480
- let mut our_contributed_weight = 0u64;
4481
4481
4482
4482
for (idx, input) in funding_inputs.iter().enumerate() {
4483
4483
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(
4488
4488
input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
4489
4489
}
4490
4490
}
4491
+ // inputs:
4492
+ let mut our_contributed_weight = (funding_inputs.len() as u64) * BASE_INPUT_WEIGHT;
4493
+ // witnesses:
4491
4494
our_contributed_weight = our_contributed_weight.saturating_add(total_witness_weight.to_wu());
4492
4495
4493
4496
// If we are the initiator, we must pay for weight of all common fields in the funding transaction.
@@ -10466,7 +10469,7 @@ mod tests {
10466
10469
use bitcoin::amount::Amount;
10467
10470
use bitcoin::constants::ChainHash;
10468
10471
use bitcoin::script::{ScriptBuf, Builder};
10469
- use bitcoin::transaction::{Transaction, TxOut, Version};
10472
+ use bitcoin::transaction::{Transaction, TxIn, TxOut, Version};
10470
10473
use bitcoin::opcodes;
10471
10474
use bitcoin::network::Network;
10472
10475
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
@@ -10488,7 +10491,7 @@ mod tests {
10488
10491
use crate::routing::router::{Path, RouteHop};
10489
10492
use crate::util::config::UserConfig;
10490
10493
use crate::util::errors::APIError;
10491
- use crate::util::ser::{ReadableArgs, Writeable};
10494
+ use crate::util::ser::{ReadableArgs, TransactionU16LenLimited, Writeable};
10492
10495
use crate::util::test_utils;
10493
10496
use crate::util::test_utils::{OnGetShutdownScriptpubkey, TestKeysInterface};
10494
10497
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
@@ -12238,4 +12241,83 @@ mod tests {
12238
12241
assert_eq!(node_a_chan.context.channel_state, ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY));
12239
12242
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
12240
12243
}
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
+ }
12241
12323
}
0 commit comments