]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Add `disconnectpeer` subcommand
authorChiru <48340051+ch1ru@users.noreply.github.com>
Tue, 1 Nov 2022 17:29:33 +0000 (17:29 +0000)
committerChiru <48340051+ch1ru@users.noreply.github.com>
Tue, 1 Nov 2022 20:58:17 +0000 (20:58 +0000)
Disconnect peer with `disconnectpeer <pubkey>`.
Checks all channels are closed before disconnecting.

src/cli.rs

index 99a43784cb69ad55b2aabd9598f6c47d6db3a9be..64ee04df71874ee24bfbd356f37ceb0ea0a85322 100644 (file)
@@ -357,6 +357,32 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                println!("SUCCESS: connected to peer {}", pubkey);
                                        }
                                }
+                               "disconnectpeer" => {
+                                       let peer_pubkey = words.next();
+                                       if peer_pubkey.is_none() {
+                                               println!("ERROR: disconnectpeer requires peer public key: `disconnectpeer <peer_pubkey>`");
+                                               continue;
+                                       }
+
+                                       let peer_pubkey =
+                                               match bitcoin::secp256k1::PublicKey::from_str(peer_pubkey.unwrap()) {
+                                                       Ok(pubkey) => pubkey,
+                                                       Err(e) => {
+                                                               println!("ERROR: {}", e.to_string());
+                                                               continue;
+                                                       }
+                                               };
+
+                                       if do_disconnect_peer(
+                                               peer_pubkey,
+                                               peer_manager.clone(),
+                                               channel_manager.clone(),
+                                       )
+                                       .is_ok()
+                                       {
+                                               println!("SUCCESS: disconnected from peer {}", peer_pubkey);
+                                       }
+                               }
                                "listchannels" => list_channels(&channel_manager, &network_graph),
                                "listpayments" => {
                                        list_payments(inbound_payments.clone(), outbound_payments.clone())
@@ -530,6 +556,7 @@ fn help() {
        println!("      listchannels");
        println!("\n  Peers:");
        println!("      connectpeer pubkey@host:port");
+       println!("      disconnectpeer <peer_pubkey>");
        println!("      listpeers");
        println!("\n  Payments:");
        println!("      sendpayment <invoice>");
@@ -688,6 +715,29 @@ pub(crate) async fn do_connect_peer(
        }
 }
 
+fn do_disconnect_peer(
+       pubkey: bitcoin::secp256k1::PublicKey, peer_manager: Arc<PeerManager>,
+       channel_manager: Arc<ChannelManager>,
+) -> Result<(), ()> {
+       //check for open channels with peer
+       for channel in channel_manager.list_channels() {
+               if channel.counterparty.node_id == pubkey {
+                       println!("Error: Node has an active channel with this peer, close any channels first");
+                       return Err(());
+               }
+       }
+
+       //check the pubkey matches a valid connected peer
+       let peers = peer_manager.get_peer_node_ids();
+       if !peers.contains(&pubkey) {
+               println!("Error: Could not find peer {}", pubkey);
+               return Err(());
+       }
+
+       peer_manager.disconnect_by_node_id(pubkey, false);
+       Ok(())
+}
+
 fn open_channel(
        peer_pubkey: PublicKey, channel_amt_sat: u64, announced_channel: bool,
        channel_manager: Arc<ChannelManager>,