+
+ fn check_not_connected_to_peer_error<T>(res_err: Result<T, APIError>, expected_public_key: PublicKey) {
+ let expected_message = format!("Not connected to node: {}", expected_public_key);
+ check_api_error_message(expected_message, res_err)
+ }
+
+ fn check_unkown_peer_error<T>(res_err: Result<T, APIError>, expected_public_key: PublicKey) {
+ let expected_message = format!("Can't find a peer matching the passed counterparty node_id {}", expected_public_key);
+ check_api_error_message(expected_message, res_err)
+ }
+
+ fn check_api_error_message<T>(expected_err_message: String, res_err: Result<T, APIError>) {
+ match res_err {
+ Err(APIError::APIMisuseError { err }) => {
+ assert_eq!(err, expected_err_message);
+ },
+ Err(APIError::ChannelUnavailable { err }) => {
+ assert_eq!(err, expected_err_message);
+ },
+ Ok(_) => panic!("Unexpected Ok"),
+ Err(_) => panic!("Unexpected Error"),
+ }
+ }
+
+ #[test]
+ fn test_api_calls_with_unkown_counterparty_node() {
+ // Tests that our API functions that expects a `counterparty_node_id` as input, behaves as
+ // expected if the `counterparty_node_id` is an unkown peer in the
+ // `ChannelManager::per_peer_state` map.
+ let chanmon_cfg = create_chanmon_cfgs(2);
+ let node_cfg = create_node_cfgs(2, &chanmon_cfg);
+ let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
+ let nodes = create_network(2, &node_cfg, &node_chanmgr);
+
+ // Dummy values
+ let channel_id = [4; 32];
+ let unkown_public_key = PublicKey::from_secret_key(&Secp256k1::signing_only(), &SecretKey::from_slice(&[42; 32]).unwrap());
+ let intercept_id = InterceptId([0; 32]);
+
+ // Test the API functions.
+ check_not_connected_to_peer_error(nodes[0].node.create_channel(unkown_public_key, 1_000_000, 500_000_000, 42, None), unkown_public_key);
+
+ check_unkown_peer_error(nodes[0].node.accept_inbound_channel(&channel_id, &unkown_public_key, 42), unkown_public_key);
+
+ check_unkown_peer_error(nodes[0].node.close_channel(&channel_id, &unkown_public_key), unkown_public_key);
+
+ check_unkown_peer_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &unkown_public_key), unkown_public_key);
+
+ check_unkown_peer_error(nodes[0].node.force_close_without_broadcasting_txn(&channel_id, &unkown_public_key), unkown_public_key);
+
+ check_unkown_peer_error(nodes[0].node.forward_intercepted_htlc(intercept_id, &channel_id, unkown_public_key, 1_000_000), unkown_public_key);
+
+ check_unkown_peer_error(nodes[0].node.update_channel_config(&unkown_public_key, &[channel_id], &ChannelConfig::default()), unkown_public_key);
+ }
+
+ #[test]
+ fn test_connection_limiting() {
+ // Test that we limit un-channel'd peers and un-funded channels properly.
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ // Note that create_network connects the nodes together for us
+
+ nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
+ let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+
+ let mut funding_tx = None;
+ for idx in 0..super::MAX_UNFUNDED_CHANS_PER_PEER {
+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
+ let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
+
+ if idx == 0 {
+ nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
+ let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100_000, 42);
+ funding_tx = Some(tx.clone());
+ nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx).unwrap();
+ let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
+
+ nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
+ check_added_monitors!(nodes[1], 1);
+ let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
+
+ nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
+ check_added_monitors!(nodes[0], 1);
+ }
+ open_channel_msg.temporary_channel_id = nodes[0].keys_manager.get_secure_random_bytes();
+ }
+
+ // A MAX_UNFUNDED_CHANS_PER_PEER + 1 channel will be summarily rejected
+ open_channel_msg.temporary_channel_id = nodes[0].keys_manager.get_secure_random_bytes();
+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
+ assert_eq!(get_err_msg!(nodes[1], nodes[0].node.get_our_node_id()).channel_id,
+ open_channel_msg.temporary_channel_id);
+
+ // Further, because all of our channels with nodes[0] are inbound, and none of them funded,
+ // it doesn't count as a "protected" peer, i.e. it counts towards the MAX_NO_CHANNEL_PEERS
+ // limit.
+ let mut peer_pks = Vec::with_capacity(super::MAX_NO_CHANNEL_PEERS);
+ for _ in 1..super::MAX_NO_CHANNEL_PEERS {
+ let random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
+ &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
+ peer_pks.push(random_pk);
+ nodes[1].node.peer_connected(&random_pk, &msgs::Init {
+ features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+ }
+ let last_random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
+ &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
+ nodes[1].node.peer_connected(&last_random_pk, &msgs::Init {
+ features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap_err();
+
+ // Also importantly, because nodes[0] isn't "protected", we will refuse a reconnection from
+ // them if we have too many un-channel'd peers.
+ nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
+ let chan_closed_events = nodes[1].node.get_and_clear_pending_events();
+ assert_eq!(chan_closed_events.len(), super::MAX_UNFUNDED_CHANS_PER_PEER - 1);
+ for ev in chan_closed_events {
+ if let Event::ChannelClosed { .. } = ev { } else { panic!(); }
+ }
+ nodes[1].node.peer_connected(&last_random_pk, &msgs::Init {
+ features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+ nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+ features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap_err();
+
+ // but of course if the connection is outbound its allowed...
+ nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+ features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
+ nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
+
+ // Now nodes[0] is disconnected but still has a pending, un-funded channel lying around.
+ // Even though we accept one more connection from new peers, we won't actually let them
+ // open channels.
+ assert!(peer_pks.len() > super::MAX_UNFUNDED_CHANNEL_PEERS - 1);
+ for i in 0..super::MAX_UNFUNDED_CHANNEL_PEERS - 1 {
+ nodes[1].node.handle_open_channel(&peer_pks[i], &open_channel_msg);
+ get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, peer_pks[i]);
+ open_channel_msg.temporary_channel_id = nodes[0].keys_manager.get_secure_random_bytes();
+ }
+ nodes[1].node.handle_open_channel(&last_random_pk, &open_channel_msg);
+ assert_eq!(get_err_msg!(nodes[1], last_random_pk).channel_id,
+ open_channel_msg.temporary_channel_id);
+
+ // Of course, however, outbound channels are always allowed
+ nodes[1].node.create_channel(last_random_pk, 100_000, 0, 42, None).unwrap();
+ get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, last_random_pk);
+
+ // If we fund the first channel, nodes[0] has a live on-chain channel with us, it is now
+ // "protected" and can connect again.
+ mine_transaction(&nodes[1], funding_tx.as_ref().unwrap());
+ nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
+ features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+ get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
+
+ // Further, because the first channel was funded, we can open another channel with
+ // last_random_pk.
+ nodes[1].node.handle_open_channel(&last_random_pk, &open_channel_msg);
+ get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
+ }
+
+ #[test]
+ fn test_outbound_chans_unlimited() {
+ // Test that we never refuse an outbound channel even if a peer is unfuned-channel-limited
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ // Note that create_network connects the nodes together for us
+
+ nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
+ let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+
+ for _ in 0..super::MAX_UNFUNDED_CHANS_PER_PEER {
+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
+ get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
+ open_channel_msg.temporary_channel_id = nodes[0].keys_manager.get_secure_random_bytes();
+ }
+
+ // Once we have MAX_UNFUNDED_CHANS_PER_PEER unfunded channels, new inbound channels will be
+ // rejected.
+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
+ assert_eq!(get_err_msg!(nodes[1], nodes[0].node.get_our_node_id()).channel_id,
+ open_channel_msg.temporary_channel_id);
+
+ // but we can still open an outbound channel.
+ nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
+ get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
+
+ // but even with such an outbound channel, additional inbound channels will still fail.
+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
+ assert_eq!(get_err_msg!(nodes[1], nodes[0].node.get_our_node_id()).channel_id,
+ open_channel_msg.temporary_channel_id);
+ }
+
+ #[test]
+ fn test_0conf_limiting() {
+ // Tests that we properly limit inbound channels when we have the manual-channel-acceptance
+ // flag set and (sometimes) accept channels as 0conf.
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let mut settings = test_default_channel_config();
+ settings.manually_accept_inbound_channels = true;
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(settings)]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ // Note that create_network connects the nodes together for us
+
+ nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
+ let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+
+ // First, get us up to MAX_UNFUNDED_CHANNEL_PEERS so we can test at the edge
+ for _ in 0..super::MAX_UNFUNDED_CHANNEL_PEERS - 1 {
+ let random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
+ &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
+ nodes[1].node.peer_connected(&random_pk, &msgs::Init {
+ features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+
+ nodes[1].node.handle_open_channel(&random_pk, &open_channel_msg);
+ let events = nodes[1].node.get_and_clear_pending_events();
+ match events[0] {
+ Event::OpenChannelRequest { temporary_channel_id, .. } => {
+ nodes[1].node.accept_inbound_channel(&temporary_channel_id, &random_pk, 23).unwrap();
+ }
+ _ => panic!("Unexpected event"),
+ }
+ get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, random_pk);
+ open_channel_msg.temporary_channel_id = nodes[0].keys_manager.get_secure_random_bytes();
+ }
+
+ // If we try to accept a channel from another peer non-0conf it will fail.
+ let last_random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
+ &SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
+ nodes[1].node.peer_connected(&last_random_pk, &msgs::Init {
+ features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
+ nodes[1].node.handle_open_channel(&last_random_pk, &open_channel_msg);
+ let events = nodes[1].node.get_and_clear_pending_events();
+ match events[0] {
+ Event::OpenChannelRequest { temporary_channel_id, .. } => {
+ match nodes[1].node.accept_inbound_channel(&temporary_channel_id, &last_random_pk, 23) {
+ Err(APIError::APIMisuseError { err }) =>
+ assert_eq!(err, "Too many peers with unfunded channels, refusing to accept new ones"),
+ _ => panic!(),
+ }
+ }
+ _ => panic!("Unexpected event"),
+ }
+ assert_eq!(get_err_msg!(nodes[1], last_random_pk).channel_id,
+ open_channel_msg.temporary_channel_id);
+
+ // ...however if we accept the same channel 0conf it should work just fine.
+ nodes[1].node.handle_open_channel(&last_random_pk, &open_channel_msg);
+ let events = nodes[1].node.get_and_clear_pending_events();
+ match events[0] {
+ Event::OpenChannelRequest { temporary_channel_id, .. } => {
+ nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &last_random_pk, 23).unwrap();
+ }
+ _ => panic!("Unexpected event"),
+ }
+ get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
+ }
+
+ #[cfg(anchors)]
+ #[test]
+ fn test_anchors_zero_fee_htlc_tx_fallback() {
+ // Tests that if both nodes support anchors, but the remote node does not want to accept
+ // anchor channels at the moment, an error it sent to the local node such that it can retry
+ // the channel without the anchors feature.
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let mut anchors_config = test_default_channel_config();
+ anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
+ anchors_config.manually_accept_inbound_channels = true;
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config.clone()), Some(anchors_config.clone())]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 0, None).unwrap();
+ let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+ assert!(open_channel_msg.channel_type.as_ref().unwrap().supports_anchors_zero_fee_htlc_tx());
+
+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
+ let events = nodes[1].node.get_and_clear_pending_events();
+ match events[0] {
+ Event::OpenChannelRequest { temporary_channel_id, .. } => {
+ nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id()).unwrap();
+ }
+ _ => panic!("Unexpected event"),
+ }
+
+ let error_msg = get_err_msg!(nodes[1], nodes[0].node.get_our_node_id());
+ nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &error_msg);
+
+ let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+ assert!(!open_channel_msg.channel_type.unwrap().supports_anchors_zero_fee_htlc_tx());
+
+ check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
+ }