reinitialize main wallet on same thread as connection change

This commit is contained in:
woodser 2024-07-18 09:14:16 -04:00
parent cb132e727a
commit ebcadb7bed

View file

@ -1304,100 +1304,102 @@ public class XmrWalletService {
} }
private void maybeInitMainWallet(boolean sync, int numAttempts) { private void maybeInitMainWallet(boolean sync, int numAttempts) {
ThreadUtils.execute(() -> { ThreadUtils.execute(() -> doMaybeInitMainWallet(sync, numAttempts), THREAD_ID);
synchronized (WALLET_LOCK) { }
if (isShutDownStarted) return;
// open or create wallet main wallet private void doMaybeInitMainWallet(boolean sync, int numAttempts) {
if (wallet == null) { synchronized (WALLET_LOCK) {
MoneroDaemonRpc daemon = xmrConnectionService.getDaemon(); if (isShutDownStarted) return;
log.info("Initializing main wallet with monerod=" + (daemon == null ? "null" : daemon.getRpcConnection().getUri()));
if (MoneroUtils.walletExists(xmrWalletFile.getPath())) {
wallet = openWallet(MONERO_WALLET_NAME, rpcBindPort, isProxyApplied(wasWalletSynced));
} else if (Boolean.TRUE.equals(xmrConnectionService.isConnected())) {
wallet = createWallet(MONERO_WALLET_NAME, rpcBindPort);
// set wallet creation date to yesterday to guarantee complete restore // open or create wallet main wallet
LocalDateTime localDateTime = LocalDate.now().atStartOfDay().minusDays(1); if (wallet == null) {
long date = localDateTime.toEpochSecond(ZoneOffset.UTC); MoneroDaemonRpc daemon = xmrConnectionService.getDaemon();
user.setWalletCreationDate(date); log.info("Initializing main wallet with monerod=" + (daemon == null ? "null" : daemon.getRpcConnection().getUri()));
} if (MoneroUtils.walletExists(xmrWalletFile.getPath())) {
isClosingWallet = false; wallet = openWallet(MONERO_WALLET_NAME, rpcBindPort, isProxyApplied(wasWalletSynced));
} else if (Boolean.TRUE.equals(xmrConnectionService.isConnected())) {
wallet = createWallet(MONERO_WALLET_NAME, rpcBindPort);
// set wallet creation date to yesterday to guarantee complete restore
LocalDateTime localDateTime = LocalDate.now().atStartOfDay().minusDays(1);
long date = localDateTime.toEpochSecond(ZoneOffset.UTC);
user.setWalletCreationDate(date);
} }
isClosingWallet = false;
}
// sync wallet and register listener // sync wallet and register listener
if (wallet != null && !isShutDownStarted) { if (wallet != null && !isShutDownStarted) {
log.info("Monero wallet path={}", wallet.getPath()); log.info("Monero wallet path={}", wallet.getPath());
// sync main wallet if applicable // sync main wallet if applicable
if (sync && numAttempts > 0) { if (sync && numAttempts > 0) {
try { try {
// switch connection if disconnected // switch connection if disconnected
if (!wallet.isConnectedToDaemon()) { if (!wallet.isConnectedToDaemon()) {
log.warn("Switching connection before syncing with progress because disconnected"); log.warn("Switching connection before syncing with progress because disconnected");
if (requestSwitchToNextBestConnection()) return; // calls back to this method if (requestSwitchToNextBestConnection()) return; // calls back to this method
} }
// sync main wallet // sync main wallet
log.info("Syncing main wallet"); log.info("Syncing main wallet");
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
syncWithProgress(); // blocking syncWithProgress(); // blocking
log.info("Done syncing main wallet in " + (System.currentTimeMillis() - time) + " ms"); log.info("Done syncing main wallet in " + (System.currentTimeMillis() - time) + " ms");
// poll wallet // poll wallet
doPollWallet(true); doPollWallet(true);
if (walletInitListener != null) xmrConnectionService.downloadPercentageProperty().removeListener(walletInitListener); if (walletInitListener != null) xmrConnectionService.downloadPercentageProperty().removeListener(walletInitListener);
// log wallet balances // log wallet balances
if (getMoneroNetworkType() != MoneroNetworkType.MAINNET) { if (getMoneroNetworkType() != MoneroNetworkType.MAINNET) {
BigInteger balance = getBalance(); BigInteger balance = getBalance();
BigInteger unlockedBalance = getAvailableBalance(); BigInteger unlockedBalance = getAvailableBalance();
log.info("Monero wallet unlocked balance={}, pending balance={}, total balance={}", unlockedBalance, balance.subtract(unlockedBalance), balance); log.info("Monero wallet unlocked balance={}, pending balance={}, total balance={}", unlockedBalance, balance.subtract(unlockedBalance), balance);
} }
// reapply connection after wallet synced (might reinitialize wallet on new thread) // reapply connection after wallet synced (might reinitialize wallet on new thread)
ThreadUtils.execute(() -> onConnectionChanged(xmrConnectionService.getConnection()), THREAD_ID); ThreadUtils.execute(() -> onConnectionChanged(xmrConnectionService.getConnection()), THREAD_ID);
// reset internal state if main wallet was swapped // reset internal state if main wallet was swapped
resetIfWalletChanged(); resetIfWalletChanged();
// signal that main wallet is synced // signal that main wallet is synced
doneDownload(); doneDownload();
// notify setup that main wallet is initialized // notify setup that main wallet is initialized
// TODO: app fully initializes after this is set to true, even though wallet might not be initialized if unconnected. wallet will be created when connection detected // TODO: app fully initializes after this is set to true, even though wallet might not be initialized if unconnected. wallet will be created when connection detected
// refactor startup to call this and sync off main thread? but the calls to e.g. getBalance() fail with 'wallet and network is not yet initialized' // refactor startup to call this and sync off main thread? but the calls to e.g. getBalance() fail with 'wallet and network is not yet initialized'
HavenoUtils.havenoSetup.getWalletInitialized().set(true);
// save but skip backup on initialization
saveMainWallet(false);
} catch (Exception e) {
if (isClosingWallet || isShutDownStarted || HavenoUtils.havenoSetup.getWalletInitialized().get()) return; // ignore if wallet closing, shut down started, or app already initialized
log.warn("Error initially syncing main wallet: {}", e.getMessage());
if (numAttempts <= 1) {
log.warn("Failed to sync main wallet. Opening app without syncing", numAttempts);
HavenoUtils.havenoSetup.getWalletInitialized().set(true); HavenoUtils.havenoSetup.getWalletInitialized().set(true);
// save but skip backup on initialization
saveMainWallet(false); saveMainWallet(false);
} catch (Exception e) {
if (isClosingWallet || isShutDownStarted || HavenoUtils.havenoSetup.getWalletInitialized().get()) return; // ignore if wallet closing, shut down started, or app already initialized
log.warn("Error initially syncing main wallet: {}", e.getMessage());
if (numAttempts <= 1) {
log.warn("Failed to sync main wallet. Opening app without syncing", numAttempts);
HavenoUtils.havenoSetup.getWalletInitialized().set(true);
saveMainWallet(false);
// reschedule to init main wallet // reschedule to init main wallet
UserThread.runAfter(() -> { UserThread.runAfter(() -> {
maybeInitMainWallet(true, MAX_SYNC_ATTEMPTS); maybeInitMainWallet(true, MAX_SYNC_ATTEMPTS);
}, xmrConnectionService.getRefreshPeriodMs() / 1000); }, xmrConnectionService.getRefreshPeriodMs() / 1000);
} else { } else {
log.warn("Trying again in {} seconds", xmrConnectionService.getRefreshPeriodMs() / 1000); log.warn("Trying again in {} seconds", xmrConnectionService.getRefreshPeriodMs() / 1000);
UserThread.runAfter(() -> { UserThread.runAfter(() -> {
maybeInitMainWallet(true, numAttempts - 1); maybeInitMainWallet(true, numAttempts - 1);
}, xmrConnectionService.getRefreshPeriodMs() / 1000); }, xmrConnectionService.getRefreshPeriodMs() / 1000);
}
} }
} }
// start polling main wallet
startPolling();
} }
// start polling main wallet
startPolling();
} }
}, THREAD_ID); }
} }
private void resetIfWalletChanged() { private void resetIfWalletChanged() {
@ -1679,7 +1681,7 @@ public class XmrWalletService {
} else { } else {
log.info("Restarting main wallet because proxy URI has changed, old={}, new={}", oldProxyUri, newProxyUri); // TODO: set proxy without restarting wallet log.info("Restarting main wallet because proxy URI has changed, old={}, new={}", oldProxyUri, newProxyUri); // TODO: set proxy without restarting wallet
closeMainWallet(true); closeMainWallet(true);
maybeInitMainWallet(false); doMaybeInitMainWallet(false, MAX_SYNC_ATTEMPTS);
return; // wallet is re-initialized return; // wallet is re-initialized
} }
} else { } else {
@ -1846,7 +1848,6 @@ public class XmrWalletService {
synchronized (HavenoUtils.getDaemonLock()) { synchronized (HavenoUtils.getDaemonLock()) {
try { try {
cachedTxs = wallet.getTxs(new MoneroTxQuery().setIncludeOutputs(true)); cachedTxs = wallet.getTxs(new MoneroTxQuery().setIncludeOutputs(true));
lastLogPollErrorTimestamp = null;
} catch (Exception e) { // fetch from pool can fail } catch (Exception e) { // fetch from pool can fail
if (!isShutDownStarted) { if (!isShutDownStarted) {
if (lastLogPollErrorTimestamp == null || System.currentTimeMillis() - lastLogPollErrorTimestamp > HavenoUtils.LOG_POLL_ERROR_PERIOD_MS) { // limit error logging if (lastLogPollErrorTimestamp == null || System.currentTimeMillis() - lastLogPollErrorTimestamp > HavenoUtils.LOG_POLL_ERROR_PERIOD_MS) { // limit error logging