Skip to content

Commit 1891b37

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 8dca0b4 commit 1891b37

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

lightning/src/ln/channel.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4723,6 +4723,15 @@ impl<Signer: Sign> Channel<Signer> {
47234723
}
47244724
}
47254725

4726+
/// Enables the possibility for tests to extract a [`msgs::AcceptChannel`] message for an
4727+
/// inbound channel without accepting it.
4728+
///
4729+
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
4730+
#[cfg(test)]
4731+
pub fn get_accept_channel_message(&self) -> msgs::AcceptChannel {
4732+
self.generate_accept_channel_message()
4733+
}
4734+
47264735
/// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
47274736
fn get_outbound_funding_created_signature<L: Deref>(&mut self, logger: &L) -> Result<Signature, ChannelError> where L::Target: Logger {
47284737
let counterparty_keys = self.build_remote_transaction_keys()?;

lightning/src/ln/functional_tests.rs

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8300,6 +8300,207 @@ fn test_override_0msat_htlc_minimum() {
83008300
assert_eq!(res.htlc_minimum_msat, 1);
83018301
}
83028302

8303+
#[test]
8304+
fn test_manually_accept_inbound_channel_request() {
8305+
let mut manually_accept_conf = UserConfig::default();
8306+
manually_accept_conf.manually_accept_inbound_channels = true;
8307+
let chanmon_cfgs = create_chanmon_cfgs(2);
8308+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8309+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8310+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8311+
8312+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8313+
let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8314+
8315+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
8316+
8317+
// Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
8318+
// accepting the inbound channel request.
8319+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8320+
8321+
let events = nodes[1].node.get_and_clear_pending_events();
8322+
match events[0] {
8323+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
8324+
nodes[1].node.accept_inbound_channel(&temporary_channel_id).unwrap();
8325+
}
8326+
_ => panic!("Unexpected event"),
8327+
}
8328+
8329+
let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8330+
assert_eq!(accept_msg_ev.len(), 1);
8331+
8332+
match accept_msg_ev[0] {
8333+
MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
8334+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8335+
}
8336+
_ => panic!("Unexpected event"),
8337+
}
8338+
}
8339+
8340+
#[test]
8341+
fn test_manually_reject_inbound_channel_request() {
8342+
let mut manually_accept_conf = UserConfig::default();
8343+
manually_accept_conf.manually_accept_inbound_channels = true;
8344+
let chanmon_cfgs = create_chanmon_cfgs(2);
8345+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8346+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8347+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8348+
8349+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8350+
let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8351+
8352+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
8353+
8354+
// Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
8355+
// rejecting the inbound channel request.
8356+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8357+
8358+
let events = nodes[1].node.get_and_clear_pending_events();
8359+
match events[0] {
8360+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
8361+
nodes[1].node.force_close_channel(&temporary_channel_id).unwrap();
8362+
}
8363+
_ => panic!("Unexpected event"),
8364+
}
8365+
8366+
let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8367+
assert_eq!(close_msg_ev.len(), 1);
8368+
8369+
match close_msg_ev[0] {
8370+
MessageSendEvent::HandleError { ref node_id, .. } => {
8371+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8372+
}
8373+
_ => panic!("Unexpected event"),
8374+
}
8375+
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
8376+
}
8377+
8378+
#[test]
8379+
fn test_reject_funding_before_inbound_channel_accepted() {
8380+
// This tests that when `UserConfig::manually_accept_inbound_channels` is set to true, inbound
8381+
// channels must to be manually accepted through `ChannelManager::accept_inbound_channel` by
8382+
// the node operator before the counterparty sends a `FundingCreated` message. If a
8383+
// `FundingCreated` message is received before the channel is accepted, it should be rejected
8384+
// and the channel should be closed.
8385+
let mut manually_accept_conf = UserConfig::default();
8386+
manually_accept_conf.manually_accept_inbound_channels = true;
8387+
let chanmon_cfgs = create_chanmon_cfgs(2);
8388+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8389+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8390+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8391+
8392+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8393+
let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8394+
let temp_channel_id = res.temporary_channel_id;
8395+
8396+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
8397+
8398+
// Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in the `msg_events`.
8399+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8400+
8401+
// Clear the `Event::OpenChannelRequest` event without responding to the request.
8402+
nodes[1].node.get_and_clear_pending_events();
8403+
8404+
// Get the `AcceptChannel` message of `nodes[1]` without calling
8405+
// `ChannelManager::accept_inbound_channel`, which generates a
8406+
// `MessageSendEvent::SendAcceptChannel` event. The message is passed to `nodes[0]`
8407+
// `handle_accept_channel`, which is required in order for `create_funding_transaction` to
8408+
// succeed when `nodes[0]` is passed to it.
8409+
{
8410+
let mut lock;
8411+
let channel = get_channel_ref!(&nodes[1], lock, temp_channel_id);
8412+
let accept_chan_msg = channel.get_accept_channel_message();
8413+
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_chan_msg);
8414+
}
8415+
8416+
let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], 100000, 42);
8417+
8418+
nodes[0].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
8419+
let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
8420+
8421+
// The `funding_created_msg` should be rejected by `nodes[1]` as it hasn't accepted the channel
8422+
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
8423+
8424+
let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8425+
assert_eq!(close_msg_ev.len(), 1);
8426+
8427+
let expected_err = "FundingCreated message received before the channel was accepted";
8428+
match close_msg_ev[0] {
8429+
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, ref node_id, } => {
8430+
assert_eq!(msg.channel_id, temp_channel_id);
8431+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8432+
assert_eq!(msg.data, expected_err);
8433+
}
8434+
_ => panic!("Unexpected event"),
8435+
}
8436+
8437+
check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: expected_err.to_string() });
8438+
}
8439+
8440+
#[test]
8441+
fn test_can_not_accept_inbound_channel_twice() {
8442+
let mut manually_accept_conf = UserConfig::default();
8443+
manually_accept_conf.manually_accept_inbound_channels = true;
8444+
let chanmon_cfgs = create_chanmon_cfgs(2);
8445+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8446+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8447+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8448+
8449+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8450+
let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8451+
8452+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
8453+
8454+
// Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
8455+
// accepting the inbound channel request.
8456+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8457+
8458+
let events = nodes[1].node.get_and_clear_pending_events();
8459+
match events[0] {
8460+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
8461+
nodes[1].node.accept_inbound_channel(&temporary_channel_id).unwrap();
8462+
let api_res = nodes[1].node.accept_inbound_channel(&temporary_channel_id);
8463+
match api_res {
8464+
Err(APIError::APIMisuseError { err }) => {
8465+
assert_eq!(err, "The channel isn't currently awaiting to be accepted.");
8466+
},
8467+
Ok(_) => panic!("Channel shouldn't be possible to be accepted twice"),
8468+
Err(_) => panic!("Unexpected Error"),
8469+
}
8470+
}
8471+
_ => panic!("Unexpected event"),
8472+
}
8473+
8474+
// Ensure that the channel wasn't closed after attempting to accept it twice.
8475+
let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8476+
assert_eq!(accept_msg_ev.len(), 1);
8477+
8478+
match accept_msg_ev[0] {
8479+
MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
8480+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8481+
}
8482+
_ => panic!("Unexpected event"),
8483+
}
8484+
}
8485+
8486+
#[test]
8487+
fn test_can_not_accept_unknown_inbound_channel() {
8488+
let chanmon_cfg = create_chanmon_cfgs(1);
8489+
let node_cfg = create_node_cfgs(1, &chanmon_cfg);
8490+
let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
8491+
let node = create_network(1, &node_cfg, &node_chanmgr)[0].node;
8492+
8493+
let unknown_channel_id = [0; 32];
8494+
let api_res = node.accept_inbound_channel(&unknown_channel_id);
8495+
match api_res {
8496+
Err(APIError::ChannelUnavailable { err }) => {
8497+
assert_eq!(err, "Can't accept a channel that doesn't exist");
8498+
},
8499+
Ok(_) => panic!("It shouldn't be possible to accept an unkown channel"),
8500+
Err(_) => panic!("Unexpected Error"),
8501+
}
8502+
}
8503+
83038504
#[test]
83048505
fn test_simple_mpp() {
83058506
// Simple test of sending a multi-path payment.

0 commit comments

Comments
 (0)