@@ -136,7 +136,6 @@ public class XmrWalletService extends XmrWalletBase {
136
136
private final WalletsSetup walletsSetup ;
137
137
138
138
private final File walletDir ;
139
- private final File xmrWalletFile ;
140
139
private final int rpcBindPort ;
141
140
private final boolean useNativeXmrWallet ;
142
141
protected final CopyOnWriteArraySet <XmrBalanceListener > balanceListeners = new CopyOnWriteArraySet <>();
@@ -180,7 +179,6 @@ public class XmrWalletService extends XmrWalletBase {
180
179
this .walletDir = walletDir ;
181
180
this .rpcBindPort = rpcBindPort ;
182
181
this .useNativeXmrWallet = useNativeXmrWallet ;
183
- this .xmrWalletFile = new File (walletDir , MONERO_WALLET_NAME );
184
182
HavenoUtils .xmrWalletService = this ;
185
183
HavenoUtils .xmrConnectionService = xmrConnectionService ;
186
184
this .xmrConnectionService = xmrConnectionService ; // TODO: super's is null unless set here from injection
@@ -1326,7 +1324,7 @@ private void doMaybeInitMainWallet(boolean sync, int numAttempts) {
1326
1324
if (wallet == null ) {
1327
1325
MoneroDaemonRpc daemon = xmrConnectionService .getDaemon ();
1328
1326
log .info ("Initializing main wallet with monerod=" + (daemon == null ? "null" : daemon .getRpcConnection ().getUri ()));
1329
- if (MoneroUtils . walletExists (xmrWalletFile . getPath () )) {
1327
+ if (walletExists (MONERO_WALLET_NAME )) {
1330
1328
wallet = openWallet (MONERO_WALLET_NAME , rpcBindPort , isProxyApplied (wasWalletSynced ));
1331
1329
} else if (Boolean .TRUE .equals (xmrConnectionService .isConnected ())) {
1332
1330
wallet = createWallet (MONERO_WALLET_NAME , rpcBindPort );
@@ -1517,7 +1515,7 @@ private MoneroWalletRpc createWalletRpc(MoneroWalletConfig config, Integer port)
1517
1515
} catch (Exception e ) {
1518
1516
e .printStackTrace ();
1519
1517
if (walletRpc != null ) forceCloseWallet (walletRpc , config .getPath ());
1520
- throw new IllegalStateException ("Could not create wallet '" + config .getPath () + "'. Please close Haveno, stop all monero-wallet-rpc processes, and restart Haveno." );
1518
+ throw new IllegalStateException ("Could not create wallet '" + config .getPath () + "'. Please close Haveno, stop all monero-wallet-rpc processes in your task manager , and restart Haveno." );
1521
1519
}
1522
1520
}
1523
1521
@@ -1536,17 +1534,60 @@ private MoneroWalletRpc openWalletRpc(MoneroWalletConfig config, Integer port, b
1536
1534
MoneroRpcConnection connection = new MoneroRpcConnection (xmrConnectionService .getConnection ());
1537
1535
if (!applyProxyUri ) connection .setProxyUri (null );
1538
1536
1539
- // open wallet
1537
+ // try opening wallet
1540
1538
log .info ("Opening RPC wallet " + config .getPath () + " with monerod=" + connection .getUri () + ", proxyUri=" + connection .getProxyUri ());
1541
1539
config .setServer (connection );
1542
- walletRpc .openWallet (config );
1540
+ try {
1541
+ walletRpc .openWallet (config );
1542
+ } catch (Exception e ) {
1543
+ log .warn ("Failed to open RPC wallet '{}', attempting to use backup cache, error={}" , config .getPath (), e .getMessage ());
1544
+ boolean retrySuccessful = false ;
1545
+ try {
1546
+
1547
+ // rename wallet cache to backup
1548
+ String cachePath = walletDir .toString () + File .separator + MONERO_WALLET_NAME ;
1549
+ File originalCacheFile = new File (cachePath );
1550
+ if (originalCacheFile .exists ()) originalCacheFile .renameTo (new File (cachePath + ".backup" ));
1551
+
1552
+ // copy latest wallet cache backup to main folder
1553
+ File backupCacheFile = FileUtil .getLatestBackupFile (walletDir , MONERO_WALLET_NAME );
1554
+ if (backupCacheFile != null ) FileUtil .copyFile (backupCacheFile , new File (cachePath ));
1555
+
1556
+ // retry opening wallet without original cache
1557
+ try {
1558
+ walletRpc .openWallet (config );
1559
+ log .info ("Successfully opened RPC wallet using backup cache" );
1560
+ retrySuccessful = true ;
1561
+ } catch (Exception e2 ) {
1562
+ // ignore
1563
+ }
1564
+
1565
+ // handle success or failure
1566
+ if (retrySuccessful ) {
1567
+ originalCacheFile .delete (); // delete original wallet cache backup
1568
+ } else {
1569
+
1570
+ // restore original wallet cache
1571
+ log .warn ("Failed to open RPC wallet using backup cache, restoring original cache" );
1572
+ File cacheFile = new File (cachePath );
1573
+ if (cacheFile .exists ()) cacheFile .delete ();
1574
+ File originalCacheBackup = new File (cachePath + ".backup" );
1575
+ if (originalCacheBackup .exists ()) originalCacheBackup .renameTo (new File (cachePath ));
1576
+
1577
+ // throw exception
1578
+ throw e ;
1579
+ }
1580
+ } catch (Exception e2 ) {
1581
+ throw e ; // throw original exception
1582
+ }
1583
+ }
1543
1584
if (walletRpc .getDaemonConnection () != null ) walletRpc .getDaemonConnection ().setPrintStackTrace (PRINT_RPC_STACK_TRACE );
1544
1585
log .info ("Done opening RPC wallet " + config .getPath ());
1545
1586
return walletRpc ;
1546
1587
} catch (Exception e ) {
1547
1588
e .printStackTrace ();
1548
1589
if (walletRpc != null ) forceCloseWallet (walletRpc , config .getPath ());
1549
- throw new IllegalStateException ("Could not open wallet '" + config .getPath () + "'. Please close Haveno, stop all monero-wallet-rpc processes, and restart Haveno.\n \n Error message: " + e .getMessage ());
1590
+ throw new IllegalStateException ("Could not open wallet '" + config .getPath () + "'. Please close Haveno, stop all monero-wallet-rpc processes in your task manager , and restart Haveno.\n \n Error message: " + e .getMessage ());
1550
1591
}
1551
1592
}
1552
1593
0 commit comments