improve stability on tor, refactor startup and shut down

refactor startup sequence to improve message reliability
refactor shut down sequence to finish processing messages
reduce monerod requests to improve slow tor connections
refactor trade wallet polling
monero node service uses default data directory unless local
connections service checks connection by polling daemon
connections service supports getRefreshPeriodMs and shutting down
add make config: monerod-stagenet-custom
fix bugs in key image polling
force stop wallet on deletion
trade manager initializes persisted trades on data received
support hardcoding monero log level and request stack traces
remove xmrAddress from Arbitrator model
fix formatting of MoneroWalletRpcManager
This commit is contained in:
woodser 2023-04-17 08:54:10 -04:00
parent 5e364f7e7e
commit 2afa5d761d
51 changed files with 1058 additions and 644 deletions

View File

@ -210,6 +210,15 @@ monerod-stagenet:
--bootstrap-daemon-address auto \ --bootstrap-daemon-address auto \
--rpc-access-control-origins http://localhost:8080 \ --rpc-access-control-origins http://localhost:8080 \
monerod-stagenet-custom:
./.localnet/monerod \
--stagenet \
--no-zmq \
--p2p-bind-port 39080 \
--rpc-bind-port 39081 \
--bootstrap-daemon-address auto \
--rpc-access-control-origins http://localhost:8080 \
seednode-stagenet: seednode-stagenet:
./haveno-seednode$(APP_EXT) \ ./haveno-seednode$(APP_EXT) \
--baseCurrencyNetwork=XMR_STAGENET \ --baseCurrencyNetwork=XMR_STAGENET \

View File

