diff --git a/core/src/main/java/haveno/core/trade/protocol/BuyerAsTakerProtocol.java b/core/src/main/java/haveno/core/trade/protocol/BuyerAsTakerProtocol.java index 927997e611..46724fd847 100644 --- a/core/src/main/java/haveno/core/trade/protocol/BuyerAsTakerProtocol.java +++ b/core/src/main/java/haveno/core/trade/protocol/BuyerAsTakerProtocol.java @@ -98,14 +98,22 @@ public class BuyerAsTakerProtocol extends BuyerProtocol implements TakerProtocol @Override public void handleInitTradeRequest(InitTradeRequest message, - NodeAddress peer) { - log.info(TradeProtocol.LOG_HIGHLIGHT + "handleInitTradeRequest() for {} {} from {}", trade.getClass().getSimpleName(), trade.getShortId(), peer); + NodeAddress sender) { + log.info(TradeProtocol.LOG_HIGHLIGHT + "handleInitTradeRequest() for {} {} from {}", trade.getClass().getSimpleName(), trade.getShortId(), sender); ThreadUtils.execute(() -> { synchronized (trade.getLock()) { + + // ignore if assigned a different arbitrator + NodeAddress nodeAddress = trade.getArbitrator().getNodeAddress(); + if (nodeAddress != null && !nodeAddress.equals(sender)) { + log.warn("Ignoring InitTradeRequest because sender is not the arbitrator for the trade, tradeId={}, sender={}", message.getOfferId(), sender); + return; + } + latchTrade(); expect(phase(Trade.Phase.INIT) .with(message) - .from(peer)) + .from(sender)) .setup(tasks( ApplyFilter.class, ProcessInitTradeRequest.class, @@ -113,10 +121,10 @@ public class BuyerAsTakerProtocol extends BuyerProtocol implements TakerProtocol .using(new TradeTaskRunner(trade, () -> { startTimeout(); - handleTaskRunnerSuccess(peer, message); + handleTaskRunnerSuccess(sender, message); }, errorMessage -> { - handleTaskRunnerFault(peer, message, errorMessage); + handleTaskRunnerFault(sender, message, errorMessage); })) .withTimeout(TRADE_STEP_TIMEOUT_SECONDS)) .executeTasks(true); diff --git a/core/src/main/java/haveno/core/trade/protocol/SellerAsTakerProtocol.java b/core/src/main/java/haveno/core/trade/protocol/SellerAsTakerProtocol.java index f4914efe60..cd7c347bc3 100644 --- a/core/src/main/java/haveno/core/trade/protocol/SellerAsTakerProtocol.java +++ b/core/src/main/java/haveno/core/trade/protocol/SellerAsTakerProtocol.java @@ -98,14 +98,22 @@ public class SellerAsTakerProtocol extends SellerProtocol implements TakerProtoc @Override public void handleInitTradeRequest(InitTradeRequest message, - NodeAddress peer) { - log.info(TradeProtocol.LOG_HIGHLIGHT + "handleInitTradeRequest() for {} {} from {}", trade.getClass().getSimpleName(), trade.getShortId(), peer); + NodeAddress sender) { + log.info(TradeProtocol.LOG_HIGHLIGHT + "handleInitTradeRequest() for {} {} from {}", trade.getClass().getSimpleName(), trade.getShortId(), sender); ThreadUtils.execute(() -> { synchronized (trade.getLock()) { + + // ignore if assigned a different arbitrator + NodeAddress nodeAddress = trade.getArbitrator().getNodeAddress(); + if (nodeAddress != null && !nodeAddress.equals(sender)) { + log.warn("Ignoring InitTradeRequest because sender is not the arbitrator for the trade, tradeId={}, sender={}", message.getOfferId(), sender); + return; + } + latchTrade(); expect(phase(Trade.Phase.INIT) .with(message) - .from(peer)) + .from(sender)) .setup(tasks( ApplyFilter.class, ProcessInitTradeRequest.class, @@ -113,10 +121,10 @@ public class SellerAsTakerProtocol extends SellerProtocol implements TakerProtoc .using(new TradeTaskRunner(trade, () -> { startTimeout(); - handleTaskRunnerSuccess(peer, message); + handleTaskRunnerSuccess(sender, message); }, errorMessage -> { - handleTaskRunnerFault(peer, message, errorMessage); + handleTaskRunnerFault(sender, message, errorMessage); })) .withTimeout(TRADE_STEP_TIMEOUT_SECONDS)) .executeTasks(true); diff --git a/core/src/main/java/haveno/core/trade/protocol/tasks/ProcessInitTradeRequest.java b/core/src/main/java/haveno/core/trade/protocol/tasks/ProcessInitTradeRequest.java index a97017d1c2..136846e232 100644 --- a/core/src/main/java/haveno/core/trade/protocol/tasks/ProcessInitTradeRequest.java +++ b/core/src/main/java/haveno/core/trade/protocol/tasks/ProcessInitTradeRequest.java @@ -132,6 +132,7 @@ public class ProcessInitTradeRequest extends TradeTask { if (request.getTradePrice() != trade.getPrice().getValue()) throw new RuntimeException("Trade price does not match request's trade price"); Arbitrator arbitrator = processModel.getUser().getAcceptedArbitratorByAddress(request.getArbitratorNodeAddress()); if (arbitrator == null) throw new RuntimeException("Arbitrator is not accepted by taker"); + if (trade.getArbitrator().getNodeAddress() != null && !trade.getArbitrator().getNodeAddress().equals(request.getArbitratorNodeAddress())) throw new RuntimeException("Trade's arbitrator node address does not match request"); trade.getArbitrator().setNodeAddress(request.getArbitratorNodeAddress()); trade.getArbitrator().setPubKeyRing(arbitrator.getPubKeyRing()); sender = trade.getTradePeer(processModel.getTempTradePeerNodeAddress());