delete and restore account restarts application automatically

added standard input to keepalive loop for issuing commands to daemon

Co-authored-by: duriancrepe <duriancrepe@protonmail.com>
This commit is contained in:
woodser 2023-01-19 19:19:56 -05:00
parent 350d2d5398
commit 9877ba87a4
14 changed files with 181 additions and 73 deletions

View file

@ -69,11 +69,15 @@ public class CoreAccountService {
}
public void addListener(AccountServiceListener listener) {
listeners.add(listener);
synchronized (listeners) {
listeners.add(listener);
}
}
public boolean removeListener(AccountServiceListener listener) {
return listeners.remove(listener);
synchronized (listeners) {
return listeners.remove(listener);
}
}
public boolean accountExists() {
@ -99,7 +103,9 @@ public class CoreAccountService {
if (!accountExists()) throw new IllegalStateException("Cannot open account if account does not exist");
if (keyRing.unlockKeys(password, false)) {
this.password = password;
for (AccountServiceListener listener : new ArrayList<AccountServiceListener>(listeners)) listener.onAccountOpened();
synchronized (listeners) {
for (AccountServiceListener listener : listeners) listener.onAccountOpened();
}
} else {
throw new IllegalStateException("keyRing.unlockKeys() returned false, that should never happen");
}
@ -110,13 +116,17 @@ public class CoreAccountService {
String oldPassword = this.password;
keyStorage.saveKeyRing(keyRing, oldPassword, password);
this.password = password;
for (AccountServiceListener listener : new ArrayList<AccountServiceListener>(listeners)) listener.onPasswordChanged(oldPassword, password);
synchronized (listeners) {
for (AccountServiceListener listener : listeners) listener.onPasswordChanged(oldPassword, password);
}
}
public void closeAccount() {
if (!isAccountOpen()) throw new IllegalStateException("Cannot close unopened account");
keyRing.lockKeys(); // closed account means the keys are locked
for (AccountServiceListener listener : new ArrayList<AccountServiceListener>(listeners)) listener.onAccountClosed();
synchronized (listeners) {
for (AccountServiceListener listener : listeners) listener.onAccountClosed();
}
}
public void backupAccount(int bufferSize, Consumer<InputStream> consume, Consumer<Exception> error) {
@ -147,13 +157,17 @@ public class CoreAccountService {
if (accountExists()) throw new IllegalStateException("Cannot restore account if there is an existing account");
File dataDir = new File(config.appDataDir.getPath());
ZipUtils.unzipToDir(dataDir, inputStream, bufferSize);
for (AccountServiceListener listener : listeners) listener.onAccountRestored(onShutdown);
synchronized (listeners) {
for (AccountServiceListener listener : listeners) listener.onAccountRestored(onShutdown);
}
}
public void deleteAccount(Runnable onShutdown) {
try {
keyRing.lockKeys();
for (AccountServiceListener listener : listeners) listener.onAccountDeleted(onShutdown);
synchronized (listeners) {
for (AccountServiceListener listener : listeners) listener.onAccountDeleted(onShutdown);
}
File dataDir = new File(config.appDataDir.getPath()); // TODO (woodser): deleting directory after gracefulShutdown() so services don't throw when they try to persist (e.g. XmrTxProofService), but gracefulShutdown() should honor read-only shutdown
FileUtil.deleteDirectory(dataDir, null, false);
} catch (Exception err) {

View file

@ -0,0 +1,64 @@
/*
* This file is part of Haveno.
*
* Haveno is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Haveno is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Haveno. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.app;
import java.util.concurrent.*;
/**
* A cancellable console input reader.
* Derived from https://www.javaspecialists.eu/archive/Issue153-Timeout-on-Console-Input.html
*/
public class ConsoleInput {
private final int tries;
private final int timeout;
private final TimeUnit unit;
private Future<String> future;
public ConsoleInput(int tries, int timeout, TimeUnit unit) {
this.tries = tries;
this.timeout = timeout;
this.unit = unit;
}
public void cancel() {
if (future != null)
future.cancel(true);
}
public String readLine() throws InterruptedException {
ExecutorService ex = Executors.newSingleThreadExecutor();
String input = null;
try {
for (int i = 0; i < tries; i++) {
future = ex.submit(new ConsoleInputReadTask());
try {
input = future.get(timeout, unit);
break;
} catch (ExecutionException e) {
e.getCause().printStackTrace();
} catch (TimeoutException e) {
future.cancel(true);
} finally {
future = null;
}
}
} finally {
ex.shutdownNow();
}
return input;
}
}

View file

@ -0,0 +1,45 @@
/*
* This file is part of Haveno.
*
* Haveno is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Haveno is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Haveno. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.app;
import java.io.*;
import java.util.concurrent.Callable;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ConsoleInputReadTask implements Callable<String> {
public String call() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
log.debug("ConsoleInputReadTask run() called.");
String input;
do {
try {
// wait until we have data to complete a readLine()
while (!br.ready()) {
Thread.sleep(100);
}
// readline will always block until an input exists.
input = br.readLine();
} catch (InterruptedException e) {
log.debug("ConsoleInputReadTask() cancelled");
return null;
}
} while ("".equals(input));
return input;
}
}

View file

@ -19,6 +19,7 @@ package bisq.core.app;
import bisq.core.api.AccountServiceListener;
import bisq.core.api.CoreAccountService;
import bisq.core.api.CoreMoneroConnectionsService;
import bisq.core.btc.setup.WalletsSetup;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.XmrWalletService;
@ -48,7 +49,11 @@ import bisq.common.util.Utilities;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.io.Console;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;
@ -60,6 +65,7 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
public static final int EXIT_SUCCESS = 0;
public static final int EXIT_FAILURE = 1;
public static final int EXIT_RESTART = 2;
private final String fullName;
private final String scriptName;
@ -72,6 +78,9 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
protected Config config;
private boolean isShutdownInProgress;
private boolean isReadOnly;
private Thread keepRunningThread;
private AtomicInteger keepRunningResult = new AtomicInteger(EXIT_SUCCESS);
private Runnable shutdownCompletedHandler;
public HavenoExecutable(String fullName, String scriptName, String appName, String version) {
this.fullName = fullName;
@ -80,7 +89,7 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
this.version = version;
}
public void execute(String[] args) {
public int execute(String[] args) {
try {
config = new Config(appName, Utilities.getUserDataDir(), args);
if (config.helpRequested) {
@ -98,14 +107,14 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
System.exit(EXIT_FAILURE);
}
doExecute();
return doExecute();
}
///////////////////////////////////////////////////////////////////////////////////////////
// First synchronous execution tasks
///////////////////////////////////////////////////////////////////////////////////////////
protected void doExecute() {
protected int doExecute() {
CommonSetup.setup(config, this);
CoreSetup.setup(config);
@ -113,6 +122,8 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
// If application is JavaFX application we need to wait until it is initialized
launchApplication();
return EXIT_SUCCESS;
}
protected abstract void configUserThread();
@ -149,8 +160,8 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
// Application needs to restart on delete and restore of account.
accountService.addListener(new AccountServiceListener() {
@Override public void onAccountDeleted(Runnable onShutdown) { shutDownNoPersist(onShutdown); }
@Override public void onAccountRestored(Runnable onShutdown) { shutDownNoPersist(onShutdown); }
@Override public void onAccountDeleted(Runnable onShutdown) { shutDownNoPersist(onShutdown, true); }
@Override public void onAccountRestored(Runnable onShutdown) { shutDownNoPersist(onShutdown, true); }
});
// Attempt to login, subclasses should implement interactive login and or rpc login.
@ -165,18 +176,27 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
/**
* Do not persist when shutting down after account restore and restarts since
* that causes the current persistables to overwrite the restored or deleted state.
*
* If restart is specified, initiates an in-process asynchronous restart of the
* application by interrupting the keepRunningThread.
*/
protected void shutDownNoPersist(Runnable onShutdown) {
protected void shutDownNoPersist(Runnable onShutdown, boolean restart) {
this.isReadOnly = true;
gracefulShutDown(() -> {
log.info("Shutdown without persisting");
if (onShutdown != null) onShutdown.run();
});
if (restart) {
shutdownCompletedHandler = onShutdown;
keepRunningResult.set(EXIT_RESTART);
keepRunningThread.interrupt();
} else {
gracefulShutDown(() -> {
log.info("Shutdown without persisting");
if (onShutdown != null) onShutdown.run();
});
}
}
/**
* Attempt to login. TODO: supply a password in config or args
*
*
* @return true if account is opened successfully.
*/
protected boolean loginAccount() {
@ -258,16 +278,32 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
// GracefulShutDownHandler implementation
///////////////////////////////////////////////////////////////////////////////////////////
// This might need to be overwritten in case the application is not using all modules
@Override
public void gracefulShutDown(ResultHandler resultHandler) {
gracefulShutDown(resultHandler, true);
}
// This might need to be overwritten in case the application is not using all modules
@Override
public void gracefulShutDown(ResultHandler onShutdown, boolean systemExit) {
log.info("Start graceful shutDown");
if (isShutdownInProgress) {
log.info("Ignoring call to gracefulShutDown, already in progress");
return;
}
isShutdownInProgress = true;
ResultHandler resultHandler;
if (shutdownCompletedHandler != null) {
resultHandler = () -> {
shutdownCompletedHandler.run();
onShutdown.handleResult();
};
} else {
resultHandler = onShutdown;
}
if (injector == null) {
log.info("Shut down called before injector was created");
resultHandler.handleResult();
@ -281,7 +317,7 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
injector.getInstance(XmrTxProofService.class).shutDown();
injector.getInstance(AvoidStandbyModeService.class).shutDown();
injector.getInstance(TradeManager.class).shutDown();
injector.getInstance(XmrWalletService.class).shutDown(); // TODO: why not shut down BtcWalletService, etc? shutdown CoreMoneroConnectionsService
injector.getInstance(XmrWalletService.class).shutDown(!isReadOnly); // TODO: why not shut down BtcWalletService, etc? shutdown CoreMoneroConnectionsService
log.info("OpenOfferManager shutdown started");
injector.getInstance(OpenOfferManager.class).shutDown(() -> {
log.info("OpenOfferManager shutdown completed");
@ -296,36 +332,31 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
injector.getInstance(P2PService.class).shutDown(() -> {
log.info("P2PService shutdown completed");
module.close(injector);
completeShutdown(resultHandler, EXIT_SUCCESS);
completeShutdown(resultHandler, EXIT_SUCCESS, systemExit);
});
});
walletsSetup.shutDown();
});
// Wait max 20 sec.
UserThread.runAfter(() -> {
log.warn("Graceful shut down not completed in 20 sec. We trigger our timeout handler.");
completeShutdown(resultHandler, EXIT_SUCCESS);
}, 20);
} catch (Throwable t) {
log.error("App shutdown failed with exception {}", t.toString());
t.printStackTrace();
completeShutdown(resultHandler, EXIT_FAILURE);
completeShutdown(resultHandler, EXIT_FAILURE, systemExit);
}
}
private void completeShutdown(ResultHandler resultHandler, int exitCode) {
private void completeShutdown(ResultHandler resultHandler, int exitCode, boolean systemExit) {
if (!isReadOnly) {
// If user tried to downgrade we do not write the persistable data to avoid data corruption
PersistenceManager.flushAllDataToDiskAtShutdown(() -> {
log.info("Graceful shutdown flushed persistence. Exiting now.");
resultHandler.handleResult();
UserThread.runAfter(() -> System.exit(exitCode), 1);
if (systemExit)
UserThread.runAfter(() -> System.exit(exitCode), 1);
});
} else {
resultHandler.handleResult();
UserThread.runAfter(() -> System.exit(exitCode), 1);
if (systemExit)
UserThread.runAfter(() -> System.exit(exitCode), 1);
}
}
@ -341,4 +372,46 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
if (doShutDown)
gracefulShutDown(() -> log.info("gracefulShutDown complete"));
}
/**
* Runs until a command interrupts the application and returns the desired command behavior.
* @return EXIT_SUCCESS to initiate a shutdown, EXIT_RESTART to initiate an in process restart.
*/
protected int keepRunning() {
keepRunningThread = new Thread(() -> {
ConsoleInput reader = new ConsoleInput(Integer.MAX_VALUE, Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
while (true) {
Console console = System.console();
try {
if (console == null) {
Thread.sleep(Long.MAX_VALUE);
} else {
var cmd = reader.readLine();
if ("exit".equals(cmd)) {
keepRunningResult.set(EXIT_SUCCESS);
break;
} else if ("restart".equals(cmd)) {
keepRunningResult.set(EXIT_RESTART);
break;
} else if ("help".equals(cmd)) {
System.out.println("Commands: restart, exit, help");
} else {
System.out.println("Unknown command, use: restart, exit, help");
}
}
} catch (InterruptedException e) {
break;
}
}
});
keepRunningThread.start();
try {
keepRunningThread.join();
} catch (InterruptedException ie) {
System.out.println(ie);
}
return keepRunningResult.get();
}
}

View file

@ -47,10 +47,10 @@ public class HavenoHeadlessAppMain extends HavenoExecutable {
}
@Override
protected void doExecute() {
protected int doExecute() {
super.doExecute();
keepRunning();
return keepRunning();
}
///////////////////////////////////////////////////////////////////////////////////////////
@ -114,14 +114,4 @@ public class HavenoHeadlessAppMain extends HavenoExecutable {
// In headless mode we don't have an async behaviour so we trigger the setup by calling onApplicationStarted
onApplicationStarted();
}
// TODO: implement interactive console which allows user to input commands; login, logoff, exit
private void keepRunning() {
while (true) {
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException ignore) {
}
}
}
}

View file

@ -97,7 +97,7 @@ public abstract class ExecutableForAppWithP2p extends HavenoExecutable {
});
});
injector.getInstance(WalletsSetup.class).shutDown();
injector.getInstance(XmrWalletService.class).shutDown(); // TODO (woodser): this is not actually called, perhaps because WalletsSetup.class completes too quick so its listener calls System.exit(0)
injector.getInstance(XmrWalletService.class).shutDown(true); // TODO (woodser): this is not actually called, perhaps because WalletsSetup.class completes too quick so its listener calls System.exit(0)
injector.getInstance(BtcWalletService.class).shutDown();
}));
// we wait max 5 sec.
@ -187,16 +187,6 @@ public abstract class ExecutableForAppWithP2p extends HavenoExecutable {
}, TimeUnit.HOURS.toSeconds(2));
}
@SuppressWarnings("InfiniteLoopStatement")
protected void keepRunning() {
while (true) {
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException ignore) {
}
}
}
protected void checkMemory(Config config, GracefulShutDownHandler gracefulShutDownHandler) {
int maxMemory = config.maxMemory;
UserThread.runPeriodically(() -> {

View file

@ -113,13 +113,15 @@ public class MoneroWalletRpcManager {
public void stopInstance(MoneroWalletRpc walletRpc) {
// unregister port
int port = -1;
synchronized (registeredPorts) {
boolean found = false;
for (Map.Entry<Integer, MoneroWalletRpc> entry : registeredPorts.entrySet()) {
if (walletRpc == entry.getValue()) {
found = true;
try {
unregisterPort(entry.getKey());
port = entry.getKey();
unregisterPort(port);
} catch (Exception e) {
throw new MoneroError(e);
}
@ -130,6 +132,8 @@ public class MoneroWalletRpcManager {
}
// stop process
String pid = walletRpc.getProcess() == null ? null : String.valueOf(walletRpc.getProcess().pid());
log.info("Stopping MoneroWalletRpc port: {} pid: {}", port, pid);
walletRpc.stopProcess();
}

View file

@ -252,7 +252,6 @@ public class WalletConfig extends AbstractIdleService {
@Override
protected void shutDown() throws Exception {
}
public NetworkParameters params() {

View file

@ -146,7 +146,7 @@ public class XmrWalletService {
@Override
public void onAccountClosed() {
log.info(getClass() + ".accountService.onAccountClosed()");
closeAllWallets();
closeAllWallets(true);
}
@Override
@ -304,7 +304,7 @@ public class XmrWalletService {
/**
* Thaw the given outputs with a lock on the wallet.
*
*
* @param keyImages the key images to thaw
*/
public void thawOutputs(Collection<String> keyImages) {
@ -527,9 +527,9 @@ public class XmrWalletService {
}
}
public void shutDown() {
public void shutDown(boolean save) {
this.isShutDown = true;
closeAllWallets();
closeAllWallets(save);
}
// ------------------------------ PRIVATE HELPERS -------------------------
@ -746,7 +746,7 @@ public class XmrWalletService {
if (!new File(path + ".address.txt").delete()) throw new RuntimeException("Failed to delete wallet file: " + path);
}
private void closeAllWallets() {
private void closeAllWallets(boolean save) {
// collect wallets to shutdown
List<MoneroWallet> openWallets = new ArrayList<MoneroWallet>();