From b2a6708ac17aba2f7fc2a45594c7205dc25892bc Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 27 Sep 2024 12:09:35 -0400 Subject: [PATCH] sync blockchain depending on last used local node --- core/src/main/java/haveno/core/api/CoreApi.java | 4 ++-- .../java/haveno/core/api/XmrConnectionService.java | 8 +++++++- .../src/main/java/haveno/core/api/XmrLocalNode.java | 13 +++++++++---- .../main/java/haveno/core/xmr/XmrNodeSettings.java | 6 +++++- proto/src/main/proto/pb.proto | 1 + 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/haveno/core/api/CoreApi.java b/core/src/main/java/haveno/core/api/CoreApi.java index 25f98c3470..9afc4ee2b9 100644 --- a/core/src/main/java/haveno/core/api/CoreApi.java +++ b/core/src/main/java/haveno/core/api/CoreApi.java @@ -260,11 +260,11 @@ public class CoreApi { } public void startXmrNode(XmrNodeSettings settings) throws IOException { - xmrLocalNode.startNode(settings); + xmrLocalNode.start(settings); } public void stopXmrNode() { - xmrLocalNode.stopNode(); + xmrLocalNode.stop(); } /////////////////////////////////////////////////////////////////////////////////////////// diff --git a/core/src/main/java/haveno/core/api/XmrConnectionService.java b/core/src/main/java/haveno/core/api/XmrConnectionService.java index 2541fb9fc5..ffeacbfd38 100644 --- a/core/src/main/java/haveno/core/api/XmrConnectionService.java +++ b/core/src/main/java/haveno/core/api/XmrConnectionService.java @@ -623,7 +623,7 @@ public final class XmrConnectionService { if (connectionManager.getConnection() != null && xmrLocalNode.equalsUri(connectionManager.getConnection().getUri()) && !xmrLocalNode.isDetected() && !xmrLocalNode.shouldBeIgnored()) { try { log.info("Starting local node"); - xmrLocalNode.startMoneroNode(); + xmrLocalNode.start(); } catch (Exception e) { log.error("Unable to start local monero node, error={}\n", e.getMessage(), e); } @@ -736,6 +736,12 @@ public final class XmrConnectionService { // connected to daemon isConnected = true; + // determine if blockchain is syncing locally + boolean blockchainSyncing = lastInfo.getHeight().equals(lastInfo.getHeightWithoutBootstrap()) || (lastInfo.getTargetHeight().equals(0l) && lastInfo.getHeightWithoutBootstrap().equals(0l)); // blockchain is syncing if height equals height without bootstrap, or target height and height without bootstrap both equal 0 + + // write sync status to preferences + preferences.getXmrNodeSettings().setSyncBlockchain(blockchainSyncing); + // throttle warnings if daemon not synced if (!isSyncedWithinTolerance() && System.currentTimeMillis() - lastLogDaemonNotSyncedTimestamp > HavenoUtils.LOG_DAEMON_NOT_SYNCED_WARN_PERIOD_MS) { log.warn("Our chain height: {} is out of sync with peer nodes chain height: {}", chainHeight.get(), getTargetHeight()); diff --git a/core/src/main/java/haveno/core/api/XmrLocalNode.java b/core/src/main/java/haveno/core/api/XmrLocalNode.java index 80682e9a34..cd5ed266f1 100644 --- a/core/src/main/java/haveno/core/api/XmrLocalNode.java +++ b/core/src/main/java/haveno/core/api/XmrLocalNode.java @@ -150,16 +150,16 @@ public class XmrLocalNode { /** * Start a local Monero node from settings. */ - public void startMoneroNode() throws IOException { + public void start() throws IOException { var settings = preferences.getXmrNodeSettings(); - this.startNode(settings); + this.start(settings); } /** * Start local Monero node. Throws MoneroError if the node cannot be started. * Persist the settings to preferences if the node started successfully. */ - public void startNode(XmrNodeSettings settings) throws IOException { + public void start(XmrNodeSettings settings) throws IOException { if (isDetected()) throw new IllegalStateException("Local Monero node already online"); log.info("Starting local Monero node: " + settings); @@ -177,6 +177,11 @@ public class XmrLocalNode { args.add("--bootstrap-daemon-address=" + bootstrapUrl); } + var syncBlockchain = settings.getSyncBlockchain(); + if (syncBlockchain != null && !syncBlockchain) { + args.add("--no-sync"); + } + var flags = settings.getStartupFlags(); if (flags != null) { args.addAll(flags); @@ -191,7 +196,7 @@ public class XmrLocalNode { * Stop the current local Monero node if we own its process. * Does not remove the last XmrNodeSettings. */ - public void stopNode() { + public void stop() { if (!isDetected()) throw new IllegalStateException("Local Monero node is not running"); if (daemon.getProcess() == null || !daemon.getProcess().isAlive()) throw new IllegalStateException("Cannot stop local Monero node because we don't own its process"); // TODO (woodser): remove isAlive() check after monero-java 0.5.4 which nullifies internal process daemon.stopProcess(); diff --git a/core/src/main/java/haveno/core/xmr/XmrNodeSettings.java b/core/src/main/java/haveno/core/xmr/XmrNodeSettings.java index 0db9868f04..9802f036c3 100644 --- a/core/src/main/java/haveno/core/xmr/XmrNodeSettings.java +++ b/core/src/main/java/haveno/core/xmr/XmrNodeSettings.java @@ -35,6 +35,8 @@ public class XmrNodeSettings implements PersistableEnvelope { String bootstrapUrl; @Nullable List startupFlags; + @Nullable + Boolean syncBlockchain; public XmrNodeSettings() { } @@ -43,7 +45,8 @@ public class XmrNodeSettings implements PersistableEnvelope { return new XmrNodeSettings( proto.getBlockchainPath(), proto.getBootstrapUrl(), - proto.getStartupFlagsList()); + proto.getStartupFlagsList(), + proto.getSyncBlockchain()); } @Override @@ -52,6 +55,7 @@ public class XmrNodeSettings implements PersistableEnvelope { Optional.ofNullable(blockchainPath).ifPresent(e -> builder.setBlockchainPath(blockchainPath)); Optional.ofNullable(bootstrapUrl).ifPresent(e -> builder.setBootstrapUrl(bootstrapUrl)); Optional.ofNullable(startupFlags).ifPresent(e -> builder.addAllStartupFlags(startupFlags)); + Optional.ofNullable(syncBlockchain).ifPresent(e -> builder.setSyncBlockchain(syncBlockchain)); return builder.build(); } } diff --git a/proto/src/main/proto/pb.proto b/proto/src/main/proto/pb.proto index 03cc6b2305..3daf8d370a 100644 --- a/proto/src/main/proto/pb.proto +++ b/proto/src/main/proto/pb.proto @@ -1754,6 +1754,7 @@ message XmrNodeSettings { string blockchain_path = 1; string bootstrap_url = 2; repeated string startup_flags = 3; + bool sync_blockchain = 4; } ///////////////////////////////////////////////////////////////////////////////////////////