Skip to content

Commit 0917689

Browse files
committed
Add unit test for calculate_our_funding_satoshis()
1 parent 8257cc3 commit 0917689

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
@@ -10438,7 +10438,7 @@ mod tests {
1043810438
use bitcoin::amount::Amount;
1043910439
use bitcoin::constants::ChainHash;
1044010440
use bitcoin::script::{ScriptBuf, Builder};
10441-
use bitcoin::transaction::{Transaction, TxOut, Version};
10441+
use bitcoin::transaction::{Transaction, TxIn, TxOut, Version};
1044210442
use bitcoin::opcodes;
1044310443
use bitcoin::network::Network;
1044410444
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
@@ -10460,7 +10460,7 @@ mod tests {
1046010460
use crate::routing::router::{Path, RouteHop};
1046110461
use crate::util::config::UserConfig;
1046210462
use crate::util::errors::APIError;
10463-
use crate::util::ser::{ReadableArgs, Writeable};
10463+
use crate::util::ser::{ReadableArgs, TransactionU16LenLimited, Writeable};
1046410464
use crate::util::test_utils;
1046510465
use crate::util::test_utils::{OnGetShutdownScriptpubkey, TestKeysInterface};
1046610466
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
@@ -12210,4 +12210,64 @@ mod tests {
1221012210
assert_eq!(node_a_chan.context.channel_state, ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY));
1221112211
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1221212212
}
12213+
12214+
fn prepare_input(value: u64) -> (TxIn, TransactionU16LenLimited) {
12215+
let input_1_prev_out = TxOut { value: Amount::from_sat(value), script_pubkey: ScriptBuf::default() };
12216+
let input_1_prev_tx = Transaction {
12217+
input: vec![], output: vec![input_1_prev_out],
12218+
version: Version::TWO, lock_time: bitcoin::absolute::LockTime::ZERO,
12219+
};
12220+
let input_1_txin = TxIn {
12221+
previous_output: bitcoin::OutPoint { txid: input_1_prev_tx.compute_txid(), vout: 0 },
12222+
..Default::default()
12223+
};
12224+
(input_1_txin, TransactionU16LenLimited::new(input_1_prev_tx).unwrap())
12225+
}
12226+
12227+
#[test]
12228+
fn test_calculate_our_funding_satoshis() {
12229+
use crate::ln::channel::calculate_our_funding_satoshis;
12230+
use bitcoin::Weight;
12231+
12232+
let inputs_1 = [
12233+
prepare_input(20_000),
12234+
];
12235+
let inputs_2 = [
12236+
prepare_input(200_000),
12237+
prepare_input(100_000),
12238+
];
12239+
let witness_weight = Weight::from_wu(300);
12240+
12241+
// normal use case, output is less than the available inputs
12242+
assert_eq!(
12243+
calculate_our_funding_satoshis(true, &inputs_2, witness_weight, 2000, 1000)
12244+
.unwrap(),
12245+
298972
12246+
);
12247+
12248+
assert_eq!(
12249+
calculate_our_funding_satoshis(true, &inputs_1, witness_weight, 2000, 1000)
12250+
.unwrap(),
12251+
18972
12252+
);
12253+
12254+
assert_eq!(
12255+
calculate_our_funding_satoshis(true, &inputs_1, Weight::from_wu(0), 2000, 1000)
12256+
.unwrap(),
12257+
19572
12258+
);
12259+
12260+
assert_eq!(
12261+
calculate_our_funding_satoshis(false, &inputs_1, Weight::from_wu(0), 2000, 1000)
12262+
.unwrap(),
12263+
20000
12264+
);
12265+
12266+
// below dust limit
12267+
assert_eq!(
12268+
calculate_our_funding_satoshis(true, &inputs_1, witness_weight, 2000, 20_000)
12269+
.unwrap(),
12270+
0
12271+
);
12272+
}
1221312273
}

0 commit comments

Comments
 (0)