Repository: Bitmessage/PyBitmessage

Issue:
Wrong lastseen values got written to knownnodes in bmproto
Hello!

While investigating #1335 I discovered another potential bug.
In the [line 380 in bmproto](../blob/v0.6/src/network/bmproto.py#L380) value of `seenTime` is often wrong. I added a simple check:
```python
    try:
        assert time.time() - seenTime > 0
    except AssertionError:
        logger.warning("node %s: lastseen was: %s!", decodedIP, seenTime)
        seenTime = time.time()
```

and found values like 281474976710890, 14699749183745158766 or 2448129849.


Discussion:
Today I also found peers with port 0 (:
Port 0 used to be filtered, maybe I forgot to move the check when moving to asyncore.
Like this?
```diff
diff --git a/src/network/bmproto.py b/src/network/bmproto.py
index 1f1c67bd..ddcbccdd 100644
--- a/src/network/bmproto.py
+++ b/src/network/bmproto.py
@@ -381,14 +381,16 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
             decodedIP = protocol.checkIPAddress(str(ip))
             if stream not in state.streamsInWhichIAmParticipating:
                 continue
-            if decodedIP is not False and seenTime > time.time() - BMProto.addressAlive:
+            if (decodedIP and time.time() - seenTime > 0 and
+                seenTime > time.time() - BMProto.addressAlive and
+                    port > 0):
                 peer = state.Peer(decodedIP, port)
                 try:
                     if knownnodes.knownNodes[stream][peer]["lastseen"] > seenTime:
                         continue
                 except KeyError:
                     pass
-                if len(knownnodes.knownNodes[stream]) < int(BMConfigParser().get("knownnodes", "maxnodes")):
+                if len(knownnodes.knownNodes[stream]) < BMConfigParser().safeGetInt("knownnodes", "maxnodes"):
                     with knownnodes.knownNodesLock:
                         try:
                             knownnodes.knownNodes[stream][peer]["lastseen"] = seenTime

```

Resolving patch:
diff --git a/src/network/bmproto.py b/src/network/bmproto.py
--- a/src/network/bmproto.py
+++ b/src/network/bmproto.py
@@ -381,14 +381,18 @@ def bm_command_addr(self):
             decodedIP = protocol.checkIPAddress(str(ip))
             if stream not in state.streamsInWhichIAmParticipating:
                 continue
-            if decodedIP is not False and seenTime > time.time() - BMProto.addressAlive:
+            if (
+                decodedIP and time.time() - seenTime > 0 and
+                seenTime > time.time() - BMProto.addressAlive and
+                port > 0
+            ):
                 peer = state.Peer(decodedIP, port)
                 try:
                     if knownnodes.knownNodes[stream][peer]["lastseen"] > seenTime:
                         continue
                 except KeyError:
                     pass
-                if len(knownnodes.knownNodes[stream]) < int(BMConfigParser().get("knownnodes", "maxnodes")):
+                if len(knownnodes.knownNodes[stream]) < BMConfigParser().safeGetInt("knownnodes", "maxnodes"):
                     with knownnodes.knownNodesLock:
                         try:
                             knownnodes.knownNodes[stream][peer]["lastseen"] = seenTime


Test patch: