Skip to content

Commit ab8f5a8

Browse files
authored
Merge pull request #374 from dongcarl/2019-08-channel-open-sanity
tests: Add sanity tests for ChannelOpens
2 parents 56f16ea + 00410bf commit ab8f5a8

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
@@ -403,7 +403,7 @@ impl Channel {
403403
/// Returns a minimum channel reserve value **they** need to maintain
404404
///
405405
/// Guaranteed to return a value no larger than channel_value_satoshis
406-
fn get_our_channel_reserve_satoshis(channel_value_satoshis: u64) -> u64 {
406+
pub(crate) fn get_our_channel_reserve_satoshis(channel_value_satoshis: u64) -> u64 {
407407
let (q, _) = channel_value_satoshis.overflowing_div(100);
408408
cmp::min(channel_value_satoshis, cmp::max(q, 1000)) //TODO
409409
}

src/ln/functional_tests.rs

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

5050
use ln::functional_test_utils::*;
5151

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

0 commit comments

Comments
 (0)