sync wallet directly within 100 blocks of target height

This commit is contained in:
woodser 2024-08-06 05:15:36 -04:00
parent 443c2f4cdb
commit 0105c1436a
3 changed files with 23 additions and 4 deletions

View file

@ -2631,8 +2631,15 @@ public abstract class Trade extends XmrWalletBase implements Tradable, Model {
private void syncWalletIfBehind() { private void syncWalletIfBehind() {
synchronized (walletLock) { synchronized (walletLock) {
if (isWalletBehind()) { if (isWalletBehind()) {
// TODO: local tests have timing failures unless sync called directly
if (xmrConnectionService.getTargetHeight() - walletHeight.get() < XmrWalletBase.DIRECT_SYNC_WITHIN_BLOCKS) {
xmrWalletService.syncWallet(wallet);
} else {
syncWithProgress(); syncWithProgress();
} }
walletHeight.set(wallet.getHeight());
}
} }
} }

View file

@ -26,6 +26,7 @@ public class XmrWalletBase {
// constants // constants
public static final int SYNC_PROGRESS_TIMEOUT_SECONDS = 60; public static final int SYNC_PROGRESS_TIMEOUT_SECONDS = 60;
public static final int DIRECT_SYNC_WITHIN_BLOCKS = 100;
// inherited // inherited
protected MoneroWallet wallet; protected MoneroWallet wallet;
@ -59,12 +60,17 @@ public class XmrWalletBase {
} }
public void syncWithProgress() { public void syncWithProgress() {
syncWithProgress(false);
}
public void syncWithProgress(boolean repeatSyncToLatestHeight) {
synchronized (walletLock) { synchronized (walletLock) {
// set initial state // set initial state
isSyncingWithProgress = true; isSyncingWithProgress = true;
syncProgressError = null; syncProgressError = null;
updateSyncProgress(walletHeight.get()); updateSyncProgress(walletHeight.get());
long targetHeightAtStart = xmrConnectionService.getTargetHeight();
// test connection changing on startup before wallet synced // test connection changing on startup before wallet synced
if (testReconnectOnStartup) { if (testReconnectOnStartup) {
@ -106,7 +112,7 @@ public class XmrWalletBase {
return; return;
} }
updateSyncProgress(height); updateSyncProgress(height);
if (height >= xmrConnectionService.getTargetHeight()) { if (height >= (repeatSyncToLatestHeight ? xmrConnectionService.getTargetHeight() : targetHeightAtStart)) {
setWalletSyncedWithProgress(); setWalletSyncedWithProgress();
syncProgressLatch.countDown(); syncProgressLatch.countDown();
} }

View file

@ -1358,7 +1358,7 @@ public class XmrWalletService extends XmrWalletBase {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
MoneroRpcConnection sourceConnection = xmrConnectionService.getConnection(); MoneroRpcConnection sourceConnection = xmrConnectionService.getConnection();
try { try {
syncWithProgress(); // blocking syncWithProgress(true); // repeat sync to latest target height
} catch (Exception e) { } catch (Exception e) {
log.warn("Error syncing wallet with progress on startup: " + e.getMessage()); log.warn("Error syncing wallet with progress on startup: " + e.getMessage());
forceCloseMainWallet(); forceCloseMainWallet();
@ -1768,9 +1768,15 @@ public class XmrWalletService extends XmrWalletBase {
// sync wallet if behind daemon // sync wallet if behind daemon
if (walletHeight.get() < xmrConnectionService.getTargetHeight()) { if (walletHeight.get() < xmrConnectionService.getTargetHeight()) {
synchronized (walletLock) { // avoid long sync from blocking other operations synchronized (walletLock) { // avoid long sync from blocking other operations
// TODO: local tests have timing failures unless sync called directly
if (xmrConnectionService.getTargetHeight() - walletHeight.get() < XmrWalletBase.DIRECT_SYNC_WITHIN_BLOCKS) {
syncMainWallet();
} else {
syncWithProgress(); syncWithProgress();
} }
} }
}
// fetch transactions from pool and store to cache // fetch transactions from pool and store to cache
// TODO: ideally wallet should sync every poll and then avoid updating from pool on fetching txs? // TODO: ideally wallet should sync every poll and then avoid updating from pool on fetching txs?