Skip to content

Commit

Permalink
recover from payment received message not processed
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Jul 17, 2024
1 parent 1d85379 commit 3f73e11
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
32 changes: 20 additions & 12 deletions core/src/main/java/haveno/core/trade/Trade.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,6 @@ public static protobuf.Trade.TradePeriodState toProtoMessage(Trade.TradePeriodSt
private Long payoutHeight;
private IdlePayoutSyncer idlePayoutSyncer;
@Getter
@Setter
private boolean isCompleted;

///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -614,8 +613,8 @@ public void onAckMessage(AckMessage ackMessage, NodeAddress sender) {
public void initialize(ProcessModelServiceProvider serviceProvider) {
if (isInitialized) throw new IllegalStateException(getClass().getSimpleName() + " " + getId() + " is already initialized");

// done if payout unlocked
if (isPayoutUnlocked()) {
// done if payout unlocked and marked complete
if (isPayoutUnlocked() && isCompleted()) {
clearAndShutDown();
return;
}
Expand Down Expand Up @@ -679,13 +678,12 @@ public void initialize(ProcessModelServiceProvider serviceProvider) {
log.info("Payout published for {} {}", getClass().getSimpleName(), getId());

// sync main wallet to update pending balance
if (!isPayoutConfirmed()) {
new Thread(() -> {
HavenoUtils.waitFor(1000);
if (isShutDownStarted) return;
if (xmrConnectionService.isConnected()) syncAndPollWallet();
}).start();
}
ThreadUtils.submitToPool(() -> {
HavenoUtils.waitFor(1000);
if (isPayoutConfirmed()) return;
if (isShutDownStarted) return;
if (xmrConnectionService.isConnected()) syncAndPollWallet();
});

// complete disputed trade
if (getDisputeState().isArbitrated() && !getDisputeState().isClosed()) {
Expand All @@ -707,7 +705,8 @@ public void initialize(ProcessModelServiceProvider serviceProvider) {
if (newValue == Trade.PayoutState.PAYOUT_UNLOCKED) {
if (!isInitialized) return;
log.info("Payout unlocked for {} {}, deleting multisig wallet", getClass().getSimpleName(), getId());
clearAndShutDown();
if (isCompleted()) clearAndShutDown();
else deleteWallet();
}
});
});
Expand Down Expand Up @@ -790,6 +789,11 @@ public NodeAddress getArbitratorNodeAddress() {
return getArbitrator() == null ? null : getArbitrator().getNodeAddress();
}

public void setCompleted(boolean completed) {
this.isCompleted = completed;
if (isPayoutUnlocked()) clearAndShutDown();
}

///////////////////////////////////////////////////////////////////////////////////////////
// WALLET MANAGEMENT
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -996,7 +1000,11 @@ private void closeWallet() {

private void forceCloseWallet() {
if (wallet != null) {
xmrWalletService.forceCloseWallet(wallet, wallet.getPath());
try {
xmrWalletService.forceCloseWallet(wallet, wallet.getPath());
} catch (Exception e) {
log.warn("Error force closing wallet for {} {}: {}", getClass().getSimpleName(), getId(), e.getMessage());
}
stopPolling();
wallet = null;
}
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/haveno/core/trade/protocol/TradeProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public void maybeReprocessPaymentReceivedMessage(boolean reprocessOnError) {
}

public void handleInitMultisigRequest(InitMultisigRequest request, NodeAddress sender) {
System.out.println(getClass().getSimpleName() + ".handleInitMultisigRequest()");
System.out.println(getClass().getSimpleName() + ".handleInitMultisigRequest() for " + trade.getClass().getSimpleName() + " " + trade.getShortId());
trade.addInitProgressStep();
ThreadUtils.execute(() -> {
synchronized (trade) {
Expand Down Expand Up @@ -333,7 +333,7 @@ public void handleInitMultisigRequest(InitMultisigRequest request, NodeAddress s
}

public void handleSignContractRequest(SignContractRequest message, NodeAddress sender) {
System.out.println(getClass().getSimpleName() + ".handleSignContractRequest() " + trade.getId());
System.out.println(getClass().getSimpleName() + ".handleSignContractRequest() for " + trade.getClass().getSimpleName() + " " + trade.getShortId());
ThreadUtils.execute(() -> {
synchronized (trade) {

Expand Down Expand Up @@ -376,7 +376,7 @@ public void handleSignContractRequest(SignContractRequest message, NodeAddress s
}

public void handleSignContractResponse(SignContractResponse message, NodeAddress sender) {
System.out.println(getClass().getSimpleName() + ".handleSignContractResponse() " + trade.getId());
System.out.println(getClass().getSimpleName() + ".handleSignContractResponse() for " + trade.getClass().getSimpleName() + " " + trade.getShortId());
trade.addInitProgressStep();
ThreadUtils.execute(() -> {
synchronized (trade) {
Expand Down Expand Up @@ -422,7 +422,7 @@ public void handleSignContractResponse(SignContractResponse message, NodeAddress
}

public void handleDepositResponse(DepositResponse response, NodeAddress sender) {
System.out.println(getClass().getSimpleName() + ".handleDepositResponse()");
System.out.println(getClass().getSimpleName() + ".handleDepositResponse() for " + trade.getClass().getSimpleName() + " " + trade.getShortId());
trade.addInitProgressStep();
ThreadUtils.execute(() -> {
synchronized (trade) {
Expand Down Expand Up @@ -452,7 +452,7 @@ public void handleDepositResponse(DepositResponse response, NodeAddress sender)
}

public void handle(DepositsConfirmedMessage message, NodeAddress sender) {
System.out.println(getClass().getSimpleName() + ".handle(DepositsConfirmedMessage) from " + sender);
System.out.println(getClass().getSimpleName() + ".handle(DepositsConfirmedMessage) from " + sender + " for " + trade.getClass().getSimpleName() + " " + trade.getShortId());
if (!trade.isInitialized() || trade.isShutDown()) return;
ThreadUtils.execute(() -> {
synchronized (trade) {
Expand Down Expand Up @@ -481,7 +481,7 @@ public void handle(DepositsConfirmedMessage message, NodeAddress sender) {

// received by seller and arbitrator
protected void handle(PaymentSentMessage message, NodeAddress peer) {
System.out.println(getClass().getSimpleName() + ".handle(PaymentSentMessage)");
System.out.println(getClass().getSimpleName() + ".handle(PaymentSentMessage) for " + trade.getClass().getSimpleName() + " " + trade.getShortId());
if (!trade.isInitialized() || trade.isShutDown()) return;
if (!(trade instanceof SellerTrade || trade instanceof ArbitratorTrade)) {
log.warn("Ignoring PaymentSentMessage since not seller or arbitrator");
Expand Down Expand Up @@ -535,7 +535,7 @@ protected void handle(PaymentReceivedMessage message, NodeAddress peer) {
}

private void handle(PaymentReceivedMessage message, NodeAddress peer, boolean reprocessOnError) {
System.out.println(getClass().getSimpleName() + ".handle(PaymentReceivedMessage)");
System.out.println(getClass().getSimpleName() + ".handle(PaymentReceivedMessage) for " + trade.getClass().getSimpleName() + " " + trade.getShortId());
if (!trade.isInitialized() || trade.isShutDown()) return;
ThreadUtils.execute(() -> {
if (!(trade instanceof BuyerTrade || trade instanceof ArbitratorTrade)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,20 @@ protected void run() {
}

// update multisig hex
sender.setUpdatedMultisigHex(request.getUpdatedMultisigHex());
if (sender.getUpdatedMultisigHex() == null) {
sender.setUpdatedMultisigHex(request.getUpdatedMultisigHex());

// try to import multisig hex (retry later)
ThreadUtils.submitToPool(() -> {
try {
trade.importMultisigHex();
} catch (Exception e) {
e.printStackTrace();
// try to import multisig hex (retry later)
if (!trade.isPayoutPublished()) {
ThreadUtils.submitToPool(() -> {
try {
trade.importMultisigHex();
} catch (Exception e) {
e.printStackTrace();
}
});
}
});
}

// persist
processModel.getTradeManager().requestPersistence();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ private void refresh() {
sellerState.set(UNDEFINED);
buyerState.set(BuyerState.UNDEFINED);
onTradeStateChanged(trade.getState());
if (trade.isPayoutPublished()) onPayoutStateChanged(trade.getPayoutState()); // TODO: payout state takes precedence in case PaymentReceivedMessage not processed
else onTradeStateChanged(trade.getState());
});
}

Expand Down

0 comments on commit 3f73e11

Please sign in to comment.