Repository: Bitmessage/PyBitmessage

Issue:
API clientStatus bug
**came over BM. I haven't tested it, just crosspasting**

------------------------------------------------------

In their infinite wisdom the devs have decided on the following logic:
If currently connected networkStatus is 'notConnected'
If not currently connected and have not received any incoming connections. networkStatus is "connectedButHaveNotReceivedIncomingConnections"
Otherwise networkStatus is "connectedAndReceivingIncomingConnections"

-------------------------------------------------------

Hmm well for me it's been working fine until just recently.

------------------------------------------------------
in --curses it never worked even be4

------------------------------------------------------
I've been running a headless BM instance for a while and I use the api.clientStatus call to monitor it.  I noticed that the networkStatus field says "notConnected" when it's connected and receiving incoming connections.
I don't know if this was today's pull or the one before that introduced it.


Discussion:
Yes, it [looks strange](../blob/v0.6/src/api.py#L1331). Introduced in 7ed459b.
Previous edition was:
```python
if len(network.stats.connectedHostsList()) == 0:
    networkStatus = 'notConnected'
```

Resolving patch:
diff --git a/src/api.py b/src/api.py
--- a/src/api.py
+++ b/src/api.py
@@ -1328,15 +1328,15 @@ def HandleGetMessageDataByDestinationHash(self, params):
     def HandleClientStatus(self, params):  # pylint: disable=unused-argument
         """Handle a request to get the status of the client"""
 
-        if network.stats.connectedHostsList():
+        connections_num = len(network.stats.connectedHostsList())
+        if connections_num == 0:
             networkStatus = 'notConnected'
-        elif not network.stats.connectedHostsList() \
-                and not shared.clientHasReceivedIncomingConnections:
-            networkStatus = 'connectedButHaveNotReceivedIncomingConnections'
-        else:
+        elif shared.clientHasReceivedIncomingConnections:
             networkStatus = 'connectedAndReceivingIncomingConnections'
+        else:
+            networkStatus = 'connectedButHaveNotReceivedIncomingConnections'
         return json.dumps({
-            'networkConnections': len(network.stats.connectedHostsList()),
+            'networkConnections': connections_num,
             'numberOfMessagesProcessed': shared.numberOfMessagesProcessed,
             'numberOfBroadcastsProcessed': shared.numberOfBroadcastsProcessed,
             'numberOfPubkeysProcessed': shared.numberOfPubkeysProcessed,


Test patch:
diff --git a/src/tests/test_api.py b/src/tests/test_api.py
--- a/src/tests/test_api.py
+++ b/src/tests/test_api.py
@@ -60,6 +60,14 @@ def test_invalid_method(self):
             'API Error 0020: Invalid method: test'
         )
 
+    def test_clientstatus_consistency(self):
+        """If networkStatus is notConnected networkConnections should be 0"""
+        status = json.loads(self.api.clientStatus())
+        if status["networkStatus"] == "notConnected":
+            self.assertEqual(status["networkConnections"], 0)
+        else:
+            self.assertGreater(status["networkConnections"], 0)
+
     def test_list_addresses(self):
         """Checking the return of API command 'listAddresses'"""
         self.assertEqual(