@@ -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 <>();
@@ -181,7 +180,6 @@ public class XmrWalletService extends XmrWalletBase {
181
180
this .walletDir = walletDir ;
182
181
this .rpcBindPort = rpcBindPort ;
183
182
this .useNativeXmrWallet = useNativeXmrWallet ;
184
- this .xmrWalletFile = new File (walletDir , MONERO_WALLET_NAME );
185
183
HavenoUtils .xmrWalletService = this ;
186
184
HavenoUtils .xmrConnectionService = xmrConnectionService ;
187
185
this .xmrConnectionService = xmrConnectionService ; // TODO: super's is null unless set here from injection
@@ -1327,7 +1325,7 @@ private void doMaybeInitMainWallet(boolean sync, int numAttempts) {
1327
1325
if (wallet == null ) {
1328
1326
MoneroDaemonRpc daemon = xmrConnectionService .getDaemon ();
1329
1327
log .info ("Initializing main wallet with monerod=" + (daemon == null ? "null" : daemon .getRpcConnection ().getUri ()));
1330
- if (MoneroUtils . walletExists (xmrWalletFile . getPath () )) {
1328
+ if (walletExists (MONERO_WALLET_NAME )) {
1331
1329
wallet = openWallet (MONERO_WALLET_NAME , rpcBindPort , isProxyApplied (wasWalletSynced ));
1332
1330
} else if (Boolean .TRUE .equals (xmrConnectionService .isConnected ())) {
1333
1331
wallet = createWallet (MONERO_WALLET_NAME , rpcBindPort );
@@ -1475,11 +1473,54 @@ private MoneroWalletFull openWalletFull(MoneroWalletConfig config, boolean apply
1475
1473
MoneroRpcConnection connection = new MoneroRpcConnection (xmrConnectionService .getConnection ());
1476
1474
if (!applyProxyUri ) connection .setProxyUri (null );
1477
1475
1478
- // open wallet
1476
+ // try opening wallet
1479
1477
config .setNetworkType (getMoneroNetworkType ());
1480
1478
config .setServer (connection );
1481
1479
log .info ("Opening full wallet " + config .getPath () + " with monerod=" + connection .getUri () + ", proxyUri=" + connection .getProxyUri ());
1482
- walletFull = MoneroWalletFull .openWallet (config );
1480
+ try {
1481
+ walletFull = MoneroWalletFull .openWallet (config );
1482
+ } catch (Exception e ) {
1483
+ log .warn ("Failed to open full wallet '{}', attempting to use backup cache, error={}" , config .getPath (), e .getMessage ());
1484
+ boolean retrySuccessful = false ;
1485
+ try {
1486
+
1487
+ // rename wallet cache to backup
1488
+ String cachePath = walletDir .toString () + File .separator + MONERO_WALLET_NAME ;
1489
+ File originalCacheFile = new File (cachePath );
1490
+ if (originalCacheFile .exists ()) originalCacheFile .renameTo (new File (cachePath + ".backup" ));
1491
+
1492
+ // copy latest wallet cache backup to main folder
1493
+ File backupCacheFile = FileUtil .getLatestBackupFile (walletDir , MONERO_WALLET_NAME );
1494
+ if (backupCacheFile != null ) FileUtil .copyFile (backupCacheFile , new File (cachePath ));
1495
+
1496
+ // retry opening wallet without original cache
1497
+ try {
1498
+ walletFull = MoneroWalletFull .openWallet (config );
1499
+ log .info ("Successfully opened full wallet using backup cache" );
1500
+ retrySuccessful = true ;
1501
+ } catch (Exception e2 ) {
1502
+ // ignore
1503
+ }
1504
+
1505
+ // handle success or failure
1506
+ if (retrySuccessful ) {
1507
+ originalCacheFile .delete (); // delete original wallet cache backup
1508
+ } else {
1509
+
1510
+ // restore original wallet cache
1511
+ log .warn ("Failed to open full wallet using backup cache, restoring original cache" );
1512
+ File cacheFile = new File (cachePath );
1513
+ if (cacheFile .exists ()) cacheFile .delete ();
1514
+ File originalCacheBackup = new File (cachePath + ".backup" );
1515
+ if (originalCacheBackup .exists ()) originalCacheBackup .renameTo (new File (cachePath ));
1516
+
1517
+ // throw exception
1518
+ throw e ;
1519
+ }
1520
+ } catch (Exception e2 ) {
1521
+ throw e ; // throw original exception
1522
+ }
1523
+ }
1483
1524
if (walletFull .getDaemonConnection () != null ) walletFull .getDaemonConnection ().setPrintStackTrace (PRINT_RPC_STACK_TRACE );
1484
1525
log .info ("Done opening full wallet " + config .getPath ());
1485
1526
return walletFull ;
@@ -1518,7 +1559,7 @@ private MoneroWalletRpc createWalletRpc(MoneroWalletConfig config, Integer port)
1518
1559
} catch (Exception e ) {
1519
1560
e .printStackTrace ();
1520
1561
if (walletRpc != null ) forceCloseWallet (walletRpc , config .getPath ());
1521
- throw new IllegalStateException ("Could not create wallet '" + config .getPath () + "'. Please close Haveno, stop all monero-wallet-rpc processes, and restart Haveno." );
1562
+ 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." );
1522
1563
}
1523
1564
}
1524
1565
@@ -1537,17 +1578,60 @@ private MoneroWalletRpc openWalletRpc(MoneroWalletConfig config, Integer port, b
1537
1578
MoneroRpcConnection connection = new MoneroRpcConnection (xmrConnectionService .getConnection ());
1538
1579
if (!applyProxyUri ) connection .setProxyUri (null );
1539
1580
1540
- // open wallet
1581
+ // try opening wallet
1541
1582
log .info ("Opening RPC wallet " + config .getPath () + " with monerod=" + connection .getUri () + ", proxyUri=" + connection .getProxyUri ());
1542
1583
config .setServer (connection );
1543
- walletRpc .openWallet (config );
1584
+ try {
1585
+ walletRpc .openWallet (config );
1586
+ } catch (Exception e ) {
1587
+ log .warn ("Failed to open RPC wallet '{}', attempting to use backup cache, error={}" , config .getPath (), e .getMessage ());
1588
+ boolean retrySuccessful = false ;
1589
+ try {
1590
+
1591
+ // rename wallet cache to backup
1592
+ String cachePath = walletDir .toString () + File .separator + MONERO_WALLET_NAME ;
1593
+ File originalCacheFile = new File (cachePath );
1594
+ if (originalCacheFile .exists ()) originalCacheFile .renameTo (new File (cachePath + ".backup" ));
1595
+
1596
+ // copy latest wallet cache backup to main folder
1597
+ File backupCacheFile = FileUtil .getLatestBackupFile (walletDir , MONERO_WALLET_NAME );
1598
+ if (backupCacheFile != null ) FileUtil .copyFile (backupCacheFile , new File (cachePath ));
1599
+
1600
+ // retry opening wallet without original cache
1601
+ try {
1602
+ walletRpc .openWallet (config );
1603
+ log .info ("Successfully opened RPC wallet using backup cache" );
1604
+ retrySuccessful = true ;
1605
+ } catch (Exception e2 ) {
1606
+ // ignore
1607
+ }
1608
+
1609
+ // handle success or failure
1610
+ if (retrySuccessful ) {
1611
+ originalCacheFile .delete (); // delete original wallet cache backup
1612
+ } else {
1613
+
1614
+ // restore original wallet cache
1615
+ log .warn ("Failed to open RPC wallet using backup cache, restoring original cache" );
1616
+ File cacheFile = new File (cachePath );
1617
+ if (cacheFile .exists ()) cacheFile .delete ();
1618
+ File originalCacheBackup = new File (cachePath + ".backup" );
1619
+ if (originalCacheBackup .exists ()) originalCacheBackup .renameTo (new File (cachePath ));
1620
+
1621
+ // throw exception
1622
+ throw e ;
1623
+ }
1624
+ } catch (Exception e2 ) {
1625
+ throw e ; // throw original exception
1626
+ }
1627
+ }
1544
1628
if (walletRpc .getDaemonConnection () != null ) walletRpc .getDaemonConnection ().setPrintStackTrace (PRINT_RPC_STACK_TRACE );
1545
1629
log .info ("Done opening RPC wallet " + config .getPath ());
1546
1630
return walletRpc ;
1547
1631
} catch (Exception e ) {
1548
1632
e .printStackTrace ();
1549
1633
if (walletRpc != null ) forceCloseWallet (walletRpc , config .getPath ());
1550
- 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 ());
1634
+ 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 ());
1551
1635
}
1552
1636
}
1553
1637
0 commit comments