Centralize some of local BTC node logic

Introduces LocalBitcoinNode::willUse and ::willIgnore to move logic that
was previously littered throughout the codebase into one place. Also,
changes the usages of LocalBitcoinNode to be more precise, specifying
which of these questions are being asked:

- "is there a local BTC node" (isDetected);
- "is it well configured" (isWellConfigured and isUsable);
- "will we ignore a local BTC node even if we found a usable one"
  (willIgnore);
- "is there a usable local BTC node and will we use it" (willUse).

These changes make related logic much easier to maintain and to read.
This commit is contained in:
Dominykas Mostauskis 2020-02-25 15:56:02 +01:00
parent aceb608e3a
commit 6b4878ada8
No known key found for this signature in database
GPG key ID: 97B407C08F2499EB
7 changed files with 51 additions and 30 deletions

View file

@ -483,13 +483,7 @@ public class BisqSetup {
}
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;
}

View file

@ -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;
@ -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;
// 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()
|| baseCurrencyNetwork.isDaoTestNet();
}
/**
* 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

View file

@ -239,12 +239,7 @@ public class WalletConfig extends AbstractIdleService {
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;

View file

@ -278,7 +278,7 @@ public class WalletsSetup {
return;
}
}
} else if (localBitcoinNode.isUsable()) {
} else if (localBitcoinNode.willUse()) {
walletConfig.setMinBroadcastConnections(1);
walletConfig.setPeerNodesForLocalHost();
} else {

View file

@ -736,12 +736,13 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid
}
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.
if ((!Config.baseCurrencyNetwork().isMainnet()
|| localBitcoinNode.isUsable())
|| localBitcoinNode.willUse())
&& !config.useTorForBtcOptionSetExplicitly)
return false;
else

View file

@ -451,7 +451,7 @@ public class MainViewModel implements ViewModel, BisqSetup.BisqSetupListener {
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

View file

@ -165,7 +165,7 @@ public class NetworkSettingsView extends ActivatableView<GridPane, Void> {
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"));
@ -380,14 +380,14 @@ public class NetworkSettingsView extends ActivatableView<GridPane, Void> {
}
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()];
@ -454,7 +454,7 @@ public class NetworkSettingsView extends ActivatableView<GridPane, Void> {
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());