From a266fab6ec451280689ee17323381265fa3ff18e Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 25 Nov 2022 11:39:35 +0000 Subject: [PATCH] process mailbox messages in order --- .../java/bisq/core/trade/TradeManager.java | 2 +- .../core/trade/protocol/SellerProtocol.java | 1 - .../core/trade/protocol/TradeProtocol.java | 27 ++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/bisq/core/trade/TradeManager.java b/core/src/main/java/bisq/core/trade/TradeManager.java index b69aa7ae73..62a2264431 100644 --- a/core/src/main/java/bisq/core/trade/TradeManager.java +++ b/core/src/main/java/bisq/core/trade/TradeManager.java @@ -381,7 +381,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi } private void initTradeAndProtocol(Trade trade, TradeProtocol tradeProtocol) { - tradeProtocol.initialize(processModelServiceProvider, this, trade.getOffer()); + tradeProtocol.initialize(processModelServiceProvider, this); requestPersistence(); // TODO requesting persistence twice with initPersistedTrade() } diff --git a/core/src/main/java/bisq/core/trade/protocol/SellerProtocol.java b/core/src/main/java/bisq/core/trade/protocol/SellerProtocol.java index 2fe57c5ad3..7e0f325cc0 100644 --- a/core/src/main/java/bisq/core/trade/protocol/SellerProtocol.java +++ b/core/src/main/java/bisq/core/trade/protocol/SellerProtocol.java @@ -19,7 +19,6 @@ package bisq.core.trade.protocol; import bisq.core.trade.SellerTrade; import bisq.core.trade.Trade; -import bisq.core.trade.messages.PaymentSentMessage; import bisq.core.trade.messages.SignContractResponse; import bisq.core.trade.messages.TradeMessage; import bisq.core.trade.protocol.tasks.ApplyFilter; diff --git a/core/src/main/java/bisq/core/trade/protocol/TradeProtocol.java b/core/src/main/java/bisq/core/trade/protocol/TradeProtocol.java index 5dad52e3d3..1e74404148 100644 --- a/core/src/main/java/bisq/core/trade/protocol/TradeProtocol.java +++ b/core/src/main/java/bisq/core/trade/protocol/TradeProtocol.java @@ -17,7 +17,8 @@ package bisq.core.trade.protocol; -import bisq.core.offer.Offer; +import bisq.core.support.dispute.messages.DisputeClosedMessage; +import bisq.core.support.dispute.messages.DisputeOpenedMessage; import bisq.core.trade.ArbitratorTrade; import bisq.core.trade.BuyerTrade; import bisq.core.trade.Trade; @@ -63,8 +64,11 @@ import bisq.common.handlers.ErrorMessageHandler; import bisq.common.proto.network.NetworkEnvelope; import bisq.common.taskrunner.Task; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; +import java.util.List; import java.util.concurrent.CountDownLatch; import lombok.extern.slf4j.Slf4j; @@ -168,9 +172,26 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D .filter(this::isMyMessage) .filter(e -> e instanceof MailboxMessage) .map(e -> (MailboxMessage) e) + .sorted(new MailboxMessageComparator()) .forEach(this::handleMailboxMessage); } + private static class MailboxMessageComparator implements Comparator { + private static List> messageOrder = Arrays.asList( + DepositsConfirmedMessage.class, + PaymentSentMessage.class, + PaymentReceivedMessage.class, + DisputeOpenedMessage.class, + DisputeClosedMessage.class); + + @Override + public int compare(MailboxMessage m1, MailboxMessage m2) { + int idx1 = messageOrder.indexOf(m1.getClass()); + int idx2 = messageOrder.indexOf(m2.getClass()); + return idx1 - idx2; + } + } + private void handleMailboxMessage(MailboxMessage mailboxMessage) { if (mailboxMessage instanceof TradeMessage) { TradeMessage tradeMessage = (TradeMessage) mailboxMessage; @@ -209,8 +230,8 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D public abstract Class[] getDepositsConfirmedTasks(); - public void initialize(ProcessModelServiceProvider serviceProvider, TradeManager tradeManager, Offer offer) { - processModel.applyTransient(serviceProvider, tradeManager, offer); + public void initialize(ProcessModelServiceProvider serviceProvider, TradeManager tradeManager) { + processModel.applyTransient(serviceProvider, tradeManager, trade.getOffer()); onInitialized(); }