From f24ebb9038738a4e4a5b0ff4f9915b04c52a0c7b Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Mon, 6 Apr 2015 12:14:27 +0200 Subject: [PATCH] Add DHT network separation by btc network --- .../bitsquare/app/BitsquareEnvironment.java | 32 +++++++++++++++++++ .../java/io/bitsquare/btc/RegTestHost.java | 4 +-- .../java/io/bitsquare/btc/WalletService.java | 2 +- .../java/io/bitsquare/p2p/BootstrapNodes.java | 6 ++-- core/src/main/java/io/bitsquare/p2p/Node.java | 5 --- 5 files changed, 39 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java b/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java index 248ab1645f..5c0ed58677 100644 --- a/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java +++ b/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java @@ -19,9 +19,13 @@ package io.bitsquare.app; import io.bitsquare.BitsquareException; import io.bitsquare.app.gui.BitsquareAppMain; +import io.bitsquare.btc.BitcoinNetwork; +import io.bitsquare.btc.RegTestHost; import io.bitsquare.btc.UserAgent; import io.bitsquare.btc.WalletService; import io.bitsquare.gui.main.MainView; +import io.bitsquare.p2p.BootstrapNodes; +import io.bitsquare.p2p.tomp2p.TomP2PModule; import io.bitsquare.storage.Storage; import io.bitsquare.util.Utilities; import io.bitsquare.util.spring.JOptCommandLinePropertySource; @@ -67,6 +71,7 @@ public class BitsquareEnvironment extends StandardEnvironment { private final String appName; private final String appDataDir; + private final String bootstrapNodePort; public BitsquareEnvironment(OptionSet options) { this(new JOptCommandLinePropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME, checkNotNull(options))); @@ -85,6 +90,18 @@ public class BitsquareEnvironment extends StandardEnvironment { (String) commandLineProperties.getProperty(APP_DATA_DIR_KEY) : appDataDir(userDataDir, appName); + String bitcoinNetwork = commandLineProperties.containsProperty(BitcoinNetwork.KEY) ? + (String) commandLineProperties.getProperty(BitcoinNetwork.KEY) : + BitcoinNetwork.DEFAULT.toString(); + + String regTestHost = commandLineProperties.containsProperty(RegTestHost.KEY) ? + (String) commandLineProperties.getProperty(RegTestHost.KEY) : + RegTestHost.DEFAULT.toString(); + + this.bootstrapNodePort = commandLineProperties.containsProperty(TomP2PModule.BOOTSTRAP_NODE_PORT_KEY) ? + (String) commandLineProperties.getProperty(TomP2PModule.BOOTSTRAP_NODE_PORT_KEY) : + getBootstrapNodePort(BitcoinNetwork.valueOf(bitcoinNetwork), RegTestHost.valueOf(regTestHost)); + MutablePropertySources propertySources = this.getPropertySources(); propertySources.addFirst(commandLineProperties); try { @@ -97,6 +114,19 @@ public class BitsquareEnvironment extends StandardEnvironment { } } + private String getBootstrapNodePort(BitcoinNetwork bitcoinNetwork, RegTestHost regTestHost) { + // We use default port 7366 for mainnet, 7367 for testnet and 7368 for regtest + if (bitcoinNetwork == BitcoinNetwork.REGTEST && regTestHost == RegTestHost.DIGITAL_OCEAN_1) { + return String.valueOf(BootstrapNodes.DEFAULT_PORT + 2); + } + else if (bitcoinNetwork == BitcoinNetwork.TESTNET) { + return String.valueOf(BootstrapNodes.DEFAULT_PORT + 1); + } + else { + return String.valueOf(BootstrapNodes.DEFAULT_PORT); + } + } + PropertySource appDirProperties() throws Exception { String location = String.format("file:%s/bitsquare.properties", appDataDir); Resource resource = resourceLoader.getResource(location); @@ -141,6 +171,8 @@ public class BitsquareEnvironment extends StandardEnvironment { setProperty(Storage.DIR_KEY, Paths.get(appDataDir, "db").toString()); setProperty(MainView.TITLE_KEY, appName); + + setProperty(TomP2PModule.BOOTSTRAP_NODE_PORT_KEY, bootstrapNodePort); } }); } diff --git a/core/src/main/java/io/bitsquare/btc/RegTestHost.java b/core/src/main/java/io/bitsquare/btc/RegTestHost.java index df21959319..46b217c794 100644 --- a/core/src/main/java/io/bitsquare/btc/RegTestHost.java +++ b/core/src/main/java/io/bitsquare/btc/RegTestHost.java @@ -20,8 +20,8 @@ package io.bitsquare.btc; public enum RegTestHost { LOCALHOST(), - BITSQUARE(); // 188.226.179.109 + DIGITAL_OCEAN_1(); // 188.226.179.109 public static final String KEY = "bitcoin.regtest.host"; - public static final RegTestHost DEFAULT = BITSQUARE; + public static final RegTestHost DEFAULT = DIGITAL_OCEAN_1; } diff --git a/core/src/main/java/io/bitsquare/btc/WalletService.java b/core/src/main/java/io/bitsquare/btc/WalletService.java index 2b9eb3188d..0ae63bfe4d 100644 --- a/core/src/main/java/io/bitsquare/btc/WalletService.java +++ b/core/src/main/java/io/bitsquare/btc/WalletService.java @@ -159,7 +159,7 @@ public class WalletService { // Now configure and start the appkit. This will take a second or two - we could show a temporary splash screen // or progress widget to keep the user engaged whilst we initialise, but we don't. if (params == RegTestParams.get()) { - if (regTestHost == RegTestHost.BITSQUARE) { + if (regTestHost == RegTestHost.DIGITAL_OCEAN_1) { try { walletAppKit.setPeerNodes(new PeerAddress(InetAddress.getByName("188.226.179.109"), params.getPort())); } catch (UnknownHostException e) { diff --git a/core/src/main/java/io/bitsquare/p2p/BootstrapNodes.java b/core/src/main/java/io/bitsquare/p2p/BootstrapNodes.java index 1aff0773c6..a09f87df83 100644 --- a/core/src/main/java/io/bitsquare/p2p/BootstrapNodes.java +++ b/core/src/main/java/io/bitsquare/p2p/BootstrapNodes.java @@ -22,7 +22,9 @@ import java.util.List; public interface BootstrapNodes { - Node DIGITAL_OCEAN_1 = Node.at("digitalocean1.bitsquare.io", "188.226.179.109", 7366); + int DEFAULT_PORT = 7366; + + Node DIGITAL_OCEAN_1 = Node.at("digitalocean1.bitsquare.io", "188.226.179.109", DEFAULT_PORT); /** * Alias to the default bootstrap node. @@ -33,7 +35,7 @@ public interface BootstrapNodes { * A locally-running BootstrapNode instance. * Typically used only for testing. Not included in results from {@link #all()}. */ - Node LOCALHOST = Node.at("localhost", "127.0.0.1", 7366); + Node LOCALHOST = Node.at("localhost", "127.0.0.1", DEFAULT_PORT); /** * All known public bootstrap nodes. diff --git a/core/src/main/java/io/bitsquare/p2p/Node.java b/core/src/main/java/io/bitsquare/p2p/Node.java index 78e81b7d98..29b145500d 100644 --- a/core/src/main/java/io/bitsquare/p2p/Node.java +++ b/core/src/main/java/io/bitsquare/p2p/Node.java @@ -32,11 +32,6 @@ public final class Node { public static final String NAME_KEY = "node.name"; public static final String PORT_KEY = "node.port"; - /** - * Default port is one - * currently unassigned by IANA (7366-7390). - */ public static int DEFAULT_PORT = findFreeSystemPort(); private final String name;