Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preparations for new trade protocol #6800

19 changes: 17 additions & 2 deletions common/src/main/java/bisq/common/app/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

package bisq.common.app;

import bisq.common.util.Utilities;

import java.net.URL;

import java.util.Arrays;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -107,7 +111,18 @@ private static int getSubVersion(String version, int index) {
// Version 1.2.2 -> TRADE_PROTOCOL_VERSION = 2
// Version 1.5.0 -> TRADE_PROTOCOL_VERSION = 3
// Version 1.7.0 -> TRADE_PROTOCOL_VERSION = 4
public static final int TRADE_PROTOCOL_VERSION = 4;
// Version 1.9.13 and after activation date -> TRADE_PROTOCOL_VERSION = 5

public static final Date PROTOCOL_5_ACTIVATION_DATE = Utilities.getUTCDate(2023, GregorianCalendar.OCTOBER, 1);

public static boolean isTradeProtocolVersion5Activated() {
return new Date().after(PROTOCOL_5_ACTIVATION_DATE);
}

public static int getTradeProtocolVersion() {
return isTradeProtocolVersion5Activated() ? 5 : 4;
}

private static int p2pMessageVersion;

public static final String BSQ_TX_VERSION = "1";
Expand Down Expand Up @@ -136,7 +151,7 @@ public static void printVersion() {
"VERSION=" + VERSION +
", P2P_NETWORK_VERSION=" + P2P_NETWORK_VERSION +
", LOCAL_DB_VERSION=" + LOCAL_DB_VERSION +
", TRADE_PROTOCOL_VERSION=" + TRADE_PROTOCOL_VERSION +
", TRADE_PROTOCOL_VERSION=" + getTradeProtocolVersion() +
", BASE_CURRENCY_NETWORK=" + BASE_CURRENCY_NETWORK +
", getP2PNetworkId()=" + getP2PMessageVersion() +
'}');
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/bisq/core/api/CoreTradesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import bisq.core.trade.model.TradeModel;
import bisq.core.trade.model.bisq_v1.Trade;
import bisq.core.trade.model.bsq_swap.BsqSwapTrade;
import bisq.core.trade.protocol.bisq_v1.BuyerProtocol;
import bisq.core.trade.protocol.bisq_v1.SellerProtocol;
import bisq.core.trade.protocol.BuyerProtocol;
import bisq.core.trade.protocol.SellerProtocol;
import bisq.core.user.User;
import bisq.core.util.validation.BtcAddressValidator;

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/offer/OfferFilterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public boolean isAnyPaymentAccountValidForOffer(Offer offer) {
}

public boolean hasSameProtocolVersion(Offer offer) {
return offer.getProtocolVersion() == Version.TRADE_PROTOCOL_VERSION;
return offer.getProtocolVersion() == Version.getTradeProtocolVersion();
}

public boolean isIgnored(Offer offer) {
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/bisq/core/offer/OpenOfferManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ private void maybeUpdatePersistedOffers() {
// Capability.REFUND_AGENT in v1.2.0 and want to rewrite a
// persisted offer after the user has updated to 1.2.0 so their offer will be accepted by the network.

if (original.getProtocolVersion() < Version.TRADE_PROTOCOL_VERSION ||
if (original.getProtocolVersion() < Version.getTradeProtocolVersion() ||
!OfferRestrictions.hasOfferMandatoryCapability(originalOffer, Capability.MEDIATION) ||
!OfferRestrictions.hasOfferMandatoryCapability(originalOffer, Capability.REFUND_AGENT) ||
!original.getOwnerNodeAddress().equals(p2PService.getAddress())) {
Expand All @@ -960,9 +960,9 @@ private void maybeUpdatePersistedOffers() {

// - Protocol version changed?
int protocolVersion = original.getProtocolVersion();
if (protocolVersion < Version.TRADE_PROTOCOL_VERSION) {
if (protocolVersion < Version.getTradeProtocolVersion()) {
// We update the trade protocol version
protocolVersion = Version.TRADE_PROTOCOL_VERSION;
protocolVersion = Version.getTradeProtocolVersion();
log.info("Updated the protocol version of offer id={}", originalOffer.getId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public Offer createAndGetOffer(String offerId,
isPrivateOffer,
hashOfChallenge,
extraDataMap,
Version.TRADE_PROTOCOL_VERSION);
Version.getTradeProtocolVersion());
Offer offer = new Offer(offerPayload);
offer.setPriceFeedService(priceFeedService);
return offer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void requestNewOffer(String offerId,
proofOfWork,
null,
Version.VERSION,
Version.TRADE_PROTOCOL_VERSION);
Version.getTradeProtocolVersion());
resultHandler.accept(new Offer(bsqSwapOfferPayload));
});
});
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/bisq/core/trade/TradeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
import bisq.core.trade.model.bsq_swap.BsqSwapSellerAsMakerTrade;
import bisq.core.trade.model.bsq_swap.BsqSwapSellerAsTakerTrade;
import bisq.core.trade.model.bsq_swap.BsqSwapTrade;
import bisq.core.trade.protocol.MakerProtocol;
import bisq.core.trade.protocol.Provider;
import bisq.core.trade.protocol.TakerProtocol;
import bisq.core.trade.protocol.TradeProtocol;
import bisq.core.trade.protocol.TradeProtocolFactory;
import bisq.core.trade.protocol.bisq_v1.MakerProtocol;
import bisq.core.trade.protocol.bisq_v1.TakerProtocol;
import bisq.core.trade.protocol.bisq_v1.messages.InputsForDepositTxRequest;
import bisq.core.trade.protocol.bisq_v1.model.ProcessModel;
import bisq.core.trade.protocol.bsq_swap.BsqSwapMakerProtocol;
Expand Down
25 changes: 25 additions & 0 deletions core/src/main/java/bisq/core/trade/protocol/BuyerProtocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.trade.protocol;

import bisq.common.handlers.ErrorMessageHandler;
import bisq.common.handlers.ResultHandler;

public interface BuyerProtocol {
void onPaymentStarted(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.trade.protocol.bisq_v1;
package bisq.core.trade.protocol;


import bisq.core.trade.protocol.bisq_v1.messages.InputsForDepositTxRequest;
Expand Down
26 changes: 26 additions & 0 deletions core/src/main/java/bisq/core/trade/protocol/SellerProtocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.trade.protocol;

import bisq.common.handlers.ErrorMessageHandler;
import bisq.common.handlers.ResultHandler;

public interface SellerProtocol {

void onPaymentReceived(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.trade.protocol.bisq_v1;

import bisq.core.trade.protocol.FluentProtocol;
package bisq.core.trade.protocol;

public interface TakerProtocol {
void onTakeOffer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,55 @@
import bisq.core.trade.model.bsq_swap.BsqSwapBuyerAsTakerTrade;
import bisq.core.trade.model.bsq_swap.BsqSwapSellerAsMakerTrade;
import bisq.core.trade.model.bsq_swap.BsqSwapSellerAsTakerTrade;
import bisq.core.trade.model.bsq_swap.BsqSwapTrade;
import bisq.core.trade.protocol.bisq_v1.BuyerAsMakerProtocol;
import bisq.core.trade.protocol.bisq_v1.BuyerAsTakerProtocol;
import bisq.core.trade.protocol.bisq_v1.SellerAsMakerProtocol;
import bisq.core.trade.protocol.bisq_v1.SellerAsTakerProtocol;
import bisq.core.trade.protocol.bisq_v5.BuyerAsMakerProtocol_v5;
import bisq.core.trade.protocol.bisq_v5.BuyerAsTakerProtocol_v5;
import bisq.core.trade.protocol.bisq_v5.SellerAsMakerProtocol_v5;
import bisq.core.trade.protocol.bisq_v5.SellerAsTakerProtocol_v5;
import bisq.core.trade.protocol.bsq_swap.BsqSwapBuyerAsMakerProtocol;
import bisq.core.trade.protocol.bsq_swap.BsqSwapBuyerAsTakerProtocol;
import bisq.core.trade.protocol.bsq_swap.BsqSwapSellerAsMakerProtocol;
import bisq.core.trade.protocol.bsq_swap.BsqSwapSellerAsTakerProtocol;

import bisq.common.app.Version;

public class TradeProtocolFactory {
public static TradeProtocol getNewTradeProtocol(TradeModel tradeModel) {
if (tradeModel instanceof BsqSwapTrade) {
if (tradeModel instanceof BsqSwapBuyerAsMakerTrade) {
return new BsqSwapBuyerAsMakerProtocol((BsqSwapBuyerAsMakerTrade) tradeModel);
} else if (tradeModel instanceof BsqSwapBuyerAsTakerTrade) {
return new BsqSwapBuyerAsTakerProtocol((BsqSwapBuyerAsTakerTrade) tradeModel);
} else if (tradeModel instanceof BsqSwapSellerAsMakerTrade) {
return new BsqSwapSellerAsMakerProtocol((BsqSwapSellerAsMakerTrade) tradeModel);
} else if (tradeModel instanceof BsqSwapSellerAsTakerTrade) {
return new BsqSwapSellerAsTakerProtocol((BsqSwapSellerAsTakerTrade) tradeModel);
}
}

boolean tradeProtocolVersion5Activated = Version.isTradeProtocolVersion5Activated();
if (tradeModel instanceof BuyerAsMakerTrade) {
return new BuyerAsMakerProtocol((BuyerAsMakerTrade) tradeModel);
return tradeProtocolVersion5Activated ?
new BuyerAsMakerProtocol_v5((BuyerAsMakerTrade) tradeModel) :
new BuyerAsMakerProtocol((BuyerAsMakerTrade) tradeModel);
} else if (tradeModel instanceof BuyerAsTakerTrade) {
return new BuyerAsTakerProtocol((BuyerAsTakerTrade) tradeModel);
return tradeProtocolVersion5Activated ?
new BuyerAsTakerProtocol_v5((BuyerAsTakerTrade) tradeModel) :
new BuyerAsTakerProtocol((BuyerAsTakerTrade) tradeModel);
} else if (tradeModel instanceof SellerAsMakerTrade) {
return new SellerAsMakerProtocol((SellerAsMakerTrade) tradeModel);
return tradeProtocolVersion5Activated ?
new SellerAsMakerProtocol_v5((SellerAsMakerTrade) tradeModel) :
new SellerAsMakerProtocol((SellerAsMakerTrade) tradeModel);
} else if (tradeModel instanceof SellerAsTakerTrade) {
return new SellerAsTakerProtocol((SellerAsTakerTrade) tradeModel);
} else if (tradeModel instanceof BsqSwapBuyerAsMakerTrade) {
return new BsqSwapBuyerAsMakerProtocol((BsqSwapBuyerAsMakerTrade) tradeModel);
} else if (tradeModel instanceof BsqSwapBuyerAsTakerTrade) {
return new BsqSwapBuyerAsTakerProtocol((BsqSwapBuyerAsTakerTrade) tradeModel);
} else if (tradeModel instanceof BsqSwapSellerAsMakerTrade) {
return new BsqSwapSellerAsMakerProtocol((BsqSwapSellerAsMakerTrade) tradeModel);
} else if (tradeModel instanceof BsqSwapSellerAsTakerTrade) {
return new BsqSwapSellerAsTakerProtocol((BsqSwapSellerAsTakerTrade) tradeModel);
} else
throw new IllegalStateException("Trade not of expected type. Trade=" + tradeModel);
return tradeProtocolVersion5Activated ?
new SellerAsTakerProtocol_v5((SellerAsTakerTrade) tradeModel) :
new SellerAsTakerProtocol((SellerAsTakerTrade) tradeModel);
}

throw new IllegalStateException("Trade not of expected type. Trade=" + tradeModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import bisq.core.trade.model.bisq_v1.BuyerTrade;
import bisq.core.trade.model.bisq_v1.Trade;
import bisq.core.trade.protocol.BuyerProtocol;
import bisq.core.trade.protocol.FluentProtocol;
import bisq.core.trade.protocol.TradeMessage;
import bisq.core.trade.protocol.TradeTaskRunner;
Expand All @@ -45,7 +46,7 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class BuyerProtocol extends DisputeProtocol {
abstract class BaseBuyerProtocol extends DisputeProtocol implements BuyerProtocol {
enum BuyerEvent implements FluentProtocol.Event {
STARTUP,
PAYMENT_SENT
Expand All @@ -55,7 +56,7 @@ enum BuyerEvent implements FluentProtocol.Event {
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////

public BuyerProtocol(BuyerTrade trade) {
protected BaseBuyerProtocol(BuyerTrade trade) {
super(trade);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bisq.core.trade.model.bisq_v1.SellerTrade;
import bisq.core.trade.model.bisq_v1.Trade;
import bisq.core.trade.protocol.FluentProtocol;
import bisq.core.trade.protocol.SellerProtocol;
import bisq.core.trade.protocol.TradeMessage;
import bisq.core.trade.protocol.TradeTaskRunner;
import bisq.core.trade.protocol.bisq_v1.messages.CounterCurrencyTransferStartedMessage;
Expand Down Expand Up @@ -47,13 +48,13 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class SellerProtocol extends DisputeProtocol {
abstract class BaseSellerProtocol extends DisputeProtocol implements SellerProtocol {
enum SellerEvent implements FluentProtocol.Event {
STARTUP,
PAYMENT_RECEIVED
}

public SellerProtocol(SellerTrade trade) {
protected BaseSellerProtocol(SellerTrade trade) {
super(trade);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import bisq.core.trade.model.bisq_v1.BuyerAsMakerTrade;
import bisq.core.trade.model.bisq_v1.Trade;
import bisq.core.trade.protocol.MakerProtocol;
import bisq.core.trade.protocol.TradeTaskRunner;
import bisq.core.trade.protocol.bisq_v1.messages.DelayedPayoutTxSignatureRequest;
import bisq.core.trade.protocol.bisq_v1.messages.DepositTxAndDelayedPayoutTxMessage;
Expand Down Expand Up @@ -49,7 +50,7 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BuyerAsMakerProtocol extends BuyerProtocol implements MakerProtocol {
public class BuyerAsMakerProtocol extends BaseBuyerProtocol implements MakerProtocol {

///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
Expand Down Expand Up @@ -111,7 +112,6 @@ protected void handle(DelayedPayoutTxSignatureRequest message, NodeAddress peer)
.executeTasks();
}

// We keep the handler here in as well to make it more transparent which messages we expect
@Override
protected void handle(DepositTxAndDelayedPayoutTxMessage message, NodeAddress peer) {
super.handle(message, peer);
Expand All @@ -122,7 +122,6 @@ protected void handle(DepositTxAndDelayedPayoutTxMessage message, NodeAddress pe
// User interaction
///////////////////////////////////////////////////////////////////////////////////////////

// We keep the handler here in as well to make it more transparent which events we expect
@Override
public void onPaymentStarted(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
super.onPaymentStarted(resultHandler, errorMessageHandler);
Expand All @@ -133,7 +132,6 @@ public void onPaymentStarted(ResultHandler resultHandler, ErrorMessageHandler er
// Incoming message Payout tx
///////////////////////////////////////////////////////////////////////////////////////////

// We keep the handler here in as well to make it more transparent which messages we expect
@Override
protected void handle(PayoutTxPublishedMessage message, NodeAddress peer) {
super.handle(message, peer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import bisq.core.offer.Offer;
import bisq.core.trade.model.bisq_v1.BuyerAsTakerTrade;
import bisq.core.trade.model.bisq_v1.Trade;
import bisq.core.trade.protocol.TakerProtocol;
import bisq.core.trade.protocol.TradeMessage;
import bisq.core.trade.protocol.bisq_v1.messages.DelayedPayoutTxSignatureRequest;
import bisq.core.trade.protocol.bisq_v1.messages.DepositTxAndDelayedPayoutTxMessage;
Expand Down Expand Up @@ -55,7 +56,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

@Slf4j
public class BuyerAsTakerProtocol extends BuyerProtocol implements TakerProtocol {
public class BuyerAsTakerProtocol extends BaseBuyerProtocol implements TakerProtocol {

///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
Expand Down Expand Up @@ -126,7 +127,6 @@ protected void handle(DelayedPayoutTxSignatureRequest message, NodeAddress peer)
.executeTasks();
}

// We keep the handler here in as well to make it more transparent which messages we expect
@Override
protected void handle(DepositTxAndDelayedPayoutTxMessage message, NodeAddress peer) {
super.handle(message, peer);
Expand All @@ -137,7 +137,6 @@ protected void handle(DepositTxAndDelayedPayoutTxMessage message, NodeAddress pe
// User interaction
///////////////////////////////////////////////////////////////////////////////////////////

// We keep the handler here in as well to make it more transparent which events we expect
@Override
public void onPaymentStarted(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
super.onPaymentStarted(resultHandler, errorMessageHandler);
Expand All @@ -148,7 +147,6 @@ public void onPaymentStarted(ResultHandler resultHandler, ErrorMessageHandler er
// Incoming message Payout tx
///////////////////////////////////////////////////////////////////////////////////////////

// We keep the handler here in as well to make it more transparent which messages we expect
@Override
protected void handle(PayoutTxPublishedMessage message, NodeAddress peer) {
super.handle(message, peer);
Expand Down
Loading