sync blockchain depending on last used local node

This commit is contained in:
woodser 2024-09-27 12:09:35 -04:00
parent 3ffda0fdd1
commit b2a6708ac1
5 changed files with 24 additions and 8 deletions

View File

@ -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();
}
///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -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());

View File

@ -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();

View File

@ -35,6 +35,8 @@ public class XmrNodeSettings implements PersistableEnvelope {
String bootstrapUrl;
@Nullable
List<String> 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();
}
}

View File

@ -1754,6 +1754,7 @@ message XmrNodeSettings {
string blockchain_path = 1;
string bootstrap_url = 2;
repeated string startup_flags = 3;
bool sync_blockchain = 4;
}
///////////////////////////////////////////////////////////////////////////////////////////