@ -98,7 +98,9 @@ public class CoreAccountService {
if (accountExists()) throw new IllegalStateException("Cannot create account if account already exists"); if (accountExists()) throw new IllegalStateException("Cannot create account if account already exists");
keyRing.generateKeys(password); keyRing.generateKeys(password);
this.password = password; this.password = password;
for (AccountServiceListener listener : new ArrayList<AccountServiceListener>(listeners)) listener.onAccountCreated(); synchronized (listeners) {
for (AccountServiceListener listener : new ArrayList<>(listeners)) listener.onAccountCreated();
}
} }
public void openAccount(String password) throws IncorrectPasswordException { public void openAccount(String password) throws IncorrectPasswordException {
@ -106,7 +108,7 @@ public class CoreAccountService {
if (keyRing.unlockKeys(password, false)) { if (keyRing.unlockKeys(password, false)) {
this.password = password; this.password = password;
synchronized (listeners) { synchronized (listeners) {
for (AccountServiceListener listener : listeners) listener.onAccountOpened(); for (AccountServiceListener listener : new ArrayList<>(listeners)) listener.onAccountOpened();
} }
} else { } else {
throw new IllegalStateException("keyRing.unlockKeys() returned false, that should never happen"); throw new IllegalStateException("keyRing.unlockKeys() returned false, that should never happen");
@ -121,7 +123,7 @@ public class CoreAccountService {
keyStorage.saveKeyRing(keyRing, oldPassword, newPassword); keyStorage.saveKeyRing(keyRing, oldPassword, newPassword);
this.password = newPassword; this.password = newPassword;
synchronized (listeners) { synchronized (listeners) {
for (AccountServiceListener listener : listeners) listener.onPasswordChanged(oldPassword, newPassword); for (AccountServiceListener listener : new ArrayList<>(listeners)) listener.onPasswordChanged(oldPassword, newPassword);
} }
} }
@ -135,7 +137,7 @@ public class CoreAccountService {
if (!isAccountOpen()) throw new IllegalStateException("Cannot close unopened account"); if (!isAccountOpen()) throw new IllegalStateException("Cannot close unopened account");
keyRing.lockKeys(); // closed account means the keys are locked keyRing.lockKeys(); // closed account means the keys are locked
synchronized (listeners) { synchronized (listeners) {
for (AccountServiceListener listener : listeners) listener.onAccountClosed(); for (AccountServiceListener listener : new ArrayList<>(listeners)) listener.onAccountClosed();
} }
} }
@ -168,7 +170,7 @@ public class CoreAccountService {
File dataDir = new File(config.appDataDir.getPath()); File dataDir = new File(config.appDataDir.getPath());
ZipUtils.unzipToDir(dataDir, inputStream, bufferSize); ZipUtils.unzipToDir(dataDir, inputStream, bufferSize);
synchronized (listeners) { synchronized (listeners) {
for (AccountServiceListener listener : listeners) listener.onAccountRestored(onShutdown); for (AccountServiceListener listener : new ArrayList<>(listeners)) listener.onAccountRestored(onShutdown);
} }
} }
@ -176,7 +178,7 @@ public class CoreAccountService {
try { try {
if (isAccountOpen()) closeAccount(); if (isAccountOpen()) closeAccount();
synchronized (listeners) { synchronized (listeners) {
for (AccountServiceListener listener : listeners) listener.onAccountDeleted(onShutdown); for (AccountServiceListener listener : new ArrayList<>(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 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); FileUtil.deleteDirectory(dataDir, null, false);

View File

@ -171,7 +171,6 @@ class CoreDisputeAgentsService {
ErrorMessageHandler errorMessageHandler) { ErrorMessageHandler errorMessageHandler) {
Arbitrator arbitrator = new Arbitrator( Arbitrator arbitrator = new Arbitrator(
p2PService.getAddress(), p2PService.getAddress(),
xmrWalletService.getWallet().getPrimaryAddress(), // TODO: how is this used?
keyRing.getPubKeyRing(), keyRing.getPubKeyRing(),
new ArrayList<>(languageCodes), new ArrayList<>(languageCodes),
new Date().getTime(), new Date().getTime(),

View File

@ -1,6 +1,5 @@
package haveno.core.api; package haveno.core.api;
import haveno.common.UserThread;
import haveno.common.app.DevEnv; import haveno.common.app.DevEnv;
import haveno.common.config.BaseCurrencyNetwork; import haveno.common.config.BaseCurrencyNetwork;
import haveno.common.config.Config; import haveno.common.config.Config;
@ -9,6 +8,8 @@ import haveno.core.xmr.model.EncryptedConnectionList;
import haveno.core.xmr.setup.DownloadListener; import haveno.core.xmr.setup.DownloadListener;
import haveno.core.xmr.setup.WalletsSetup; import haveno.core.xmr.setup.WalletsSetup;
import haveno.network.Socks5ProxyProvider; import haveno.network.Socks5ProxyProvider;
import haveno.network.p2p.P2PService;
import haveno.network.p2p.P2PServiceListener;
import javafx.beans.property.IntegerProperty; import javafx.beans.property.IntegerProperty;
import javafx.beans.property.LongProperty; import javafx.beans.property.LongProperty;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
@ -57,8 +58,9 @@ public final class CoreMoneroConnectionsService {
new MoneroRpcConnection("http://127.0.0.1:28081").setPriority(1) new MoneroRpcConnection("http://127.0.0.1:28081").setPriority(1)
)); ));
DEFAULT_CONNECTIONS.put(BaseCurrencyNetwork.XMR_STAGENET, Arrays.asList( DEFAULT_CONNECTIONS.put(BaseCurrencyNetwork.XMR_STAGENET, Arrays.asList(
new MoneroRpcConnection("http://127.0.0.1:38081").setPriority(1), // localhost is first priority, use loopback address to match url generated by local node service new MoneroRpcConnection("http://127.0.0.1:38081").setPriority(1), // localhost is first priority, use loopback address 127.0.0.1 to match url used by local node service
new MoneroRpcConnection("http://45.63.8.26:38081").setPriority(2), new MoneroRpcConnection("http://127.0.0.1:39081").setPriority(2), // from makefile: `monerod-stagenet-custom`
new MoneroRpcConnection("http://45.63.8.26:38081").setPriority(2), // hosted by haveno
new MoneroRpcConnection("http://stagenet.community.rino.io:38081").setPriority(2), new MoneroRpcConnection("http://stagenet.community.rino.io:38081").setPriority(2),
new MoneroRpcConnection("http://stagenet.melo.tools:38081").setPriority(2), new MoneroRpcConnection("http://stagenet.melo.tools:38081").setPriority(2),
new MoneroRpcConnection("http://node.sethforprivacy.com:38089").setPriority(2), new MoneroRpcConnection("http://node.sethforprivacy.com:38089").setPriority(2),
@ -89,14 +91,17 @@ public final class CoreMoneroConnectionsService {
private final DownloadListener downloadListener = new DownloadListener(); private final DownloadListener downloadListener = new DownloadListener();
private Socks5ProxyProvider socks5ProxyProvider; private Socks5ProxyProvider socks5ProxyProvider;
private boolean isInitialized;
private MoneroDaemonRpc daemon; private MoneroDaemonRpc daemon;
@Getter @Getter
private MoneroDaemonInfo lastInfo; private MoneroDaemonInfo lastInfo;
private boolean isInitialized = false; private TaskLooper daemonPollLooper;
private TaskLooper updateDaemonLooper;; private boolean isShutDownStarted;
private List<MoneroConnectionManagerListener> listeners = new ArrayList<>();
@Inject @Inject
public CoreMoneroConnectionsService(Config config, public CoreMoneroConnectionsService(P2PService p2PService,
Config config,
CoreContext coreContext, CoreContext coreContext,
WalletsSetup walletsSetup, WalletsSetup walletsSetup,
CoreAccountService accountService, CoreAccountService accountService,
@ -112,35 +117,43 @@ public final class CoreMoneroConnectionsService {
this.connectionList = connectionList; this.connectionList = connectionList;
this.socks5ProxyProvider = socks5ProxyProvider; this.socks5ProxyProvider = socks5ProxyProvider;
// initialize after account open and basic setup // initialize when connected to p2p network
walletsSetup.addSetupTaskHandler(() -> { // TODO: use something better than legacy WalletSetup for notification to initialize p2PService.addP2PServiceListener(new P2PServiceListener() {
@Override
// initialize from connections read from disk public void onTorNodeReady() {
initialize(); initialize();
}
// listen for account to be opened or password changed @Override
accountService.addListener(new AccountServiceListener() { public void onHiddenServicePublished() {}
@Override
@Override public void onDataReceived() {}
public void onAccountOpened() { @Override
try { public void onNoSeedNodeAvailable() {}
log.info(getClass() + ".onAccountOpened() called"); @Override
initialize(); public void onNoPeersAvailable() {}
} catch (Exception e) { @Override
e.printStackTrace(); public void onUpdatedDataReceived() {}
throw new RuntimeException(e);
}
}
@Override
public void onPasswordChanged(String oldPassword, String newPassword) {
log.info(getClass() + ".onPasswordChanged({}, {}) called", oldPassword, newPassword);
connectionList.changePassword(oldPassword, newPassword);
}
});
}); });
} }
public void onShutDownStarted() {
log.info("{}.onShutDownStarted()", getClass().getSimpleName());
isShutDownStarted = true;
synchronized (this) {
// ensures request not in progress
}
}
public void shutDown() {
log.info("Shutting down started for {}", getClass().getSimpleName());
synchronized (lock) {
isInitialized = false;
if (daemonPollLooper != null) daemonPollLooper.stop();
connectionManager.stopCheckingConnection();
daemon = null;
}
}
// ------------------------ CONNECTION MANAGEMENT ------------------------- // ------------------------ CONNECTION MANAGEMENT -------------------------
public MoneroDaemonRpc getDaemon() { public MoneroDaemonRpc getDaemon() {
@ -154,11 +167,11 @@ public final class CoreMoneroConnectionsService {
public void addListener(MoneroConnectionManagerListener listener) { public void addListener(MoneroConnectionManagerListener listener) {
synchronized (lock) { synchronized (lock) {
connectionManager.addListener(listener); listeners.add(listener);
} }
} }
public boolean isConnected() { public Boolean isConnected() {
return connectionManager.isConnected(); return connectionManager.isConnected();
} }
@ -225,8 +238,8 @@ public final class CoreMoneroConnectionsService {
public void startCheckingConnection(Long refreshPeriod) { public void startCheckingConnection(Long refreshPeriod) {
synchronized (lock) { synchronized (lock) {
accountService.checkAccountOpen(); accountService.checkAccountOpen();
connectionManager.startCheckingConnection(refreshPeriod == null ? getDefaultRefreshPeriodMs() : refreshPeriod);
connectionList.setRefreshPeriod(refreshPeriod); connectionList.setRefreshPeriod(refreshPeriod);
updatePolling();
} }
} }
@ -257,17 +270,11 @@ public final class CoreMoneroConnectionsService {
return getConnection() != null && HavenoUtils.isLocalHost(getConnection().getUri()); return getConnection() != null && HavenoUtils.isLocalHost(getConnection().getUri());
} }
public long getDefaultRefreshPeriodMs() { public long getRefreshPeriodMs() {
if (daemon == null) return REFRESH_PERIOD_LOCAL_MS; if (connectionList.getRefreshPeriod() < 0 || connectionList.getRefreshPeriod() > 0) {
else { return connectionList.getRefreshPeriod();
if (isConnectionLocal()) { } else {
if (lastInfo != null && (lastInfo.isBusySyncing() || (lastInfo.getHeightWithoutBootstrap() != null && lastInfo.getHeightWithoutBootstrap() > 0 && lastInfo.getHeightWithoutBootstrap() < lastInfo.getHeight()))) return REFRESH_PERIOD_HTTP_MS; // refresh slower if syncing or bootstrapped return getDefaultRefreshPeriodMs();
else return REFRESH_PERIOD_LOCAL_MS; // TODO: announce faster refresh after done syncing
} else if (getConnection().isOnion()) {
return REFRESH_PERIOD_ONION_MS;
} else {
return REFRESH_PERIOD_HTTP_MS;
}
} }
} }
@ -329,7 +336,48 @@ public final class CoreMoneroConnectionsService {
// ------------------------------- HELPERS -------------------------------- // ------------------------------- HELPERS --------------------------------
private long getDefaultRefreshPeriodMs() {
if (daemon == null) return REFRESH_PERIOD_LOCAL_MS;
else {
if (isConnectionLocal()) {
if (lastInfo != null && (lastInfo.isBusySyncing() || (lastInfo.getHeightWithoutBootstrap() != null && lastInfo.getHeightWithoutBootstrap() > 0 && lastInfo.getHeightWithoutBootstrap() < lastInfo.getHeight()))) return REFRESH_PERIOD_HTTP_MS; // refresh slower if syncing or bootstrapped
else return REFRESH_PERIOD_LOCAL_MS; // TODO: announce faster refresh after done syncing
} else if (getConnection().isOnion()) {
return REFRESH_PERIOD_ONION_MS;
} else {
return REFRESH_PERIOD_HTTP_MS;
}
}
}
private void initialize() { private void initialize() {
// initialize connections
initializeConnections();
// listen for account to be opened or password changed
accountService.addListener(new AccountServiceListener() {
@Override
public void onAccountOpened() {
try {
log.info(getClass() + ".onAccountOpened() called");
initialize();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
public void onPasswordChanged(String oldPassword, String newPassword) {
log.info(getClass() + ".onPasswordChanged({}, {}) called", oldPassword, newPassword);
connectionList.changePassword(oldPassword, newPassword);
}
});
}
private void initializeConnections() {
synchronized (lock) { synchronized (lock) {
// reset connection manager // reset connection manager
@ -365,12 +413,11 @@ public final class CoreMoneroConnectionsService {
currentConnectionUri = Optional.of(connection.getUri()); currentConnectionUri = Optional.of(connection.getUri());
} }
// restore configuration and check connection // restore configuration
if ("".equals(config.xmrNode)) connectionManager.setAutoSwitch(connectionList.getAutoSwitch()); if ("".equals(config.xmrNode)) connectionManager.setAutoSwitch(connectionList.getAutoSwitch());
long refreshPeriod = connectionList.getRefreshPeriod();
if (refreshPeriod > 0) connectionManager.startCheckingConnection(refreshPeriod); // check connection
else if (refreshPeriod == 0) connectionManager.startCheckingConnection(); checkConnection();
else checkConnection();
// run once // run once
if (!isInitialized) { if (!isInitialized) {
@ -392,21 +439,23 @@ public final class CoreMoneroConnectionsService {
}); });
} }
// if offline and last connection is local, start local node if offline // if offline and last connection is local node, start local node if it's offline
currentConnectionUri.ifPresent(uri -> { currentConnectionUri.ifPresent(uri -> {
try { try {
if (!connectionManager.isConnected() && HavenoUtils.isLocalHost(uri) && !nodeService.isOnline()) { if (!connectionManager.isConnected() && nodeService.equalsUri(uri) && !nodeService.isOnline()) {
log.info("Starting local node");
nodeService.startMoneroNode(); nodeService.startMoneroNode();
} }
} catch (Exception e) { } catch (Exception e) {
log.warn("Unable to start local monero node: " + e.getMessage()); log.warn("Unable to start local monero node: " + e.getMessage());
e.printStackTrace();
} }
}); });
// prefer to connect to local node unless prevented by configuration // prefer to connect to local node unless prevented by configuration
if ("".equals(config.xmrNode) && if ("".equals(config.xmrNode) &&
(!connectionManager.isConnected() || connectionManager.getAutoSwitch()) && (!connectionManager.isConnected() || connectionManager.getAutoSwitch()) &&
nodeService.isConnected()) { nodeService.isConnected()) {
MoneroRpcConnection connection = connectionManager.getConnectionByUri(nodeService.getDaemon().getRpcConnection().getUri()); MoneroRpcConnection connection = connectionManager.getConnectionByUri(nodeService.getDaemon().getRpcConnection().getUri());
if (connection != null) { if (connection != null) {
connection.checkConnection(connectionManager.getTimeout()); connection.checkConnection(connectionManager.getTimeout());
@ -417,22 +466,23 @@ public final class CoreMoneroConnectionsService {
// if using legacy desktop app, connect to best available connection // if using legacy desktop app, connect to best available connection
if (!coreContext.isApiUser() && "".equals(config.xmrNode)) { if (!coreContext.isApiUser() && "".equals(config.xmrNode)) {
connectionManager.setAutoSwitch(true); connectionManager.setAutoSwitch(true);
connectionManager.setConnection(connectionManager.getBestAvailableConnection()); MoneroRpcConnection bestConnection = connectionManager.getBestAvailableConnection();
log.info("Setting best available connection for monerod: " + (bestConnection == null ? null : bestConnection.getUri()));
connectionManager.setConnection(bestConnection);
} }
// register connection change listener // register connection change listener
if (!isInitialized) { connectionManager.addListener(this::onConnectionChanged);
connectionManager.addListener(this::onConnectionChanged); isInitialized = true;
isInitialized = true;
}
// announce connection // update connection state
onConnectionChanged(connectionManager.getConnection()); onConnectionChanged(connectionManager.getConnection());
} }
} }
private void onConnectionChanged(MoneroRpcConnection currentConnection) { private void onConnectionChanged(MoneroRpcConnection currentConnection) {
// TODO: ignore if shutdown log.info("CoreMoneroConnetionsService.onConnectionChanged() uri={}, connected=", currentConnection == null ? null : currentConnection.getUri(), currentConnection == null ? "false" : currentConnection.isConnected());
if (isShutDownStarted) return;
synchronized (lock) { synchronized (lock) {
if (currentConnection == null) { if (currentConnection == null) {
daemon = null; daemon = null;
@ -443,31 +493,46 @@ public final class CoreMoneroConnectionsService {
connectionList.addConnection(currentConnection); connectionList.addConnection(currentConnection);
connectionList.setCurrentConnectionUri(currentConnection.getUri()); connectionList.setCurrentConnectionUri(currentConnection.getUri());
} }
startPollingDaemon();
} }
} updatePolling();
private void startPollingDaemon() { // notify listeners
synchronized (lock) { synchronized (lock) {
updateDaemonInfo(); for (MoneroConnectionManagerListener listener : listeners) listener.onConnectionChanged(currentConnection);
if (updateDaemonLooper != null) updateDaemonLooper.stop();
UserThread.runAfter(() -> {
synchronized (lock) {
if (updateDaemonLooper != null) updateDaemonLooper.stop();
updateDaemonLooper = new TaskLooper(() -> updateDaemonInfo());
updateDaemonLooper.start(getDefaultRefreshPeriodMs());
}
}, getDefaultRefreshPeriodMs() / 1000);
} }
} }
private void updateDaemonInfo() { private void updatePolling() {
new Thread(() -> {
synchronized (lock) {
stopPolling();
if (getRefreshPeriodMs() > 0) startPolling();
}
}).start();
}
private void startPolling() {
synchronized (lock) {
if (daemonPollLooper != null) daemonPollLooper.stop();
daemonPollLooper = new TaskLooper(() -> pollDaemonInfo());
daemonPollLooper.start(getRefreshPeriodMs());
}
}
private void stopPolling() {
synchronized (lock) {
if (daemonPollLooper != null) daemonPollLooper.stop();
}
}
private void pollDaemonInfo() {
if (isShutDownStarted) return;
try { try {
log.trace("Updating daemon info"); log.debug("Polling daemon info");
if (daemon == null) throw new RuntimeException("No daemon connection"); if (daemon == null) throw new RuntimeException("No daemon connection");
lastInfo = daemon.getInfo(); synchronized (this) {
//System.out.println(JsonUtils.serialize(lastInfo)); lastInfo = daemon.getInfo();
//System.out.println(JsonUtils.serialize(daemon.getSyncInfo())); }
chainHeight.set(lastInfo.getTargetHeight() == 0 ? lastInfo.getHeight() : lastInfo.getTargetHeight()); chainHeight.set(lastInfo.getTargetHeight() == 0 ? lastInfo.getHeight() : lastInfo.getTargetHeight());
// set peer connections // set peer connections
@ -478,20 +543,33 @@ public final class CoreMoneroConnectionsService {
// // TODO: peers unknown due to restricted RPC call // // TODO: peers unknown due to restricted RPC call
// } // }
// numPeers.set(peers.get().size()); // numPeers.set(peers.get().size());
numPeers.set(lastInfo.getNumOutgoingConnections() + lastInfo.getNumIncomingConnections()); numPeers.set(lastInfo.getNumOutgoingConnections() + lastInfo.getNumIncomingConnections());
peers.set(new ArrayList<MoneroPeer>()); peers.set(new ArrayList<MoneroPeer>());
// log recovery message
if (lastErrorTimestamp != null) { if (lastErrorTimestamp != null) {
log.info("Successfully fetched daemon info after previous error"); log.info("Successfully fetched daemon info after previous error");
lastErrorTimestamp = null; lastErrorTimestamp = null;
} }
// update and notify connected state
if (!Boolean.TRUE.equals(connectionManager.isConnected())) {
connectionManager.checkConnection();
}
} catch (Exception e) { } catch (Exception e) {
if (lastErrorTimestamp == null || System.currentTimeMillis() - lastErrorTimestamp > MIN_ERROR_LOG_PERIOD_MS) {
// log error message periodically
if ((lastErrorTimestamp == null || System.currentTimeMillis() - lastErrorTimestamp > MIN_ERROR_LOG_PERIOD_MS)) {
lastErrorTimestamp = System.currentTimeMillis(); lastErrorTimestamp = System.currentTimeMillis();
log.warn("Could not update daemon info: " + e.getMessage()); log.warn("Could not update daemon info: " + e.getMessage());
if (DevEnv.isDevMode()) e.printStackTrace(); if (DevEnv.isDevMode()) e.printStackTrace();
} }
if (connectionManager.getAutoSwitch()) connectionManager.setConnection(connectionManager.getBestAvailableConnection());
// check connection which notifies of changes
synchronized (this) {
if (connectionManager.getAutoSwitch()) connectionManager.setConnection(connectionManager.getBestAvailableConnection());
else connectionManager.checkConnection();
}
} }
} }

View File

@ -23,6 +23,7 @@ import haveno.core.trade.HavenoUtils;
import haveno.core.user.Preferences; import haveno.core.user.Preferences;
import haveno.core.xmr.MoneroNodeSettings; import haveno.core.xmr.MoneroNodeSettings;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import monero.common.MoneroUtils;
import monero.daemon.MoneroDaemonRpc; import monero.daemon.MoneroDaemonRpc;
import javax.inject.Inject; import javax.inject.Inject;
@ -44,7 +45,7 @@ public class CoreMoneroNodeService {
public static final String MONEROD_DIR = Config.baseCurrencyNetwork() == BaseCurrencyNetwork.XMR_LOCAL ? System.getProperty("user.dir") + File.separator + ".localnet" : Config.appDataDir().getAbsolutePath(); public static final String MONEROD_DIR = Config.baseCurrencyNetwork() == BaseCurrencyNetwork.XMR_LOCAL ? System.getProperty("user.dir") + File.separator + ".localnet" : Config.appDataDir().getAbsolutePath();
public static final String MONEROD_NAME = Utilities.isWindows() ? "monerod.exe" : "monerod"; public static final String MONEROD_NAME = Utilities.isWindows() ? "monerod.exe" : "monerod";
public static final String MONEROD_PATH = MONEROD_DIR + File.separator + MONEROD_NAME; public static final String MONEROD_PATH = MONEROD_DIR + File.separator + MONEROD_NAME;
private static final String MONEROD_DATADIR = MONEROD_DIR + File.separator + Config.baseCurrencyNetwork().toString().toLowerCase() + File.separator + "node1"; private static final String MONEROD_DATADIR = Config.baseCurrencyNetwork() == BaseCurrencyNetwork.XMR_LOCAL ? MONEROD_DIR + File.separator + Config.baseCurrencyNetwork().toString().toLowerCase() + File.separator + "node1" : null; // use default directory unless local
private final Preferences preferences; private final Preferences preferences;
private final List<MoneroNodeServiceListener> listeners = new ArrayList<>(); private final List<MoneroNodeServiceListener> listeners = new ArrayList<>();
@ -59,15 +60,17 @@ public class CoreMoneroNodeService {
// client to the local Monero node // client to the local Monero node
private MoneroDaemonRpc daemon; private MoneroDaemonRpc daemon;
private static Integer rpcPort;
@Inject static {
public CoreMoneroNodeService(Preferences preferences) {
this.preferences = preferences;
Integer rpcPort = null;
if (Config.baseCurrencyNetwork().isMainnet()) rpcPort = 18081; if (Config.baseCurrencyNetwork().isMainnet()) rpcPort = 18081;
else if (Config.baseCurrencyNetwork().isTestnet()) rpcPort = 28081; else if (Config.baseCurrencyNetwork().isTestnet()) rpcPort = 28081;
else if (Config.baseCurrencyNetwork().isStagenet()) rpcPort = 38081; else if (Config.baseCurrencyNetwork().isStagenet()) rpcPort = 38081;
else throw new RuntimeException("Base network is not local testnet, stagenet, or mainnet"); else throw new RuntimeException("Base network is not local testnet, stagenet, or mainnet");
}
@Inject
public CoreMoneroNodeService(Preferences preferences) {
this.preferences = preferences;
this.daemon = new MoneroDaemonRpc("http://" + HavenoUtils.LOOPBACK_HOST + ":" + rpcPort); this.daemon = new MoneroDaemonRpc("http://" + HavenoUtils.LOOPBACK_HOST + ":" + rpcPort);
} }
@ -90,6 +93,10 @@ public class CoreMoneroNodeService {
return daemon.getRpcConnection().checkConnection(5000); return daemon.getRpcConnection().checkConnection(5000);
} }
public boolean equalsUri(String uri) {
return HavenoUtils.isLocalHost(uri) && MoneroUtils.parseUri(uri).getPort() == rpcPort;
}
/** /**
* Check if local Monero node is online. * Check if local Monero node is online.
*/ */

View File

@ -34,25 +34,28 @@ import haveno.common.setup.UncaughtExceptionHandler;
import haveno.common.util.Utilities; import haveno.common.util.Utilities;
import haveno.core.api.AccountServiceListener; import haveno.core.api.AccountServiceListener;
import haveno.core.api.CoreAccountService; import haveno.core.api.CoreAccountService;
import haveno.core.api.CoreMoneroConnectionsService;
import haveno.core.offer.OfferBookService;
import haveno.core.offer.OpenOfferManager; import haveno.core.offer.OpenOfferManager;
import haveno.core.provider.price.PriceFeedService; import haveno.core.provider.price.PriceFeedService;
import haveno.core.setup.CorePersistedDataHost; import haveno.core.setup.CorePersistedDataHost;
import haveno.core.setup.CoreSetup; import haveno.core.setup.CoreSetup;
import haveno.core.support.dispute.arbitration.arbitrator.ArbitratorManager; import haveno.core.support.dispute.arbitration.arbitrator.ArbitratorManager;
import haveno.core.trade.HavenoUtils; import haveno.core.trade.HavenoUtils;
import haveno.core.trade.TradeManager;
import haveno.core.trade.statistics.TradeStatisticsManager; import haveno.core.trade.statistics.TradeStatisticsManager;
import haveno.core.trade.txproof.xmr.XmrTxProofService; import haveno.core.trade.txproof.xmr.XmrTxProofService;
import haveno.core.xmr.setup.WalletsSetup; import haveno.core.xmr.setup.WalletsSetup;
import haveno.core.xmr.wallet.BtcWalletService; import haveno.core.xmr.wallet.BtcWalletService;
import haveno.core.xmr.wallet.XmrWalletService; import haveno.core.xmr.wallet.XmrWalletService;
import haveno.network.p2p.P2PService; import haveno.network.p2p.P2PService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.Console; import java.io.Console;
import java.util.Arrays; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -74,7 +77,8 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
protected Injector injector; protected Injector injector;
protected AppModule module; protected AppModule module;
protected Config config; protected Config config;
private boolean isShutdownInProgress; @Getter
protected boolean isShutdownInProgress;
private boolean isReadOnly; private boolean isReadOnly;
private Thread keepRunningThread; private Thread keepRunningThread;
private AtomicInteger keepRunningResult = new AtomicInteger(EXIT_SUCCESS); private AtomicInteger keepRunningResult = new AtomicInteger(EXIT_SUCCESS);
@ -303,12 +307,13 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
// This might need to be overwritten in case the application is not using all modules // This might need to be overwritten in case the application is not using all modules
@Override @Override
public void gracefulShutDown(ResultHandler onShutdown, boolean systemExit) { public void gracefulShutDown(ResultHandler onShutdown, boolean systemExit) {
log.info("Start graceful shutDown"); log.info("Starting graceful shut down of {}", getClass().getSimpleName());
// ignore if shut down in progress
if (isShutdownInProgress) { if (isShutdownInProgress) {
log.info("Ignoring call to gracefulShutDown, already in progress"); log.info("Ignoring call to gracefulShutDown, already in progress");
return; return;
} }
isShutdownInProgress = true; isShutdownInProgress = true;
ResultHandler resultHandler; ResultHandler resultHandler;
@ -328,33 +333,41 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven
} }
try { try {
// notify trade protocols and wallets to prepare for shut down before shutting down
Set<Runnable> tasks = new HashSet<Runnable>();
tasks.add(() -> injector.getInstance(XmrWalletService.class).onShutDownStarted());
tasks.add(() -> injector.getInstance(CoreMoneroConnectionsService.class).onShutDownStarted());
HavenoUtils.executeTasks(tasks); // notify in parallel
injector.getInstance(PriceFeedService.class).shutDown(); injector.getInstance(PriceFeedService.class).shutDown();
injector.getInstance(ArbitratorManager.class).shutDown(); injector.getInstance(ArbitratorManager.class).shutDown();
injector.getInstance(TradeStatisticsManager.class).shutDown(); injector.getInstance(TradeStatisticsManager.class).shutDown();
injector.getInstance(XmrTxProofService.class).shutDown(); injector.getInstance(XmrTxProofService.class).shutDown();
injector.getInstance(AvoidStandbyModeService.class).shutDown(); injector.getInstance(AvoidStandbyModeService.class).shutDown();
log.info("TradeManager and XmrWalletService shutdown started");
HavenoUtils.executeTasks(Arrays.asList( // shut down trade and main wallets at same time // shut down open offer manager
() -> injector.getInstance(TradeManager.class).shutDown(), log.info("Shutting down OpenOfferManager, OfferBookService, and P2PService");
() -> injector.getInstance(XmrWalletService.class).shutDown(!isReadOnly)));
log.info("OpenOfferManager shutdown started");
injector.getInstance(OpenOfferManager.class).shutDown(() -> { injector.getInstance(OpenOfferManager.class).shutDown(() -> {
log.info("OpenOfferManager shutdown completed");
injector.getInstance(BtcWalletService.class).shutDown(); // shut down offer book service
injector.getInstance(OfferBookService.class).shutDown();
// We need to shutdown BitcoinJ before the P2PService as it uses Tor. // shut down p2p service
WalletsSetup walletsSetup = injector.getInstance(WalletsSetup.class); injector.getInstance(P2PService.class).shutDown(() -> {
walletsSetup.shutDownComplete.addListener((ov, o, n) -> { log.info("Done shutting down OpenOfferManager, OfferBookService, and P2PService");
log.info("WalletsSetup shutdown completed");
injector.getInstance(P2PService.class).shutDown(() -> { // shut down monero wallets and connections
log.info("P2PService shutdown completed"); injector.getInstance(WalletsSetup.class).shutDownComplete.addListener((ov, o, n) -> {
log.info("Graceful shutdown completed. Exiting now.");
module.close(injector); module.close(injector);
completeShutdown(resultHandler, EXIT_SUCCESS, systemExit); completeShutdown(resultHandler, EXIT_SUCCESS, systemExit);
}); });
injector.getInstance(BtcWalletService.class).shutDown();
injector.getInstance(XmrWalletService.class).shutDown();
injector.getInstance(CoreMoneroConnectionsService.class).shutDown();
injector.getInstance(WalletsSetup.class).shutDown();
}); });
walletsSetup.shutDown();
}); });
} catch (Throwable t) { } catch (Throwable t) {
log.error("App shutdown failed with exception {}", t.toString()); log.error("App shutdown failed with exception {}", t.toString());

View File

@ -44,6 +44,7 @@ import haveno.core.support.dispute.Dispute;
import haveno.core.support.dispute.arbitration.ArbitrationManager; import haveno.core.support.dispute.arbitration.ArbitrationManager;
import haveno.core.support.dispute.mediation.MediationManager; import haveno.core.support.dispute.mediation.MediationManager;
import haveno.core.support.dispute.refund.RefundManager; import haveno.core.support.dispute.refund.RefundManager;
import haveno.core.trade.HavenoUtils;
import haveno.core.trade.TradeManager; import haveno.core.trade.TradeManager;
import haveno.core.trade.TradeTxException; import haveno.core.trade.TradeTxException;
import haveno.core.user.Preferences; import haveno.core.user.Preferences;
@ -246,7 +247,7 @@ public class HavenoSetup {
this.refundManager = refundManager; this.refundManager = refundManager;
this.arbitrationManager = arbitrationManager; this.arbitrationManager = arbitrationManager;
xmrWalletService.setHavenoSetup(this); HavenoUtils.havenoSetup = this;
MemPoolSpaceTxBroadcaster.init(socks5ProxyProvider, preferences, localBitcoinNode); MemPoolSpaceTxBroadcaster.init(socks5ProxyProvider, preferences, localBitcoinNode);
} }

View File

@ -81,7 +81,7 @@ public class P2PNetworkSetup {
this.preferences = preferences; this.preferences = preferences;
} }
BooleanProperty init(Runnable initWalletServiceHandler, @Nullable Consumer<Boolean> displayTorNetworkSettingsHandler) { BooleanProperty init(Runnable onReadyHandler, @Nullable Consumer<Boolean> displayTorNetworkSettingsHandler) {
StringProperty bootstrapState = new SimpleStringProperty(); StringProperty bootstrapState = new SimpleStringProperty();
StringProperty bootstrapWarning = new SimpleStringProperty(); StringProperty bootstrapWarning = new SimpleStringProperty();
BooleanProperty hiddenServicePublished = new SimpleBooleanProperty(); BooleanProperty hiddenServicePublished = new SimpleBooleanProperty();
@ -146,8 +146,8 @@ public class P2PNetworkSetup {
priceFeedService.setCurrencyCodeOnInit(); priceFeedService.setCurrencyCodeOnInit();
priceFeedService.requestPrices(); priceFeedService.requestPrices();
// invoke handler to initialize wallet // invoke handler when network ready
initWalletServiceHandler.run(); onReadyHandler.run();
} }
@Override @Override

View File

@ -40,6 +40,8 @@ import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import monero.daemon.model.MoneroDaemonInfo;
import org.bitcoinj.core.RejectMessage; import org.bitcoinj.core.RejectMessage;
import org.bitcoinj.core.VersionMessage; import org.bitcoinj.core.VersionMessage;
import org.bitcoinj.store.BlockStoreException; import org.bitcoinj.store.BlockStoreException;
@ -115,7 +117,8 @@ public class WalletAppSetup {
if (exception == null) { if (exception == null) {
double percentage = (double) downloadPercentage; double percentage = (double) downloadPercentage;
btcSyncProgress.set(percentage); btcSyncProgress.set(percentage);
Long bestChainHeight = connectionService.getDaemon() == null ? null : connectionService.getDaemon().getInfo().getHeight(); MoneroDaemonInfo lastInfo = connectionService.getLastInfo();
Long bestChainHeight = lastInfo == null ? null : lastInfo.getHeight();
String chainHeightAsString = bestChainHeight != null && bestChainHeight > 0 ? String chainHeightAsString = bestChainHeight != null && bestChainHeight > 0 ?
String.valueOf(bestChainHeight) : String.valueOf(bestChainHeight) :
""; "";

View File

@ -26,9 +26,12 @@ import haveno.common.handlers.ResultHandler;
import haveno.common.persistence.PersistenceManager; import haveno.common.persistence.PersistenceManager;
import haveno.common.setup.GracefulShutDownHandler; import haveno.common.setup.GracefulShutDownHandler;
import haveno.common.util.Profiler; import haveno.common.util.Profiler;
import haveno.core.api.CoreMoneroConnectionsService;
import haveno.core.app.HavenoExecutable; import haveno.core.app.HavenoExecutable;
import haveno.core.offer.OfferBookService;
import haveno.core.offer.OpenOfferManager; import haveno.core.offer.OpenOfferManager;
import haveno.core.support.dispute.arbitration.arbitrator.ArbitratorManager; import haveno.core.support.dispute.arbitration.arbitrator.ArbitratorManager;
import haveno.core.trade.HavenoUtils;
import haveno.core.xmr.setup.WalletsSetup; import haveno.core.xmr.setup.WalletsSetup;
import haveno.core.xmr.wallet.BtcWalletService; import haveno.core.xmr.wallet.BtcWalletService;
import haveno.core.xmr.wallet.XmrWalletService; import haveno.core.xmr.wallet.XmrWalletService;
@ -42,7 +45,9 @@ import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -76,25 +81,54 @@ public abstract class ExecutableForAppWithP2p extends HavenoExecutable {
// We don't use the gracefulShutDown implementation of the super class as we have a limited set of modules // We don't use the gracefulShutDown implementation of the super class as we have a limited set of modules
@Override @Override
public void gracefulShutDown(ResultHandler resultHandler) { public void gracefulShutDown(ResultHandler resultHandler) {
log.info("gracefulShutDown"); log.info("Starting graceful shut down of {}", getClass().getSimpleName());
// ignore if shut down in progress
if (isShutdownInProgress) {
log.info("Ignoring call to gracefulShutDown, already in progress");
return;
}
isShutdownInProgress = true;
try { try {
if (injector != null) { if (injector != null) {
// notify trade protocols and wallets to prepare for shut down before shutting down
Set<Runnable> tasks = new HashSet<Runnable>();
tasks.add(() -> injector.getInstance(XmrWalletService.class).onShutDownStarted());
tasks.add(() -> injector.getInstance(CoreMoneroConnectionsService.class).onShutDownStarted());
HavenoUtils.executeTasks(tasks); // notify in parallel
JsonFileManager.shutDownAllInstances(); JsonFileManager.shutDownAllInstances();
injector.getInstance(ArbitratorManager.class).shutDown(); injector.getInstance(ArbitratorManager.class).shutDown();
injector.getInstance(XmrWalletService.class).shutDown(true);
injector.getInstance(OpenOfferManager.class).shutDown(() -> injector.getInstance(P2PService.class).shutDown(() -> {
injector.getInstance(WalletsSetup.class).shutDownComplete.addListener((ov, o, n) -> {
module.close(injector);
PersistenceManager.flushAllDataToDiskAtShutdown(() -> { // shut down open offer manager
resultHandler.handleResult(); log.info("Shutting down OpenOfferManager, OfferBookService, and P2PService");
log.info("Graceful shutdown completed. Exiting now."); injector.getInstance(OpenOfferManager.class).shutDown(() -> {
UserThread.runAfter(() -> System.exit(HavenoExecutable.EXIT_SUCCESS), 1);
// shut down offer book service
injector.getInstance(OfferBookService.class).shutDown();
// shut down p2p service
injector.getInstance(P2PService.class).shutDown(() -> {
log.info("Done shutting down OpenOfferManager, OfferBookService, and P2PService");
// shut down monero wallets and connections
injector.getInstance(WalletsSetup.class).shutDownComplete.addListener((ov, o, n) -> {
module.close(injector);
PersistenceManager.flushAllDataToDiskAtShutdown(() -> {
resultHandler.handleResult();
log.info("Graceful shutdown completed. Exiting now.");
UserThread.runAfter(() -> System.exit(HavenoExecutable.EXIT_SUCCESS), 1);
});
}); });
injector.getInstance(BtcWalletService.class).shutDown();
injector.getInstance(XmrWalletService.class).shutDown();
injector.getInstance(CoreMoneroConnectionsService.class).shutDown();
injector.getInstance(WalletsSetup.class).shutDown();
}); });
injector.getInstance(WalletsSetup.class).shutDown(); });
injector.getInstance(BtcWalletService.class).shutDown();
}));
// we wait max 5 sec. // we wait max 5 sec.
UserThread.runAfter(() -> { UserThread.runAfter(() -> {
PersistenceManager.flushAllDataToDiskAtShutdown(() -> { PersistenceManager.flushAllDataToDiskAtShutdown(() -> {

View File

@ -97,7 +97,7 @@ public class OfferBookService {
connectionsService.addListener(new MoneroConnectionManagerListener() { connectionsService.addListener(new MoneroConnectionManagerListener() {
@Override @Override
public void onConnectionChanged(MoneroRpcConnection connection) { public void onConnectionChanged(MoneroRpcConnection connection) {
if (keyImagePoller == null) return; maybeInitializeKeyImagePoller();
keyImagePoller.setDaemon(connectionsService.getDaemon()); keyImagePoller.setDaemon(connectionsService.getDaemon());
keyImagePoller.setRefreshPeriodMs(getKeyImageRefreshPeriodMs()); keyImagePoller.setRefreshPeriodMs(getKeyImageRefreshPeriodMs());
} }
@ -111,8 +111,8 @@ public class OfferBookService {
synchronized (offerBookChangedListeners) { synchronized (offerBookChangedListeners) {
offerBookChangedListeners.forEach(listener -> { offerBookChangedListeners.forEach(listener -> {
if (protectedStorageEntry.getProtectedStoragePayload() instanceof OfferPayload) { if (protectedStorageEntry.getProtectedStoragePayload() instanceof OfferPayload) {
maybeInitializeKeyImagePoller();
OfferPayload offerPayload = (OfferPayload) protectedStorageEntry.getProtectedStoragePayload(); OfferPayload offerPayload = (OfferPayload) protectedStorageEntry.getProtectedStoragePayload();
maybeInitializeKeyImagePoller();
keyImagePoller.addKeyImages(offerPayload.getReserveTxKeyImages()); keyImagePoller.addKeyImages(offerPayload.getReserveTxKeyImages());
Offer offer = new Offer(offerPayload); Offer offer = new Offer(offerPayload);
offer.setPriceFeedService(priceFeedService); offer.setPriceFeedService(priceFeedService);
@ -130,8 +130,8 @@ public class OfferBookService {
synchronized (offerBookChangedListeners) { synchronized (offerBookChangedListeners) {
offerBookChangedListeners.forEach(listener -> { offerBookChangedListeners.forEach(listener -> {
if (protectedStorageEntry.getProtectedStoragePayload() instanceof OfferPayload) { if (protectedStorageEntry.getProtectedStoragePayload() instanceof OfferPayload) {
maybeInitializeKeyImagePoller();
OfferPayload offerPayload = (OfferPayload) protectedStorageEntry.getProtectedStoragePayload(); OfferPayload offerPayload = (OfferPayload) protectedStorageEntry.getProtectedStoragePayload();
maybeInitializeKeyImagePoller();
keyImagePoller.removeKeyImages(offerPayload.getReserveTxKeyImages()); keyImagePoller.removeKeyImages(offerPayload.getReserveTxKeyImages());
Offer offer = new Offer(offerPayload); Offer offer = new Offer(offerPayload);
offer.setPriceFeedService(priceFeedService); offer.setPriceFeedService(priceFeedService);
@ -257,6 +257,10 @@ public class OfferBookService {
} }
} }
public void shutDown() {
if (keyImagePoller != null) keyImagePoller.clearKeyImages();
}
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Private // Private
@ -276,11 +280,12 @@ public class OfferBookService {
} }
}); });
// first poll after 5s // first poll after 20s
// TODO: remove?
new Thread(() -> { new Thread(() -> {
GenUtils.waitFor(5000); GenUtils.waitFor(20000);
keyImagePoller.poll(); keyImagePoller.poll();
}); }).start();
} }
private long getKeyImageRefreshPeriodMs() { private long getKeyImageRefreshPeriodMs() {

View File

@ -202,8 +202,6 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
@Override @Override
public void onConnectionChanged(MoneroRpcConnection connection) { public void onConnectionChanged(MoneroRpcConnection connection) {
maybeInitializeKeyImagePoller(); maybeInitializeKeyImagePoller();
signedOfferKeyImagePoller.setDaemon(connectionsService.getDaemon());
signedOfferKeyImagePoller.setRefreshPeriodMs(getKeyImageRefreshPeriodMs());
} }
}); });
@ -258,10 +256,11 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
}); });
// first poll in 5s // first poll in 5s
// TODO: remove?
new Thread(() -> { new Thread(() -> {
GenUtils.waitFor(5000); GenUtils.waitFor(5000);
signedOfferKeyImagePoller.poll(); signedOfferKeyImagePoller.poll();
}); }).start();
} }
private long getKeyImageRefreshPeriodMs() { private long getKeyImageRefreshPeriodMs() {
@ -334,6 +333,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
stopped = true; stopped = true;
p2PService.getPeerManager().removeListener(this); p2PService.getPeerManager().removeListener(this);
p2PService.removeDecryptedDirectMessageListener(this); p2PService.removeDecryptedDirectMessageListener(this);
signedOfferKeyImagePoller.clearKeyImages();
stopPeriodicRefreshOffersTimer(); stopPeriodicRefreshOffersTimer();
stopPeriodicRepublishOffersTimer(); stopPeriodicRepublishOffersTimer();

View File

@ -53,12 +53,12 @@ public class MakerReserveOfferFunds extends Task<PlaceOfferModel> {
BigInteger makerFee = offer.getMakerFee(); BigInteger makerFee = offer.getMakerFee();
BigInteger sendAmount = offer.getDirection() == OfferDirection.BUY ? BigInteger.valueOf(0) : offer.getAmount(); BigInteger sendAmount = offer.getDirection() == OfferDirection.BUY ? BigInteger.valueOf(0) : offer.getAmount();
BigInteger securityDeposit = offer.getDirection() == OfferDirection.BUY ? offer.getBuyerSecurityDeposit() : offer.getSellerSecurityDeposit(); BigInteger securityDeposit = offer.getDirection() == OfferDirection.BUY ? offer.getBuyerSecurityDeposit() : offer.getSellerSecurityDeposit();
String returnAddress = model.getXmrWalletService().getNewAddressEntry(offer.getId(), XmrAddressEntry.Context.TRADE_PAYOUT).getAddressString(); String returnAddress = model.getXmrWalletService().getNewAddressEntry(offer.getId(), XmrAddressEntry.Context.RESERVED_FOR_TRADE).getAddressString();
MoneroTxWallet reserveTx = model.getXmrWalletService().createReserveTx(makerFee, sendAmount, securityDeposit, returnAddress); MoneroTxWallet reserveTx = model.getXmrWalletService().createReserveTx(makerFee, sendAmount, securityDeposit, returnAddress);
// check for error in case creating reserve tx exceeded timeout // check for error in case creating reserve tx exceeded timeout
// TODO: better way? // TODO: better way?
if (!model.getXmrWalletService().getAddressEntry(offer.getId(), XmrAddressEntry.Context.TRADE_PAYOUT).isPresent()) { if (!model.getXmrWalletService().getAddressEntry(offer.getId(), XmrAddressEntry.Context.RESERVED_FOR_TRADE).isPresent()) {
throw new RuntimeException("An error has occurred posting offer " + offer.getId() + " causing its subaddress entry to be deleted"); throw new RuntimeException("An error has occurred posting offer " + offer.getId() + " causing its subaddress entry to be deleted");
} }

View File

@ -60,7 +60,7 @@ public class MakerSendSignOfferRequest extends Task<PlaceOfferModel> {
runInterceptHook(); runInterceptHook();
// create request for arbitrator to sign offer // create request for arbitrator to sign offer
String returnAddress = model.getXmrWalletService().getAddressEntry(offer.getId(), XmrAddressEntry.Context.TRADE_PAYOUT).get().getAddressString(); String returnAddress = model.getXmrWalletService().getAddressEntry(offer.getId(), XmrAddressEntry.Context.RESERVED_FOR_TRADE).get().getAddressString();
SignOfferRequest request = new SignOfferRequest( SignOfferRequest request = new SignOfferRequest(
model.getOffer().getId(), model.getOffer().getId(),
P2PService.getMyNodeAddress(), P2PService.getMyNodeAddress(),

View File

@ -465,13 +465,21 @@ public abstract class DisputeManager<T extends DisputeList<Dispute>> extends Sup
DisputeValidation.validateDisputeData(dispute); DisputeValidation.validateDisputeData(dispute);
DisputeValidation.validateNodeAddresses(dispute, config); DisputeValidation.validateNodeAddresses(dispute, config);
DisputeValidation.validateSenderNodeAddress(dispute, message.getSenderNodeAddress()); DisputeValidation.validateSenderNodeAddress(dispute, message.getSenderNodeAddress());
DisputeValidation.validatePaymentAccountPayload(dispute);
//DisputeValidation.testIfDisputeTriesReplay(dispute, disputeList.getList()); //DisputeValidation.testIfDisputeTriesReplay(dispute, disputeList.getList());
} catch (DisputeValidation.ValidationException e) { } catch (DisputeValidation.ValidationException e) {
validationExceptions.add(e); validationExceptions.add(e);
throw e; throw e;
} }
// try to validate payment account
// TODO: add field to dispute details: valid, invalid, missing
try {
DisputeValidation.validatePaymentAccountPayload(dispute);
} catch (Exception e) {
log.warn(e.getMessage());
trade.prependErrorMessage(e.getMessage());
}
// get sender // get sender
senderPubKeyRing = trade.isArbitrator() ? (dispute.isDisputeOpenerIsBuyer() ? contract.getBuyerPubKeyRing() : contract.getSellerPubKeyRing()) : trade.getArbitrator().getPubKeyRing(); senderPubKeyRing = trade.isArbitrator() ? (dispute.isDisputeOpenerIsBuyer() ? contract.getBuyerPubKeyRing() : contract.getSellerPubKeyRing()) : trade.getArbitrator().getPubKeyRing();
TradePeer sender = trade.getTradePeer(senderPubKeyRing); TradePeer sender = trade.getTradePeer(senderPubKeyRing);

View File

@ -37,10 +37,8 @@ import java.util.Optional;
@Slf4j @Slf4j
@Getter @Getter
public final class Arbitrator extends DisputeAgent { public final class Arbitrator extends DisputeAgent {
private final String xmrAddress;
public Arbitrator(NodeAddress nodeAddress, public Arbitrator(NodeAddress nodeAddress,
String xmrAddress,
PubKeyRing pubKeyRing, PubKeyRing pubKeyRing,
List<String> languageCodes, List<String> languageCodes,
long registrationDate, long registrationDate,
@ -59,8 +57,6 @@ public final class Arbitrator extends DisputeAgent {
emailAddress, emailAddress,
info, info,
extraDataMap); extraDataMap);
this.xmrAddress = xmrAddress;
} }
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -71,7 +67,6 @@ public final class Arbitrator extends DisputeAgent {
public protobuf.StoragePayload toProtoMessage() { public protobuf.StoragePayload toProtoMessage() {
protobuf.Arbitrator.Builder builder = protobuf.Arbitrator.newBuilder() protobuf.Arbitrator.Builder builder = protobuf.Arbitrator.newBuilder()
.setNodeAddress(nodeAddress.toProtoMessage()) .setNodeAddress(nodeAddress.toProtoMessage())
.setXmrAddress(xmrAddress)
.setPubKeyRing(pubKeyRing.toProtoMessage()) .setPubKeyRing(pubKeyRing.toProtoMessage())
.addAllLanguageCodes(languageCodes) .addAllLanguageCodes(languageCodes)
.setRegistrationDate(registrationDate) .setRegistrationDate(registrationDate)
@ -85,7 +80,6 @@ public final class Arbitrator extends DisputeAgent {
public static Arbitrator fromProto(protobuf.Arbitrator proto) { public static Arbitrator fromProto(protobuf.Arbitrator proto) {
return new Arbitrator(NodeAddress.fromProto(proto.getNodeAddress()), return new Arbitrator(NodeAddress.fromProto(proto.getNodeAddress()),
proto.getXmrAddress(),
PubKeyRing.fromProto(proto.getPubKeyRing()), PubKeyRing.fromProto(proto.getPubKeyRing()),
new ArrayList<>(proto.getLanguageCodesList()), new ArrayList<>(proto.getLanguageCodesList()),
proto.getRegistrationDate(), proto.getRegistrationDate(),
@ -103,8 +97,6 @@ public final class Arbitrator extends DisputeAgent {
@Override @Override
public String toString() { public String toString() {
return "Arbitrator{" + return "Arbitrator{} " + super.toString();
",\n xmrAddress='" + xmrAddress + '\'' +
"\n} " + super.toString();
} }
} }

View File

@ -25,6 +25,7 @@ import haveno.common.crypto.KeyRing;
import haveno.common.crypto.PubKeyRing; import haveno.common.crypto.PubKeyRing;
import haveno.common.crypto.Sig; import haveno.common.crypto.Sig;
import haveno.common.util.Utilities; import haveno.common.util.Utilities;
import haveno.core.app.HavenoSetup;
import haveno.core.offer.Offer; import haveno.core.offer.Offer;
import haveno.core.offer.OfferPayload; import haveno.core.offer.OfferPayload;
import haveno.core.support.dispute.arbitration.ArbitrationManager; import haveno.core.support.dispute.arbitration.ArbitrationManager;
@ -35,6 +36,8 @@ import haveno.core.trade.messages.PaymentSentMessage;
import haveno.core.util.JsonUtil; import haveno.core.util.JsonUtil;
import haveno.network.p2p.NodeAddress; import haveno.network.p2p.NodeAddress;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import monero.common.MoneroRpcConnection;
import org.bitcoinj.core.Coin; import org.bitcoinj.core.Coin;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -69,8 +72,9 @@ public class HavenoUtils {
private static final int POOL_SIZE = 10; private static final int POOL_SIZE = 10;
private static final ExecutorService POOL = Executors.newFixedThreadPool(POOL_SIZE); private static final ExecutorService POOL = Executors.newFixedThreadPool(POOL_SIZE);
public static ArbitrationManager arbitrationManager; // TODO: better way to share reference? // TODO: better way to share refernces?
public static ArbitrationManager arbitrationManager;
public static HavenoSetup havenoSetup;
// ----------------------- CONVERSION UTILS ------------------------------- // ----------------------- CONVERSION UTILS -------------------------------
@ -502,4 +506,10 @@ public class HavenoUtils {
public static String toCamelCase(String underscore) { public static String toCamelCase(String underscore) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, underscore); return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, underscore);
} }
public static boolean connectionConfigsEqual(MoneroRpcConnection c1, MoneroRpcConnection c2) {
if (c1 == c2) return true;
if (c1 == null) return false;
return c1.equals(c2); // equality considers uri, username, and password
}
} }

View File

@ -20,6 +20,7 @@ package haveno.core.trade;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import com.google.protobuf.Message; import com.google.protobuf.Message;
import haveno.common.UserThread; import haveno.common.UserThread;
import haveno.common.crypto.Encryption; import haveno.common.crypto.Encryption;
import haveno.common.crypto.PubKeyRing; import haveno.common.crypto.PubKeyRing;
@ -72,6 +73,7 @@ import monero.common.TaskLooper;
import monero.daemon.MoneroDaemon; import monero.daemon.MoneroDaemon;
import monero.daemon.model.MoneroTx; import monero.daemon.model.MoneroTx;
import monero.wallet.MoneroWallet; import monero.wallet.MoneroWallet;
import monero.wallet.MoneroWalletRpc;
import monero.wallet.model.MoneroDestination; import monero.wallet.model.MoneroDestination;
import monero.wallet.model.MoneroMultisigSignResult; import monero.wallet.model.MoneroMultisigSignResult;
import monero.wallet.model.MoneroOutputWallet; import monero.wallet.model.MoneroOutputWallet;
@ -80,6 +82,8 @@ import monero.wallet.model.MoneroTxQuery;
import monero.wallet.model.MoneroTxSet; import monero.wallet.model.MoneroTxSet;
import monero.wallet.model.MoneroTxWallet; import monero.wallet.model.MoneroTxWallet;
import monero.wallet.model.MoneroWalletListener; import monero.wallet.model.MoneroWalletListener;
import org.apache.commons.lang3.StringUtils;
import org.bitcoinj.core.Coin; import org.bitcoinj.core.Coin;
import org.fxmisc.easybind.EasyBind; import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.Subscription; import org.fxmisc.easybind.Subscription;
@ -94,7 +98,6 @@ import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -385,6 +388,8 @@ public abstract class Trade implements Tradable, Model {
@Getter @Getter
transient private boolean isInitialized; transient private boolean isInitialized;
@Getter @Getter
transient private boolean isShutDownStarted;
@Getter
transient private boolean isShutDown; transient private boolean isShutDown;
// Added in v1.2.0 // Added in v1.2.0
@ -572,12 +577,15 @@ public abstract class Trade implements Tradable, Model {
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
public void initialize(ProcessModelServiceProvider serviceProvider) { public void initialize(ProcessModelServiceProvider serviceProvider) {
if (isInitialized) throw new IllegalStateException(getClass().getSimpleName() + " " + getId() + " is already initialized");
// set arbitrator pub key ring once known
serviceProvider.getArbitratorManager().getDisputeAgentByNodeAddress(getArbitratorNodeAddress()).ifPresent(arbitrator -> { serviceProvider.getArbitratorManager().getDisputeAgentByNodeAddress(getArbitratorNodeAddress()).ifPresent(arbitrator -> {
getArbitrator().setPubKeyRing(arbitrator.getPubKeyRing()); getArbitrator().setPubKeyRing(arbitrator.getPubKeyRing());
}); });
// listen to daemon connection // listen to daemon connection
xmrWalletService.getConnectionsService().addListener(newConnection -> setDaemonConnection(newConnection)); xmrWalletService.getConnectionsService().addListener(newConnection -> onConnectionChanged(newConnection));
// check if done // check if done
if (isPayoutUnlocked()) { if (isPayoutUnlocked()) {
@ -585,19 +593,19 @@ public abstract class Trade implements Tradable, Model {
return; return;
} }
// reset payment sent state if no ack receive // reset buyer's payment sent state if no ack receive
if (getState().ordinal() >= Trade.State.BUYER_CONFIRMED_IN_UI_PAYMENT_SENT.ordinal() && getState().ordinal() < Trade.State.BUYER_STORED_IN_MAILBOX_PAYMENT_SENT_MSG.ordinal()) { if (this instanceof BuyerTrade && getState().ordinal() >= Trade.State.BUYER_CONFIRMED_IN_UI_PAYMENT_SENT.ordinal() && getState().ordinal() < Trade.State.BUYER_STORED_IN_MAILBOX_PAYMENT_SENT_MSG.ordinal()) {
log.warn("Resetting state of {} {} from {} to {} because no ack was received", getClass().getSimpleName(), getId(), getState(), Trade.State.DEPOSIT_TXS_UNLOCKED_IN_BLOCKCHAIN); log.warn("Resetting state of {} {} from {} to {} because no ack was received", getClass().getSimpleName(), getId(), getState(), Trade.State.DEPOSIT_TXS_UNLOCKED_IN_BLOCKCHAIN);
setState(Trade.State.DEPOSIT_TXS_UNLOCKED_IN_BLOCKCHAIN); setState(Trade.State.DEPOSIT_TXS_UNLOCKED_IN_BLOCKCHAIN);
} }
// reset payment received state if no ack receive // reset seller's payment received state if no ack receive
if (getState().ordinal() >= Trade.State.SELLER_CONFIRMED_IN_UI_PAYMENT_RECEIPT.ordinal() && getState().ordinal() < Trade.State.SELLER_STORED_IN_MAILBOX_PAYMENT_RECEIVED_MSG.ordinal()) { if (this instanceof SellerTrade && getState().ordinal() >= Trade.State.SELLER_CONFIRMED_IN_UI_PAYMENT_RECEIPT.ordinal() && getState().ordinal() < Trade.State.SELLER_STORED_IN_MAILBOX_PAYMENT_RECEIVED_MSG.ordinal()) {
log.warn("Resetting state of {} {} from {} to {} because no ack was received", getClass().getSimpleName(), getId(), getState(), Trade.State.SELLER_RECEIVED_PAYMENT_SENT_MSG); log.warn("Resetting state of {} {} from {} to {} because no ack was received", getClass().getSimpleName(), getId(), getState(), Trade.State.SELLER_RECEIVED_PAYMENT_SENT_MSG);
setState(Trade.State.SELLER_RECEIVED_PAYMENT_SENT_MSG); setState(Trade.State.SELLER_RECEIVED_PAYMENT_SENT_MSG);
} }
// handle trade state events // handle trade phase events
tradePhaseSubscription = EasyBind.subscribe(phaseProperty, newValue -> { tradePhaseSubscription = EasyBind.subscribe(phaseProperty, newValue -> {
if (isDepositsPublished() && !isPayoutUnlocked()) updateWalletRefreshPeriod(); if (isDepositsPublished() && !isPayoutUnlocked()) updateWalletRefreshPeriod();
if (isCompleted()) { if (isCompleted()) {
@ -652,18 +660,28 @@ public abstract class Trade implements Tradable, Model {
xmrWalletService.addWalletListener(idlePayoutSyncer); xmrWalletService.addWalletListener(idlePayoutSyncer);
} }
if (isDepositRequested()) { // send deposit confirmed message on startup or event
if (isDepositsConfirmed()) {
// start syncing and polling trade wallet new Thread(() -> getProtocol().maybeSendDepositsConfirmedMessages()).start();
updateSyncing(); } else {
EasyBind.subscribe(stateProperty(), state -> {
// allow state notifications to process before returning if (isDepositsConfirmed()) {
CountDownLatch latch = new CountDownLatch(1); new Thread(() -> getProtocol().maybeSendDepositsConfirmedMessages()).start();
UserThread.execute(() -> latch.countDown()); }
HavenoUtils.awaitLatch(latch); });
} }
// reprocess pending payout messages
this.getProtocol().maybeReprocessPaymentReceivedMessage(false);
HavenoUtils.arbitrationManager.maybeReprocessDisputeClosedMessage(this, false);
// trade is initialized but not synced
isInitialized = true; isInitialized = true;
// sync wallet if applicable
if (!isDepositRequested() || isPayoutUnlocked()) return;
if (xmrWalletService.getConnectionsService().getConnection() == null || Boolean.FALSE.equals(xmrWalletService.getConnectionsService().isConnected())) return;
updateSyncing();
} }
public void requestPersistence() { public void requestPersistence() {
@ -710,8 +728,8 @@ public abstract class Trade implements Tradable, Model {
synchronized (walletLock) { synchronized (walletLock) {
if (wallet != null) return wallet; if (wallet != null) return wallet;
if (!walletExists()) return null; if (!walletExists()) return null;
if (isShutDown) throw new RuntimeException("Cannot open wallet for " + getClass().getSimpleName() + " " + getId() + " because trade is shut down"); if (isShutDownStarted) throw new RuntimeException("Cannot open wallet for " + getClass().getSimpleName() + " " + getId() + " because shut down is started");
if (!isShutDown) wallet = xmrWalletService.openWallet(getWalletName()); else wallet = xmrWalletService.openWallet(getWalletName());
return wallet; return wallet;
} }
} }
@ -744,7 +762,7 @@ public abstract class Trade implements Tradable, Model {
if (getWallet() == null) throw new RuntimeException("Cannot sync trade wallet because it doesn't exist for " + getClass().getSimpleName() + ", " + getId()); if (getWallet() == null) throw new RuntimeException("Cannot sync trade wallet because it doesn't exist for " + getClass().getSimpleName() + ", " + getId());
if (getWallet().getDaemonConnection() == null) throw new RuntimeException("Cannot sync trade wallet because it's not connected to a Monero daemon for " + getClass().getSimpleName() + ", " + getId()); if (getWallet().getDaemonConnection() == null) throw new RuntimeException("Cannot sync trade wallet because it's not connected to a Monero daemon for " + getClass().getSimpleName() + ", " + getId());
log.info("Syncing wallet for {} {}", getClass().getSimpleName(), getId()); log.info("Syncing wallet for {} {}", getClass().getSimpleName(), getId());
getWallet().sync(); xmrWalletService.syncWallet(getWallet());
pollWallet(); pollWallet();
log.info("Done syncing wallet for {} {}", getClass().getSimpleName(), getId()); log.info("Done syncing wallet for {} {}", getClass().getSimpleName(), getId());
} }
@ -753,7 +771,7 @@ public abstract class Trade implements Tradable, Model {
try { try {
syncWallet(); syncWallet();
} catch (Exception e) { } catch (Exception e) {
if (!isShutDown) { if (!isShutDown && walletExists()) {
log.warn("Error syncing trade wallet for {} {}: {}", getClass().getSimpleName(), getId(), e.getMessage()); log.warn("Error syncing trade wallet for {} {}: {}", getClass().getSimpleName(), getId(), e.getMessage());
} }
} }
@ -761,7 +779,7 @@ public abstract class Trade implements Tradable, Model {
public void syncWalletNormallyForMs(long syncNormalDuration) { public void syncWalletNormallyForMs(long syncNormalDuration) {
syncNormalStartTime = System.currentTimeMillis(); syncNormalStartTime = System.currentTimeMillis();
setWalletRefreshPeriod(xmrWalletService.getConnectionsService().getDefaultRefreshPeriodMs()); setWalletRefreshPeriod(xmrWalletService.getConnectionsService().getRefreshPeriodMs());
UserThread.runAfter(() -> { UserThread.runAfter(() -> {
if (!isShutDown && System.currentTimeMillis() >= syncNormalStartTime + syncNormalDuration) updateWalletRefreshPeriod(); if (!isShutDown && System.currentTimeMillis() >= syncNormalStartTime + syncNormalDuration) updateWalletRefreshPeriod();
}, syncNormalDuration); }, syncNormalDuration);
@ -772,7 +790,11 @@ public abstract class Trade implements Tradable, Model {
if (getBuyer().getUpdatedMultisigHex() != null) multisigHexes.add(getBuyer().getUpdatedMultisigHex()); if (getBuyer().getUpdatedMultisigHex() != null) multisigHexes.add(getBuyer().getUpdatedMultisigHex());
if (getSeller().getUpdatedMultisigHex() != null) multisigHexes.add(getSeller().getUpdatedMultisigHex()); if (getSeller().getUpdatedMultisigHex() != null) multisigHexes.add(getSeller().getUpdatedMultisigHex());
if (getArbitrator().getUpdatedMultisigHex() != null) multisigHexes.add(getArbitrator().getUpdatedMultisigHex()); if (getArbitrator().getUpdatedMultisigHex() != null) multisigHexes.add(getArbitrator().getUpdatedMultisigHex());
if (!multisigHexes.isEmpty()) getWallet().importMultisigHex(multisigHexes.toArray(new String[0])); if (!multisigHexes.isEmpty()) {
log.info("Importing multisig hex for {} {}", getClass().getSimpleName(), getId());
getWallet().importMultisigHex(multisigHexes.toArray(new String[0]));
log.info("Done importing multisig hex for {} {}", getClass().getSimpleName(), getId());
}
} }
public void changeWalletPassword(String oldPassword, String newPassword) { public void changeWalletPassword(String oldPassword, String newPassword) {
@ -791,13 +813,22 @@ public abstract class Trade implements Tradable, Model {
private void closeWallet() { private void closeWallet() {
synchronized (walletLock) { synchronized (walletLock) {
if (wallet == null) throw new RuntimeException("Trade wallet to close was not previously opened for trade " + getId()); if (wallet == null) throw new RuntimeException("Trade wallet to close is not open for trade " + getId());
stopPolling(); stopPolling();
xmrWalletService.closeWallet(wallet, true); xmrWalletService.closeWallet(wallet, true);
wallet = null; wallet = null;
} }
} }
private void stopWallet() {
synchronized (walletLock) {
if (wallet == null) throw new RuntimeException("Trade wallet to close is not open for trade " + getId());
stopPolling();
xmrWalletService.stopWallet(wallet, wallet.getPath(), true);
wallet = null;
}
}
public void deleteWallet() { public void deleteWallet() {
synchronized (walletLock) { synchronized (walletLock) {
if (walletExists()) { if (walletExists()) {
@ -808,14 +839,10 @@ public abstract class Trade implements Tradable, Model {
throw new RuntimeException("Refusing to delete wallet for " + getClass().getSimpleName() + " " + getId() + " because the deposit txs have been published but payout tx has not unlocked"); throw new RuntimeException("Refusing to delete wallet for " + getClass().getSimpleName() + " " + getId() + " because the deposit txs have been published but payout tx has not unlocked");
} }
// check if wallet balance > dust // force stop the wallet
BigInteger maxBalance = isDepositsPublished() ? getMakerDepositTx().getFee().min(getTakerDepositTx().getFee()) : BigInteger.ZERO; if (wallet != null) stopWallet();
if (getWallet().getBalance().compareTo(maxBalance) > 0) {
throw new RuntimeException("Refusing to delete wallet for " + getClass().getSimpleName() + " " + getId() + " because its balance is more than dust");
}
// close and delete trade wallet // delete wallet
if (wallet != null) closeWallet();
log.info("Deleting wallet for {} {}", getClass().getSimpleName(), getId()); log.info("Deleting wallet for {} {}", getClass().getSimpleName(), getId());
xmrWalletService.deleteWallet(getWalletName()); xmrWalletService.deleteWallet(getWalletName());
@ -882,8 +909,7 @@ public abstract class Trade implements Tradable, Model {
// check connection to monero daemon // check connection to monero daemon
checkWalletConnection(); checkWalletConnection();
// import multisig hex // check multisig import
importMultisigHex();
if (getWallet().isMultisigImportNeeded()) throw new RuntimeException("Cannot create payout tx because multisig import is needed"); if (getWallet().isMultisigImportNeeded()) throw new RuntimeException("Cannot create payout tx because multisig import is needed");
// gather info // gather info
@ -979,8 +1005,8 @@ public abstract class Trade implements Tradable, Model {
BigInteger expectedSellerPayout = sellerDepositAmount.subtract(tradeAmount).subtract(txCost.divide(BigInteger.valueOf(2))); BigInteger expectedSellerPayout = sellerDepositAmount.subtract(tradeAmount).subtract(txCost.divide(BigInteger.valueOf(2)));
if (!sellerPayoutDestination.getAmount().equals(expectedSellerPayout)) throw new IllegalArgumentException("Seller destination amount is not deposit amount - trade amount - 1/2 tx costs, " + sellerPayoutDestination.getAmount() + " vs " + expectedSellerPayout); if (!sellerPayoutDestination.getAmount().equals(expectedSellerPayout)) throw new IllegalArgumentException("Seller destination amount is not deposit amount - trade amount - 1/2 tx costs, " + sellerPayoutDestination.getAmount() + " vs " + expectedSellerPayout);
// check wallet's daemon connection // check wallet connection
checkWalletConnection(); if (sign || publish) checkWalletConnection();
// handle tx signing // handle tx signing
if (sign) { if (sign) {
@ -1005,18 +1031,11 @@ public abstract class Trade implements Tradable, Model {
// verify fee is within tolerance by recreating payout tx // verify fee is within tolerance by recreating payout tx
// TODO (monero-project): creating tx will require exchanging updated multisig hex if message needs reprocessed. provide weight with describe_transfer so fee can be estimated? // TODO (monero-project): creating tx will require exchanging updated multisig hex if message needs reprocessed. provide weight with describe_transfer so fee can be estimated?
MoneroTxWallet feeEstimateTx = null; MoneroTxWallet feeEstimateTx = createPayoutTx();;
try { BigInteger feeEstimate = feeEstimateTx.getFee();
feeEstimateTx = createPayoutTx(); double feeDiff = payoutTx.getFee().subtract(feeEstimate).abs().doubleValue() / feeEstimate.doubleValue(); // TODO: use BigDecimal?
} catch (Exception e) { if (feeDiff > XmrWalletService.MINER_FEE_TOLERANCE) throw new IllegalArgumentException("Miner fee is not within " + (XmrWalletService.MINER_FEE_TOLERANCE * 100) + "% of estimated fee, expected " + feeEstimate + " but was " + payoutTx.getFee());
log.warn("Could not recreate payout tx to verify fee: " + e.getMessage()); log.info("Payout tx fee {} is within tolerance, diff %={}", payoutTx.getFee(), feeDiff);
}
if (feeEstimateTx != null) {
BigInteger feeEstimate = feeEstimateTx.getFee();
double feeDiff = payoutTx.getFee().subtract(feeEstimate).abs().doubleValue() / feeEstimate.doubleValue(); // TODO: use BigDecimal?
if (feeDiff > XmrWalletService.MINER_FEE_TOLERANCE) throw new IllegalArgumentException("Miner fee is not within " + (XmrWalletService.MINER_FEE_TOLERANCE * 100) + "% of estimated fee, expected " + feeEstimate + " but was " + payoutTx.getFee());
log.info("Payout tx fee {} is within tolerance, diff %={}", payoutTx.getFee(), feeDiff);
}
} }
// update trade state // update trade state
@ -1072,7 +1091,7 @@ public abstract class Trade implements Tradable, Model {
if (depositId == null) return null; if (depositId == null) return null;
try { try {
if (trader.getDepositTx() == null || !trader.getDepositTx().isConfirmed()) { if (trader.getDepositTx() == null || !trader.getDepositTx().isConfirmed()) {
trader.setDepositTx(getTxFromWalletOrDaemon(depositId)); trader.setDepositTx(getDepositTxFromWalletOrDaemon(depositId));
} }
return trader.getDepositTx(); return trader.getDepositTx();
} catch (MoneroError e) { } catch (MoneroError e) {
@ -1081,12 +1100,18 @@ public abstract class Trade implements Tradable, Model {
} }
} }
private MoneroTx getTxFromWalletOrDaemon(String txId) { private MoneroTx getDepositTxFromWalletOrDaemon(String txId) {
MoneroTx tx = null; MoneroTx tx = null;
// first check wallet
if (getWallet() != null) { if (getWallet() != null) {
try { tx = getWallet().getTx(txId); } // TODO monero-java: return null if tx not found List<MoneroTxWallet> filteredTxs = getWallet().getTxs(new MoneroTxQuery()
catch (Exception e) { } .setHash(txId)
.setIsConfirmed(isDepositsConfirmed() ? true : null)); // avoid checking pool if confirmed
if (filteredTxs.size() == 1) tx = filteredTxs.get(0);
} }
// then check daemon
if (tx == null) tx = getXmrWalletService().getTxWithCache(txId); if (tx == null) tx = getXmrWalletService().getTxWithCache(txId);
return tx; return tx;
} }
@ -1126,19 +1151,27 @@ public abstract class Trade implements Tradable, Model {
} }
} }
public void shutDown() { public void onShutDownStarted() {
isShutDownStarted = true;
if (wallet != null) log.info("{} {} onShutDownStarted()", getClass().getSimpleName(), getId());
synchronized (this) {
synchronized (walletLock) {
stopPolling(); // allow locks to release before stopping
}
}
}
public void shutDown() {
if (wallet != null) log.info("{} {} onShutDown()", getClass().getSimpleName(), getId());
synchronized (this) { synchronized (this) {
log.info("Shutting down {} {}", getClass().getSimpleName(), getId());
isInitialized = false; isInitialized = false;
isShutDown = true; isShutDown = true;
if (wallet != null) closeWallet(); synchronized (walletLock) {
if (wallet != null) closeWallet();
}
if (tradePhaseSubscription != null) tradePhaseSubscription.unsubscribe(); if (tradePhaseSubscription != null) tradePhaseSubscription.unsubscribe();
if (payoutStateSubscription != null) payoutStateSubscription.unsubscribe(); if (payoutStateSubscription != null) payoutStateSubscription.unsubscribe();
if (idlePayoutSyncer != null) { idlePayoutSyncer = null; // main wallet removes listener itself
xmrWalletService.removeWalletListener(idlePayoutSyncer);
idlePayoutSyncer = null;
}
log.info("Done shutting down {} {}", getClass().getSimpleName(), getId());
} }
} }
@ -1280,6 +1313,18 @@ public abstract class Trade implements Tradable, Model {
errorMessageProperty.set(errorMessage); errorMessageProperty.set(errorMessage);
} }
public void prependErrorMessage(String errorMessage) {
StringBuilder sb = new StringBuilder();
sb.append(errorMessage);
if (this.errorMessage != null && !this.errorMessage.isEmpty()) {
sb.append("\n\n---- Previous Error -----\n\n");
sb.append(this.errorMessage);
}
String appendedErrorMessage = sb.toString();
this.errorMessage = appendedErrorMessage;
errorMessageProperty.set(appendedErrorMessage);
}
public void setAssetTxProofResult(@Nullable AssetTxProofResult assetTxProofResult) { public void setAssetTxProofResult(@Nullable AssetTxProofResult assetTxProofResult) {
this.assetTxProofResult = assetTxProofResult; this.assetTxProofResult = assetTxProofResult;
assetTxProofResultUpdateProperty.set(assetTxProofResultUpdateProperty.get() + 1); assetTxProofResultUpdateProperty.set(assetTxProofResultUpdateProperty.get() + 1);
@ -1434,6 +1479,8 @@ public abstract class Trade implements Tradable, Model {
final long tradeTime = getTakeOfferDate().getTime(); final long tradeTime = getTakeOfferDate().getTime();
MoneroDaemon daemonRpc = xmrWalletService.getDaemon(); MoneroDaemon daemonRpc = xmrWalletService.getDaemon();
if (daemonRpc == null) throw new RuntimeException("Cannot set start time for trade " + getId() + " because it has no connection to monerod"); if (daemonRpc == null) throw new RuntimeException("Cannot set start time for trade " + getId() + " because it has no connection to monerod");
if (getMakerDepositTx() == null || getTakerDepositTx() == null) throw new RuntimeException("Cannot set start time for trade " + getId() + " because its unlocked deposit tx is null. Is client connected to a daemon?");
long maxHeight = Math.max(getMakerDepositTx().getHeight(), getTakerDepositTx().getHeight()); long maxHeight = Math.max(getMakerDepositTx().getHeight(), getTakerDepositTx().getHeight());
long blockTime = daemonRpc.getBlockByHeight(maxHeight).getTimestamp(); long blockTime = daemonRpc.getBlockByHeight(maxHeight).getTimestamp();
@ -1465,7 +1512,7 @@ public abstract class Trade implements Tradable, Model {
} }
public boolean isDepositsPublished() { public boolean isDepositsPublished() {
return getState().getPhase().ordinal() >= Phase.DEPOSITS_PUBLISHED.ordinal(); return getState().getPhase().ordinal() >= Phase.DEPOSITS_PUBLISHED.ordinal() && getMaker().getDepositTxHash() != null && getTaker().getDepositTxHash() != null;
} }
public boolean isFundsLockedIn() { public boolean isFundsLockedIn() {
@ -1473,11 +1520,11 @@ public abstract class Trade implements Tradable, Model {
} }
public boolean isDepositsConfirmed() { public boolean isDepositsConfirmed() {
return getState().getPhase().ordinal() >= Phase.DEPOSITS_CONFIRMED.ordinal(); return isDepositsPublished() && getState().getPhase().ordinal() >= Phase.DEPOSITS_CONFIRMED.ordinal();
} }
public boolean isDepositsUnlocked() { public boolean isDepositsUnlocked() {
return getState().getPhase().ordinal() >= Phase.DEPOSITS_UNLOCKED.ordinal(); return isDepositsPublished() && getState().getPhase().ordinal() >= Phase.DEPOSITS_UNLOCKED.ordinal();
} }
public boolean isPaymentSent() { public boolean isPaymentSent() {
@ -1616,7 +1663,7 @@ public abstract class Trade implements Tradable, Model {
*/ */
public long getReprocessDelayInSeconds(int reprocessCount) { public long getReprocessDelayInSeconds(int reprocessCount) {
int retryCycles = 3; // reprocess on next refresh periods for first few attempts (app might auto switch to a good connection) int retryCycles = 3; // reprocess on next refresh periods for first few attempts (app might auto switch to a good connection)
if (reprocessCount < retryCycles) return xmrWalletService.getConnectionsService().getDefaultRefreshPeriodMs() / 1000; if (reprocessCount < retryCycles) return xmrWalletService.getConnectionsService().getRefreshPeriodMs() / 1000;
long delay = 60; long delay = 60;
for (int i = retryCycles; i < reprocessCount; i++) delay *= 2; for (int i = retryCycles; i < reprocessCount; i++) delay *= 2;
return Math.min(MAX_REPROCESS_DELAY_SECONDS, delay); return Math.min(MAX_REPROCESS_DELAY_SECONDS, delay);
@ -1642,15 +1689,27 @@ public abstract class Trade implements Tradable, Model {
return tradeVolumeProperty; return tradeVolumeProperty;
} }
private void setDaemonConnection(MoneroRpcConnection connection) { private void onConnectionChanged(MoneroRpcConnection connection) {
synchronized (walletLock) { synchronized (walletLock) {
if (isShutDown) return;
MoneroWallet wallet = getWallet(); // check if ignored
if (wallet == null) return; if (isShutDownStarted) return;
if (getWallet() == null) return;
if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) return;
// set daemon connection (must restart monero-wallet-rpc if proxy uri changed)
String oldProxyUri = wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getProxyUri();
String newProxyUri = connection == null ? null : connection.getProxyUri();
log.info("Setting daemon connection for trade wallet {}: {}", getId() , connection == null ? null : connection.getUri()); log.info("Setting daemon connection for trade wallet {}: {}", getId() , connection == null ? null : connection.getUri());
wallet.setDaemonConnection(connection); if (wallet instanceof MoneroWalletRpc && !StringUtils.equals(oldProxyUri, newProxyUri)) {
log.info("Restarting monero-wallet-rpc for trade wallet to set proxy URI {}: {}", getId() , connection == null ? null : connection.getUri());
closeWallet();
wallet = getWallet();
} else {
wallet.setDaemonConnection(connection);
}
updateWalletRefreshPeriod(); updateWalletRefreshPeriod();
// sync and reprocess messages on new thread // sync and reprocess messages on new thread
if (connection != null && !Boolean.FALSE.equals(connection.isConnected())) { if (connection != null && !Boolean.FALSE.equals(connection.isConnected())) {
HavenoUtils.submitTask(() -> { HavenoUtils.submitTask(() -> {
@ -1665,14 +1724,14 @@ public abstract class Trade implements Tradable, Model {
} }
private void updateSyncing() { private void updateSyncing() {
if (isShutDown) return; if (isShutDownStarted) return;
if (!isIdling()) { if (!isIdling()) {
updateWalletRefreshPeriod(); updateWalletRefreshPeriod();
trySyncWallet(); trySyncWallet();
} else { } else {
long startSyncingInMs = ThreadLocalRandom.current().nextLong(0, getWalletRefreshPeriod()); // random time to start syncing long startSyncingInMs = ThreadLocalRandom.current().nextLong(0, getWalletRefreshPeriod()); // random time to start syncing
UserThread.runAfter(() -> { UserThread.runAfter(() -> {
if (!isShutDown) { if (!isShutDownStarted) {
updateWalletRefreshPeriod(); updateWalletRefreshPeriod();
trySyncWallet(); trySyncWallet();
} }
@ -1680,13 +1739,13 @@ public abstract class Trade implements Tradable, Model {
} }
} }
private void updateWalletRefreshPeriod() { public void updateWalletRefreshPeriod() {
setWalletRefreshPeriod(getWalletRefreshPeriod()); setWalletRefreshPeriod(getWalletRefreshPeriod());
} }
private void setWalletRefreshPeriod(long walletRefreshPeriod) { private void setWalletRefreshPeriod(long walletRefreshPeriod) {
synchronized (walletLock) { synchronized (walletLock) {
if (this.isShutDown) return; if (this.isShutDownStarted) return;
if (this.walletRefreshPeriod != null && this.walletRefreshPeriod == walletRefreshPeriod) return; if (this.walletRefreshPeriod != null && this.walletRefreshPeriod == walletRefreshPeriod) return;
this.walletRefreshPeriod = walletRefreshPeriod; this.walletRefreshPeriod = walletRefreshPeriod;
if (getWallet() != null) { if (getWallet() != null) {
@ -1702,7 +1761,7 @@ public abstract class Trade implements Tradable, Model {
synchronized (walletLock) { synchronized (walletLock) {
if (txPollLooper != null) return; if (txPollLooper != null) return;
log.info("Starting to poll wallet for {} {}", getClass().getSimpleName(), getId()); log.info("Starting to poll wallet for {} {}", getClass().getSimpleName(), getId());
txPollLooper = new TaskLooper(() -> { pollWallet(); }); txPollLooper = new TaskLooper(() -> pollWallet());
txPollLooper.start(walletRefreshPeriod); txPollLooper.start(walletRefreshPeriod);
} }
} }
@ -1719,53 +1778,61 @@ public abstract class Trade implements Tradable, Model {
private void pollWallet() { private void pollWallet() {
try { try {
// skip if either deposit tx id is unknown
if (processModel.getMaker().getDepositTxHash() == null || processModel.getTaker().getDepositTxHash() == null) return;
// skip if payout unlocked // skip if payout unlocked
if (isPayoutUnlocked()) return; if (isPayoutUnlocked()) return;
// rescan spent if deposits unlocked // rescan spent outputs to detect payout tx after deposits unlocked
if (isDepositsUnlocked()) getWallet().rescanSpent(); if (isDepositsUnlocked() && !isPayoutPublished()) getWallet().rescanSpent();
// get txs with outputs // get txs from trade wallet
List<MoneroTxWallet> txs; boolean payoutExpected = isPaymentReceived() || processModel.getPaymentReceivedMessage() != null || disputeState.ordinal() > DisputeState.ARBITRATOR_SENT_DISPUTE_CLOSED_MSG.ordinal() || processModel.getDisputeClosedMessage() != null;
try { boolean checkPool = !isDepositsConfirmed() || (!isPayoutConfirmed() && payoutExpected);
txs = getWallet().getTxs(new MoneroTxQuery() MoneroTxQuery query = new MoneroTxQuery().setIncludeOutputs(true);
.setHashes(Arrays.asList(processModel.getMaker().getDepositTxHash(), processModel.getTaker().getDepositTxHash())) if (!checkPool) query.setInTxPool(false); // avoid pool check if possible
.setIncludeOutputs(true)); List<MoneroTxWallet> txs = wallet.getTxs(query);
} catch (Exception e) {
if (!isShutDown) log.info("Could not fetch deposit txs from wallet for {} {}: {}", getClass().getSimpleName(), getId(), e.getMessage()); // expected at first // warn on double spend // TODO: other handling?
return; for (MoneroTxWallet tx : txs) {
if (Boolean.TRUE.equals(tx.isDoubleSpendSeen())) log.warn("Double spend seen for tx {} for {} {}", tx.getHash(), getClass().getSimpleName(), getId());
} }
// check deposit txs // check deposit txs
if (!isDepositsUnlocked()) { if (!isDepositsUnlocked()) {
if (txs.size() == 2) {
// update trader txs
// update trader state MoneroTxWallet makerDepositTx = null;
boolean makerFirst = txs.get(0).getHash().equals(processModel.getMaker().getDepositTxHash()); MoneroTxWallet takerDepositTx = null;
getMaker().setDepositTx(makerFirst ? txs.get(0) : txs.get(1)); for (MoneroTxWallet tx : txs) {
getTaker().setDepositTx(makerFirst ? txs.get(1) : txs.get(0)); if (tx.getHash().equals(processModel.getMaker().getDepositTxHash())) makerDepositTx = tx;
if (tx.getHash().equals(processModel.getTaker().getDepositTxHash())) takerDepositTx = tx;
// set security deposits
if (getBuyer().getSecurityDeposit().longValueExact() == 0) {
BigInteger buyerSecurityDeposit = ((MoneroTxWallet) getBuyer().getDepositTx()).getIncomingAmount();
BigInteger sellerSecurityDeposit = ((MoneroTxWallet) getSeller().getDepositTx()).getIncomingAmount().subtract(getAmount());
getBuyer().setSecurityDeposit(buyerSecurityDeposit);
getSeller().setSecurityDeposit(sellerSecurityDeposit);
}
// set deposits published state
setStateDepositsPublished();
// check if deposit txs confirmed
if (txs.get(0).isConfirmed() && txs.get(1).isConfirmed()) setStateDepositsConfirmed();
if (!txs.get(0).isLocked() && !txs.get(1).isLocked()) setStateDepositsUnlocked();
} }
getMaker().setDepositTx(makerDepositTx);
getTaker().setDepositTx(takerDepositTx);
// skip if deposit txs not seen
if (makerDepositTx == null || takerDepositTx == null) return;
// set security deposits
if (getBuyer().getSecurityDeposit().longValueExact() == 0) {
BigInteger buyerSecurityDeposit = ((MoneroTxWallet) getBuyer().getDepositTx()).getIncomingAmount();
BigInteger sellerSecurityDeposit = ((MoneroTxWallet) getSeller().getDepositTx()).getIncomingAmount().subtract(getAmount());
getBuyer().setSecurityDeposit(buyerSecurityDeposit);
getSeller().setSecurityDeposit(sellerSecurityDeposit);
}
// update state
setStateDepositsPublished();
if (makerDepositTx.isConfirmed() && takerDepositTx.isConfirmed()) setStateDepositsConfirmed();
if (!makerDepositTx.isLocked() && !takerDepositTx.isLocked()) setStateDepositsUnlocked();
} }
// check payout tx // check payout tx
else { if (isDepositsUnlocked()) {
// check if deposit txs spent (appears on payout published) // check if any outputs spent (observed on payout published)
for (MoneroTxWallet tx : txs) { for (MoneroTxWallet tx : txs) {
for (MoneroOutputWallet output : tx.getOutputsWallet()) { for (MoneroOutputWallet output : tx.getOutputsWallet()) {
if (Boolean.TRUE.equals(output.isSpent())) { if (Boolean.TRUE.equals(output.isSpent())) {
@ -1775,19 +1842,18 @@ public abstract class Trade implements Tradable, Model {
} }
// check for outgoing txs (appears after wallet submits payout tx or on payout confirmed) // check for outgoing txs (appears after wallet submits payout tx or on payout confirmed)
List<MoneroTxWallet> outgoingTxs = getWallet().getTxs(new MoneroTxQuery().setIsOutgoing(true)); for (MoneroTxWallet tx : txs) {
if (!outgoingTxs.isEmpty()) { if (tx.isOutgoing()) {
MoneroTxWallet payoutTx = outgoingTxs.get(0); setPayoutTx(tx);
setPayoutTx(payoutTx); setPayoutStatePublished();
setPayoutStatePublished(); if (tx.isConfirmed()) setPayoutStateConfirmed();
if (payoutTx.isConfirmed()) setPayoutStateConfirmed(); if (!tx.isLocked()) setPayoutStateUnlocked();
if (!payoutTx.isLocked()) setPayoutStateUnlocked(); }
} }
} }
} catch (Exception e) { } catch (Exception e) {
if (!isShutDown && getWallet() != null && isWalletConnected()) { if (!isShutDown && getWallet() != null && isWalletConnected()) {
log.warn("Error polling trade wallet {}: {}", getId(), e.getMessage()); log.warn("Error polling trade wallet {}: {}", getId(), e.getMessage());
e.printStackTrace();
} }
} }
} }
@ -1795,7 +1861,7 @@ public abstract class Trade implements Tradable, Model {
private long getWalletRefreshPeriod() { private long getWalletRefreshPeriod() {
if (isIdling()) return IDLE_SYNC_PERIOD_MS; if (isIdling()) return IDLE_SYNC_PERIOD_MS;
return xmrWalletService.getConnectionsService().getDefaultRefreshPeriodMs(); return xmrWalletService.getConnectionsService().getRefreshPeriodMs();
} }
private void setStateDepositsPublished() { private void setStateDepositsPublished() {
@ -1867,7 +1933,7 @@ public abstract class Trade implements Tradable, Model {
} catch (Exception e) { } catch (Exception e) {
processing = false; processing = false;
e.printStackTrace(); e.printStackTrace();
if (isInitialized && !isShutDown && !isWalletConnected()) throw e; if (isInitialized && !isShutDownStarted && !isWalletConnected()) throw e;
} }
}); });
} }

View File

@ -18,6 +18,8 @@
package haveno.core.trade; package haveno.core.trade;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import common.utils.GenUtils;
import haveno.common.ClockWatcher; import haveno.common.ClockWatcher;
import haveno.common.UserThread; import haveno.common.UserThread;
import haveno.common.crypto.KeyRing; import haveno.common.crypto.KeyRing;
@ -67,6 +69,7 @@ import haveno.core.user.User;
import haveno.core.util.Validator; import haveno.core.util.Validator;
import haveno.core.xmr.model.XmrAddressEntry; import haveno.core.xmr.model.XmrAddressEntry;
import haveno.core.xmr.wallet.XmrWalletService; import haveno.core.xmr.wallet.XmrWalletService;
import haveno.network.p2p.BootstrapListener;
import haveno.network.p2p.DecryptedDirectMessageListener; import haveno.network.p2p.DecryptedDirectMessageListener;
import haveno.network.p2p.DecryptedMessageWithPubKey; import haveno.network.p2p.DecryptedMessageWithPubKey;
import haveno.network.p2p.NodeAddress; import haveno.network.p2p.NodeAddress;
@ -85,6 +88,7 @@ import monero.wallet.model.MoneroOutputQuery;
import org.bitcoinj.core.Coin; import org.bitcoinj.core.Coin;
import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.KeyParameter;
import org.fxmisc.easybind.EasyBind; import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.monadic.MonadicBinding;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -198,6 +202,13 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
p2PService.addDecryptedDirectMessageListener(this); p2PService.addDecryptedDirectMessageListener(this);
failedTradesManager.setUnFailTradeCallback(this::unFailTrade); failedTradesManager.setUnFailTradeCallback(this::unFailTrade);
// initialize trades when connected to p2p network
p2PService.addP2PServiceListener(new BootstrapListener() {
public void onUpdatedDataReceived() {
initPersistedTrades();
}
});
} }
@ -249,27 +260,24 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
public void onAllServicesInitialized() { public void onAllServicesInitialized() {
// initialize
initialize();
// listen for account updates // listen for account updates
accountService.addListener(new AccountServiceListener() { accountService.addListener(new AccountServiceListener() {
@Override @Override
public void onAccountCreated() { public void onAccountCreated() {
log.info(getClass().getSimpleName() + ".accountService.onAccountCreated()"); log.info(TradeManager.class + ".accountService.onAccountCreated()");
initialize(); initPersistedTrades();
} }
@Override @Override
public void onAccountOpened() { public void onAccountOpened() {
log.info(getClass().getSimpleName() + ".accountService.onAccountOpened()"); log.info(TradeManager.class + ".accountService.onAccountOpened()");
initialize(); initPersistedTrades();
} }
@Override @Override
public void onAccountClosed() { public void onAccountClosed() {
log.info(getClass().getSimpleName() + ".accountService.onAccountClosed()"); log.info(TradeManager.class + ".accountService.onAccountClosed()");
closeAllTrades(); closeAllTrades();
} }
@ -280,21 +288,36 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
}); });
} }
private void initialize() { public void onShutDownStarted() {
log.info("{}.onShutDownStarted()", getClass().getSimpleName());
// initialize trades off main thread // collect trades to prepare
new Thread(() -> initPersistedTrades()).start(); Set<Trade> trades = new HashSet<Trade>();
trades.addAll(tradableList.getList());
trades.addAll(closedTradableManager.getClosedTrades());
trades.addAll(failedTradesManager.getObservableList());
getObservableList().addListener((ListChangeListener<Trade>) change -> onTradesChanged()); // prepare to shut down trades in parallel
onTradesChanged(); Set<Runnable> tasks = new HashSet<Runnable>();
for (Trade trade : trades) tasks.add(() -> {
xmrWalletService.setTradeManager(this); try {
trade.onShutDownStarted();
// thaw unreserved outputs } catch (Exception e) {
thawUnreservedOutputs(); if (e.getMessage() != null && e.getMessage().contains("Connection reset")) return; // expected if shut down with ctrl+c
log.warn("Error notifying {} {} that shut down started {}", getClass().getSimpleName(), trade.getId());
e.printStackTrace();
}
});
try {
HavenoUtils.executeTasks(tasks);
} catch (Exception e) {
log.warn("Error notifying trades that shut down started: {}", e.getMessage());
e.printStackTrace();
}
} }
public void shutDown() { public void shutDown() {
log.info("Shutting down {}", getClass().getSimpleName());
isShutDown = true; isShutDown = true;
closeAllTrades(); closeAllTrades();
} }
@ -313,7 +336,9 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
try { try {
trade.shutDown(); trade.shutDown();
} catch (Exception e) { } catch (Exception e) {
log.warn("Error closing trade subprocess. Was Haveno stopped manually with ctrl+c?"); if (e.getMessage() != null && (e.getMessage().contains("Connection reset") || e.getMessage().contains("Connection refused"))) return; // expected if shut down with ctrl+c
log.warn("Error closing {} {}", trade.getClass().getSimpleName(), trade.getId());
e.printStackTrace();
} }
}); });
try { try {
@ -372,54 +397,80 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
private void initPersistedTrades() { private void initPersistedTrades() {
log.info("Initializing persisted trades");
// get all trades // initialize off main thread
List<Trade> trades = getAllTrades(); new Thread(() -> {
// open trades in parallel since each may open a multisig wallet // get all trades
log.info("Initializing trades"); List<Trade> trades = getAllTrades();
int threadPoolSize = 10;
Set<Runnable> tasks = new HashSet<Runnable>(); // initialize trades in parallel
for (Trade trade : trades) { int threadPoolSize = 10;
tasks.add(new Runnable() { Set<Runnable> tasks = new HashSet<Runnable>();
@Override for (Trade trade : trades) {
public void run() { tasks.add(() -> {
initPersistedTrade(trade); initPersistedTrade(trade);
}
});
};
HavenoUtils.executeTasks(tasks, threadPoolSize);
log.info("Done initializing trades");
// reset any available address entries // remove trade if protocol didn't initialize
if (isShutDown) return; if (getOpenTrade(trade.getId()).isPresent() && !trade.isDepositRequested()) {
xmrWalletService.getAddressEntriesForAvailableBalanceStream() log.warn("Removing persisted {} {} with uid={} because it did not finish initializing (state={})", trade.getClass().getSimpleName(), trade.getId(), trade.getUid(), trade.getState());
.filter(addressEntry -> addressEntry.getOfferId() != null) removeTradeOnError(trade);
.forEach(addressEntry -> { }
log.warn("Swapping pending {} entries at startup. offerId={}", addressEntry.getContext(), addressEntry.getOfferId());
xmrWalletService.swapTradeEntryToAvailableEntry(addressEntry.getOfferId(), addressEntry.getContext());
}); });
};
HavenoUtils.executeTasks(tasks, threadPoolSize);
log.info("Done initializing persisted trades");
if (isShutDown) return;
// notify that persisted trades initialized // sync idle trades once in background after active trades
persistedTradesInitialized.set(true); for (Trade trade : trades) {
if (trade.isIdling()) {
// We do not include failed trades as they should not be counted anyway in the trade statistics HavenoUtils.submitTask(() -> trade.syncWallet());
Set<Trade> nonFailedTrades = new HashSet<>(closedTradableManager.getClosedTrades()); }
nonFailedTrades.addAll(tradableList.getList());
String referralId = referralIdService.getOptionalReferralId().orElse(null);
boolean isTorNetworkNode = p2PService.getNetworkNode() instanceof TorNetworkNode;
tradeStatisticsManager.maybeRepublishTradeStatistics(nonFailedTrades, referralId, isTorNetworkNode);
// sync idle trades once in background after active trades
for (Trade trade : trades) {
if (trade.isIdling()) {
HavenoUtils.submitTask(() -> trade.syncWallet());
} }
}
getObservableList().addListener((ListChangeListener<Trade>) change -> onTradesChanged());
onTradesChanged();
xmrWalletService.setTradeManager(this);
// process after all wallets initialized
MonadicBinding<Boolean> walletsInitialized = EasyBind.combine(HavenoUtils.havenoSetup.getWalletInitialized(), persistedTradesInitialized, (a, b) -> a && b);
walletsInitialized.subscribe((observable, oldValue, newValue) -> {
if (!newValue) return;
// thaw unreserved outputs
thawUnreservedOutputs();
// reset any available funded address entries
xmrWalletService.getAddressEntriesForAvailableBalanceStream()
.filter(addressEntry -> addressEntry.getOfferId() != null)
.forEach(addressEntry -> {
log.warn("Swapping pending {} entries at startup. offerId={}", addressEntry.getContext(), addressEntry.getOfferId());
xmrWalletService.swapTradeEntryToAvailableEntry(addressEntry.getOfferId(), addressEntry.getContext());
});
});
// notify that persisted trades initialized
persistedTradesInitialized.set(true);
// We do not include failed trades as they should not be counted anyway in the trade statistics
// TODO: remove stats?
Set<Trade> nonFailedTrades = new HashSet<>(closedTradableManager.getClosedTrades());
nonFailedTrades.addAll(tradableList.getList());
String referralId = referralIdService.getOptionalReferralId().orElse(null);
boolean isTorNetworkNode = p2PService.getNetworkNode() instanceof TorNetworkNode;
tradeStatisticsManager.maybeRepublishTradeStatistics(nonFailedTrades, referralId, isTorNetworkNode);
}).start();
// allow execution to start
GenUtils.waitFor(100);
} }
private void initPersistedTrade(Trade trade) { private void initPersistedTrade(Trade trade) {
if (isShutDown) return; if (isShutDown) return;
if (getTradeProtocol(trade) != null) return;
initTradeAndProtocol(trade, createTradeProtocol(trade)); initTradeAndProtocol(trade, createTradeProtocol(trade));
requestPersistence(); requestPersistence();
scheduleDeletionIfUnfunded(trade); scheduleDeletionIfUnfunded(trade);
@ -1100,16 +1151,16 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
} }
private void addTrade(Trade trade) { private void addTrade(Trade trade) {
synchronized(tradableList) { synchronized (tradableList) {
if (tradableList.add(trade)) { if (tradableList.add(trade)) {
requestPersistence(); requestPersistence();
} }
} }
} }
private synchronized void removeTrade(Trade trade) { private void removeTrade(Trade trade) {
log.info("TradeManager.removeTrade() " + trade.getId()); log.info("TradeManager.removeTrade() " + trade.getId());
synchronized(tradableList) { synchronized (tradableList) {
if (!tradableList.contains(trade)) return; if (!tradableList.contains(trade)) return;
// remove trade // remove trade
@ -1121,9 +1172,9 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
} }
} }
private synchronized void removeTradeOnError(Trade trade) { private void removeTradeOnError(Trade trade) {
log.info("TradeManager.removeTradeOnError() " + trade.getId()); log.info("TradeManager.removeTradeOnError() " + trade.getId());
synchronized(tradableList) { synchronized (tradableList) {
if (!tradableList.contains(trade)) return; if (!tradableList.contains(trade)) return;
// unreserve taker key images // unreserve taker key images
@ -1150,18 +1201,18 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
} }
private void scheduleDeletionIfUnfunded(Trade trade) { private void scheduleDeletionIfUnfunded(Trade trade) {
if (trade.isDepositRequested() && !trade.isDepositsPublished()) { if (getOpenTrade(trade.getId()).isPresent() && trade.isDepositRequested() && !trade.isDepositsPublished()) {
log.warn("Scheduling to delete trade if unfunded for {} {}", trade.getClass().getSimpleName(), trade.getId()); log.warn("Scheduling to delete open trade if unfunded for {} {}", trade.getClass().getSimpleName(), trade.getId());
UserThread.runAfter(() -> { UserThread.runAfter(() -> {
if (isShutDown) return; if (isShutDown) return;
// get trade's deposit txs from daemon // get trade's deposit txs from daemon
MoneroTx makerDepositTx = xmrWalletService.getDaemon().getTx(trade.getMaker().getDepositTxHash()); MoneroTx makerDepositTx = trade.getMaker().getDepositTxHash() == null ? null : xmrWalletService.getDaemon().getTx(trade.getMaker().getDepositTxHash());
MoneroTx takerDepositTx = xmrWalletService.getDaemon().getTx(trade.getTaker().getDepositTxHash()); MoneroTx takerDepositTx = trade.getTaker().getDepositTxHash() == null ? null : xmrWalletService.getDaemon().getTx(trade.getTaker().getDepositTxHash());
// delete multisig trade wallet if neither deposit tx published // delete multisig trade wallet if neither deposit tx published
if (makerDepositTx == null && takerDepositTx == null) { if (makerDepositTx == null && takerDepositTx == null) {
log.warn("Deleting {} {} after protocol timeout", trade.getClass().getSimpleName(), trade.getId()); log.warn("Deleting {} {} after protocol error", trade.getClass().getSimpleName(), trade.getId());
removeTrade(trade); removeTrade(trade);
failedTradesManager.removeTrade(trade); failedTradesManager.removeTrade(trade);
if (trade.walletExists()) trade.deleteWallet(); if (trade.walletExists()) trade.deleteWallet();

View File

@ -182,7 +182,7 @@ public final class InitTradeRequest extends TradeMessage implements DirectMessag
",\n reserveTxHex=" + reserveTxHex + ",\n reserveTxHex=" + reserveTxHex +
",\n reserveTxKey=" + reserveTxKey + ",\n reserveTxKey=" + reserveTxKey +
",\n payoutAddress=" + payoutAddress + ",\n payoutAddress=" + payoutAddress +
",\n makerSignature=" + Utilities.byteArrayToInteger(makerSignature) + ",\n makerSignature=" + (makerSignature == null ? null : Utilities.byteArrayToInteger(makerSignature)) +
"\n} " + super.toString(); "\n} " + super.toString();
} }
} }

View File

@ -141,10 +141,11 @@ public final class PaymentSentMessage extends TradeMailboxMessage {
@Override @Override
public String toString() { public String toString() {
return "PaymentSentMessage{" + return "PaymentSentMessage{" +
",\n tradeId=" + tradeId +
",\n uid='" + uid + '\'' +
",\n senderNodeAddress=" + senderNodeAddress + ",\n senderNodeAddress=" + senderNodeAddress +
",\n counterCurrencyTxId=" + counterCurrencyTxId + ",\n counterCurrencyTxId=" + counterCurrencyTxId +
",\n counterCurrencyExtraData=" + counterCurrencyExtraData + ",\n counterCurrencyExtraData=" + counterCurrencyExtraData +
",\n uid='" + uid + '\'' +
",\n payoutTxHex=" + payoutTxHex + ",\n payoutTxHex=" + payoutTxHex +
",\n updatedMultisigHex=" + updatedMultisigHex + ",\n updatedMultisigHex=" + updatedMultisigHex +
",\n paymentAccountKey=" + paymentAccountKey + ",\n paymentAccountKey=" + paymentAccountKey +

View File

@ -324,7 +324,7 @@ public class FluentProtocol {
log.info(info); log.info(info);
return Result.VALID.info(info); return Result.VALID.info(info);
} else { } else {
String info = MessageFormat.format("We received a {0} but we are are not in the expected state. " + String info = MessageFormat.format("We received a {0} but we are not in the expected state. " +
"Expected states={1}, Trade state= {2}, tradeId={3}", "Expected states={1}, Trade state= {2}, tradeId={3}",
trigger, trigger,
expectedStates, expectedStates,

View File

@ -52,7 +52,7 @@ import haveno.core.trade.protocol.tasks.ProcessPaymentSentMessage;
import haveno.core.trade.protocol.tasks.ProcessSignContractRequest; import haveno.core.trade.protocol.tasks.ProcessSignContractRequest;
import haveno.core.trade.protocol.tasks.ProcessSignContractResponse; import haveno.core.trade.protocol.tasks.ProcessSignContractResponse;
import haveno.core.trade.protocol.tasks.RemoveOffer; import haveno.core.trade.protocol.tasks.RemoveOffer;
import haveno.core.trade.protocol.tasks.ResendDisputeClosedMessageWithPayout; import haveno.core.trade.protocol.tasks.MaybeResendDisputeClosedMessageWithPayout;
import haveno.core.trade.protocol.tasks.TradeTask; import haveno.core.trade.protocol.tasks.TradeTask;
import haveno.core.trade.protocol.tasks.VerifyPeersAccountAgeWitness; import haveno.core.trade.protocol.tasks.VerifyPeersAccountAgeWitness;
import haveno.core.util.Validator; import haveno.core.util.Validator;
@ -242,32 +242,19 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
} }
protected void onInitialized() { protected void onInitialized() {
if (!trade.isCompleted()) {
processModel.getP2PService().addDecryptedDirectMessageListener(this);
}
// initialize trade // listen for direct messages unless completed
trade.initialize(processModel.getProvider()); if (!trade.isCompleted()) processModel.getP2PService().addDecryptedDirectMessageListener(this);
// initialize trade with lock
synchronized (trade) {
trade.initialize(processModel.getProvider());
}
// process mailbox messages // process mailbox messages
MailboxMessageService mailboxMessageService = processModel.getP2PService().getMailboxMessageService(); MailboxMessageService mailboxMessageService = processModel.getP2PService().getMailboxMessageService();
mailboxMessageService.addDecryptedMailboxListener(this); if (!trade.isCompleted()) mailboxMessageService.addDecryptedMailboxListener(this);
handleMailboxCollection(mailboxMessageService.getMyDecryptedMailboxMessages()); handleMailboxCollection(mailboxMessageService.getMyDecryptedMailboxMessages());
// send deposit confirmed message on startup or event
if (trade.getState().ordinal() >= Trade.State.DEPOSIT_TXS_CONFIRMED_IN_BLOCKCHAIN.ordinal()) {
new Thread(() -> sendDepositsConfirmedMessages()).start();
} else {
EasyBind.subscribe(trade.stateProperty(), state -> {
if (state == Trade.State.DEPOSIT_TXS_CONFIRMED_IN_BLOCKCHAIN) {
new Thread(() -> sendDepositsConfirmedMessages()).start();
}
});
}
// reprocess payout messages if pending
maybeReprocessPaymentReceivedMessage(true);
HavenoUtils.arbitrationManager.maybeReprocessDisputeClosedMessage(trade, true);
} }
public void maybeReprocessPaymentReceivedMessage(boolean reprocessOnError) { public void maybeReprocessPaymentReceivedMessage(boolean reprocessOnError) {
@ -448,7 +435,7 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
.setup(tasks( .setup(tasks(
ProcessDepositsConfirmedMessage.class, ProcessDepositsConfirmedMessage.class,
VerifyPeersAccountAgeWitness.class, VerifyPeersAccountAgeWitness.class,
ResendDisputeClosedMessageWithPayout.class) MaybeResendDisputeClosedMessageWithPayout.class)
.using(new TradeTaskRunner(trade, .using(new TradeTaskRunner(trade,
() -> { () -> {
handleTaskRunnerSuccess(sender, response); handleTaskRunnerSuccess(sender, response);
@ -475,7 +462,8 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
// the mailbox msg once wallet is ready and trade state set. // the mailbox msg once wallet is ready and trade state set.
synchronized (trade) { synchronized (trade) {
if (trade.getPhase().ordinal() >= Trade.Phase.PAYMENT_SENT.ordinal()) { if (trade.getPhase().ordinal() >= Trade.Phase.PAYMENT_SENT.ordinal()) {
log.warn("Ignoring PaymentSentMessage which was already processed"); log.warn("Received another PaymentSentMessage which was already processed, ACKing");
handleTaskRunnerSuccess(peer, message);
return; return;
} }
latchTrade(); latchTrade();
@ -518,6 +506,11 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
return; return;
} }
synchronized (trade) { synchronized (trade) {
if (trade.getPhase().ordinal() >= Trade.Phase.PAYMENT_RECEIVED.ordinal()) {
log.warn("Received another PaymentReceivedMessage which was already processed, ACKing");
handleTaskRunnerSuccess(peer, message);
return;
}
latchTrade(); latchTrade();
Validator.checkTradeId(processModel.getOfferId(), message); Validator.checkTradeId(processModel.getOfferId(), message);
processModel.setTradeMessage(message); processModel.setTradeMessage(message);
@ -844,7 +837,7 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
} }
} }
private void sendDepositsConfirmedMessages() { public void maybeSendDepositsConfirmedMessages() {
synchronized (trade) { synchronized (trade) {
if (!trade.isInitialized()) return; // skip if shutting down if (!trade.isInitialized()) return; // skip if shutting down
if (trade.getProcessModel().isDepositsConfirmedMessagesDelivered()) return; // skip if already delivered if (trade.getProcessModel().isDepositsConfirmedMessagesDelivered()) return; // skip if already delivered
@ -860,7 +853,7 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
// retry in 15 minutes // retry in 15 minutes
UserThread.runAfter(() -> { UserThread.runAfter(() -> {
sendDepositsConfirmedMessages(); maybeSendDepositsConfirmedMessages();
}, 15, TimeUnit.MINUTES); }, 15, TimeUnit.MINUTES);
handleTaskRunnerFault(null, null, "SendDepositsConfirmedMessages", errorMessage); handleTaskRunnerFault(null, null, "SendDepositsConfirmedMessages", errorMessage);
}))) })))

View File

@ -70,7 +70,7 @@ public class MakerSendInitTradeRequest extends TradeTask {
trade.getSelf().getReserveTxHash(), trade.getSelf().getReserveTxHash(),
trade.getSelf().getReserveTxHex(), trade.getSelf().getReserveTxHex(),
trade.getSelf().getReserveTxKey(), trade.getSelf().getReserveTxKey(),
model.getXmrWalletService().getAddressEntry(offer.getId(), XmrAddressEntry.Context.TRADE_PAYOUT).get().getAddressString(), model.getXmrWalletService().getAddressEntry(offer.getId(), XmrAddressEntry.Context.RESERVED_FOR_TRADE).get().getAddressString(),
null); null);
// send request to arbitrator // send request to arbitrator

View File

@ -32,10 +32,10 @@ import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@Slf4j @Slf4j
public class ResendDisputeClosedMessageWithPayout extends TradeTask { public class MaybeResendDisputeClosedMessageWithPayout extends TradeTask {
@SuppressWarnings({"unused"}) @SuppressWarnings({"unused"})
public ResendDisputeClosedMessageWithPayout(TaskRunner taskHandler, Trade trade) { public MaybeResendDisputeClosedMessageWithPayout(TaskRunner taskHandler, Trade trade) {
super(taskHandler, trade); super(taskHandler, trade);
} }

View File

@ -83,7 +83,7 @@ public class MaybeSendSignContractRequest extends TradeTask {
trade.getSelf().setDepositTx(depositTx); trade.getSelf().setDepositTx(depositTx);
trade.getSelf().setDepositTxHash(depositTx.getHash()); trade.getSelf().setDepositTxHash(depositTx.getHash());
trade.getSelf().setReserveTxKeyImages(reservedKeyImages); trade.getSelf().setReserveTxKeyImages(reservedKeyImages);
trade.getSelf().setPayoutAddressString(trade.getXmrWalletService().getAddressEntry(processModel.getOffer().getId(), XmrAddressEntry.Context.TRADE_PAYOUT).get().getAddressString()); // TODO (woodser): allow custom payout address? trade.getSelf().setPayoutAddressString(trade.getXmrWalletService().getOrCreateAddressEntry(processModel.getOffer().getId(), XmrAddressEntry.Context.TRADE_PAYOUT).getAddressString()); // TODO (woodser): allow custom payout address?
trade.getSelf().setPaymentAccountPayload(trade.getProcessModel().getPaymentAccountPayload(trade.getSelf().getPaymentAccountId())); trade.getSelf().setPaymentAccountPayload(trade.getProcessModel().getPaymentAccountPayload(trade.getSelf().getPaymentAccountId()));
// maker signs deposit hash nonce to avoid challenge protocol // maker signs deposit hash nonce to avoid challenge protocol

View File

@ -53,14 +53,21 @@ public class ProcessDepositsConfirmedMessage extends TradeTask {
if (sender.getNodeAddress().equals(trade.getSeller().getNodeAddress()) && sender != trade.getSeller()) trade.getSeller().setNodeAddress(null); if (sender.getNodeAddress().equals(trade.getSeller().getNodeAddress()) && sender != trade.getSeller()) trade.getSeller().setNodeAddress(null);
if (sender.getNodeAddress().equals(trade.getArbitrator().getNodeAddress()) && sender != trade.getArbitrator()) trade.getArbitrator().setNodeAddress(null); if (sender.getNodeAddress().equals(trade.getArbitrator().getNodeAddress()) && sender != trade.getArbitrator()) trade.getArbitrator().setNodeAddress(null);
// update multisig hex
sender.setUpdatedMultisigHex(request.getUpdatedMultisigHex());
// decrypt seller payment account payload if key given // decrypt seller payment account payload if key given
if (request.getSellerPaymentAccountKey() != null && trade.getTradePeer().getPaymentAccountPayload() == null) { if (request.getSellerPaymentAccountKey() != null && trade.getTradePeer().getPaymentAccountPayload() == null) {
log.info(trade.getClass().getSimpleName() + " decrypting using seller payment account key"); log.info(trade.getClass().getSimpleName() + " decrypting using seller payment account key");
trade.decryptPeerPaymentAccountPayload(request.getSellerPaymentAccountKey()); trade.decryptPeerPaymentAccountPayload(request.getSellerPaymentAccountKey());
} }
processModel.getTradeManager().requestPersistence(); // in case importing multisig hex fails
// update multisig hex
sender.setUpdatedMultisigHex(request.getUpdatedMultisigHex());
try {
trade.importMultisigHex();
} catch (Exception e) {
log.warn("Error importing multisig hex for {} {}: {}", trade.getClass().getSimpleName(), trade.getId(), e.getMessage());
e.printStackTrace();
}
// persist and complete // persist and complete
processModel.getTradeManager().requestPersistence(); processModel.getTradeManager().requestPersistence();

View File

@ -111,6 +111,7 @@ public class ProcessInitMultisigRequest extends TradeTask {
processModel.setMultisigAddress(result.getAddress()); processModel.setMultisigAddress(result.getAddress());
trade.saveWallet(); // save multisig wallet on completion trade.saveWallet(); // save multisig wallet on completion
trade.setStateIfValidTransitionTo(Trade.State.MULTISIG_COMPLETED); trade.setStateIfValidTransitionTo(Trade.State.MULTISIG_COMPLETED);
trade.updateWalletRefreshPeriod(); // starts syncing
} }
// update multisig participants if new state to communicate // update multisig participants if new state to communicate

View File

@ -113,7 +113,8 @@ public class ProcessPaymentReceivedMessage extends TradeTask {
// wait to sign and publish payout tx if defer flag set (seller recently saw payout tx arrive at buyer) // wait to sign and publish payout tx if defer flag set (seller recently saw payout tx arrive at buyer)
boolean isSigned = message.getSignedPayoutTxHex() != null; boolean isSigned = message.getSignedPayoutTxHex() != null;
if (trade instanceof ArbitratorTrade && !isSigned && message.isDeferPublishPayout()) { boolean deferSignAndPublish = trade instanceof ArbitratorTrade && !isSigned && message.isDeferPublishPayout();
if (deferSignAndPublish) {
log.info("Deferring signing and publishing payout tx for {} {}", trade.getClass().getSimpleName(), trade.getId()); log.info("Deferring signing and publishing payout tx for {} {}", trade.getClass().getSimpleName(), trade.getId());
GenUtils.waitFor(Trade.DEFER_PUBLISH_MS); GenUtils.waitFor(Trade.DEFER_PUBLISH_MS);
if (!trade.isPayoutUnlocked()) trade.syncWallet(); if (!trade.isPayoutUnlocked()) trade.syncWallet();
@ -135,6 +136,7 @@ public class ProcessPaymentReceivedMessage extends TradeTask {
trade.verifyPayoutTx(trade.getPayoutTxHex(), false, true); trade.verifyPayoutTx(trade.getPayoutTxHex(), false, true);
} }
} catch (Exception e) { } catch (Exception e) {
trade.syncWallet();
if (trade.isPayoutPublished()) log.info("Payout tx already published for {} {}", trade.getClass().getName(), trade.getId()); if (trade.isPayoutPublished()) log.info("Payout tx already published for {} {}", trade.getClass().getName(), trade.getId());
else throw e; else throw e;
} }

View File

@ -47,10 +47,7 @@ public class ProcessPaymentSentMessage extends TradeTask {
// update latest peer address // update latest peer address
trade.getBuyer().setNodeAddress(processModel.getTempTradePeerNodeAddress()); trade.getBuyer().setNodeAddress(processModel.getTempTradePeerNodeAddress());
// if seller, decrypt buyer's payment account payload // update state from message
if (trade.isSeller()) trade.decryptPeerPaymentAccountPayload(message.getPaymentAccountKey());
// update state
processModel.setPaymentSentMessage(message); processModel.setPaymentSentMessage(message);
trade.setPayoutTxHex(message.getPayoutTxHex()); trade.setPayoutTxHex(message.getPayoutTxHex());
trade.getBuyer().setUpdatedMultisigHex(message.getUpdatedMultisigHex()); trade.getBuyer().setUpdatedMultisigHex(message.getUpdatedMultisigHex());
@ -59,6 +56,20 @@ public class ProcessPaymentSentMessage extends TradeTask {
if (counterCurrencyTxId != null && counterCurrencyTxId.length() < 100) trade.setCounterCurrencyTxId(counterCurrencyTxId); if (counterCurrencyTxId != null && counterCurrencyTxId.length() < 100) trade.setCounterCurrencyTxId(counterCurrencyTxId);
String counterCurrencyExtraData = message.getCounterCurrencyExtraData(); String counterCurrencyExtraData = message.getCounterCurrencyExtraData();
if (counterCurrencyExtraData != null && counterCurrencyExtraData.length() < 100) trade.setCounterCurrencyExtraData(counterCurrencyExtraData); if (counterCurrencyExtraData != null && counterCurrencyExtraData.length() < 100) trade.setCounterCurrencyExtraData(counterCurrencyExtraData);
// if seller, decrypt buyer's payment account payload
if (trade.isSeller()) trade.decryptPeerPaymentAccountPayload(message.getPaymentAccountKey());
trade.requestPersistence();
// import multisig hex
try {
trade.importMultisigHex();
} catch (Exception e) {
log.warn("Error importing multisig hex for {} {}: {}", trade.getClass().getSimpleName(), trade.getId(), e.getMessage());
e.printStackTrace();
}
// update state
trade.advanceState(trade.isSeller() ? Trade.State.SELLER_RECEIVED_PAYMENT_SENT_MSG : Trade.State.BUYER_SENT_PAYMENT_SENT_MSG); trade.advanceState(trade.isSeller() ? Trade.State.SELLER_RECEIVED_PAYMENT_SENT_MSG : Trade.State.BUYER_SENT_PAYMENT_SENT_MSG);
trade.requestPersistence(); trade.requestPersistence();
complete(); complete();

View File

@ -58,8 +58,8 @@ public abstract class SendMailboxMessageTask extends TradeTask {
TradeMailboxMessage message = getTradeMailboxMessage(id); TradeMailboxMessage message = getTradeMailboxMessage(id);
setStateSent(); setStateSent();
NodeAddress peersNodeAddress = getReceiverNodeAddress(); NodeAddress peersNodeAddress = getReceiverNodeAddress();
log.info("Send {} to peer {} for {} {}", trade.getClass().getSimpleName(), trade.getId(), log.info("Send {} to peer {} for {} {}, uid={}",
message.getClass().getSimpleName(), peersNodeAddress, message.getTradeId(), message.getUid()); message.getClass().getSimpleName(), peersNodeAddress, trade.getClass().getSimpleName(), trade.getId(), message.getUid());
TradeTask task = this; TradeTask task = this;
processModel.getP2PService().getMailboxMessageService().sendEncryptedMailboxMessage( processModel.getP2PService().getMailboxMessageService().sendEncryptedMailboxMessage(

View File

@ -43,7 +43,7 @@ public class TakerReserveTradeFunds extends TradeTask {
BigInteger takerFee = trade.getTakerFee(); BigInteger takerFee = trade.getTakerFee();
BigInteger sendAmount = trade.getOffer().getDirection() == OfferDirection.BUY ? trade.getOffer().getAmount() : BigInteger.valueOf(0); BigInteger sendAmount = trade.getOffer().getDirection() == OfferDirection.BUY ? trade.getOffer().getAmount() : BigInteger.valueOf(0);
BigInteger securityDeposit = trade.getOffer().getDirection() == OfferDirection.BUY ? trade.getOffer().getSellerSecurityDeposit() : trade.getOffer().getBuyerSecurityDeposit(); BigInteger securityDeposit = trade.getOffer().getDirection() == OfferDirection.BUY ? trade.getOffer().getSellerSecurityDeposit() : trade.getOffer().getBuyerSecurityDeposit();
String returnAddress = model.getXmrWalletService().getAddressEntry(trade.getOffer().getId(), XmrAddressEntry.Context.TRADE_PAYOUT).get().getAddressString(); String returnAddress = model.getXmrWalletService().getOrCreateAddressEntry(trade.getOffer().getId(), XmrAddressEntry.Context.TRADE_PAYOUT).getAddressString();
MoneroTxWallet reserveTx = model.getXmrWalletService().createReserveTx(takerFee, sendAmount, securityDeposit, returnAddress); MoneroTxWallet reserveTx = model.getXmrWalletService().createReserveTx(takerFee, sendAmount, securityDeposit, returnAddress);
// collect reserved key images // collect reserved key images

View File

@ -78,7 +78,7 @@ public final class TradeStatistics3 implements ProcessOncePersistableNetworkPayl
extraDataMap.put(OfferPayload.REFERRAL_ID, referralId); extraDataMap.put(OfferPayload.REFERRAL_ID, referralId);
} }
NodeAddress arbitratorNodeAddress = checkNotNull(trade.getArbitrator().getNodeAddress()); NodeAddress arbitratorNodeAddress = checkNotNull(trade.getArbitrator().getNodeAddress(), "Arbitrator address is null", trade.getClass().getSimpleName(), trade.getId());
// The first 4 chars are sufficient to identify an arbitrator. // The first 4 chars are sufficient to identify an arbitrator.
// For testing with regtest/localhost we use the full address as its localhost and would result in // For testing with regtest/localhost we use the full address as its localhost and would result in

View File

@ -170,7 +170,13 @@ public class TradeStatisticsManager {
return; return;
} }
TradeStatistics3 tradeStatistics3 = TradeStatistics3.from(trade, referralId, isTorNetworkNode); TradeStatistics3 tradeStatistics3 = null;
try {
tradeStatistics3 = TradeStatistics3.from(trade, referralId, isTorNetworkNode);
} catch (Exception e) {
log.warn("Error getting trade statistic for {} {}: {}", trade.getClass().getName(), trade.getId(), e.getMessage());
return;
}
boolean hasTradeStatistics3 = hashes.contains(new P2PDataStorage.ByteArray(tradeStatistics3.getHash())); boolean hasTradeStatistics3 = hashes.contains(new P2PDataStorage.ByteArray(tradeStatistics3.getHash()));
if (hasTradeStatistics3) { if (hasTradeStatistics3) {
log.debug("Trade: {}. We have already a tradeStatistics matching the hash of tradeStatistics3.", log.debug("Trade: {}. We have already a tradeStatistics matching the hash of tradeStatistics3.",

View File

@ -37,6 +37,7 @@ import lombok.extern.slf4j.Slf4j;
import monero.common.MoneroError; import monero.common.MoneroError;
import monero.wallet.model.MoneroOutputQuery; import monero.wallet.model.MoneroOutputQuery;
import monero.wallet.model.MoneroOutputWallet; import monero.wallet.model.MoneroOutputWallet;
import monero.wallet.model.MoneroTxQuery;
import monero.wallet.model.MoneroTxWallet; import monero.wallet.model.MoneroTxWallet;
import javax.inject.Inject; import javax.inject.Inject;
@ -127,8 +128,10 @@ public class Balances {
List<Trade> openTrades = tradeManager.getTradesStreamWithFundsLockedIn().collect(Collectors.toList()); List<Trade> openTrades = tradeManager.getTradesStreamWithFundsLockedIn().collect(Collectors.toList());
for (Trade trade : openTrades) { for (Trade trade : openTrades) {
try { try {
MoneroTxWallet depositTx = xmrWalletService.getWallet().getTx(trade.getSelf().getDepositTxHash()); List<MoneroTxWallet> depositTxs = xmrWalletService.getWallet().getTxs(new MoneroTxQuery()
if (!depositTx.isConfirmed()) continue; // outputs are frozen until confirmed by arbitrator's broadcast .setHash(trade.getSelf().getDepositTxHash())
.setInTxPool(false)); // don't check pool
if (depositTxs.size() != 1 || !depositTxs.get(0).isConfirmed()) continue; // outputs are frozen until confirmed by arbitrator's broadcast
} catch (MoneroError e) { } catch (MoneroError e) {
continue; continue;
} }

View File

@ -17,135 +17,144 @@ import java.util.Map;
@Slf4j @Slf4j
public class MoneroWalletRpcManager { public class MoneroWalletRpcManager {
private static final String RPC_BIND_PORT_ARGUMENT = "--rpc-bind-port"; private static final String RPC_BIND_PORT_ARGUMENT = "--rpc-bind-port";
private static int NUM_ALLOWED_ATTEMPTS = 2; // allow this many attempts to bind to an assigned port private static int NUM_ALLOWED_ATTEMPTS = 2; // allow this many attempts to bind to an assigned port
private Integer startPort; private Integer startPort;
private final Map<Integer, MoneroWalletRpc> registeredPorts = new HashMap<>(); private final Map<Integer, MoneroWalletRpc> registeredPorts = new HashMap<>();
/** /**
* Manage monero-wallet-rpc instances by auto-assigning ports. * Manage monero-wallet-rpc instances by auto-assigning ports.
*/ */
public MoneroWalletRpcManager() { } public MoneroWalletRpcManager() {
}
/** /**
* Manage monero-wallet-rpc instances by assigning consecutive ports from a starting port. * Manage monero-wallet-rpc instances by assigning consecutive ports from a
* * starting port.
* @param startPort is the starting port to bind to *
*/ * @param startPort is the starting port to bind to
public MoneroWalletRpcManager(int startPort) { */
this.startPort = startPort; public MoneroWalletRpcManager(int startPort) {
} this.startPort = startPort;
}
/** /**
* Start a new instance of monero-wallet-rpc. * Start a new instance of monero-wallet-rpc.
* *
* @param cmd command line parameters to start monero-wallet-rpc * @param cmd command line parameters to start monero-wallet-rpc
* @return a client connected to the monero-wallet-rpc instance * @return a client connected to the monero-wallet-rpc instance
*/ */
public MoneroWalletRpc startInstance(List<String> cmd) { public MoneroWalletRpc startInstance(List<String> cmd) {
try { try {
// register given port // register given port
if (cmd.contains(RPC_BIND_PORT_ARGUMENT)) { if (cmd.contains(RPC_BIND_PORT_ARGUMENT)) {
int portArgumentPosition = cmd.indexOf(RPC_BIND_PORT_ARGUMENT) + 1; int portArgumentPosition = cmd.indexOf(RPC_BIND_PORT_ARGUMENT) + 1;
int port = Integer.parseInt(cmd.get(portArgumentPosition)); int port = Integer.parseInt(cmd.get(portArgumentPosition));
synchronized (registeredPorts) { synchronized (registeredPorts) {
if (registeredPorts.containsKey(port)) throw new RuntimeException("Port " + port + " is already registered"); if (registeredPorts.containsKey(port)) throw new RuntimeException("Port " + port + " is already registered");
registeredPorts.put(port, null); registeredPorts.put(port, null);
} }
MoneroWalletRpc walletRpc = new MoneroWalletRpc(cmd); // starts monero-wallet-rpc process MoneroWalletRpc walletRpc = new MoneroWalletRpc(cmd); // starts monero-wallet-rpc process
synchronized (registeredPorts) { synchronized (registeredPorts) {
registeredPorts.put(port, walletRpc); registeredPorts.put(port, walletRpc);
} }
return walletRpc; return walletRpc;
} }
// register assigned ports up to maximum attempts // register assigned ports up to maximum attempts
else { else {
int numAttempts = 0; int numAttempts = 0;
while (numAttempts < NUM_ALLOWED_ATTEMPTS) { while (numAttempts < NUM_ALLOWED_ATTEMPTS) {
int port; int port;
try { try {
numAttempts++; numAttempts++;
// get port // get port
if (startPort != null) port = registerNextPort(); if (startPort != null) port = registerNextPort();
else { else {
ServerSocket socket = new ServerSocket(0); ServerSocket socket = new ServerSocket(0);
port = socket.getLocalPort(); port = socket.getLocalPort();
socket.close(); socket.close();
synchronized (registeredPorts) { synchronized (registeredPorts) {
registeredPorts.put(port, null); registeredPorts.put(port, null);
}
} }
}
// start monero-wallet-rpc // start monero-wallet-rpc
List<String> cmdCopy = new ArrayList<>(cmd); // preserve original cmd List<String> cmdCopy = new ArrayList<>(cmd); // preserve original cmd
cmdCopy.add(RPC_BIND_PORT_ARGUMENT); cmdCopy.add(RPC_BIND_PORT_ARGUMENT);
cmdCopy.add("" + port); cmdCopy.add("" + port);
MoneroWalletRpc walletRpc = new MoneroWalletRpc(cmdCopy); // start monero-wallet-rpc process MoneroWalletRpc walletRpc = new MoneroWalletRpc(cmdCopy); // start monero-wallet-rpc process
synchronized (registeredPorts) { synchronized (registeredPorts) {
registeredPorts.put(port, walletRpc); registeredPorts.put(port, walletRpc);
} }
return walletRpc; return walletRpc;
} catch (Exception e) { } catch (Exception e) {
if (numAttempts >= NUM_ALLOWED_ATTEMPTS) { if (numAttempts >= NUM_ALLOWED_ATTEMPTS) {
log.error("Unable to start monero-wallet-rpc instance after {} attempts", NUM_ALLOWED_ATTEMPTS); log.error("Unable to start monero-wallet-rpc instance after {} attempts", NUM_ALLOWED_ATTEMPTS);
throw e; throw e;
} }
} }
} }
throw new MoneroError("Failed to start monero-wallet-rpc instance after " + NUM_ALLOWED_ATTEMPTS + " attempts"); // should never reach here throw new MoneroError("Failed to start monero-wallet-rpc instance after " + NUM_ALLOWED_ATTEMPTS + " attempts"); // should never reach here
} }
} catch (IOException e) { } catch (IOException e) {
throw new MoneroError(e); throw new MoneroError(e);
} }
} }
/** /**
* Stop an instance of monero-wallet-rpc. * Stop an instance of monero-wallet-rpc.
* *
* @param walletRpc the client connected to the monero-wallet-rpc instance to stop * @param walletRpc the client connected to the monero-wallet-rpc instance to stop
*/ * @param path the wallet path, since the wallet might be closed
public void stopInstance(MoneroWalletRpc walletRpc) { * @param force specifies if the process should be forcibly destroyed
*/
public void stopInstance(MoneroWalletRpc walletRpc, String path, boolean force) {
// unregister port // unregister port
int port = -1; int port = unregisterPort(walletRpc);
synchronized (registeredPorts) {
boolean found = false;
for (Map.Entry<Integer, MoneroWalletRpc> entry : registeredPorts.entrySet()) {
if (walletRpc == entry.getValue()) {
found = true;
try {
port = entry.getKey();
unregisterPort(port);
} catch (Exception e) {
throw new MoneroError(e);
}
break;
}
}
if (!found) throw new RuntimeException("MoneroWalletRpc instance not registered with a port");
}
// stop process // stop process
String pid = walletRpc.getProcess() == null ? null : String.valueOf(walletRpc.getProcess().pid()); String pid = walletRpc.getProcess() == null ? null : String.valueOf(walletRpc.getProcess().pid());
log.info("Stopping MoneroWalletRpc path={}, port={}, pid={}", walletRpc.getPath(), port, pid); log.info("Stopping MoneroWalletRpc path={}, port={}, pid={}", path, port, pid);
walletRpc.stopProcess(); walletRpc.stopProcess(force);
} }
private int registerNextPort() throws IOException { private int registerNextPort() throws IOException {
synchronized (registeredPorts) { synchronized (registeredPorts) {
int port = startPort; int port = startPort;
while (registeredPorts.containsKey(port)) port++; while (registeredPorts.containsKey(port)) port++;
registeredPorts.put(port, null); registeredPorts.put(port, null);
return port; return port;
} }
} }
private void unregisterPort(int port) { private int unregisterPort(MoneroWalletRpc walletRpc) {
synchronized (registeredPorts) { synchronized (registeredPorts) {
registeredPorts.remove(port); int port = -1;
} boolean found = false;
} for (Map.Entry<Integer, MoneroWalletRpc> entry : registeredPorts.entrySet()) {
if (walletRpc == entry.getValue()) {
found = true;
try {
port = entry.getKey();
removePort(port);
} catch (Exception e) {
throw new MoneroError(e);
}
break;
}
}
if (!found) throw new RuntimeException("MoneroWalletRpc instance not registered with a port");
return port;
}
}
private void removePort(int port) {
synchronized (registeredPorts) {
registeredPorts.remove(port);
}
}
} }

View File

@ -117,7 +117,9 @@ public class MoneroKeyImagePoller {
* @return the key images to listen to * @return the key images to listen to
*/ */
public Collection<String> getKeyImages() { public Collection<String> getKeyImages() {
return new ArrayList<String>(keyImages); synchronized (keyImages) {
return new ArrayList<String>(keyImages);
}
} }
/** /**
@ -197,6 +199,13 @@ public class MoneroKeyImagePoller {
} }
} }
/**
* Clear the key images which stops polling.
*/
public void clearKeyImages() {
setKeyImages();
}
/** /**
* Indicates if the given key image is spent. * Indicates if the given key image is spent.
* *
@ -215,37 +224,45 @@ public class MoneroKeyImagePoller {
log.warn("Cannot poll key images because daemon is null"); log.warn("Cannot poll key images because daemon is null");
return; return;
} }
// get copy of key images to fetch
List<String> keyImages = new ArrayList<String>(getKeyImages());
// fetch spent statuses
List<MoneroKeyImageSpentStatus> spentStatuses = null;
try { try {
if (keyImages.isEmpty()) spentStatuses = new ArrayList<MoneroKeyImageSpentStatus>();
// fetch spent statuses else {
List<MoneroKeyImageSpentStatus> spentStatuses = keyImages.isEmpty() ? new ArrayList<MoneroKeyImageSpentStatus>() : daemon.getKeyImageSpentStatuses(keyImages); spentStatuses = daemon.getKeyImageSpentStatuses(keyImages); // TODO monero-java: if order of getKeyImageSpentStatuses is guaranteed, then it should take list parameter
// collect changed statuses
Map<String, MoneroKeyImageSpentStatus> changedStatuses = new HashMap<String, MoneroKeyImageSpentStatus>();
synchronized (lastStatuses) {
synchronized (keyImages) {
for (int i = 0; i < keyImages.size(); i++) {
if (lastStatuses.get(keyImages.get(i)) != spentStatuses.get(i)) {
lastStatuses.put(keyImages.get(i), spentStatuses.get(i));
changedStatuses.put(keyImages.get(i), spentStatuses.get(i));
}
}
}
}
// announce changes
if (!changedStatuses.isEmpty()) {
for (MoneroKeyImageListener listener : new ArrayList<MoneroKeyImageListener>(listeners)) {
listener.onSpentStatusChanged(changedStatuses);
}
} }
} catch (Exception e) { } catch (Exception e) {
log.warn("Error polling key images: " + e.getMessage()); log.warn("Error polling spent status of key images: " + e.getMessage());
return;
}
// collect changed statuses
Map<String, MoneroKeyImageSpentStatus> changedStatuses = new HashMap<String, MoneroKeyImageSpentStatus>();
synchronized (lastStatuses) {
for (int i = 0; i < spentStatuses.size(); i++) {
if (spentStatuses.get(i) != lastStatuses.get(keyImages.get(i))) {
lastStatuses.put(keyImages.get(i), spentStatuses.get(i));
changedStatuses.put(keyImages.get(i), spentStatuses.get(i));
}
}
}
// announce changes
if (!changedStatuses.isEmpty()) {
for (MoneroKeyImageListener listener : new ArrayList<MoneroKeyImageListener>(listeners)) {
listener.onSpentStatusChanged(changedStatuses);
}
} }
} }
private void refreshPolling() { private void refreshPolling() {
setIsPolling(keyImages.size() > 0 && listeners.size() > 0); synchronized (keyImages) {
setIsPolling(keyImages.size() > 0 && listeners.size() > 0);
}
} }
private synchronized void setIsPolling(boolean enabled) { private synchronized void setIsPolling(boolean enabled) {

View File

@ -2,6 +2,8 @@ package haveno.core.xmr.wallet;
import com.google.common.util.concurrent.Service.State; import com.google.common.util.concurrent.Service.State;
import com.google.inject.name.Named; import com.google.inject.name.Named;
import common.utils.GenUtils;
import common.utils.JsonUtils; import common.utils.JsonUtils;
import haveno.common.UserThread; import haveno.common.UserThread;
import haveno.common.config.BaseCurrencyNetwork; import haveno.common.config.BaseCurrencyNetwork;
@ -12,7 +14,6 @@ import haveno.common.util.Utilities;
import haveno.core.api.AccountServiceListener; import haveno.core.api.AccountServiceListener;
import haveno.core.api.CoreAccountService; import haveno.core.api.CoreAccountService;
import haveno.core.api.CoreMoneroConnectionsService; import haveno.core.api.CoreMoneroConnectionsService;
import haveno.core.app.HavenoSetup;
import haveno.core.offer.Offer; import haveno.core.offer.Offer;
import haveno.core.trade.BuyerTrade; import haveno.core.trade.BuyerTrade;
import haveno.core.trade.HavenoUtils; import haveno.core.trade.HavenoUtils;
@ -41,6 +42,7 @@ import monero.wallet.model.MoneroDestination;
import monero.wallet.model.MoneroOutputQuery; import monero.wallet.model.MoneroOutputQuery;
import monero.wallet.model.MoneroOutputWallet; import monero.wallet.model.MoneroOutputWallet;
import monero.wallet.model.MoneroSubaddress; import monero.wallet.model.MoneroSubaddress;
import monero.wallet.model.MoneroSyncResult;
import monero.wallet.model.MoneroTransferQuery; import monero.wallet.model.MoneroTransferQuery;
import monero.wallet.model.MoneroTxConfig; import monero.wallet.model.MoneroTxConfig;
import monero.wallet.model.MoneroTxQuery; import monero.wallet.model.MoneroTxQuery;
@ -48,6 +50,8 @@ import monero.wallet.model.MoneroTxWallet;
import monero.wallet.model.MoneroWalletConfig; import monero.wallet.model.MoneroWalletConfig;
import monero.wallet.model.MoneroWalletListener; import monero.wallet.model.MoneroWalletListener;
import monero.wallet.model.MoneroWalletListenerI; import monero.wallet.model.MoneroWalletListenerI;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -64,7 +68,11 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -88,6 +96,8 @@ public class XmrWalletService {
private static final double SECURITY_DEPOSIT_TOLERANCE = Config.baseCurrencyNetwork() == BaseCurrencyNetwork.XMR_LOCAL ? 0.25 : 0.05; // security deposit can absorb miner fee up to percent private static final double SECURITY_DEPOSIT_TOLERANCE = Config.baseCurrencyNetwork() == BaseCurrencyNetwork.XMR_LOCAL ? 0.25 : 0.05; // security deposit can absorb miner fee up to percent
private static final double DUST_TOLERANCE = 0.01; // max dust as percent of mining fee private static final double DUST_TOLERANCE = 0.01; // max dust as percent of mining fee
private static final int NUM_MAX_BACKUP_WALLETS = 10; private static final int NUM_MAX_BACKUP_WALLETS = 10;
private static final int MONERO_LOG_LEVEL = 0;
private static final boolean PRINT_STACK_TRACE = false;
private final CoreAccountService accountService; private final CoreAccountService accountService;
private final CoreMoneroConnectionsService connectionsService; private final CoreMoneroConnectionsService connectionsService;
@ -103,9 +113,8 @@ public class XmrWalletService {
private TradeManager tradeManager; private TradeManager tradeManager;
private MoneroWalletRpc wallet; private MoneroWalletRpc wallet;
private final Map<String, Optional<MoneroTx>> txCache = new HashMap<String, Optional<MoneroTx>>(); private final Map<String, Optional<MoneroTx>> txCache = new HashMap<String, Optional<MoneroTx>>();
private boolean isShutDown = false; private boolean isShutDownStarted = false;
private ExecutorService syncWalletThreadPool = Executors.newFixedThreadPool(10); // TODO: adjust based on connection type
private HavenoSetup havenoSetup;
@Inject @Inject
XmrWalletService(CoreAccountService accountService, XmrWalletService(CoreAccountService accountService,
@ -122,6 +131,9 @@ public class XmrWalletService {
this.rpcBindPort = rpcBindPort; this.rpcBindPort = rpcBindPort;
this.xmrWalletFile = new File(walletDir, MONERO_WALLET_NAME); this.xmrWalletFile = new File(walletDir, MONERO_WALLET_NAME);
// set monero logging
MoneroUtils.setLogLevel(MONERO_LOG_LEVEL);
// initialize after account open and basic setup // initialize after account open and basic setup
walletsSetup.addSetupTaskHandler(() -> { // TODO: use something better than legacy WalletSetup for notification to initialize walletsSetup.addSetupTaskHandler(() -> { // TODO: use something better than legacy WalletSetup for notification to initialize
@ -210,7 +222,7 @@ public class XmrWalletService {
public MoneroWalletRpc createWallet(String walletName) { public MoneroWalletRpc createWallet(String walletName) {
log.info("{}.createWallet({})", getClass().getSimpleName(), walletName); log.info("{}.createWallet({})", getClass().getSimpleName(), walletName);
if (isShutDown) throw new IllegalStateException("Cannot create wallet because shutting down"); if (isShutDownStarted) throw new IllegalStateException("Cannot create wallet because shutting down");
return createWalletRpc(new MoneroWalletConfig() return createWalletRpc(new MoneroWalletConfig()
.setPath(walletName) .setPath(walletName)
.setPassword(getWalletPassword()), .setPassword(getWalletPassword()),
@ -219,13 +231,26 @@ public class XmrWalletService {
public MoneroWalletRpc openWallet(String walletName) { public MoneroWalletRpc openWallet(String walletName) {
log.info("{}.openWallet({})", getClass().getSimpleName(), walletName); log.info("{}.openWallet({})", getClass().getSimpleName(), walletName);
if (isShutDown) throw new IllegalStateException("Cannot open wallet because shutting down"); if (isShutDownStarted) throw new IllegalStateException("Cannot open wallet because shutting down");
return openWalletRpc(new MoneroWalletConfig() return openWalletRpc(new MoneroWalletConfig()
.setPath(walletName) .setPath(walletName)
.setPassword(getWalletPassword()), .setPassword(getWalletPassword()),
null); null);
} }
/**
* Sync the given wallet in a thread pool with other wallets.
*/
public MoneroSyncResult syncWallet(MoneroWallet wallet) {
Callable<MoneroSyncResult> task = () -> wallet.sync();
Future<MoneroSyncResult> future = syncWalletThreadPool.submit(task);
try {
return future.get();
} catch (Exception e) {
throw new MoneroError(e.getMessage());
}
}
public void saveWallet(MoneroWallet wallet, boolean backup) { public void saveWallet(MoneroWallet wallet, boolean backup) {
wallet.save(); wallet.save();
if (backup) backupWallet(wallet.getPath()); if (backup) backupWallet(wallet.getPath());
@ -234,17 +259,25 @@ public class XmrWalletService {
public void closeWallet(MoneroWallet wallet, boolean save) { public void closeWallet(MoneroWallet wallet, boolean save) {
log.info("{}.closeWallet({}, {})", getClass().getSimpleName(), wallet.getPath(), save); log.info("{}.closeWallet({}, {})", getClass().getSimpleName(), wallet.getPath(), save);
MoneroError err = null; MoneroError err = null;
String path = wallet.getPath();
try { try {
String path = wallet.getPath();
wallet.close(save); wallet.close(save);
if (save) backupWallet(path); if (save) backupWallet(path);
} catch (MoneroError e) { } catch (MoneroError e) {
err = e; err = e;
} }
MONERO_WALLET_RPC_MANAGER.stopInstance((MoneroWalletRpc) wallet); stopWallet(wallet, path);
if (err != null) throw err; if (err != null) throw err;
} }
public void stopWallet(MoneroWallet wallet, String path) {
stopWallet(wallet, path, false);
}
public void stopWallet(MoneroWallet wallet, String path, boolean force) {
MONERO_WALLET_RPC_MANAGER.stopInstance((MoneroWalletRpc) wallet, path, force);
}
public void deleteWallet(String walletName) { public void deleteWallet(String walletName) {
log.info("{}.deleteWallet({})", getClass().getSimpleName(), walletName); log.info("{}.deleteWallet({})", getClass().getSimpleName(), walletName);
if (!walletExists(walletName)) throw new Error("Wallet does not exist at path: " + walletName); if (!walletExists(walletName)) throw new Error("Wallet does not exist at path: " + walletName);
@ -506,7 +539,7 @@ public class XmrWalletService {
synchronized (txCache) { synchronized (txCache) {
for (MoneroTx tx : txs) txCache.remove(tx.getHash()); for (MoneroTx tx : txs) txCache.remove(tx.getHash());
} }
}, connectionsService.getDefaultRefreshPeriodMs() / 1000); }, connectionsService.getRefreshPeriodMs() / 1000);
return txs; return txs;
} }
} }
@ -518,33 +551,50 @@ public class XmrWalletService {
public List<MoneroTx> getTxsWithCache(List<String> txHashes) { public List<MoneroTx> getTxsWithCache(List<String> txHashes) {
synchronized (txCache) { synchronized (txCache) {
try {
// get cached txs
List<MoneroTx> cachedTxs = new ArrayList<MoneroTx>();
List<String> uncachedTxHashes = new ArrayList<String>();
for (int i = 0; i < txHashes.size(); i++) {
if (txCache.containsKey(txHashes.get(i))) cachedTxs.add(txCache.get(txHashes.get(i)).orElse(null));
else uncachedTxHashes.add(txHashes.get(i));
}
// get cached txs // return txs from cache if available, otherwise fetch
List<MoneroTx> cachedTxs = new ArrayList<MoneroTx>(); return uncachedTxHashes.isEmpty() ? cachedTxs : getTxs(txHashes);
List<String> uncachedTxHashes = new ArrayList<String>(); } catch (Exception e) {
for (int i = 0; i < txHashes.size(); i++) { if (!isShutDownStarted) throw e;
if (txCache.containsKey(txHashes.get(i))) cachedTxs.add(txCache.get(txHashes.get(i)).orElse(null)); return null;
else uncachedTxHashes.add(txHashes.get(i));
} }
// return txs from cache if available, otherwise fetch
return uncachedTxHashes.isEmpty() ? cachedTxs : getTxs(txHashes);
} }
} }
private void closeMainWallet(boolean save) { public void onShutDownStarted() {
try { log.info("XmrWalletService.onShutDownStarted()");
closeWallet(wallet, true); this.isShutDownStarted = true;
wallet = null;
walletListeners.clear(); // remove listeners which stops polling wallet
} catch (Exception e) { // TODO monero-java: wallet.stopPolling()?
log.warn("Error closing main monero-wallet-rpc subprocess. Was Haveno stopped manually with ctrl+c?"); if (wallet != null) {
for (MoneroWalletListenerI listener : new HashSet<>(wallet.getListeners())) {
wallet.removeListener(listener);
}
} }
// prepare trades for shut down
if (tradeManager != null) tradeManager.onShutDownStarted();
} }
public void shutDown(boolean save) { public void shutDown() {
this.isShutDown = true; log.info("Shutting down {}", getClass().getSimpleName());
closeMainWallet(save);
// shut down trade and main wallets at same time
walletListeners.clear();
List<Runnable> tasks = new ArrayList<Runnable>();
if (tradeManager != null) tasks.add(() -> tradeManager.shutDown());
tasks.add(() -> closeMainWallet(true));
HavenoUtils.executeTasks(tasks);
log.info("Done shutting down all wallets");
} }
// ------------------------------ PRIVATE HELPERS ------------------------- // ------------------------------ PRIVATE HELPERS -------------------------
@ -555,48 +605,53 @@ public class XmrWalletService {
maybeInitMainWallet(); maybeInitMainWallet();
// set and listen to daemon connection // set and listen to daemon connection
connectionsService.addListener(newConnection -> setDaemonConnection(newConnection)); connectionsService.addListener(newConnection -> onConnectionChanged(newConnection));
} }
private void maybeInitMainWallet() { private synchronized void maybeInitMainWallet() {
if (wallet != null) throw new RuntimeException("Main wallet is already initialized");
// open or create wallet // open or create wallet main wallet
MoneroDaemonRpc daemon = connectionsService.getDaemon(); if (wallet == null) {
log.info("Initializing main wallet with " + (daemon == null ? "daemon: null" : "monerod uri=" + daemon.getRpcConnection().getUri())); MoneroDaemonRpc daemon = connectionsService.getDaemon();
MoneroWalletConfig walletConfig = new MoneroWalletConfig().setPath(MONERO_WALLET_NAME).setPassword(getWalletPassword()); log.info("Initializing main wallet with monerod=" + (daemon == null ? "null" : daemon.getRpcConnection().getUri()));
if (MoneroUtils.walletExists(xmrWalletFile.getPath())) { MoneroWalletConfig walletConfig = new MoneroWalletConfig().setPath(MONERO_WALLET_NAME).setPassword(getWalletPassword());
wallet = openWalletRpc(walletConfig, rpcBindPort); if (MoneroUtils.walletExists(xmrWalletFile.getPath())) {
} else if (connectionsService.getConnection() != null && Boolean.TRUE.equals(connectionsService.getConnection().isConnected())) { wallet = openWalletRpc(walletConfig, rpcBindPort);
wallet = createWalletRpc(walletConfig, rpcBindPort); } else if (connectionsService.getConnection() != null && Boolean.TRUE.equals(connectionsService.getConnection().isConnected())) {
wallet = createWalletRpc(walletConfig, rpcBindPort);
}
} }
// handle when wallet initialized and synced // sync wallet if open
if (wallet != null) { if (wallet != null) {
log.info("Monero wallet uri={}, path={}", wallet.getRpcConnection().getUri(), wallet.getPath()); log.info("Monero wallet uri={}, path={}", wallet.getRpcConnection().getUri(), wallet.getPath());
try { while (!HavenoUtils.havenoSetup.getWalletInitialized().get()) {
try {
// sync main wallet // sync main wallet
log.info("Syncing main wallet"); log.info("Syncing main wallet");
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
wallet.sync(); // blocking wallet.sync(); // blocking
log.info("Done syncing main wallet in " + (System.currentTimeMillis() - time) + " ms"); log.info("Done syncing main wallet in " + (System.currentTimeMillis() - time) + " ms");
wallet.startSyncing(connectionsService.getDefaultRefreshPeriodMs()); wallet.startSyncing(connectionsService.getRefreshPeriodMs());
if (getMoneroNetworkType() != MoneroNetworkType.MAINNET) log.info("Monero wallet balance={}, unlocked balance={}", wallet.getBalance(0), wallet.getUnlockedBalance(0)); if (getMoneroNetworkType() != MoneroNetworkType.MAINNET) log.info("Monero wallet balance={}, unlocked balance={}", wallet.getBalance(0), wallet.getUnlockedBalance(0));
// TODO: using this to signify both daemon and wallet synced, refactor sync handling of both // TODO: using this to signify both daemon and wallet synced, use separate sync handlers
connectionsService.doneDownload(); connectionsService.doneDownload();
// save but skip backup on initialization // notify setup that main wallet is initialized
saveMainWallet(false); // 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
} catch (Exception e) { // 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'
log.warn("Error syncing main wallet: {}", e.getMessage()); HavenoUtils.havenoSetup.getWalletInitialized().set(true);
// save but skip backup on initialization
saveMainWallet(false);
} catch (Exception e) {
log.warn("Error syncing main wallet: {}. Trying again in {} seconds", e.getMessage(), connectionsService.getRefreshPeriodMs() / 1000);
GenUtils.waitFor(connectionsService.getRefreshPeriodMs());
}
} }
// notify setup that main wallet is initialized
// TODO: move to try..catch? refactor startup to call this and sync off main thread?
havenoSetup.getWalletInitialized().set(true); // TODO: change to listener pattern
// register internal listener to notify external listeners // register internal listener to notify external listeners
wallet.addListener(new XmrWalletListener()); wallet.addListener(new XmrWalletListener());
} }
@ -610,6 +665,7 @@ public class XmrWalletService {
// start monero-wallet-rpc instance // start monero-wallet-rpc instance
MoneroWalletRpc walletRpc = startWalletRpcInstance(port); MoneroWalletRpc walletRpc = startWalletRpcInstance(port);
walletRpc.getRpcConnection().setPrintStackTrace(PRINT_STACK_TRACE);
// create wallet // create wallet
try { try {
@ -621,11 +677,12 @@ public class XmrWalletService {
log.info("Creating wallet " + config.getPath() + " connected to daemon " + connection.getUri()); log.info("Creating wallet " + config.getPath() + " connected to daemon " + connection.getUri());
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
walletRpc.createWallet(config.setServer(connection)); walletRpc.createWallet(config.setServer(connection));
walletRpc.getDaemonConnection().setPrintStackTrace(PRINT_STACK_TRACE);
log.info("Done creating wallet " + config.getPath() + " in " + (System.currentTimeMillis() - time) + " ms"); log.info("Done creating wallet " + config.getPath() + " in " + (System.currentTimeMillis() - time) + " ms");
return walletRpc; return walletRpc;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
MONERO_WALLET_RPC_MANAGER.stopInstance(walletRpc); stopWallet(walletRpc, config.getPath());
throw e; throw e;
} }
} }
@ -634,6 +691,7 @@ public class XmrWalletService {
// start monero-wallet-rpc instance // start monero-wallet-rpc instance
MoneroWalletRpc walletRpc = startWalletRpcInstance(port); MoneroWalletRpc walletRpc = startWalletRpcInstance(port);
walletRpc.getRpcConnection().setPrintStackTrace(PRINT_STACK_TRACE);
// open wallet // open wallet
try { try {
@ -644,11 +702,12 @@ public class XmrWalletService {
// open wallet // open wallet
log.info("Opening wallet " + config.getPath()); log.info("Opening wallet " + config.getPath());
walletRpc.openWallet(config.setServer(connectionsService.getConnection())); walletRpc.openWallet(config.setServer(connectionsService.getConnection()));
if (walletRpc.getDaemonConnection() != null) walletRpc.getDaemonConnection().setPrintStackTrace(PRINT_STACK_TRACE);
log.info("Done opening wallet " + config.getPath()); log.info("Done opening wallet " + config.getPath());
return walletRpc; return walletRpc;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
MONERO_WALLET_RPC_MANAGER.stopInstance(walletRpc); stopWallet(walletRpc, config.getPath());
throw e; throw e;
} }
} }
@ -693,15 +752,23 @@ public class XmrWalletService {
return MONERO_WALLET_RPC_MANAGER.startInstance(cmd); return MONERO_WALLET_RPC_MANAGER.startInstance(cmd);
} }
// TODO: monero-wallet-rpc needs restarted if applying tor proxy private void onConnectionChanged(MoneroRpcConnection connection) {
private void setDaemonConnection(MoneroRpcConnection connection) { if (isShutDownStarted) return;
if (isShutDown) return; if (wallet != null && HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) return;
log.info("Setting wallet daemon connection: " + (connection == null ? null : connection.getUri()));
log.info("Setting main wallet daemon connection: " + (connection == null ? null : connection.getUri()));
String oldProxyUri = wallet == null || wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getProxyUri();
String newProxyUri = connection == null ? null : connection.getProxyUri();
if (wallet == null) maybeInitMainWallet(); if (wallet == null) maybeInitMainWallet();
else { else if (wallet instanceof MoneroWalletRpc && !StringUtils.equals(oldProxyUri, newProxyUri)) {
log.info("Restarting main wallet since proxy URI has changed");
closeMainWallet(true);
maybeInitMainWallet();
} else {
wallet.setDaemonConnection(connection); wallet.setDaemonConnection(connection);
if (connection != null) wallet.getDaemonConnection().setPrintStackTrace(PRINT_STACK_TRACE);
if (connection != null && !Boolean.FALSE.equals(connection.isConnected())) { if (connection != null && !Boolean.FALSE.equals(connection.isConnected())) {
wallet.startSyncing(connectionsService.getDefaultRefreshPeriodMs()); wallet.startSyncing(connectionsService.getRefreshPeriodMs());
new Thread(() -> { new Thread(() -> {
try { try {
wallet.sync(); wallet.sync();
@ -711,6 +778,8 @@ public class XmrWalletService {
}).start(); }).start();
} }
} }
log.info("Done setting main wallet daemon connection: " + (connection == null ? null : connection.getUri()));
} }
private void notifyBalanceListeners() { private void notifyBalanceListeners() {
@ -755,6 +824,17 @@ public class XmrWalletService {
HavenoUtils.executeTasks(tasks, Math.min(10, 1 + trades.size())); HavenoUtils.executeTasks(tasks, Math.min(10, 1 + trades.size()));
} }
private void closeMainWallet(boolean save) {
try {
if (wallet != null) {
closeWallet(wallet, true);
wallet = null;
}
} catch (Exception e) {
log.warn("Error closing main monero-wallet-rpc subprocess: " + e.getMessage() + ". Was Haveno stopped manually with ctrl+c?");
}
}
// ----------------------------- LEGACY APP ------------------------------- // ----------------------------- LEGACY APP -------------------------------
public synchronized XmrAddressEntry getNewAddressEntry() { public synchronized XmrAddressEntry getNewAddressEntry() {
@ -764,7 +844,7 @@ public class XmrWalletService {
public synchronized XmrAddressEntry getNewAddressEntry(String offerId, XmrAddressEntry.Context context) { public synchronized XmrAddressEntry getNewAddressEntry(String offerId, XmrAddressEntry.Context context) {
// try to use available and not yet used entries // try to use available and not yet used entries
List<MoneroTxWallet> incomingTxs = getIncomingTxs(null); // pre-fetch all incoming txs to avoid query per subaddress List<MoneroTxWallet> incomingTxs = getIncomingTxs(); // prefetch all incoming txs to avoid query per subaddress
Optional<XmrAddressEntry> emptyAvailableAddressEntry = getAddressEntryListAsImmutableList().stream().filter(e -> XmrAddressEntry.Context.AVAILABLE == e.getContext()).filter(e -> isSubaddressUnused(e.getSubaddressIndex(), incomingTxs)).findAny(); Optional<XmrAddressEntry> emptyAvailableAddressEntry = getAddressEntryListAsImmutableList().stream().filter(e -> XmrAddressEntry.Context.AVAILABLE == e.getContext()).filter(e -> isSubaddressUnused(e.getSubaddressIndex(), incomingTxs)).findAny();
if (emptyAvailableAddressEntry.isPresent()) return xmrAddressEntryList.swapAvailableToAddressEntryWithOfferId(emptyAvailableAddressEntry.get(), context, offerId); if (emptyAvailableAddressEntry.isPresent()) return xmrAddressEntryList.swapAvailableToAddressEntryWithOfferId(emptyAvailableAddressEntry.get(), context, offerId);
@ -820,7 +900,6 @@ public class XmrWalletService {
log.info("resetAddressEntriesForOpenOffer offerId={}", offerId); log.info("resetAddressEntriesForOpenOffer offerId={}", offerId);
swapTradeEntryToAvailableEntry(offerId, XmrAddressEntry.Context.OFFER_FUNDING); swapTradeEntryToAvailableEntry(offerId, XmrAddressEntry.Context.OFFER_FUNDING);
swapTradeEntryToAvailableEntry(offerId, XmrAddressEntry.Context.RESERVED_FOR_TRADE); swapTradeEntryToAvailableEntry(offerId, XmrAddressEntry.Context.RESERVED_FOR_TRADE);
swapTradeEntryToAvailableEntry(offerId, XmrAddressEntry.Context.TRADE_PAYOUT);
} }
public synchronized void resetAddressEntriesForPendingTrade(String offerId) { public synchronized void resetAddressEntriesForPendingTrade(String offerId) {
@ -881,8 +960,9 @@ public class XmrWalletService {
} }
public List<XmrAddressEntry> getUnusedAddressEntries() { public List<XmrAddressEntry> getUnusedAddressEntries() {
List<MoneroTxWallet> incomingTxs = getIncomingTxs(); // prefetch all incoming txs to avoid query per subaddress
return getAvailableAddressEntries().stream() return getAvailableAddressEntries().stream()
.filter(e -> isSubaddressUnused(e.getSubaddressIndex())) .filter(e -> isSubaddressUnused(e.getSubaddressIndex(), incomingTxs))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -997,10 +1077,6 @@ public class XmrWalletService {
log.info("\n" + tracePrefix + ":" + sb.toString()); log.info("\n" + tracePrefix + ":" + sb.toString());
} }
public void setHavenoSetup(HavenoSetup havenoSetup) {
this.havenoSetup = havenoSetup;
}
// -------------------------------- HELPERS ------------------------------- // -------------------------------- HELPERS -------------------------------
/** /**

View File

@ -54,11 +54,11 @@ public class ArbitratorManagerTest {
add("es"); add("es");
}}; }};
Arbitrator one = new Arbitrator(new NodeAddress("arbitrator:1"), null, null, Arbitrator one = new Arbitrator(new NodeAddress("arbitrator:1"), null,
languagesOne, 0L, null, "", null, languagesOne, 0L, null, "", null,
null, null); null, null);
Arbitrator two = new Arbitrator(new NodeAddress("arbitrator:2"), null, null, Arbitrator two = new Arbitrator(new NodeAddress("arbitrator:2"), null,
languagesTwo, 0L, null, "", null, languagesTwo, 0L, null, "", null,
null, null); null, null);
@ -90,11 +90,11 @@ public class ArbitratorManagerTest {
add("es"); add("es");
}}; }};
Arbitrator one = new Arbitrator(new NodeAddress("arbitrator:1"), null, null, Arbitrator one = new Arbitrator(new NodeAddress("arbitrator:1"), null,
languagesOne, 0L, null, "", null, languagesOne, 0L, null, "", null,
null, null); null, null);
Arbitrator two = new Arbitrator(new NodeAddress("arbitrator:2"), null, null, Arbitrator two = new Arbitrator(new NodeAddress("arbitrator:2"), null,
languagesTwo, 0L, null, "", null, languagesTwo, 0L, null, "", null,
null, null); null, null);

View File

@ -39,7 +39,6 @@ public class ArbitratorTest {
public static Arbitrator getArbitratorMock() { public static Arbitrator getArbitratorMock() {
return new Arbitrator(new NodeAddress("host", 1000), return new Arbitrator(new NodeAddress("host", 1000),
"xmraddress",
new PubKeyRing(getBytes(100), getBytes(100)), new PubKeyRing(getBytes(100), getBytes(100)),
Lists.newArrayList(), Lists.newArrayList(),
new Date().getTime(), new Date().getTime(),

View File

@ -45,7 +45,6 @@ public class ArbitratorRegistrationViewModel extends AgentRegistrationViewModel<
String emailAddress) { String emailAddress) {
return new Arbitrator( return new Arbitrator(
p2PService.getAddress(), p2PService.getAddress(),
xmrWalletService.getWallet().getPrimaryAddress(), // TODO: how is arbitrator address used?
keyRing.getPubKeyRing(), keyRing.getPubKeyRing(),
new ArrayList<>(languageCodes), new ArrayList<>(languageCodes),
new Date().getTime(), new Date().getTime(),

View File

@ -19,6 +19,8 @@ package haveno.desktop.main.funds.deposit;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
import common.types.Filter;
import haveno.core.locale.Res; import haveno.core.locale.Res;
import haveno.core.trade.HavenoUtils; import haveno.core.trade.HavenoUtils;
import haveno.core.util.coin.CoinFormatter; import haveno.core.util.coin.CoinFormatter;
@ -85,7 +87,7 @@ class DepositListItem {
tooltip = new Tooltip(Res.get("shared.notUsedYet")); tooltip = new Tooltip(Res.get("shared.notUsedYet"));
txConfidenceIndicator.setProgress(0); txConfidenceIndicator.setProgress(0);
txConfidenceIndicator.setTooltip(tooltip); txConfidenceIndicator.setTooltip(tooltip);
MoneroTx tx = getTxWithFewestConfirmations(); MoneroTx tx = getTxWithFewestConfirmations(cachedTxs);
if (tx == null) { if (tx == null) {
txConfidenceIndicator.setVisible(false); txConfidenceIndicator.setVisible(false);
} else { } else {
@ -132,20 +134,20 @@ class DepositListItem {
return numTxOutputs; return numTxOutputs;
} }
public long getNumConfirmationsSinceFirstUsed() { public long getNumConfirmationsSinceFirstUsed(List<MoneroTxWallet> incomingTxs) {
MoneroTx tx = getTxWithFewestConfirmations(); MoneroTx tx = getTxWithFewestConfirmations(incomingTxs);
return tx == null ? 0 : tx.getNumConfirmations(); return tx == null ? 0 : tx.getNumConfirmations();
} }
private MoneroTxWallet getTxWithFewestConfirmations() { private MoneroTxWallet getTxWithFewestConfirmations(List<MoneroTxWallet> incomingTxs) {
// get txs with incoming transfers to subaddress // get txs with incoming transfers to subaddress
List<MoneroTxWallet> txs = xmrWalletService.getWallet() MoneroTxQuery query = new MoneroTxQuery()
.getTxs(new MoneroTxQuery() .setTransferQuery(new MoneroTransferQuery()
.setTransferQuery(new MoneroTransferQuery() .setIsIncoming(true)
.setIsIncoming(true) .setSubaddressIndex(addressEntry.getSubaddressIndex()));
.setSubaddressIndex(addressEntry.getSubaddressIndex()))); List<MoneroTxWallet> txs = incomingTxs == null ? xmrWalletService.getWallet().getTxs(query) : Filter.apply(query, incomingTxs);
// get tx with fewest confirmations // get tx with fewest confirmations
MoneroTxWallet highestTx = null; MoneroTxWallet highestTx = null;
for (MoneroTxWallet tx : txs) if (highestTx == null || tx.getNumConfirmations() < highestTx.getNumConfirmations()) highestTx = tx; for (MoneroTxWallet tx : txs) if (highestTx == null || tx.getNumConfirmations() < highestTx.getNumConfirmations()) highestTx = tx;

View File

@ -147,9 +147,12 @@ public class DepositView extends ActivatableView<VBox, Void> {
setUsageColumnCellFactory(); setUsageColumnCellFactory();
setConfidenceColumnCellFactory(); setConfidenceColumnCellFactory();
// prefetch all incoming txs to avoid query per subaddress
List<MoneroTxWallet> incomingTxs = xmrWalletService.getIncomingTxs();
addressColumn.setComparator(Comparator.comparing(DepositListItem::getAddressString)); addressColumn.setComparator(Comparator.comparing(DepositListItem::getAddressString));
balanceColumn.setComparator(Comparator.comparing(DepositListItem::getBalanceAsBI)); balanceColumn.setComparator(Comparator.comparing(DepositListItem::getBalanceAsBI));
confirmationsColumn.setComparator(Comparator.comparingLong(o -> o.getNumConfirmationsSinceFirstUsed())); confirmationsColumn.setComparator(Comparator.comparingLong(o -> o.getNumConfirmationsSinceFirstUsed(incomingTxs)));
usageColumn.setComparator(Comparator.comparingInt(DepositListItem::getNumTxOutputs)); usageColumn.setComparator(Comparator.comparingInt(DepositListItem::getNumTxOutputs));
tableView.getSortOrder().add(usageColumn); tableView.getSortOrder().add(usageColumn);
tableView.setItems(sortedList); tableView.setItems(sortedList);

View File

@ -35,7 +35,7 @@ class TransactionAwareOpenOffer implements TransactionAwareTradable {
String txId = transaction.getHash(); String txId = transaction.getHash();
return paymentTxId.equals(txId); return txId.equals(paymentTxId);
} }
public Tradable asTradable() { public Tradable asTradable() {

View File

@ -34,6 +34,7 @@ import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import monero.wallet.model.MoneroIncomingTransfer; import monero.wallet.model.MoneroIncomingTransfer;
import monero.wallet.model.MoneroOutgoingTransfer; import monero.wallet.model.MoneroOutgoingTransfer;
import monero.wallet.model.MoneroTxQuery;
import monero.wallet.model.MoneroTxWallet; import monero.wallet.model.MoneroTxWallet;
import monero.wallet.model.MoneroWalletListener; import monero.wallet.model.MoneroWalletListener;
@ -60,6 +61,8 @@ class TransactionsListItem {
@Getter @Getter
private boolean initialTxConfidenceVisibility = true; private boolean initialTxConfidenceVisibility = true;
private final Supplier<LazyFields> lazyFieldsSupplier; private final Supplier<LazyFields> lazyFieldsSupplier;
private XmrWalletService xmrWalletService;
MoneroWalletListener walletListener;
private static class LazyFields { private static class LazyFields {
TxConfidenceIndicator txConfidenceIndicator; TxConfidenceIndicator txConfidenceIndicator;
@ -82,6 +85,8 @@ class TransactionsListItem {
TransactionAwareTradable transactionAwareTradable) { TransactionAwareTradable transactionAwareTradable) {
this.memo = tx.getNote(); this.memo = tx.getNote();
this.txId = tx.getHash(); this.txId = tx.getHash();
this.xmrWalletService = xmrWalletService;
this.confirmations = tx.getNumConfirmations() == null ? 0 : tx.getNumConfirmations();
Optional<Tradable> optionalTradable = Optional.ofNullable(transactionAwareTradable) Optional<Tradable> optionalTradable = Optional.ofNullable(transactionAwareTradable)
.map(TransactionAwareTradable::asTradable); .map(TransactionAwareTradable::asTradable);
@ -182,18 +187,24 @@ class TransactionsListItem {
}}); }});
// listen for tx updates // listen for tx updates
// TODO: this only listens for new blocks, listen for double spend walletListener = new MoneroWalletListener() {
xmrWalletService.addWalletListener(new MoneroWalletListener() {
@Override @Override
public void onNewBlock(long height) { public void onNewBlock(long height) {
MoneroTxWallet tx = xmrWalletService.getWallet().getTx(txId); MoneroTxWallet tx = xmrWalletService.getWallet().getTxs(new MoneroTxQuery()
.setHash(txId)
.setInTxPool(confirmations > 0 ? false : null)).get(0);
GUIUtil.updateConfidence(tx, lazy().tooltip, lazy().txConfidenceIndicator); GUIUtil.updateConfidence(tx, lazy().tooltip, lazy().txConfidenceIndicator);
confirmations = tx.getNumConfirmations(); confirmations = tx.getNumConfirmations();
} }
}); };
xmrWalletService.addWalletListener(walletListener);
} }
public void cleanup() { public void cleanup() {
if (walletListener != null) {
xmrWalletService.removeWalletListener(walletListener);
walletListener = null;
}
} }
public TxConfidenceIndicator getTxConfidenceIndicator() { public TxConfidenceIndicator getTxConfidenceIndicator() {

View File

@ -67,7 +67,6 @@ import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import monero.wallet.model.MoneroTxWallet; import monero.wallet.model.MoneroTxWallet;
import org.bitcoinj.core.Coin;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Date; import java.util.Date;
@ -727,13 +726,13 @@ public class DisputeSummaryWindow extends Overlay<DisputeSummaryWindow> {
sellerPayoutAmount.equals(sellerSecurityDeposit)) { sellerPayoutAmount.equals(sellerSecurityDeposit)) {
buyerGetsTradeAmountRadioButton.setSelected(true); buyerGetsTradeAmountRadioButton.setSelected(true);
} else if (buyerPayoutAmount.equals(tradeAmount.add(buyerSecurityDeposit).add(sellerSecurityDeposit)) && } else if (buyerPayoutAmount.equals(tradeAmount.add(buyerSecurityDeposit).add(sellerSecurityDeposit)) &&
sellerPayoutAmount.equals(Coin.ZERO)) { sellerPayoutAmount.equals(BigInteger.valueOf(0))) {
buyerGetsAllRadioButton.setSelected(true); buyerGetsAllRadioButton.setSelected(true);
} else if (sellerPayoutAmount.equals(tradeAmount.add(sellerSecurityDeposit)) } else if (sellerPayoutAmount.equals(tradeAmount.add(sellerSecurityDeposit))
&& buyerPayoutAmount.equals(buyerSecurityDeposit)) { && buyerPayoutAmount.equals(buyerSecurityDeposit)) {
sellerGetsTradeAmountRadioButton.setSelected(true); sellerGetsTradeAmountRadioButton.setSelected(true);
} else if (sellerPayoutAmount.equals(tradeAmount.add(buyerSecurityDeposit).add(sellerSecurityDeposit)) } else if (sellerPayoutAmount.equals(tradeAmount.add(buyerSecurityDeposit).add(sellerSecurityDeposit))
&& buyerPayoutAmount.equals(Coin.ZERO)) { && buyerPayoutAmount.equals(BigInteger.valueOf(0))) {
sellerGetsAllRadioButton.setSelected(true); sellerGetsAllRadioButton.setSelected(true);
} else { } else {
customRadioButton.setSelected(true); customRadioButton.setSelected(true);

View File

@ -448,6 +448,8 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
switch (payoutState) { switch (payoutState) {
case PAYOUT_PUBLISHED: case PAYOUT_PUBLISHED:
case PAYOUT_CONFIRMED:
case PAYOUT_UNLOCKED:
sellerState.set(SellerState.STEP4); sellerState.set(SellerState.STEP4);
buyerState.set(BuyerState.STEP4); buyerState.set(BuyerState.STEP4);
break; break;

View File

@ -788,6 +788,14 @@ public class PreferencesView extends ActivatableViewAndModel<GridPane, Preferenc
.show(); .show();
}); });
// We use opposite property (useStandbyMode) in preferences to have the default value (false) set as we want it,
// so users who update gets set avoidStandbyMode=true (useStandbyMode=false)
if (displayStandbyModeFeature) {
avoidStandbyMode.setSelected(!preferences.isUseStandbyMode());
avoidStandbyMode.setOnAction(e -> preferences.setUseStandbyMode(!avoidStandbyMode.isSelected()));
} else {
preferences.setUseStandbyMode(false);
}
} }
private void activateAutoConfirmPreferences() { private void activateAutoConfirmPreferences() {

View File

@ -190,6 +190,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
} }
private void doShutDown() { private void doShutDown() {
if (p2PDataStorage != null) { if (p2PDataStorage != null) {
p2PDataStorage.shutDown(); p2PDataStorage.shutDown();
} }

View File

@ -64,7 +64,6 @@ import javax.inject.Singleton;
import java.security.PublicKey; import java.security.PublicKey;
import java.time.Clock; import java.time.Clock;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
@ -345,11 +344,7 @@ public class MailboxMessageService implements HashMapChangedListener, PersistedD
.map(e -> (ProtectedMailboxStorageEntry) e) .map(e -> (ProtectedMailboxStorageEntry) e)
.filter(e -> networkNode.getNodeAddress() != null) .filter(e -> networkNode.getNodeAddress() != null)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
if (entries.size() > 1) { threadedBatchProcessMailboxEntries(entries);
threadedBatchProcessMailboxEntries(entries);
} else if (entries.size() == 1) {
processSingleMailboxEntry(entries);
}
} }
@Override @Override
@ -375,14 +370,6 @@ public class MailboxMessageService implements HashMapChangedListener, PersistedD
p2PDataStorage.addHashMapChangedListener(this); p2PDataStorage.addHashMapChangedListener(this);
} }
private void processSingleMailboxEntry(Collection<ProtectedMailboxStorageEntry> protectedMailboxStorageEntries) {
checkArgument(protectedMailboxStorageEntries.size() == 1);
var mailboxItems = new ArrayList<>(getMailboxItems(protectedMailboxStorageEntries));
if (mailboxItems.size() == 1) {
handleMailboxItem(mailboxItems.get(0));
}
}
// We run the batch processing of all mailbox messages we have received at startup in a thread to not block the UI. // We run the batch processing of all mailbox messages we have received at startup in a thread to not block the UI.
// For about 1000 messages decryption takes about 1 sec. // For about 1000 messages decryption takes about 1 sec.
private void threadedBatchProcessMailboxEntries(Collection<ProtectedMailboxStorageEntry> protectedMailboxStorageEntries) { private void threadedBatchProcessMailboxEntries(Collection<ProtectedMailboxStorageEntry> protectedMailboxStorageEntries) {
@ -390,7 +377,7 @@ public class MailboxMessageService implements HashMapChangedListener, PersistedD
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
ListenableFuture<Set<MailboxItem>> future = executor.submit(() -> { ListenableFuture<Set<MailboxItem>> future = executor.submit(() -> {
var mailboxItems = getMailboxItems(protectedMailboxStorageEntries); var mailboxItems = getMailboxItems(protectedMailboxStorageEntries);
log.info("Batch processing of {} mailbox entries took {} ms", log.trace("Batch processing of {} mailbox entries took {} ms",
protectedMailboxStorageEntries.size(), protectedMailboxStorageEntries.size(),
System.currentTimeMillis() - ts); System.currentTimeMillis() - ts);
return mailboxItems; return mailboxItems;

View File

@ -520,10 +520,9 @@ message Arbitrator {
string registration_signature = 4; string registration_signature = 4;
bytes registration_pub_key = 5; bytes registration_pub_key = 5;
PubKeyRing pub_key_ring = 6; PubKeyRing pub_key_ring = 6;
string xmr_address = 7; string email_address = 7;
string email_address = 8; string info = 8;
string info = 9; map<string, string> extra_data = 9;
map<string, string> extra_data = 10;
} }
message Mediator { message Mediator {