From 846aa494caf860f2103c7a6a0bdc0e8c93ef2820 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Wed, 10 Jan 2018 19:41:26 +0100 Subject: [PATCH] Add check for min peers connected for broadcast --- .../core/btc/wallet/TradeWalletService.java | 8 ++++--- .../io/bisq/core/btc/wallet/WalletsSetup.java | 10 +++++++- .../createoffer/CreateOfferViewModel.java | 6 ++++- .../main/offer/offerbook/OfferBookView.java | 2 +- .../offer/offerbook/OfferBookViewModel.java | 23 +++++++++++++++---- .../openoffer/OpenOffersViewModel.java | 4 +++- 6 files changed, 42 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/io/bisq/core/btc/wallet/TradeWalletService.java b/core/src/main/java/io/bisq/core/btc/wallet/TradeWalletService.java index 18693b14f5..176728e4fe 100644 --- a/core/src/main/java/io/bisq/core/btc/wallet/TradeWalletService.java +++ b/core/src/main/java/io/bisq/core/btc/wallet/TradeWalletService.java @@ -1205,9 +1205,10 @@ public class TradeWalletService { } private void addAvailableInputsAndChangeOutputs(Transaction transaction, Address address, Address changeAddress, Coin txFee) throws WalletException { + SendRequest sendRequest = null; try { // Lets let the framework do the work to find the right inputs - SendRequest sendRequest = SendRequest.forTx(transaction); + sendRequest = SendRequest.forTx(transaction); sendRequest.shuffleOutputs = false; sendRequest.aesKey = aesKey; // We use a fixed fee @@ -1222,10 +1223,11 @@ public class TradeWalletService { // We don't commit that tx to the wallet as it will be changed later and it's not signed yet. // So it will not change the wallet balance. checkNotNull(wallet, "wallet must not be null"); - // TODO we got here exceptions with missing funds. Not reproducable but leave log for better debugging. - log.info("print tx before wallet.completeTx: " + sendRequest.tx.toString()); wallet.completeTx(sendRequest); } catch (Throwable t) { + if (sendRequest != null && sendRequest.tx != null) + log.warn("addAvailableInputsAndChangeOutputs: sendRequest.tx={}, sendRequest.tx.getOutputs()={}", sendRequest.tx, sendRequest.tx.getOutputs()); + throw new WalletException(t); } } diff --git a/core/src/main/java/io/bisq/core/btc/wallet/WalletsSetup.java b/core/src/main/java/io/bisq/core/btc/wallet/WalletsSetup.java index 2e35485baf..3c8c127fc0 100644 --- a/core/src/main/java/io/bisq/core/btc/wallet/WalletsSetup.java +++ b/core/src/main/java/io/bisq/core/btc/wallet/WalletsSetup.java @@ -97,6 +97,7 @@ public class WalletsSetup { private final List setupCompletedHandlers = new ArrayList<>(); public final BooleanProperty shutDownComplete = new SimpleBooleanProperty(); private WalletConfig walletConfig; + private int minBroadcastConnections; /////////////////////////////////////////////////////////////////////////////////////////// // Constructor @@ -273,6 +274,7 @@ public class WalletsSetup { private void configPeerNodes(Socks5Proxy socks5Proxy) { if (params == RegTestParams.get()) { + minBroadcastConnections = 1; if (regTestHost == RegTestHost.REG_TEST_SERVER) { try { walletConfig.setPeerNodes(new PeerAddress(InetAddress.getByName(RegTestHost.SERVER_IP), params.getPort())); @@ -286,6 +288,7 @@ public class WalletsSetup { } } else { List btcNodeList = new ArrayList<>(); + minBroadcastConnections = (int) Math.floor(DEFAULT_CONNECTIONS * 0.8); switch (BitcoinNodes.BitcoinNodesOption.values()[preferences.getBitcoinNodesOptionOrdinal()]) { case CUSTOM: String bitcoinNodesString = preferences.getBitcoinNodes(); @@ -307,7 +310,8 @@ public class WalletsSetup { // We require only 4 nodes instead of 7 (for 9 max connections) because our provided nodes // are more reliable than random public nodes. - walletConfig.setMinBroadcastConnections(4); + minBroadcastConnections = 4; + walletConfig.setMinBroadcastConnections(minBroadcastConnections); break; } @@ -511,6 +515,10 @@ public class WalletsSetup { .collect(Collectors.toSet()); } + public boolean hasSufficientPeersForBroadcast() { + return bisqEnvironment.isBitcoinLocalhostNodeRunning() ? true : numPeers.get() >= minBroadcastConnections; + } + /////////////////////////////////////////////////////////////////////////////////////////// // Inner classes diff --git a/gui/src/main/java/io/bisq/gui/main/offer/createoffer/CreateOfferViewModel.java b/gui/src/main/java/io/bisq/gui/main/offer/createoffer/CreateOfferViewModel.java index d512f89953..4ed346b4b2 100644 --- a/gui/src/main/java/io/bisq/gui/main/offer/createoffer/CreateOfferViewModel.java +++ b/gui/src/main/java/io/bisq/gui/main/offer/createoffer/CreateOfferViewModel.java @@ -29,6 +29,7 @@ import io.bisq.common.monetary.Volume; import io.bisq.common.util.MathUtils; import io.bisq.core.app.BisqEnvironment; import io.bisq.core.btc.Restrictions; +import io.bisq.core.btc.wallet.WalletsSetup; import io.bisq.core.offer.Offer; import io.bisq.core.offer.OfferPayload; import io.bisq.core.payment.PaymentAccount; @@ -64,6 +65,7 @@ class CreateOfferViewModel extends ActivatableWithDataModel().information(Res.get("popup.warning.notFullyConnected")).show(); diff --git a/gui/src/main/java/io/bisq/gui/main/offer/offerbook/OfferBookViewModel.java b/gui/src/main/java/io/bisq/gui/main/offer/offerbook/OfferBookViewModel.java index a1927f15a4..8c61617a62 100644 --- a/gui/src/main/java/io/bisq/gui/main/offer/offerbook/OfferBookViewModel.java +++ b/gui/src/main/java/io/bisq/gui/main/offer/offerbook/OfferBookViewModel.java @@ -26,6 +26,7 @@ import io.bisq.common.handlers.ResultHandler; import io.bisq.common.locale.*; import io.bisq.common.monetary.Price; import io.bisq.common.monetary.Volume; +import io.bisq.core.btc.wallet.WalletsSetup; import io.bisq.core.filter.FilterManager; import io.bisq.core.offer.Offer; import io.bisq.core.offer.OfferPayload; @@ -72,6 +73,7 @@ class OfferBookViewModel extends ActivatableViewModel { final PriceFeedService priceFeedService; private final ClosedTradableManager closedTradableManager; private final FilterManager filterManager; + private final WalletsSetup walletsSetup; final AccountAgeWitnessService accountAgeWitnessService; private final Navigation navigation; final BSFormatter formatter; @@ -103,10 +105,18 @@ class OfferBookViewModel extends ActivatableViewModel { @SuppressWarnings("WeakerAccess") @Inject - public OfferBookViewModel(User user, OpenOfferManager openOfferManager, OfferBook offerBook, - Preferences preferences, P2PService p2PService, PriceFeedService priceFeedService, - ClosedTradableManager closedTradableManager, FilterManager filterManager, - AccountAgeWitnessService accountAgeWitnessService, Navigation navigation, BSFormatter formatter) { + public OfferBookViewModel(User user, + OpenOfferManager openOfferManager, + OfferBook offerBook, + Preferences preferences, + P2PService p2PService, + PriceFeedService priceFeedService, + ClosedTradableManager closedTradableManager, + FilterManager filterManager, + WalletsSetup walletsSetup, + AccountAgeWitnessService accountAgeWitnessService, + Navigation navigation, + BSFormatter formatter) { super(); this.openOfferManager = openOfferManager; @@ -117,6 +127,7 @@ class OfferBookViewModel extends ActivatableViewModel { this.priceFeedService = priceFeedService; this.closedTradableManager = closedTradableManager; this.filterManager = filterManager; + this.walletsSetup = walletsSetup; this.accountAgeWitnessService = accountAgeWitnessService; this.navigation = navigation; this.formatter = formatter; @@ -247,6 +258,10 @@ class OfferBookViewModel extends ActivatableViewModel { return p2PService.isBootstrapped(); } + boolean hasSufficientPeersForBroadcast() { + return walletsSetup.hasSufficientPeersForBroadcast(); + } + TradeCurrency getSelectedTradeCurrency() { return selectedTradeCurrency; } diff --git a/gui/src/main/java/io/bisq/gui/main/portfolio/openoffer/OpenOffersViewModel.java b/gui/src/main/java/io/bisq/gui/main/portfolio/openoffer/OpenOffersViewModel.java index 56790780d4..370a136da6 100644 --- a/gui/src/main/java/io/bisq/gui/main/portfolio/openoffer/OpenOffersViewModel.java +++ b/gui/src/main/java/io/bisq/gui/main/portfolio/openoffer/OpenOffersViewModel.java @@ -36,7 +36,9 @@ class OpenOffersViewModel extends ActivatableWithDataModel @Inject - public OpenOffersViewModel(OpenOffersDataModel dataModel, P2PService p2PService, BSFormatter formatter) { + public OpenOffersViewModel(OpenOffersDataModel dataModel, + P2PService p2PService, + BSFormatter formatter) { super(dataModel); this.p2PService = p2PService;