latch trade awaits trade initialization

This commit is contained in:
woodser 2024-05-07 11:02:06 -04:00
parent 4ec5339e5d
commit a6d827c369
2 changed files with 13 additions and 2 deletions

View File

@ -431,6 +431,7 @@ public abstract class Trade implements Tradable, Model {
// Mutable // Mutable
@Getter @Getter
transient private boolean isInitialized; transient private boolean isInitialized;
transient private boolean isFullyInitialized;
@Getter @Getter
transient private boolean isShutDownStarted; transient private boolean isShutDownStarted;
@Getter @Getter
@ -723,7 +724,10 @@ public abstract class Trade implements Tradable, Model {
isInitialized = true; isInitialized = true;
// done if deposit not requested or payout unlocked // done if deposit not requested or payout unlocked
if (!isDepositRequested() || isPayoutUnlocked()) return; if (!isDepositRequested() || isPayoutUnlocked()) {
isFullyInitialized = true;
return;
}
// open wallet or done if wallet does not exist // open wallet or done if wallet does not exist
if (walletExists()) getWallet(); if (walletExists()) getWallet();
@ -732,6 +736,7 @@ public abstract class Trade implements Tradable, Model {
if (payoutTx != null && payoutTx.getNumConfirmations() >= XmrWalletService.NUM_BLOCKS_UNLOCK) { if (payoutTx != null && payoutTx.getNumConfirmations() >= XmrWalletService.NUM_BLOCKS_UNLOCK) {
log.warn("Payout state for {} {} is {} but payout is unlocked, updating state", getClass().getSimpleName(), getId(), getPayoutState()); log.warn("Payout state for {} {} is {} but payout is unlocked, updating state", getClass().getSimpleName(), getId(), getPayoutState());
setPayoutStateUnlocked(); setPayoutStateUnlocked();
isFullyInitialized = true;
return; return;
} else { } else {
log.warn("Missing trade wallet for {} {}, state={}, marked completed={}", getClass().getSimpleName(), getShortId(), getState(), isCompleted()); log.warn("Missing trade wallet for {} {}, state={}, marked completed={}", getClass().getSimpleName(), getShortId(), getState(), isCompleted());
@ -741,6 +746,11 @@ public abstract class Trade implements Tradable, Model {
// start polling if deposit requested // start polling if deposit requested
if (isDepositRequested()) tryInitPolling(); if (isDepositRequested()) tryInitPolling();
isFullyInitialized = true;
}
public void awaitInitialized() {
while (!isFullyInitialized) HavenoUtils.waitFor(100); // TODO: use proper notification and refactor isInitialized, fullyInitialized, and arbitrator idling
} }
public void requestPersistence() { public void requestPersistence() {
@ -1203,6 +1213,7 @@ public abstract class Trade implements Tradable, Model {
for (int i = 0; i < TradeProtocol.MAX_ATTEMPTS; i++) { for (int i = 0; i < TradeProtocol.MAX_ATTEMPTS; i++) {
try { try {
wallet.submitMultisigTxHex(payoutTxHex); wallet.submitMultisigTxHex(payoutTxHex);
ThreadUtils.submitToPool(() -> pollWallet());
break; break;
} catch (Exception e) { } catch (Exception e) {
log.warn("Failed to submit payout tx, attempt={}/{}, tradeId={}, error={}", i + 1, TradeProtocol.MAX_ATTEMPTS, getShortId(), e.getMessage()); log.warn("Failed to submit payout tx, attempt={}/{}, tradeId={}, error={}", i + 1, TradeProtocol.MAX_ATTEMPTS, getShortId(), e.getMessage());
@ -1211,7 +1222,6 @@ public abstract class Trade implements Tradable, Model {
} }
} }
} }
pollWallet();
} }
/** /**

View File

@ -848,6 +848,7 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
} }
protected void latchTrade() { protected void latchTrade() {
trade.awaitInitialized();
if (tradeLatch != null) throw new RuntimeException("Trade latch is not null. That should never happen."); if (tradeLatch != null) throw new RuntimeException("Trade latch is not null. That should never happen.");
if (trade.isShutDown()) throw new RuntimeException("Cannot latch trade " + trade.getId() + " for protocol because it's shut down"); if (trade.isShutDown()) throw new RuntimeException("Cannot latch trade " + trade.getId() + " for protocol because it's shut down");
tradeLatch = new CountDownLatch(1); tradeLatch = new CountDownLatch(1);