Add DHT network separation by btc network

This commit is contained in:
Manfred Karrer 2015-04-06 12:14:27 +02:00
parent f4f1f9f451
commit f24ebb9038
5 changed files with 39 additions and 10 deletions

View File

@ -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);
}
});
}

View File

@ -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;
}

View File

@ -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) {

View File

@ -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.

View File

@ -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 <a
* href="https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?&page=103">
* currently unassigned by IANA</a> (7366-7390).
*/
public static int DEFAULT_PORT = findFreeSystemPort();
private final String name;