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

Add local Bitcoin node configuration detection #3982

Merged
merged 20 commits into from Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 1 addition & 7 deletions core/src/main/java/bisq/core/app/BisqSetup.java
Expand Up @@ -483,13 +483,7 @@ private void maybeShowTac() {
}

private void maybeCheckLocalBitcoinNode(Runnable nextStep) {
BaseCurrencyNetwork baseCurrencyNetwork = config.baseCurrencyNetwork;

var shouldIgnoreLocalNode =
config.ignoreLocalBtcNode
|| baseCurrencyNetwork.isDaoRegTest()
|| baseCurrencyNetwork.isDaoTestNet();
if (shouldIgnoreLocalNode) {
if (localBitcoinNode.willIgnore()) {
nextStep.run();
return;
}
Expand Down
33 changes: 32 additions & 1 deletion core/src/main/java/bisq/core/btc/nodes/LocalBitcoinNode.java
@@ -1,5 +1,8 @@
package bisq.core.btc.nodes;

import bisq.common.config.BaseCurrencyNetwork;
import bisq.common.config.Config;

import org.bitcoinj.core.AbstractBlockChain;
import org.bitcoinj.core.Context;
import org.bitcoinj.core.Peer;
Expand Down Expand Up @@ -50,16 +53,44 @@ public class LocalBitcoinNode {
private static final Logger log = LoggerFactory.getLogger(LocalBitcoinNode.class);
private static final int CONNECTION_TIMEOUT = 5000;

private final Config config;

private final int port;

private Boolean detected;
private Boolean wellConfigured;

@Inject
public LocalBitcoinNode(@Named(LOCAL_BITCOIN_NODE_PORT) int port) {
public LocalBitcoinNode(Config config, @Named(LOCAL_BITCOIN_NODE_PORT) int port) {
this.config = config;
this.port = port;
}

/**
* Returns whether Bisq will use a local Bitcoin node. Meaning a usable node was found
* and conditions under which we would ignore it are not met. If we're ignoring the
* local node, a call to this method will not trigger an unnecessary detection
* attempt.
*/
public boolean willUse() {
return !willIgnore() && isUsable();
}

/**
* Returns whether Bisq will ignore a local Bitcoin node even if it is usable.
*/
public boolean willIgnore() {
BaseCurrencyNetwork baseCurrencyNetwork = config.baseCurrencyNetwork;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line

// For dao testnet (server side regtest) we disable the use of local bitcoin node to
// avoid confusion if local btc node is not synced with our dao testnet master node.
// Note: above comment was previously in WalletConfig::createPeerGroup.

return config.ignoreLocalBtcNode
|| baseCurrencyNetwork.isDaoRegTest()
dmos62 marked this conversation as resolved.
Show resolved Hide resolved
|| baseCurrencyNetwork.isDaoTestNet();
dmos62 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Returns whether or not a local Bitcion node was detected and was well configured
* at the time the checks were performed. All checks are triggered in case they have
Expand Down
7 changes: 1 addition & 6 deletions core/src/main/java/bisq/core/btc/setup/WalletConfig.java
Expand Up @@ -239,12 +239,7 @@ private PeerGroup createPeerGroup() {
peerGroup.setConnectTimeoutMillis(TOR_VERSION_EXCHANGE_TIMEOUT);
}

// For dao testnet (server side regtest) we disable the use of local bitcoin node to
// avoid confusion if local btc node is not synced with our dao testnet master node.
// It is also disabled if the local node was not found or was found to be misconfigured.
if (Config.baseCurrencyNetwork().isDaoRegTest() ||
Config.baseCurrencyNetwork().isDaoTestNet() ||
!localBitcoinNode.isUsable())
if (!localBitcoinNode.willUse())
peerGroup.setUseLocalhostPeerWhenPossible(false);

return peerGroup;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/btc/setup/WalletsSetup.java
Expand Up @@ -278,7 +278,7 @@ protected void onSetupCompleted() {
return;
}
}
} else if (localBitcoinNode.isUsable()) {
} else if (localBitcoinNode.willUse()) {
walletConfig.setMinBroadcastConnections(1);
walletConfig.setPeerNodesForLocalHost();
} else {
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/bisq/core/user/Preferences.java
Expand Up @@ -736,12 +736,13 @@ public boolean showAgain(String key) {
}

public boolean getUseTorForBitcoinJ() {
// We override the useTorForBitcoinJ and set it to false if we found a usable localhost node or if we are not on mainnet,
// unless the useTorForBtc parameter is explicitly provided.
// On testnet there are very few Bitcoin tor nodes and we don't provide tor nodes.
// We override the useTorForBitcoinJ and set it to false if we will use a
// localhost Bitcoin node or if we are not on mainnet, unless the useTorForBtc
// parameter is explicitly provided. On testnet there are very few Bitcoin tor
// nodes and we don't provide tor nodes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line

if ((!Config.baseCurrencyNetwork().isMainnet()
|| localBitcoinNode.isUsable())
|| localBitcoinNode.willUse())
&& !config.useTorForBtcOptionSetExplicitly)
return false;
else
Expand Down
2 changes: 1 addition & 1 deletion desktop/src/main/java/bisq/desktop/main/MainViewModel.java
Expand Up @@ -451,7 +451,7 @@ private void setupBtcNumPeersWatcher() {
checkNumberOfBtcPeersTimer = UserThread.runAfter(() -> {
// check again numPeers
if (walletsSetup.numPeersProperty().get() == 0) {
if (localBitcoinNode.isUsable())
if (localBitcoinNode.willUse())
getWalletServiceErrorMsg().set(
Res.get("mainView.networkWarning.localhostBitcoinLost", Res.getBaseCurrencyName().toLowerCase()));
else
Expand Down
Expand Up @@ -165,7 +165,7 @@ public void initialize() {
bitcoinPeerSubVersionColumn.setGraphic(new AutoTooltipLabel(Res.get("settings.net.subVersionColumn")));
bitcoinPeerHeightColumn.setGraphic(new AutoTooltipLabel(Res.get("settings.net.heightColumn")));
localhostBtcNodeInfoLabel.setText(Res.get("settings.net.localhostBtcNodeInfo"));
if (!localBitcoinNode.isUsable()) {
if (!localBitcoinNode.willUse()) {
localhostBtcNodeInfoLabel.setVisible(false);
}
useProvidedNodesRadio.setText(Res.get("settings.net.useProvidedNodesRadio"));
Expand Down Expand Up @@ -380,14 +380,14 @@ private void showShutDownPopup() {
}

private void onBitcoinPeersToggleSelected(boolean calledFromUser) {
boolean bitcoinLocalhostNodeRunning = localBitcoinNode.isUsable();
useTorForBtcJCheckBox.setDisable(bitcoinLocalhostNodeRunning);
bitcoinNodesLabel.setDisable(bitcoinLocalhostNodeRunning);
btcNodesLabel.setDisable(bitcoinLocalhostNodeRunning);
btcNodesInputTextField.setDisable(bitcoinLocalhostNodeRunning);
useProvidedNodesRadio.setDisable(bitcoinLocalhostNodeRunning || !btcNodes.useProvidedBtcNodes());
useCustomNodesRadio.setDisable(bitcoinLocalhostNodeRunning);
usePublicNodesRadio.setDisable(bitcoinLocalhostNodeRunning || isPreventPublicBtcNetwork());
boolean bitcoinLocalhostNodeBeingUsed = localBitcoinNode.willUse();
useTorForBtcJCheckBox.setDisable(bitcoinLocalhostNodeBeingUsed);
bitcoinNodesLabel.setDisable(bitcoinLocalhostNodeBeingUsed);
btcNodesLabel.setDisable(bitcoinLocalhostNodeBeingUsed);
btcNodesInputTextField.setDisable(bitcoinLocalhostNodeBeingUsed);
useProvidedNodesRadio.setDisable(bitcoinLocalhostNodeBeingUsed || !btcNodes.useProvidedBtcNodes());
useCustomNodesRadio.setDisable(bitcoinLocalhostNodeBeingUsed);
usePublicNodesRadio.setDisable(bitcoinLocalhostNodeBeingUsed || isPreventPublicBtcNetwork());

BtcNodes.BitcoinNodesOption currentBitcoinNodesOption = BtcNodes.BitcoinNodesOption.values()[preferences.getBitcoinNodesOptionOrdinal()];

Expand Down Expand Up @@ -454,7 +454,7 @@ private void onBitcoinPeersToggleSelected(boolean calledFromUser) {

private void applyPreventPublicBtcNetwork() {
final boolean preventPublicBtcNetwork = isPreventPublicBtcNetwork();
usePublicNodesRadio.setDisable(localBitcoinNode.isUsable() || preventPublicBtcNetwork);
usePublicNodesRadio.setDisable(localBitcoinNode.willUse() || preventPublicBtcNetwork);
if (preventPublicBtcNetwork && selectedBitcoinNodesOption == BtcNodes.BitcoinNodesOption.PUBLIC) {
selectedBitcoinNodesOption = BtcNodes.BitcoinNodesOption.PROVIDED;
preferences.setBitcoinNodesOptionOrdinal(selectedBitcoinNodesOption.ordinal());
Expand Down