Skip to content

Commit dbe587c

Browse files
Add tests for responding to inbound channel reqs
Add functional tests for manually responding to inbound channel requests. Responding to inbound channel requests are required when the `manually_accept_inbound_channels` config flag is set to true. The tests cover the following cases: * Accepting an inbound channel request * Rejecting an inbound channel request * FundingCreated message sent by the counterparty before accepting the inbound channel request * Attempting to accept an inbound channel request twice * Attempting to accept an unkown inbound channel
1 parent 59a4ac6 commit dbe587c

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8174,6 +8174,210 @@ fn test_override_0msat_htlc_minimum() {
81748174
assert_eq!(res.htlc_minimum_msat, 1);
81758175
}
81768176

8177+
#[test]
8178+
fn test_manually_accept_inbound_channel_request() {
8179+
let mut manually_accept_conf = UserConfig::default();
8180+
manually_accept_conf.manually_accept_inbound_channels = true;
8181+
let chanmon_cfgs = create_chanmon_cfgs(2);
8182+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8183+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8184+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8185+
8186+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8187+
let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8188+
8189+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
8190+
8191+
// Assert that [`nodes[1]`] has no [`MessageSendEvent::SendAcceptChannel`] in the
8192+
// [`msg_events`] before accepting the inbound request
8193+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8194+
8195+
let events = nodes[1].node.get_and_clear_pending_events();
8196+
match events[0] {
8197+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
8198+
nodes[1].node.accept_inbound_channel(&temporary_channel_id).unwrap();
8199+
}
8200+
_ => panic!("Unexpected event"),
8201+
}
8202+
8203+
let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8204+
assert_eq!(accept_msg_ev.len(), 1);
8205+
8206+
match accept_msg_ev[0] {
8207+
MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
8208+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8209+
}
8210+
_ => panic!("Unexpected event"),
8211+
}
8212+
}
8213+
8214+
#[test]
8215+
fn test_manually_reject_inbound_channel_request() {
8216+
let mut manually_accept_conf = UserConfig::default();
8217+
manually_accept_conf.manually_accept_inbound_channels = true;
8218+
let chanmon_cfgs = create_chanmon_cfgs(2);
8219+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8220+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8221+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8222+
8223+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8224+
let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8225+
8226+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
8227+
8228+
// Assert that [`nodes[1]`] has no [`MessageSendEvent::SendAcceptChannel`] in the
8229+
// [`msg_events`] before rejecting the inbound request
8230+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8231+
8232+
let events = nodes[1].node.get_and_clear_pending_events();
8233+
match events[0] {
8234+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
8235+
nodes[1].node.force_close_channel(&temporary_channel_id).unwrap();
8236+
}
8237+
_ => panic!("Unexpected event"),
8238+
}
8239+
8240+
let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8241+
assert_eq!(close_msg_ev.len(), 1);
8242+
8243+
match close_msg_ev[0] {
8244+
MessageSendEvent::HandleError { ref node_id, .. } => {
8245+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8246+
}
8247+
_ => panic!("Unexpected event"),
8248+
}
8249+
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
8250+
}
8251+
8252+
#[test]
8253+
fn test_reject_funding_before_inbound_channel_accepted() {
8254+
// This tests that when the [`util::config::UserConfig.manually_accept_inbound_channels`] is
8255+
// set to true, inbound channels must to be manually accepted through the
8256+
// [`ln::channelmanager::ChannelManager::accept_inbound_channel`] function by the node operator
8257+
// before the counterparty sends a [`FundingCreated`] message. If a [`FundingCreated`] message
8258+
// is received before the channel is accepted, it should be rejected and the channel should be
8259+
// closed.
8260+
let mut manually_accept_conf = UserConfig::default();
8261+
manually_accept_conf.manually_accept_inbound_channels = true;
8262+
let chanmon_cfgs = create_chanmon_cfgs(2);
8263+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8264+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8265+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8266+
8267+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8268+
let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8269+
let temp_channel_id = res.temporary_channel_id;
8270+
8271+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
8272+
8273+
// Assert that [`nodes[1]`] has no [`MessageSendEvent::SendAcceptChannel`] in the
8274+
// [`msg_events`].
8275+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8276+
8277+
// Clear the [`Event::OpenChannelRequest`] event without responding to the request.
8278+
nodes[1].node.get_and_clear_pending_events();
8279+
8280+
// Get the [`AcceptChannel`] message of [`nodes[1]`] without calling the
8281+
// [`ChannelManager::accept_inbound_channel`] function, which generates the
8282+
// [`MessageSendEvent::SendAcceptChannel`] event. This is passed to [`nodes[0]`]
8283+
// [`handle_accept_channel`] function, which is required in order for the
8284+
// [`create_funding_transaction`] function to succeed when [`nodes[0]`] is passed to it.
8285+
{
8286+
let mut lock;
8287+
let channel = get_channel_ref!(&nodes[1], lock, temp_channel_id);
8288+
let accept_chan_msg = channel.get_accept_channel_message();
8289+
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_chan_msg);
8290+
}
8291+
8292+
let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], 100000, 42);
8293+
8294+
nodes[0].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
8295+
let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
8296+
8297+
// The [`funding_created_msg`] should be rejected by [`nodes[1]`] as it hasn't accepted the
8298+
// channel.
8299+
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
8300+
8301+
let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8302+
assert_eq!(close_msg_ev.len(), 1);
8303+
8304+
let expected_err = "FundingCreated message received before the channel was accepted";
8305+
match close_msg_ev[0] {
8306+
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, ref node_id, } => {
8307+
assert_eq!(msg.channel_id, temp_channel_id);
8308+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8309+
assert_eq!(msg.data, expected_err);
8310+
}
8311+
_ => panic!("Unexpected event"),
8312+
}
8313+
8314+
check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: expected_err.to_string() });
8315+
}
8316+
8317+
#[test]
8318+
fn test_can_not_accept_inbound_channel_twice() {
8319+
let mut manually_accept_conf = UserConfig::default();
8320+
manually_accept_conf.manually_accept_inbound_channels = true;
8321+
let chanmon_cfgs = create_chanmon_cfgs(2);
8322+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8323+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8324+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8325+
8326+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8327+
let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8328+
8329+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
8330+
8331+
// Assert that [`nodes[1]`] has no [`MessageSendEvent::SendAcceptChannel`] in the
8332+
// [`msg_events`] before accepting the inbound request
8333+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8334+
8335+
let events = nodes[1].node.get_and_clear_pending_events();
8336+
match events[0] {
8337+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
8338+
nodes[1].node.accept_inbound_channel(&temporary_channel_id).unwrap();
8339+
let api_res = nodes[1].node.accept_inbound_channel(&temporary_channel_id);
8340+
match api_res {
8341+
Err(APIError::APIMisuseError { err }) => {
8342+
assert_eq!(err, "The channel isn't currently awaiting to be accepted.");
8343+
},
8344+
Ok(_) => panic!("Channel shouldn't be possible to be accepted twice"),
8345+
Err(_) => panic!("Unexpected Error"),
8346+
}
8347+
}
8348+
_ => panic!("Unexpected event"),
8349+
}
8350+
8351+
// Ensure that the channel wasn't closed after attempting to accept it twice.
8352+
let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8353+
assert_eq!(accept_msg_ev.len(), 1);
8354+
8355+
match accept_msg_ev[0] {
8356+
MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
8357+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8358+
}
8359+
_ => panic!("Unexpected event"),
8360+
}
8361+
}
8362+
8363+
#[test]
8364+
fn test_can_not_accept_unknown_inbound_channel() {
8365+
let chanmon_cfg = create_chanmon_cfgs(1);
8366+
let node_cfg = create_node_cfgs(1, &chanmon_cfg);
8367+
let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
8368+
let node = create_network(1, &node_cfg, &node_chanmgr)[0].node;
8369+
8370+
let unknown_channel_id = [0; 32];
8371+
let api_res = node.accept_inbound_channel(&unknown_channel_id);
8372+
match api_res {
8373+
Err(APIError::ChannelUnavailable { err }) => {
8374+
assert_eq!(err, "Can't accept a channel that doesn't exist");
8375+
},
8376+
Ok(_) => panic!("It shouldn't be possible to accept an unkown channel"),
8377+
Err(_) => panic!("Unexpected Error"),
8378+
}
8379+
}
8380+
81778381
#[test]
81788382
fn test_simple_mpp() {
81798383
// Simple test of sending a multi-path payment.

0 commit comments

Comments
 (0)