Skip to content

Commit b2a6708

Browse files
committed
sync blockchain depending on last used local node
1 parent 3ffda0f commit b2a6708

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

core/src/main/java/haveno/core/api/CoreApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,11 @@ public XmrNodeSettings getXmrNodeSettings() {
260260
}
261261

262262
public void startXmrNode(XmrNodeSettings settings) throws IOException {
263-
xmrLocalNode.startNode(settings);
263+
xmrLocalNode.start(settings);
264264
}
265265

266266
public void stopXmrNode() {
267-
xmrLocalNode.stopNode();
267+
xmrLocalNode.stop();
268268
}
269269

270270
///////////////////////////////////////////////////////////////////////////////////////////

core/src/main/java/haveno/core/api/XmrConnectionService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ private void maybeStartLocalNode() {
623623
if (connectionManager.getConnection() != null && xmrLocalNode.equalsUri(connectionManager.getConnection().getUri()) && !xmrLocalNode.isDetected() && !xmrLocalNode.shouldBeIgnored()) {
624624
try {
625625
log.info("Starting local node");
626-
xmrLocalNode.startMoneroNode();
626+
xmrLocalNode.start();
627627
} catch (Exception e) {
628628
log.error("Unable to start local monero node, error={}\n", e.getMessage(), e);
629629
}
@@ -736,6 +736,12 @@ private void doPollDaemon() {
736736
// connected to daemon
737737
isConnected = true;
738738

739+
// determine if blockchain is syncing locally
740+
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
741+
742+
// write sync status to preferences
743+
preferences.getXmrNodeSettings().setSyncBlockchain(blockchainSyncing);
744+
739745
// throttle warnings if daemon not synced
740746
if (!isSyncedWithinTolerance() && System.currentTimeMillis() - lastLogDaemonNotSyncedTimestamp > HavenoUtils.LOG_DAEMON_NOT_SYNCED_WARN_PERIOD_MS) {
741747
log.warn("Our chain height: {} is out of sync with peer nodes chain height: {}", chainHeight.get(), getTargetHeight());

core/src/main/java/haveno/core/api/XmrLocalNode.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,16 @@ public XmrNodeSettings getNodeSettings() {
150150
/**
151151
* Start a local Monero node from settings.
152152
*/
153-
public void startMoneroNode() throws IOException {
153+
public void start() throws IOException {
154154
var settings = preferences.getXmrNodeSettings();
155-
this.startNode(settings);
155+
this.start(settings);
156156
}
157157

158158
/**
159159
* Start local Monero node. Throws MoneroError if the node cannot be started.
160160
* Persist the settings to preferences if the node started successfully.
161161
*/
162-
public void startNode(XmrNodeSettings settings) throws IOException {
162+
public void start(XmrNodeSettings settings) throws IOException {
163163
if (isDetected()) throw new IllegalStateException("Local Monero node already online");
164164

165165
log.info("Starting local Monero node: " + settings);
@@ -177,6 +177,11 @@ public void startNode(XmrNodeSettings settings) throws IOException {
177177
args.add("--bootstrap-daemon-address=" + bootstrapUrl);
178178
}
179179

180+
var syncBlockchain = settings.getSyncBlockchain();
181+
if (syncBlockchain != null && !syncBlockchain) {
182+
args.add("--no-sync");
183+
}
184+
180185
var flags = settings.getStartupFlags();
181186
if (flags != null) {
182187
args.addAll(flags);
@@ -191,7 +196,7 @@ public void startNode(XmrNodeSettings settings) throws IOException {
191196
* Stop the current local Monero node if we own its process.
192197
* Does not remove the last XmrNodeSettings.
193198
*/
194-
public void stopNode() {
199+
public void stop() {
195200
if (!isDetected()) throw new IllegalStateException("Local Monero node is not running");
196201
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
197202
daemon.stopProcess();

core/src/main/java/haveno/core/xmr/XmrNodeSettings.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class XmrNodeSettings implements PersistableEnvelope {
3535
String bootstrapUrl;
3636
@Nullable
3737
List<String> startupFlags;
38+
@Nullable
39+
Boolean syncBlockchain;
3840

3941
public XmrNodeSettings() {
4042
}
@@ -43,7 +45,8 @@ public static XmrNodeSettings fromProto(protobuf.XmrNodeSettings proto) {
4345
return new XmrNodeSettings(
4446
proto.getBlockchainPath(),
4547
proto.getBootstrapUrl(),
46-
proto.getStartupFlagsList());
48+
proto.getStartupFlagsList(),
49+
proto.getSyncBlockchain());
4750
}
4851

4952
@Override
@@ -52,6 +55,7 @@ public protobuf.XmrNodeSettings toProtoMessage() {
5255
Optional.ofNullable(blockchainPath).ifPresent(e -> builder.setBlockchainPath(blockchainPath));
5356
Optional.ofNullable(bootstrapUrl).ifPresent(e -> builder.setBootstrapUrl(bootstrapUrl));
5457
Optional.ofNullable(startupFlags).ifPresent(e -> builder.addAllStartupFlags(startupFlags));
58+
Optional.ofNullable(syncBlockchain).ifPresent(e -> builder.setSyncBlockchain(syncBlockchain));
5559
return builder.build();
5660
}
5761
}

proto/src/main/proto/pb.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,7 @@ message XmrNodeSettings {
17541754
string blockchain_path = 1;
17551755
string bootstrap_url = 2;
17561756
repeated string startup_flags = 3;
1757+
bool sync_blockchain = 4;
17571758
}
17581759

17591760
///////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)