From bb14110b09f79944d0fd622d761375b18269e577 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Thu, 27 Sep 2018 11:33:18 -0500 Subject: [PATCH 1/2] Fix small issue in the NodeLatencyVerifier that caused incorrect (very small) readings to no-reachable nodes due to no internet connection. --- .../network/NodeLatencyVerifier.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/graphenej/src/main/java/cy/agorise/graphenej/network/NodeLatencyVerifier.java b/graphenej/src/main/java/cy/agorise/graphenej/network/NodeLatencyVerifier.java index 6531b92..cc1a3ef 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/network/NodeLatencyVerifier.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/network/NodeLatencyVerifier.java @@ -22,7 +22,7 @@ import okhttp3.WebSocketListener; public class NodeLatencyVerifier { private final String TAG = this.getClass().getName(); - public static final int DEFAULT_LATENCY_VERIFICATION_PERIOD = 5 * 1000; + private static final int DEFAULT_LATENCY_VERIFICATION_PERIOD = 5 * 1000; // Variable used to store the list of nodes that should be verified private List mNodeList; @@ -88,7 +88,7 @@ public class NodeLatencyVerifier { long before = System.currentTimeMillis(); timestamps.put(fullNode, before); - // We want to reuse the same OkHttoClient instance if possible + // We want to reuse the same OkHttpClient instance if possible if(client == null) client = new OkHttpClient(); // Same thing with the Request instance, we want to reuse them. But since @@ -145,9 +145,18 @@ public class NodeLatencyVerifier { HttpUrl url = webSocket.request().url(); if(nodeURLMap.containsKey(url)){ FullNode fullNode = nodeURLMap.get(url); - long after = System.currentTimeMillis(); - long before = timestamps.get(fullNode); - long delay = after - before; + long delay; + + if(response == null) { + // There is no internet connection, or the node is unreachable. We are just + // putting an artificial delay. + delay = 10000; + } else { + long after = System.currentTimeMillis(); + long before = timestamps.get(fullNode); + delay = after - before; + } + fullNode.addLatencyValue(delay); subject.onNext(fullNode); }else{ From 9374b125a1d44d46c35a16ddbc58faf410c00544 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Thu, 27 Sep 2018 13:10:28 -0500 Subject: [PATCH 2/2] Use a more descriptive value (simulating infinite) when the node could not be reached, in NodeLatencyVerifier --- .../java/cy/agorise/graphenej/network/NodeLatencyVerifier.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphenej/src/main/java/cy/agorise/graphenej/network/NodeLatencyVerifier.java b/graphenej/src/main/java/cy/agorise/graphenej/network/NodeLatencyVerifier.java index cc1a3ef..93712d9 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/network/NodeLatencyVerifier.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/network/NodeLatencyVerifier.java @@ -150,7 +150,7 @@ public class NodeLatencyVerifier { if(response == null) { // There is no internet connection, or the node is unreachable. We are just // putting an artificial delay. - delay = 10000; + delay = Long.MAX_VALUE; } else { long after = System.currentTimeMillis(); long before = timestamps.get(fullNode);