From 2903ccaeb499a18cfe052b40c475892248e6b8a9 Mon Sep 17 00:00:00 2001
From: Jack Robison <jackrobison@lbry.io>
Date: Thu, 29 Mar 2018 10:46:29 -0400
Subject: [PATCH] add peer_ping

---
 CHANGELOG.md             |  1 +
 lbrynet/daemon/Daemon.py | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6281c5236..37acab451 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -47,6 +47,7 @@ at anytime.
   * virtual kademlia network and mock udp transport for dht integration tests
   * integration tests for bootstrapping the dht
   * configurable `concurrent_announcers` setting
+  * `peer_ping` command
 
 ### Removed
   * `announce_all` argument from `blob_announce`
diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py
index 44a87200c..61147a9e5 100644
--- a/lbrynet/daemon/Daemon.py
+++ b/lbrynet/daemon/Daemon.py
@@ -48,6 +48,7 @@ from lbrynet.core.server.ServerProtocol import ServerProtocolFactory
 from lbrynet.core.Error import InsufficientFundsError, UnknownNameError
 from lbrynet.core.Error import DownloadDataTimeout, DownloadSDTimeout
 from lbrynet.core.Error import NullFundsError, NegativeFundsError
+from lbrynet.dht.error import TimeoutError
 from lbrynet.core.Peer import Peer
 from lbrynet.core.SinglePeerDownloader import SinglePeerDownloader
 from lbrynet.core.client.StandaloneBlobDownloader import StandaloneBlobDownloader
@@ -3040,6 +3041,32 @@ class Daemon(AuthJSONRPCServer):
         d.addCallback(lambda r: self._render_response(r))
         return d
 
+    @defer.inlineCallbacks
+    def jsonrpc_peer_ping(self, node_id):
+        """
+        Find and ping a peer by node id
+
+        Usage:
+            peer_ping (<node_id> | --node_id=<node_id>)
+
+        Returns:
+            (str) pong, or {'error': <error message>} if an error is encountered
+        """
+
+        contact = None
+        try:
+            contact = yield self.session.dht_node.findContact(node_id.decode('hex'))
+        except TimeoutError:
+            result = {'error': 'timeout finding peer'}
+            defer.returnValue(result)
+        if not contact:
+            defer.returnValue({'error': 'peer not found'})
+        try:
+            result = yield contact.ping()
+        except TimeoutError:
+            result = {'error': 'ping timeout'}
+        defer.returnValue(result)
+
     def jsonrpc_routing_table_get(self):
         """
         Get DHT routing information