sync idle trades once in background after active trades

This commit is contained in:
woodser 2023-02-10 09:02:26 -05:00
parent 0c4ce623a7
commit 88f0ad543a
2 changed files with 19 additions and 8 deletions

View file

@ -743,6 +743,10 @@ public abstract class Trade implements Tradable, Model {
}
}
public boolean isIdling() {
return this instanceof ArbitratorTrade && isDepositsConfirmed() && walletExists(); // arbitrator idles trade after deposits confirm
}
public void syncWallet() {
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());
@ -750,7 +754,6 @@ public abstract class Trade implements Tradable, Model {
getWallet().sync();
pollWallet();
log.info("Done syncing wallet for {} {}", getClass().getSimpleName(), getId());
updateWalletRefreshPeriod();
}
private void trySyncWallet() {
@ -1634,11 +1637,16 @@ public abstract class Trade implements Tradable, Model {
}
private void updateSyncing() {
if (!isIdling()) trySyncWallet();
else {
if (!isIdling()) {
trySyncWallet();
updateWalletRefreshPeriod();
} else {
long startSyncingInMs = ThreadLocalRandom.current().nextLong(0, getWalletRefreshPeriod()); // random time to start syncing
UserThread.runAfter(() -> {
if (isInitialized) trySyncWallet();
if (isInitialized) {
trySyncWallet();
updateWalletRefreshPeriod();
}
}, startSyncingInMs / 1000l);
}
}
@ -1744,10 +1752,6 @@ public abstract class Trade implements Tradable, Model {
return xmrWalletService.getConnectionsService().getDefaultRefreshPeriodMs();
}
private boolean isIdling() {
return this instanceof ArbitratorTrade && isDepositsConfirmed(); // arbitrator idles trade after deposits confirm
}
private void setStateDepositsPublished() {
if (!isDepositsPublished()) setState(State.DEPOSIT_TXS_SEEN_IN_NETWORK);
}

View file

@ -417,6 +417,13 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
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());
}
}
}
private void initPersistedTrade(Trade trade) {