Skip to content

Commit c33c4ab

Browse files
committed
request connection switch every other attempt
1 parent d948ca7 commit c33c4ab

File tree

8 files changed

+15
-13
lines changed

8 files changed

+15
-13
lines changed

core/src/main/java/haveno/core/offer/OpenOfferManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ private MoneroTxWallet splitAndSchedule(OpenOffer openOffer) {
13581358
} catch (Exception e) {
13591359
if (e.getMessage().contains("not enough")) throw e; // do not retry if not enough funds
13601360
log.warn("Error creating split output tx to fund offer, offerId={}, subaddress={}, attempt={}/{}, error={}", openOffer.getShortId(), entry.getSubaddressIndex(), i + 1, TradeProtocol.MAX_ATTEMPTS, e.getMessage());
1361-
xmrWalletService.handleWalletError(e, sourceConnection);
1361+
xmrWalletService.handleWalletError(e, sourceConnection, i + 1);
13621362
if (stopped || i == TradeProtocol.MAX_ATTEMPTS - 1) throw e;
13631363
HavenoUtils.waitFor(TradeProtocol.REPROCESS_DELAY_MS); // wait before retrying
13641364
}

core/src/main/java/haveno/core/offer/placeoffer/tasks/MakerReserveOfferFunds.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected void run() {
100100
throw e;
101101
} catch (Exception e) {
102102
log.warn("Error creating reserve tx, offerId={}, attempt={}/{}, error={}", openOffer.getShortId(), i + 1, TradeProtocol.MAX_ATTEMPTS, e.getMessage());
103-
model.getXmrWalletService().handleWalletError(e, sourceConnection);
103+
model.getXmrWalletService().handleWalletError(e, sourceConnection, i + 1);
104104
verifyPending();
105105
if (i == TradeProtocol.MAX_ATTEMPTS - 1) throw e;
106106
model.getProtocol().startTimeoutTimer(); // reset protocol timeout

core/src/main/java/haveno/core/trade/Trade.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ public void importMultisigHex() {
11521152
throw e;
11531153
} catch (Exception e) {
11541154
log.warn("Failed to import multisig hex, tradeId={}, attempt={}/{}, error={}", getShortId(), i + 1, TradeProtocol.MAX_ATTEMPTS, e.getMessage());
1155-
handleWalletError(e, sourceConnection);
1155+
handleWalletError(e, sourceConnection, i + 1);
11561156
doPollWallet();
11571157
if (isPayoutPublished()) break;
11581158
if (i == TradeProtocol.MAX_ATTEMPTS - 1) throw e;
@@ -1237,9 +1237,9 @@ private void doImportMultisigHex() {
12371237
log.info("Done importing multisig hexes for {} {} in {} ms, count={}", getClass().getSimpleName(), getShortId(), System.currentTimeMillis() - startTime, multisigHexes.size());
12381238
}
12391239

1240-
private void handleWalletError(Exception e, MoneroRpcConnection sourceConnection) {
1240+
private void handleWalletError(Exception e, MoneroRpcConnection sourceConnection, int numAttempts) {
12411241
if (HavenoUtils.isUnresponsive(e)) forceCloseWallet(); // wallet can be stuck a while
1242-
if (!HavenoUtils.isIllegal(e) && xmrConnectionService.isConnected()) requestSwitchToNextBestConnection(sourceConnection);
1242+
if (numAttempts % TradeProtocol.REQUEST_CONNECTION_SWITCH_EVERY_NUM_ATTEMPTS == 0 && !HavenoUtils.isIllegal(e) && xmrConnectionService.isConnected()) requestSwitchToNextBestConnection(sourceConnection); // request connection switch every n attempts
12431243
getWallet(); // re-open wallet
12441244
}
12451245

@@ -1275,7 +1275,7 @@ public MoneroTxWallet createPayoutTx() {
12751275
} catch (IllegalArgumentException | IllegalStateException e) {
12761276
throw e;
12771277
} catch (Exception e) {
1278-
handleWalletError(e, sourceConnection);
1278+
handleWalletError(e, sourceConnection, i + 1);
12791279
doPollWallet();
12801280
if (isPayoutPublished()) break;
12811281
log.warn("Failed to create payout tx, tradeId={}, attempt={}/{}, error={}", getShortId(), i + 1, TradeProtocol.MAX_ATTEMPTS, e.getMessage());
@@ -1337,7 +1337,7 @@ public MoneroTxWallet createDisputePayoutTx(MoneroTxConfig txConfig) {
13371337
throw e;
13381338
} catch (Exception e) {
13391339
if (e.getMessage().contains("not possible")) throw new IllegalArgumentException("Loser payout is too small to cover the mining fee");
1340-
handleWalletError(e, sourceConnection);
1340+
handleWalletError(e, sourceConnection, i + 1);
13411341
doPollWallet();
13421342
if (isPayoutPublished()) break;
13431343
log.warn("Failed to create dispute payout tx, tradeId={}, attempt={}/{}, error={}", getShortId(), i + 1, TradeProtocol.MAX_ATTEMPTS, e.getMessage());
@@ -1368,7 +1368,7 @@ public void processPayoutTx(String payoutTxHex, boolean sign, boolean publish) {
13681368
} catch (IllegalArgumentException | IllegalStateException e) {
13691369
throw e;
13701370
} catch (Exception e) {
1371-
handleWalletError(e, sourceConnection);
1371+
handleWalletError(e, sourceConnection, i + 1);
13721372
doPollWallet();
13731373
if (isPayoutPublished()) break;
13741374
log.warn("Failed to process payout tx, tradeId={}, attempt={}/{}, error={}", getShortId(), i + 1, TradeProtocol.MAX_ATTEMPTS, e.getMessage(), e);

core/src/main/java/haveno/core/trade/protocol/TradeProtocol.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
101101
public static final int TRADE_STEP_TIMEOUT_SECONDS = Config.baseCurrencyNetwork().isTestnet() ? 60 : 180;
102102
private static final String TIMEOUT_REACHED = "Timeout reached.";
103103
public static final int MAX_ATTEMPTS = 5; // max attempts to create txs and other wallet functions
104+
public static final int REQUEST_CONNECTION_SWITCH_EVERY_NUM_ATTEMPTS = 2; // request connection switch on even attempts
104105
public static final long REPROCESS_DELAY_MS = 5000;
105106
public static final String LOG_HIGHLIGHT = ""; // TODO: how to highlight some logs with cyan? ("\u001B[36m")? coloring works in the terminal but prints character literals to .log files
106107

core/src/main/java/haveno/core/trade/protocol/tasks/MakerRecreateReserveTx.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected void run() {
9898
throw e;
9999
} catch (Exception e) {
100100
log.warn("Error creating reserve tx, tradeId={}, attempt={}/{}, error={}", trade.getShortId(), i + 1, TradeProtocol.MAX_ATTEMPTS, e.getMessage());
101-
trade.getXmrWalletService().handleWalletError(e, sourceConnection);
101+
trade.getXmrWalletService().handleWalletError(e, sourceConnection, i + 1);
102102
if (isTimedOut()) throw new RuntimeException("Trade protocol has timed out while creating reserve tx, tradeId=" + trade.getShortId());
103103
if (i == TradeProtocol.MAX_ATTEMPTS - 1) throw e;
104104
HavenoUtils.waitFor(TradeProtocol.REPROCESS_DELAY_MS); // wait before retrying

core/src/main/java/haveno/core/trade/protocol/tasks/MaybeSendSignContractRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected void run() {
113113
depositTx = trade.getXmrWalletService().createDepositTx(trade, reserveExactAmount, subaddressIndex);
114114
} catch (Exception e) {
115115
log.warn("Error creating deposit tx, tradeId={}, attempt={}/{}, error={}", trade.getShortId(), i + 1, TradeProtocol.MAX_ATTEMPTS, e.getMessage());
116-
trade.getXmrWalletService().handleWalletError(e, sourceConnection);
116+
trade.getXmrWalletService().handleWalletError(e, sourceConnection, i + 1);
117117
if (isTimedOut()) throw new RuntimeException("Trade protocol has timed out while creating deposit tx, tradeId=" + trade.getShortId());
118118
if (i == TradeProtocol.MAX_ATTEMPTS - 1) throw e;
119119
HavenoUtils.waitFor(TradeProtocol.REPROCESS_DELAY_MS); // wait before retrying

core/src/main/java/haveno/core/trade/protocol/tasks/TakerReserveTradeFunds.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected void run() {
7575
throw e;
7676
} catch (Exception e) {
7777
log.warn("Error creating reserve tx, tradeId={}, attempt={}/{}, error={}", trade.getShortId(), i + 1, TradeProtocol.MAX_ATTEMPTS, e.getMessage());
78-
trade.getXmrWalletService().handleWalletError(e, sourceConnection);
78+
trade.getXmrWalletService().handleWalletError(e, sourceConnection, i + 1);
7979
if (isTimedOut()) throw new RuntimeException("Trade protocol has timed out while creating reserve tx, tradeId=" + trade.getShortId());
8080
if (i == TradeProtocol.MAX_ATTEMPTS - 1) throw e;
8181
HavenoUtils.waitFor(TradeProtocol.REPROCESS_DELAY_MS); // wait before retrying

core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import haveno.core.trade.MakerTrade;
3838
import haveno.core.trade.Trade;
3939
import haveno.core.trade.TradeManager;
40+
import haveno.core.trade.protocol.TradeProtocol;
4041
import haveno.core.user.Preferences;
4142
import haveno.core.user.User;
4243
import haveno.core.xmr.listeners.XmrBalanceListener;
@@ -1944,9 +1945,9 @@ public void forceRestartMainWallet() {
19441945
doMaybeInitMainWallet(true, MAX_SYNC_ATTEMPTS);
19451946
}
19461947

1947-
public void handleWalletError(Exception e, MoneroRpcConnection sourceConnection) {
1948+
public void handleWalletError(Exception e, MoneroRpcConnection sourceConnection, int numAttempts) {
19481949
if (HavenoUtils.isUnresponsive(e)) forceCloseMainWallet(); // wallet can be stuck a while
1949-
requestSwitchToNextBestConnection(sourceConnection);
1950+
if (numAttempts % TradeProtocol.REQUEST_CONNECTION_SWITCH_EVERY_NUM_ATTEMPTS == 0) requestSwitchToNextBestConnection(sourceConnection); // request connection switch every n attempts
19501951
if (wallet == null) doMaybeInitMainWallet(true, MAX_SYNC_ATTEMPTS);
19511952
}
19521953

0 commit comments

Comments
 (0)