improve reliability of wallet initialization by starting with connection

avoid redundant call to start syncing
This commit is contained in:
woodser 2023-03-13 13:10:27 -04:00
parent 4650003838
commit e369487457

View file

@ -213,8 +213,7 @@ public class XmrWalletService {
return createWallet(new MoneroWalletConfig() return createWallet(new MoneroWalletConfig()
.setPath(walletName) .setPath(walletName)
.setPassword(getWalletPassword()), .setPassword(getWalletPassword()),
null, null);
true);
} }
public MoneroWalletRpc openWallet(String walletName) { public MoneroWalletRpc openWallet(String walletName) {
@ -552,15 +551,11 @@ public class XmrWalletService {
if (MoneroUtils.walletExists(xmrWalletFile.getPath())) { if (MoneroUtils.walletExists(xmrWalletFile.getPath())) {
wallet = openWallet(walletConfig, rpcBindPort); wallet = openWallet(walletConfig, rpcBindPort);
} else if (connectionsService.getConnection() != null && Boolean.TRUE.equals(connectionsService.getConnection().isConnected())) { } else if (connectionsService.getConnection() != null && Boolean.TRUE.equals(connectionsService.getConnection().isConnected())) {
wallet = createWallet(walletConfig, rpcBindPort, true); wallet = createWallet(walletConfig, rpcBindPort);
} }
// wallet is not initialized until connected to a daemon // wallet is not initialized until connected to a daemon
if (wallet != null) { if (wallet != null) {
// sync wallet which updates app startup state
trySyncMainWallet();
if (connectionsService.getDaemon() == null) System.out.println("Daemon: null"); if (connectionsService.getDaemon() == null) System.out.println("Daemon: null");
else { else {
System.out.println("Daemon uri: " + connectionsService.getDaemon().getRpcConnection().getUri()); System.out.println("Daemon uri: " + connectionsService.getDaemon().getRpcConnection().getUri());
@ -569,7 +564,20 @@ public class XmrWalletService {
System.out.println("Monero wallet uri: " + wallet.getRpcConnection().getUri()); System.out.println("Monero wallet uri: " + wallet.getRpcConnection().getUri());
System.out.println("Monero wallet path: " + wallet.getPath()); System.out.println("Monero wallet path: " + wallet.getPath());
System.out.println("Monero wallet primary address: " + wallet.getPrimaryAddress()); System.out.println("Monero wallet primary address: " + wallet.getPrimaryAddress());
System.out.println("Monero wallet height: " + wallet.getHeight());
// sync wallet which updates app startup state
try {
log.info("Syncing main wallet");
long time = System.currentTimeMillis();
wallet.sync(); // blocking
log.info("Done syncing main wallet in " + (System.currentTimeMillis() - time) + " ms");
wallet.startSyncing(connectionsService.getDefaultRefreshPeriodMs());
connectionsService.doneDownload(); // TODO: using this to signify both daemon and wallet synced, refactor sync handling of both
saveMainWallet(false); // skip backup on open
} catch (Exception e) {
log.warn("Error syncing main wallet: {}", e.getMessage());
}
System.out.println("Monero wallet balance: " + wallet.getBalance(0)); System.out.println("Monero wallet balance: " + wallet.getBalance(0));
System.out.println("Monero wallet unlocked balance: " + wallet.getUnlockedBalance(0)); System.out.println("Monero wallet unlocked balance: " + wallet.getUnlockedBalance(0));
@ -581,29 +589,22 @@ public class XmrWalletService {
} }
} }
private MoneroWalletRpc createWallet(MoneroWalletConfig config, Integer port, boolean sync) { private MoneroWalletRpc createWallet(MoneroWalletConfig config, Integer port) {
// start monero-wallet-rpc instance
MoneroWalletRpc walletRpc = startWalletRpcInstance(port, false);
// must be connected to daemon // must be connected to daemon
MoneroRpcConnection connection = connectionsService.getConnection(); MoneroRpcConnection connection = connectionsService.getConnection();
if (connection == null || !Boolean.TRUE.equals(connection.isConnected())) throw new RuntimeException("Must be connected to daemon before creating wallet"); if (connection == null || !Boolean.TRUE.equals(connection.isConnected())) throw new RuntimeException("Must be connected to daemon before creating wallet");
config.setServer(connection); config.setServer(connection);
// start monero-wallet-rpc instance
MoneroWalletRpc walletRpc = startWalletRpcInstance(port);
// create wallet // create wallet
try { try {
log.info("Creating wallet " + config.getPath()); log.info("Creating wallet " + config.getPath());
if (!sync) config.setServer(null); long time = System.currentTimeMillis();
walletRpc.createWallet(config); walletRpc.createWallet(config);
if (sync) { log.info("Done creating wallet " + walletRpc.getPath() + " in " + (System.currentTimeMillis() - time) + " ms");
log.info("Syncing wallet " + config.getPath() + " in background");
walletRpc.startSyncing(connectionsService.getDefaultRefreshPeriodMs());
log.info("Done starting background sync for wallet " + config.getPath());
} else {
walletRpc.setDaemonConnection(connection);
}
log.info("Done creating wallet " + walletRpc.getPath());
return walletRpc; return walletRpc;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -615,7 +616,7 @@ public class XmrWalletService {
private MoneroWalletRpc openWallet(MoneroWalletConfig config, Integer port) { private MoneroWalletRpc openWallet(MoneroWalletConfig config, Integer port) {
// start monero-wallet-rpc instance // start monero-wallet-rpc instance
MoneroWalletRpc walletRpc = startWalletRpcInstance(port, true); MoneroWalletRpc walletRpc = startWalletRpcInstance(port);
// open wallet // open wallet
try { try {
@ -631,7 +632,7 @@ public class XmrWalletService {
} }
} }
private MoneroWalletRpc startWalletRpcInstance(Integer port, boolean withConnection) { private MoneroWalletRpc startWalletRpcInstance(Integer port) {
// check if monero-wallet-rpc exists // check if monero-wallet-rpc exists
if (!new File(MONERO_WALLET_RPC_PATH).exists()) throw new Error("monero-wallet-rpc executable doesn't exist at path " + MONERO_WALLET_RPC_PATH if (!new File(MONERO_WALLET_RPC_PATH).exists()) throw new Error("monero-wallet-rpc executable doesn't exist at path " + MONERO_WALLET_RPC_PATH
@ -649,7 +650,7 @@ public class XmrWalletService {
cmd.add("--" + MONERO_NETWORK_TYPE.toString().toLowerCase()); cmd.add("--" + MONERO_NETWORK_TYPE.toString().toLowerCase());
} }
MoneroRpcConnection connection = withConnection ? connectionsService.getConnection() : null; MoneroRpcConnection connection = connectionsService.getConnection();
if (connection != null) { if (connection != null) {
cmd.add("--daemon-address"); cmd.add("--daemon-address");
cmd.add(connection.getUri()); cmd.add(connection.getUri());
@ -667,25 +668,15 @@ public class XmrWalletService {
return MONERO_WALLET_RPC_MANAGER.startInstance(cmd); return MONERO_WALLET_RPC_MANAGER.startInstance(cmd);
} }
private void trySyncMainWallet() {
try {
log.info("Syncing main wallet");
wallet.startSyncing(connectionsService.getDefaultRefreshPeriodMs()); // start syncing wallet in background
wallet.sync(); // blocking
connectionsService.doneDownload(); // TODO: using this to signify both daemon and wallet synced, refactor sync handling of both
log.info("Done syncing main wallet");
saveMainWallet(false); // skip backup on open
} catch (Exception e) {
log.warn("Error syncing main wallet: {}", e.getMessage());
}
}
private void setDaemonConnection(MoneroRpcConnection connection) { private void setDaemonConnection(MoneroRpcConnection connection) {
log.info("Setting wallet daemon connection: " + (connection == null ? null : connection.getUri())); log.info("Setting wallet daemon connection: " + (connection == null ? null : connection.getUri()));
if (wallet == null) maybeInitMainWallet(); if (wallet == null) maybeInitMainWallet();
else { else {
wallet.setDaemonConnection(connection); wallet.setDaemonConnection(connection);
if (connection != null && !Boolean.FALSE.equals(connection.isConnected())) new Thread(() -> trySyncMainWallet()).start(); if (connection != null && !Boolean.FALSE.equals(connection.isConnected())) {
wallet.startSyncing(connectionsService.getDefaultRefreshPeriodMs());
new Thread(() -> wallet.sync()).start();
}
} }
} }