Skip to content

Commit 4459d20

Browse files
committed
Add unit test for calculate_our_funding_satoshis()
1 parent b1fc7d8 commit 4459d20

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

lightning/src/ln/channel.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10464,7 +10464,7 @@ mod tests {
1046410464
use bitcoin::amount::Amount;
1046510465
use bitcoin::constants::ChainHash;
1046610466
use bitcoin::script::{ScriptBuf, Builder};
10467-
use bitcoin::transaction::{Transaction, TxOut, Version};
10467+
use bitcoin::transaction::{Transaction, TxIn, TxOut, Version};
1046810468
use bitcoin::opcodes;
1046910469
use bitcoin::network::Network;
1047010470
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
@@ -10486,7 +10486,7 @@ mod tests {
1048610486
use crate::routing::router::{Path, RouteHop};
1048710487
use crate::util::config::UserConfig;
1048810488
use crate::util::errors::APIError;
10489-
use crate::util::ser::{ReadableArgs, Writeable};
10489+
use crate::util::ser::{ReadableArgs, TransactionU16LenLimited, Writeable};
1049010490
use crate::util::test_utils;
1049110491
use crate::util::test_utils::{OnGetShutdownScriptpubkey, TestKeysInterface};
1049210492
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
@@ -12236,4 +12236,64 @@ mod tests {
1223612236
assert_eq!(node_a_chan.context.channel_state, ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY));
1223712237
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1223812238
}
12239+
12240+
fn prepare_input(value: u64) -> (TxIn, TransactionU16LenLimited) {
12241+
let input_1_prev_out = TxOut { value: Amount::from_sat(value), script_pubkey: ScriptBuf::default() };
12242+
let input_1_prev_tx = Transaction {
12243+
input: vec![], output: vec![input_1_prev_out],
12244+
version: Version::TWO, lock_time: bitcoin::absolute::LockTime::ZERO,
12245+
};
12246+
let input_1_txin = TxIn {
12247+
previous_output: bitcoin::OutPoint { txid: input_1_prev_tx.compute_txid(), vout: 0 },
12248+
..Default::default()
12249+
};
12250+
(input_1_txin, TransactionU16LenLimited::new(input_1_prev_tx).unwrap())
12251+
}
12252+
12253+
#[test]
12254+
fn test_calculate_our_funding_satoshis() {
12255+
use crate::ln::channel::calculate_our_funding_satoshis;
12256+
use bitcoin::Weight;
12257+
12258+
let inputs_1 = [
12259+
prepare_input(20_000),
12260+
];
12261+
let inputs_2 = [
12262+
prepare_input(200_000),
12263+
prepare_input(100_000),
12264+
];
12265+
let witness_weight = Weight::from_wu(300);
12266+
12267+
// normal use case, output is less than the available inputs
12268+
assert_eq!(
12269+
calculate_our_funding_satoshis(true, &inputs_2, witness_weight, 2000, 1000)
12270+
.unwrap(),
12271+
298972
12272+
);
12273+
12274+
assert_eq!(
12275+
calculate_our_funding_satoshis(true, &inputs_1, witness_weight, 2000, 1000)
12276+
.unwrap(),
12277+
18972
12278+
);
12279+
12280+
assert_eq!(
12281+
calculate_our_funding_satoshis(true, &inputs_1, Weight::from_wu(0), 2000, 1000)
12282+
.unwrap(),
12283+
19572
12284+
);
12285+
12286+
assert_eq!(
12287+
calculate_our_funding_satoshis(false, &inputs_1, Weight::from_wu(0), 2000, 1000)
12288+
.unwrap(),
12289+
20000
12290+
);
12291+
12292+
// below dust limit
12293+
assert_eq!(
12294+
calculate_our_funding_satoshis(true, &inputs_1, witness_weight, 2000, 20_000)
12295+
.unwrap(),
12296+
0
12297+
);
12298+
}
1223912299
}

0 commit comments

Comments
 (0)