From 7fba0faac1fd4341e487e4c47077bf97f367bbd5 Mon Sep 17 00:00:00 2001 From: woodser <woodser@protonmail.com> Date: Tue, 14 Jan 2025 11:02:03 -0500 Subject: [PATCH] best connection defaults to singular instance --- core/src/main/java/haveno/core/api/CoreApi.java | 4 ++-- .../haveno/core/api/XmrConnectionService.java | 17 ++++++++--------- .../daemon/grpc/GrpcXmrConnectionService.java | 16 ++++++++-------- proto/src/main/proto/grpc.proto | 6 +++--- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/haveno/core/api/CoreApi.java b/core/src/main/java/haveno/core/api/CoreApi.java index c91d4a405b..5b0c9e247a 100644 --- a/core/src/main/java/haveno/core/api/CoreApi.java +++ b/core/src/main/java/haveno/core/api/CoreApi.java @@ -239,8 +239,8 @@ public class CoreApi { xmrConnectionService.stopCheckingConnection(); } - public MoneroRpcConnection getBestAvailableXmrConnection() { - return xmrConnectionService.getBestAvailableConnection(); + public MoneroRpcConnection getBestXmrConnection() { + return xmrConnectionService.getBestConnection(); } public void setXmrConnectionAutoSwitch(boolean autoSwitch) { diff --git a/core/src/main/java/haveno/core/api/XmrConnectionService.java b/core/src/main/java/haveno/core/api/XmrConnectionService.java index dfac9bc85a..640aa2b404 100644 --- a/core/src/main/java/haveno/core/api/XmrConnectionService.java +++ b/core/src/main/java/haveno/core/api/XmrConnectionService.java @@ -255,17 +255,16 @@ public final class XmrConnectionService { updatePolling(); } - public MoneroRpcConnection getBestAvailableConnection() { + public MoneroRpcConnection getBestConnection() { accountService.checkAccountOpen(); - List<MoneroRpcConnection> ignoredConnections = new ArrayList<MoneroRpcConnection>(); - addLocalNodeIfIgnored(ignoredConnections); - return connectionManager.getBestAvailableConnection(ignoredConnections.toArray(new MoneroRpcConnection[0])); + return getBestConnection(new ArrayList<MoneroRpcConnection>()); } - private MoneroRpcConnection getBestAvailableConnection(Collection<MoneroRpcConnection> ignoredConnections) { + private MoneroRpcConnection getBestConnection(Collection<MoneroRpcConnection> ignoredConnections) { accountService.checkAccountOpen(); Set<MoneroRpcConnection> ignoredConnectionsSet = new HashSet<>(ignoredConnections); addLocalNodeIfIgnored(ignoredConnectionsSet); + if (connectionManager.getConnections().size() == 1 && !ignoredConnectionsSet.contains(connectionManager.getConnections().get(0))) return connectionManager.getConnections().get(0); return connectionManager.getBestAvailableConnection(ignoredConnectionsSet.toArray(new MoneroRpcConnection[0])); } @@ -278,7 +277,7 @@ public final class XmrConnectionService { log.info("Skipping switch to best Monero connection because connection is fixed or auto switch is disabled"); return; } - MoneroRpcConnection bestConnection = getBestAvailableConnection(); + MoneroRpcConnection bestConnection = getBestConnection(); if (bestConnection != null) setConnection(bestConnection); } @@ -329,7 +328,7 @@ public final class XmrConnectionService { if (currentConnection != null) excludedConnections.add(currentConnection); // get connection to switch to - MoneroRpcConnection bestConnection = getBestAvailableConnection(excludedConnections); + MoneroRpcConnection bestConnection = getBestConnection(excludedConnections); // remove from excluded connections after period UserThread.runAfter(() -> { @@ -545,7 +544,7 @@ public final class XmrConnectionService { if (isConnected) { setConnection(connection.getUri()); } else if (getConnection() != null && getConnection().getUri().equals(connection.getUri())) { - MoneroRpcConnection bestConnection = getBestAvailableConnection(); + MoneroRpcConnection bestConnection = getBestConnection(); if (bestConnection != null) setConnection(bestConnection); // switch to best connection } } @@ -610,7 +609,7 @@ public final class XmrConnectionService { // update connection if (connectionManager.getConnection() == null || connectionManager.getAutoSwitch()) { - MoneroRpcConnection bestConnection = getBestAvailableConnection(); + MoneroRpcConnection bestConnection = getBestConnection(); if (bestConnection != null) setConnection(bestConnection); } } else if (!isInitialized) { diff --git a/daemon/src/main/java/haveno/daemon/grpc/GrpcXmrConnectionService.java b/daemon/src/main/java/haveno/daemon/grpc/GrpcXmrConnectionService.java index a03dc5a73e..f0fcdcc984 100644 --- a/daemon/src/main/java/haveno/daemon/grpc/GrpcXmrConnectionService.java +++ b/daemon/src/main/java/haveno/daemon/grpc/GrpcXmrConnectionService.java @@ -47,8 +47,8 @@ import haveno.proto.grpc.CheckConnectionsReply; import haveno.proto.grpc.CheckConnectionsRequest; import haveno.proto.grpc.GetAutoSwitchReply; import haveno.proto.grpc.GetAutoSwitchRequest; -import haveno.proto.grpc.GetBestAvailableConnectionReply; -import haveno.proto.grpc.GetBestAvailableConnectionRequest; +import haveno.proto.grpc.GetBestConnectionReply; +import haveno.proto.grpc.GetBestConnectionRequest; import haveno.proto.grpc.GetConnectionReply; import haveno.proto.grpc.GetConnectionRequest; import haveno.proto.grpc.GetConnectionsReply; @@ -68,7 +68,7 @@ import static haveno.proto.grpc.XmrConnectionsGrpc.XmrConnectionsImplBase; import static haveno.proto.grpc.XmrConnectionsGrpc.getAddConnectionMethod; import static haveno.proto.grpc.XmrConnectionsGrpc.getCheckConnectionMethod; import static haveno.proto.grpc.XmrConnectionsGrpc.getCheckConnectionsMethod; -import static haveno.proto.grpc.XmrConnectionsGrpc.getGetBestAvailableConnectionMethod; +import static haveno.proto.grpc.XmrConnectionsGrpc.getGetBestConnectionMethod; import static haveno.proto.grpc.XmrConnectionsGrpc.getGetConnectionMethod; import static haveno.proto.grpc.XmrConnectionsGrpc.getGetConnectionsMethod; import static haveno.proto.grpc.XmrConnectionsGrpc.getRemoveConnectionMethod; @@ -201,12 +201,12 @@ class GrpcXmrConnectionService extends XmrConnectionsImplBase { } @Override - public void getBestAvailableConnection(GetBestAvailableConnectionRequest request, - StreamObserver<GetBestAvailableConnectionReply> responseObserver) { + public void getBestConnection(GetBestConnectionRequest request, + StreamObserver<GetBestConnectionReply> responseObserver) { handleRequest(responseObserver, () -> { - MoneroRpcConnection connection = coreApi.getBestAvailableXmrConnection(); + MoneroRpcConnection connection = coreApi.getBestXmrConnection(); UrlConnection replyConnection = toUrlConnection(connection); - GetBestAvailableConnectionReply.Builder builder = GetBestAvailableConnectionReply.newBuilder(); + GetBestConnectionReply.Builder builder = GetBestConnectionReply.newBuilder(); if (replyConnection != null) { builder.setConnection(replyConnection); } @@ -314,7 +314,7 @@ class GrpcXmrConnectionService extends XmrConnectionsImplBase { put(getCheckConnectionsMethod().getFullMethodName(), new GrpcCallRateMeter(allowedCallsPerTimeWindow, SECONDS)); put(getStartCheckingConnectionMethod().getFullMethodName(), new GrpcCallRateMeter(allowedCallsPerTimeWindow, SECONDS)); put(getStopCheckingConnectionMethod().getFullMethodName(), new GrpcCallRateMeter(allowedCallsPerTimeWindow, SECONDS)); - put(getGetBestAvailableConnectionMethod().getFullMethodName(), new GrpcCallRateMeter(allowedCallsPerTimeWindow, SECONDS)); + put(getGetBestConnectionMethod().getFullMethodName(), new GrpcCallRateMeter(allowedCallsPerTimeWindow, SECONDS)); put(getSetAutoSwitchMethod().getFullMethodName(), new GrpcCallRateMeter(allowedCallsPerTimeWindow, SECONDS)); }} ))); diff --git a/proto/src/main/proto/grpc.proto b/proto/src/main/proto/grpc.proto index c9b8a75445..f99a0feae5 100644 --- a/proto/src/main/proto/grpc.proto +++ b/proto/src/main/proto/grpc.proto @@ -319,7 +319,7 @@ service XmrConnections { } rpc StopCheckingConnection(StopCheckingConnectionRequest) returns (StopCheckingConnectionReply) { } - rpc GetBestAvailableConnection(GetBestAvailableConnectionRequest) returns (GetBestAvailableConnectionReply) { + rpc GetBestConnection(GetBestConnectionRequest) returns (GetBestConnectionReply) { } rpc SetAutoSwitch(SetAutoSwitchRequest) returns (SetAutoSwitchReply) { } @@ -400,9 +400,9 @@ message StopCheckingConnectionRequest {} message StopCheckingConnectionReply {} -message GetBestAvailableConnectionRequest {} +message GetBestConnectionRequest {} -message GetBestAvailableConnectionReply { +message GetBestConnectionReply { UrlConnection connection = 1; }