confirm payment sent and received off main thread

This commit is contained in:
woodser 2022-07-30 16:07:29 -04:00
parent 50126874a0
commit 3d43ae1f20
4 changed files with 90 additions and 82 deletions

View File

@ -126,32 +126,34 @@ public abstract class BuyerProtocol extends DisputeProtocol {
public void onPaymentStarted(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) { public void onPaymentStarted(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
System.out.println("BuyerProtocol.onPaymentStarted()"); System.out.println("BuyerProtocol.onPaymentStarted()");
synchronized (trade) { new Thread(() -> {
latchTrade(); synchronized (trade) {
this.errorMessageHandler = errorMessageHandler; latchTrade();
BuyerEvent event = BuyerEvent.PAYMENT_SENT; this.errorMessageHandler = errorMessageHandler;
expect(phase(Trade.Phase.DEPOSIT_UNLOCKED) BuyerEvent event = BuyerEvent.PAYMENT_SENT;
.with(event) expect(phase(Trade.Phase.DEPOSIT_UNLOCKED)
.preCondition(trade.confirmPermitted())) .with(event)
.setup(tasks(ApplyFilter.class, .preCondition(trade.confirmPermitted()))
getVerifyPeersFeePaymentClass(), .setup(tasks(ApplyFilter.class,
//UpdateMultisigWithTradingPeer.class, // TODO (woodser): can use this to test protocol with updated multisig from peer. peer should attempt to send updated multisig hex earlier as part of protocol. cannot use with countdown latch because response comes back in a separate thread and blocks on trade getVerifyPeersFeePaymentClass(),
BuyerPreparesPaymentSentMessage.class, //UpdateMultisigWithTradingPeer.class, // TODO (woodser): can use this to test protocol with updated multisig from peer. peer should attempt to send updated multisig hex earlier as part of protocol. cannot use with countdown latch because response comes back in a separate thread and blocks on trade
//BuyerSetupPayoutTxListener.class, BuyerPreparesPaymentSentMessage.class,
BuyerSendsPaymentSentMessage.class) // don't latch trade because this blocks and runs in background //BuyerSetupPayoutTxListener.class,
.using(new TradeTaskRunner(trade, BuyerSendsPaymentSentMessage.class) // don't latch trade because this blocks and runs in background
() -> { .using(new TradeTaskRunner(trade,
this.errorMessageHandler = null; () -> {
handleTaskRunnerSuccess(event); this.errorMessageHandler = null;
resultHandler.handleResult(); handleTaskRunnerSuccess(event);
}, resultHandler.handleResult();
(errorMessage) -> { },
handleTaskRunnerFault(event, errorMessage); (errorMessage) -> {
}))) handleTaskRunnerFault(event, errorMessage);
.run(() -> trade.setState(Trade.State.BUYER_CONFIRMED_IN_UI_PAYMENT_SENT)) })))
.executeTasks(); .run(() -> trade.setState(Trade.State.BUYER_CONFIRMED_IN_UI_PAYMENT_SENT))
awaitTradeLatch(); .executeTasks(true);
} awaitTradeLatch();
}
}).start();
} }
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -124,29 +124,31 @@ public abstract class SellerProtocol extends DisputeProtocol {
public void onPaymentReceived(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) { public void onPaymentReceived(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
log.info("SellerProtocol.onPaymentReceived()"); log.info("SellerProtocol.onPaymentReceived()");
synchronized (trade) { new Thread(() -> {
latchTrade(); synchronized (trade) {
this.errorMessageHandler = errorMessageHandler; latchTrade();
SellerEvent event = SellerEvent.PAYMENT_RECEIVED; this.errorMessageHandler = errorMessageHandler;
expect(anyPhase(Trade.Phase.PAYMENT_SENT, Trade.Phase.PAYMENT_RECEIVED) SellerEvent event = SellerEvent.PAYMENT_RECEIVED;
.with(event) expect(anyPhase(Trade.Phase.PAYMENT_SENT, Trade.Phase.PAYMENT_RECEIVED)
.preCondition(trade.confirmPermitted())) .with(event)
.setup(tasks( .preCondition(trade.confirmPermitted()))
ApplyFilter.class, .setup(tasks(
getVerifyPeersFeePaymentClass(), ApplyFilter.class,
SellerPreparesPaymentReceivedMessage.class, getVerifyPeersFeePaymentClass(),
SellerSendsPaymentReceivedMessage.class) SellerPreparesPaymentReceivedMessage.class,
.using(new TradeTaskRunner(trade, () -> { SellerSendsPaymentReceivedMessage.class)
this.errorMessageHandler = null; .using(new TradeTaskRunner(trade, () -> {
handleTaskRunnerSuccess(event); this.errorMessageHandler = null;
resultHandler.handleResult(); handleTaskRunnerSuccess(event);
}, (errorMessage) -> { resultHandler.handleResult();
handleTaskRunnerFault(event, errorMessage); }, (errorMessage) -> {
}))) handleTaskRunnerFault(event, errorMessage);
.run(() -> trade.setState(Trade.State.SELLER_CONFIRMED_IN_UI_PAYMENT_RECEIPT)) })))
.executeTasks(); .run(() -> trade.setState(Trade.State.SELLER_CONFIRMED_IN_UI_PAYMENT_RECEIPT))
awaitTradeLatch(); .executeTasks(true);
} awaitTradeLatch();
}
}).start();
} }
@Override @Override

View File

@ -159,41 +159,42 @@ public class BuyerStep2View extends TradeStepView {
showPopup(); showPopup();
} else if (state.ordinal() <= Trade.State.BUYER_SEND_FAILED_PAYMENT_SENT_MSG.ordinal()) { } else if (state.ordinal() <= Trade.State.BUYER_SEND_FAILED_PAYMENT_SENT_MSG.ordinal()) {
if (!trade.hasFailed()) { if (!trade.hasFailed()) {
UserThread.execute(() -> { switch (state) {
switch (state) { case BUYER_CONFIRMED_IN_UI_PAYMENT_SENT:
case BUYER_CONFIRMED_IN_UI_PAYMENT_SENT: busyAnimation.play();
case BUYER_SENT_PAYMENT_SENT_MSG: statusLabel.setText("Confirming payment sent...");
busyAnimation.play(); break;
statusLabel.setText(Res.get("shared.sendingConfirmation")); case BUYER_SENT_PAYMENT_SENT_MSG:
model.setMessageStateProperty(MessageState.SENT); busyAnimation.play();
timeoutTimer = UserThread.runAfter(() -> { statusLabel.setText(Res.get("shared.sendingConfirmation"));
busyAnimation.stop(); model.setMessageStateProperty(MessageState.SENT);
statusLabel.setText(Res.get("shared.sendingConfirmationAgain")); timeoutTimer = UserThread.runAfter(() -> {
}, 10);
break;
case BUYER_SAW_ARRIVED_PAYMENT_SENT_MSG:
busyAnimation.stop();
statusLabel.setText(Res.get("shared.messageArrived"));
model.setMessageStateProperty(MessageState.ARRIVED);
break;
case BUYER_STORED_IN_MAILBOX_PAYMENT_SENT_MSG:
busyAnimation.stop();
statusLabel.setText(Res.get("shared.messageStoredInMailbox"));
model.setMessageStateProperty(MessageState.STORED_IN_MAILBOX);
break;
case BUYER_SEND_FAILED_PAYMENT_SENT_MSG:
// We get a popup and the trade closed, so we dont need to show anything here
busyAnimation.stop();
statusLabel.setText("");
model.setMessageStateProperty(MessageState.FAILED);
break;
default:
log.warn("Unexpected case: State={}, tradeId={} " + state.name(), trade.getId());
busyAnimation.stop(); busyAnimation.stop();
statusLabel.setText(Res.get("shared.sendingConfirmationAgain")); statusLabel.setText(Res.get("shared.sendingConfirmationAgain"));
break; }, 10);
} break;
}); case BUYER_SAW_ARRIVED_PAYMENT_SENT_MSG:
busyAnimation.stop();
statusLabel.setText(Res.get("shared.messageArrived"));
model.setMessageStateProperty(MessageState.ARRIVED);
break;
case BUYER_STORED_IN_MAILBOX_PAYMENT_SENT_MSG:
busyAnimation.stop();
statusLabel.setText(Res.get("shared.messageStoredInMailbox"));
model.setMessageStateProperty(MessageState.STORED_IN_MAILBOX);
break;
case BUYER_SEND_FAILED_PAYMENT_SENT_MSG:
// We get a popup and the trade closed, so we dont need to show anything here
busyAnimation.stop();
statusLabel.setText("");
model.setMessageStateProperty(MessageState.FAILED);
break;
default:
log.warn("Unexpected case: State={}, tradeId={} " + state.name(), trade.getId());
busyAnimation.stop();
statusLabel.setText(Res.get("shared.sendingConfirmationAgain"));
break;
}
} else { } else {
log.warn("Trade contains error message {}", trade.getErrorMessage()); log.warn("Trade contains error message {}", trade.getErrorMessage());
statusLabel.setText(""); statusLabel.setText("");

View File

@ -120,6 +120,9 @@ public class SellerStep3View extends TradeStepView {
if (!trade.hasFailed()) { if (!trade.hasFailed()) {
switch (state) { switch (state) {
case SELLER_CONFIRMED_IN_UI_PAYMENT_RECEIPT: case SELLER_CONFIRMED_IN_UI_PAYMENT_RECEIPT:
busyAnimation.play();
statusLabel.setText(Res.get("Confirming payment received..."));
break;
case SELLER_PUBLISHED_PAYOUT_TX: case SELLER_PUBLISHED_PAYOUT_TX:
case SELLER_SENT_PAYOUT_TX_PUBLISHED_MSG: case SELLER_SENT_PAYOUT_TX_PUBLISHED_MSG:
case SELLER_SENT_PAYMENT_RECEIVED_MSG: case SELLER_SENT_PAYMENT_RECEIVED_MSG: