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

Verify maker & taker fee transactions via Mempool lookup #5160

Merged
merged 2 commits into from Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/src/main/java/bisq/common/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public class Config {
public static final String BTC_TX_FEE = "btcTxFee";
public static final String BTC_MIN_TX_FEE = "btcMinTxFee";
public static final String BTC_FEES_TS = "bitcoinFeesTs";
public static final String BYPASS_MEMPOOL_VALIDATION = "bypassMempoolValidation";

// Default values for certain options
public static final int UNSPECIFIED_PORT = -1;
Expand Down Expand Up @@ -212,6 +213,7 @@ public class Config {
public final int apiPort;
public final boolean preventPeriodicShutdownAtSeedNode;
public final boolean republishMailboxEntries;
public final boolean bypassMempoolValidation;

// Properties derived from options but not exposed as options themselves
public final File torDir;
Expand Down Expand Up @@ -660,6 +662,13 @@ public Config(String defaultAppName, File defaultUserDataDir, String... args) {
.ofType(boolean.class)
.defaultsTo(false);

ArgumentAcceptingOptionSpec<Boolean> bypassMempoolValidationOpt =
parser.accepts(BYPASS_MEMPOOL_VALIDATION,
"Prevents mempool check of trade parameters")
.withRequiredArg()
.ofType(boolean.class)
.defaultsTo(false);

try {
CompositeOptionSet options = new CompositeOptionSet();

Expand Down Expand Up @@ -777,6 +786,7 @@ public Config(String defaultAppName, File defaultUserDataDir, String... args) {
this.apiPort = options.valueOf(apiPortOpt);
this.preventPeriodicShutdownAtSeedNode = options.valueOf(preventPeriodicShutdownAtSeedNodeOpt);
this.republishMailboxEntries = options.valueOf(republishMailboxEntriesOpt);
this.bypassMempoolValidation = options.valueOf(bypassMempoolValidationOpt);
} catch (OptionException ex) {
throw new ConfigException("problem parsing option '%s': %s",
ex.options().get(0),
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/java/bisq/core/app/DomainInitialisation.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import bisq.core.payment.RevolutAccount;
import bisq.core.payment.TradeLimits;
import bisq.core.provider.fee.FeeService;
import bisq.core.provider.mempool.MempoolService;
import bisq.core.provider.price.PriceFeedService;
import bisq.core.support.dispute.arbitration.ArbitrationManager;
import bisq.core.support.dispute.arbitration.arbitrator.ArbitratorManager;
Expand Down Expand Up @@ -110,6 +111,7 @@ public class DomainInitialisation {
private final User user;
private final DaoStateSnapshotService daoStateSnapshotService;
private final TriggerPriceService triggerPriceService;
private final MempoolService mempoolService;

@Inject
public DomainInitialisation(ClockWatcher clockWatcher,
Expand Down Expand Up @@ -146,7 +148,8 @@ public DomainInitialisation(ClockWatcher clockWatcher,
MarketAlerts marketAlerts,
User user,
DaoStateSnapshotService daoStateSnapshotService,
TriggerPriceService triggerPriceService) {
TriggerPriceService triggerPriceService,
MempoolService mempoolService) {
this.clockWatcher = clockWatcher;
this.tradeLimits = tradeLimits;
this.arbitrationManager = arbitrationManager;
Expand Down Expand Up @@ -182,6 +185,7 @@ public DomainInitialisation(ClockWatcher clockWatcher,
this.user = user;
this.daoStateSnapshotService = daoStateSnapshotService;
this.triggerPriceService = triggerPriceService;
this.mempoolService = mempoolService;
}

public void initDomainServices(Consumer<String> rejectedTxErrorMessageHandler,
Expand Down Expand Up @@ -264,6 +268,7 @@ public void initDomainServices(Consumer<String> rejectedTxErrorMessageHandler,
priceAlert.onAllServicesInitialized();
marketAlerts.onAllServicesInitialized();
triggerPriceService.onAllServicesInitialized();
mempoolService.onAllServicesInitialized();

if (revolutAccountsUpdateHandler != null) {
revolutAccountsUpdateHandler.accept(user.getPaymentAccountsAsObservable().stream()
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/bisq/core/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
private final Set<String> nodeAddressesBannedFromNetwork;
private final boolean disableApi;

// added at v1.6.0
private final boolean disableMempoolValidation;

// After we have created the signature from the filter data we clone it and apply the signature
static Filter cloneWithSig(Filter filter, String signatureAsBase64) {
return new Filter(filter.getBannedOfferIds(),
Expand Down Expand Up @@ -126,6 +129,7 @@ static Filter cloneWithSig(Filter filter, String signatureAsBase64) {
filter.isDisableAutoConf(),
filter.getBannedAutoConfExplorers(),
filter.getNodeAddressesBannedFromNetwork(),
filter.isDisableMempoolValidation(),
filter.isDisableApi());
}

Expand Down Expand Up @@ -157,6 +161,7 @@ static Filter cloneWithoutSig(Filter filter) {
filter.isDisableAutoConf(),
filter.getBannedAutoConfExplorers(),
filter.getNodeAddressesBannedFromNetwork(),
filter.isDisableMempoolValidation(),
filter.isDisableApi());
}

Expand All @@ -183,6 +188,7 @@ public Filter(List<String> bannedOfferIds,
boolean disableAutoConf,
List<String> bannedAutoConfExplorers,
Set<String> nodeAddressesBannedFromNetwork,
boolean disableMempoolValidation,
boolean disableApi) {
this(bannedOfferIds,
nodeAddressesBannedFromTrading,
Expand Down Expand Up @@ -210,6 +216,7 @@ public Filter(List<String> bannedOfferIds,
disableAutoConf,
bannedAutoConfExplorers,
nodeAddressesBannedFromNetwork,
disableMempoolValidation,
disableApi);
}

Expand Down Expand Up @@ -245,6 +252,7 @@ public Filter(List<String> bannedOfferIds,
boolean disableAutoConf,
List<String> bannedAutoConfExplorers,
Set<String> nodeAddressesBannedFromNetwork,
boolean disableMempoolValidation,
boolean disableApi) {
this.bannedOfferIds = bannedOfferIds;
this.nodeAddressesBannedFromTrading = nodeAddressesBannedFromTrading;
Expand Down Expand Up @@ -272,6 +280,7 @@ public Filter(List<String> bannedOfferIds,
this.disableAutoConf = disableAutoConf;
this.bannedAutoConfExplorers = bannedAutoConfExplorers;
this.nodeAddressesBannedFromNetwork = nodeAddressesBannedFromNetwork;
this.disableMempoolValidation = disableMempoolValidation;
this.disableApi = disableApi;

// ownerPubKeyBytes can be null when called from tests
Expand Down Expand Up @@ -312,6 +321,7 @@ public protobuf.StoragePayload toProtoMessage() {
.setDisableAutoConf(disableAutoConf)
.addAllBannedAutoConfExplorers(bannedAutoConfExplorers)
.addAllNodeAddressesBannedFromNetwork(nodeAddressesBannedFromNetwork)
.setDisableMempoolValidation(disableMempoolValidation)
.setDisableApi(disableApi);

Optional.ofNullable(signatureAsBase64).ifPresent(builder::setSignatureAsBase64);
Expand Down Expand Up @@ -352,6 +362,7 @@ public static Filter fromProto(protobuf.Filter proto) {
proto.getDisableAutoConf(),
ProtoUtil.protocolStringListToList(proto.getBannedAutoConfExplorersList()),
ProtoUtil.protocolStringListToSet(proto.getNodeAddressesBannedFromNetworkList()),
proto.getDisableMempoolValidation(),
proto.getDisableApi()
);
}
Expand Down Expand Up @@ -396,6 +407,7 @@ public String toString() {
",\n ownerPubKey=" + ownerPubKey +
",\n disableAutoConf=" + disableAutoConf +
",\n nodeAddressesBannedFromNetwork=" + nodeAddressesBannedFromNetwork +
",\n disableMempoolValidation=" + disableMempoolValidation +
",\n disableApi=" + disableApi +
"\n}";
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/bisq/core/offer/OpenOffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public enum State {
// If market price reaches that trigger price the offer gets deactivated
@Getter
private final long triggerPrice;
@Getter
@Setter
transient private long mempoolStatus = -1;

public OpenOffer(Offer offer) {
this(offer, 0);
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/java/bisq/core/offer/TriggerPriceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bisq.core.locale.CurrencyUtil;
import bisq.core.monetary.Altcoin;
import bisq.core.monetary.Price;
import bisq.core.provider.mempool.MempoolService;
import bisq.core.provider.price.MarketPrice;
import bisq.core.provider.price.PriceFeedService;

Expand Down Expand Up @@ -52,15 +53,18 @@
public class TriggerPriceService {
private final P2PService p2PService;
private final OpenOfferManager openOfferManager;
private final MempoolService mempoolService;
private final PriceFeedService priceFeedService;
private final Map<String, Set<OpenOffer>> openOffersByCurrency = new HashMap<>();

@Inject
public TriggerPriceService(P2PService p2PService,
OpenOfferManager openOfferManager,
MempoolService mempoolService,
PriceFeedService priceFeedService) {
this.p2PService = p2PService;
this.openOfferManager = openOfferManager;
this.mempoolService = mempoolService;
this.priceFeedService = priceFeedService;
}

Expand Down Expand Up @@ -152,6 +156,20 @@ private void checkPriceThreshold(MarketPrice marketPrice, OpenOffer openOffer) {
openOfferManager.deactivateOpenOffer(openOffer, () -> {
}, errorMessage -> {
});
} else if (openOffer.getState() == OpenOffer.State.AVAILABLE) {
// check the mempool if it has not been done before
if (openOffer.getMempoolStatus() < 0 && mempoolService.canRequestBeMade(openOffer.getOffer().getOfferPayload())) {
mempoolService.validateOfferMakerTx(openOffer.getOffer().getOfferPayload(), (txValidator -> {
openOffer.setMempoolStatus(txValidator.isFail() ? 0 : 1);
}));
}
// if the mempool indicated failure then deactivate the open offer
if (openOffer.getMempoolStatus() == 0) {
log.info("Deactivating open offer {} due to mempool validation", openOffer.getOffer().getShortId());
openOfferManager.deactivateOpenOffer(openOffer, () -> {
}, errorMessage -> {
});
}
}
}

Expand Down
45 changes: 45 additions & 0 deletions core/src/main/java/bisq/core/provider/MempoolHttpClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.provider;

import bisq.network.Socks5ProxyProvider;
import bisq.network.http.HttpClientImpl;

import bisq.common.app.Version;

import javax.inject.Inject;
import javax.inject.Singleton;

import java.io.IOException;

import javax.annotation.Nullable;

@Singleton
public class MempoolHttpClient extends HttpClientImpl {
@Inject
public MempoolHttpClient(@Nullable Socks5ProxyProvider socks5ProxyProvider) {
super(socks5ProxyProvider);
}

// returns JSON of the transaction details
public String getTxDetails(String txId) throws IOException {
super.shutDown(); // close any prior incomplete request
String api = "/" + txId;
return get(api, "User-Agent", "bisq/" + Version.VERSION);
}
}
86 changes: 86 additions & 0 deletions core/src/main/java/bisq/core/provider/mempool/MempoolRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.provider.mempool;

import bisq.core.provider.MempoolHttpClient;
import bisq.core.user.Preferences;

import bisq.network.Socks5ProxyProvider;

import bisq.common.util.Utilities;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import lombok.extern.slf4j.Slf4j;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;

@Slf4j
public class MempoolRequest {
private static final ListeningExecutorService executorService = Utilities.getListeningExecutorService("MempoolRequest", 3, 5, 10 * 60);
private final List<String> txBroadcastServices = new ArrayList<>();
private final MempoolHttpClient mempoolHttpClient;

public MempoolRequest(Preferences preferences, Socks5ProxyProvider socks5ProxyProvider) {
this.txBroadcastServices.addAll(preferences.getDefaultTxBroadcastServices());
this.mempoolHttpClient = new MempoolHttpClient(socks5ProxyProvider);
}

public void getTxStatus(SettableFuture<String> mempoolServiceCallback, String txId) {
mempoolHttpClient.setBaseUrl(getRandomServiceAddress(txBroadcastServices));
ListenableFuture<String> future = executorService.submit(() -> {
Thread.currentThread().setName("MempoolRequest @ " + mempoolHttpClient.getBaseUrl());
log.info("Making http request for information on txId: {}", txId);
return mempoolHttpClient.getTxDetails(txId);
});

Futures.addCallback(future, new FutureCallback<>() {
public void onSuccess(String mempoolData) {
log.info("Received mempoolData of [{}] from provider", mempoolData);
mempoolServiceCallback.set(mempoolData);
}
public void onFailure(@NotNull Throwable throwable) {
mempoolServiceCallback.setException(throwable);
}
}, MoreExecutors.directExecutor());
}

public boolean switchToAnotherProvider() {
txBroadcastServices.remove(mempoolHttpClient.getBaseUrl());
return txBroadcastServices.size() > 0;
}

@Nullable
private static String getRandomServiceAddress(List<String> txBroadcastServices) {
List<String> list = checkNotNull(txBroadcastServices);
return !list.isEmpty() ? list.get(new Random().nextInt(list.size())) : null;
}
}

Loading