Skip to content

Commit 00410bf

Browse files
committed
tests: Add sanity tests for ChannelOpens
1 parent e65e03f commit 00410bf

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/ln/channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl Channel {
390390
/// Returns a minimum channel reserve value **they** need to maintain
391391
///
392392
/// Guaranteed to return a value no larger than channel_value_satoshis
393-
fn get_our_channel_reserve_satoshis(channel_value_satoshis: u64) -> u64 {
393+
pub(crate) fn get_our_channel_reserve_satoshis(channel_value_satoshis: u64) -> u64 {
394394
let (q, _) = channel_value_satoshis.overflowing_div(100);
395395
cmp::min(channel_value_satoshis, cmp::max(q, 1000)) //TODO
396396
}

src/ln/functional_tests.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,58 @@ use rand::{thread_rng, Rng};
4747

4848
use ln::functional_test_utils::*;
4949

50+
#[test]
51+
fn test_insane_channel_opens() {
52+
// Stand up a network of 2 nodes
53+
let nodes = create_network(2, &[None, None]);
54+
55+
// Instantiate channel parameters where we push the maximum msats given our
56+
// funding satoshis
57+
let channel_value_sat = 31337; // same as funding satoshis
58+
let channel_reserve_satoshis = Channel::get_our_channel_reserve_satoshis(channel_value_sat);
59+
let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000;
60+
61+
// Have node0 initiate a channel to node1 with aforementioned parameters
62+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_sat, push_msat, 42).unwrap();
63+
64+
// Extract the channel open message from node0 to node1
65+
let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
66+
67+
// Test helper that asserts we get the correct error string given a mutator
68+
// that supposedly makes the channel open message insane
69+
let insane_open_helper = |expected_error_str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| {
70+
match nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &message_mutator(open_channel_message.clone())) {
71+
Err(msgs::HandleError{ err: error_str, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) => {
72+
assert_eq!(error_str, expected_error_str, "unexpected HandleError string (expected `{}`, actual `{}`)", expected_error_str, error_str)
73+
},
74+
Err(msgs::HandleError{..}) => {panic!("unexpected HandleError action")},
75+
_ => panic!("insane OpenChannel message was somehow Ok"),
76+
}
77+
};
78+
79+
use ln::channel::MAX_FUNDING_SATOSHIS;
80+
use ln::channelmanager::MAX_LOCAL_BREAKDOWN_TIMEOUT;
81+
82+
// Test all mutations that would make the channel open message insane
83+
insane_open_helper("funding value > 2^24", |mut msg| { msg.funding_satoshis = MAX_FUNDING_SATOSHIS; msg });
84+
85+
insane_open_helper("Bogus channel_reserve_satoshis", |mut msg| { msg.channel_reserve_satoshis = msg.funding_satoshis + 1; msg });
86+
87+
insane_open_helper("push_msat larger than funding value", |mut msg| { msg.push_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 + 1; msg });
88+
89+
insane_open_helper("Peer never wants payout outputs?", |mut msg| { msg.dust_limit_satoshis = msg.funding_satoshis + 1 ; msg });
90+
91+
insane_open_helper("Bogus; channel reserve is less than dust limit", |mut msg| { msg.dust_limit_satoshis = msg.channel_reserve_satoshis + 1; msg });
92+
93+
insane_open_helper("Minimum htlc value is full channel value", |mut msg| { msg.htlc_minimum_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000; msg });
94+
95+
insane_open_helper("They wanted our payments to be delayed by a needlessly long period", |mut msg| { msg.to_self_delay = MAX_LOCAL_BREAKDOWN_TIMEOUT + 1; msg });
96+
97+
insane_open_helper("0 max_accpted_htlcs makes for a useless channel", |mut msg| { msg.max_accepted_htlcs = 0; msg });
98+
99+
insane_open_helper("max_accpted_htlcs > 483", |mut msg| { msg.max_accepted_htlcs = 484; msg });
100+
}
101+
50102
#[test]
51103
fn test_async_inbound_update_fee() {
52104
let mut nodes = create_network(2, &[None, None]);

0 commit comments

Comments
 (0)