Add new bootstrap node servers

This commit is contained in:
Manfred Karrer 2015-05-10 09:17:09 +02:00
parent 44c8cf9c26
commit c1e0524090
18 changed files with 231 additions and 115 deletions

View file

@ -43,7 +43,6 @@ public abstract class BitsquareModule extends AbstractModule {
protected void install(BitsquareModule module) {
super.install(module);
log.trace("install " + module.getClass().getSimpleName());
modules.add(module);
}
@ -68,6 +67,5 @@ public abstract class BitsquareModule extends AbstractModule {
* @param injector the Injector originally initialized with this module
*/
protected void doClose(Injector injector) {
log.trace("doClose " + getClass().getSimpleName());
}
}

View file

@ -19,7 +19,6 @@ package io.bitsquare.app;
import io.bitsquare.BitsquareException;
import io.bitsquare.btc.BitcoinNetwork;
import io.bitsquare.btc.RegTestHost;
import io.bitsquare.btc.UserAgent;
import io.bitsquare.btc.WalletService;
import io.bitsquare.crypto.KeyStorage;
@ -93,13 +92,8 @@ public class BitsquareEnvironment extends StandardEnvironment {
(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));
(String) commandLineProperties.getProperty(TomP2PModule.BOOTSTRAP_NODE_PORT_KEY) : String.valueOf(BootstrapNodes.PORT);
MutablePropertySources propertySources = this.getPropertySources();
propertySources.addFirst(commandLineProperties);
@ -113,19 +107,6 @@ 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);

View file

@ -29,7 +29,7 @@ public enum BitcoinNetwork {
REGTEST(RegTestParams.get());
public static final String KEY = "bitcoin.network";
public static final BitcoinNetwork DEFAULT = REGTEST;
public static final BitcoinNetwork DEFAULT = TESTNET;
private final NetworkParameters parameters;

View file

@ -19,9 +19,11 @@ package io.bitsquare.btc;
public enum RegTestHost {
LOCALHOST(),
DIGITAL_OCEAN_1(); // 188.226.179.109
LOCALHOST,
REG_TEST_SERVER; // 188.226.179.109
public static final String KEY = "bitcoin.regtest.host";
public static final RegTestHost DEFAULT = DIGITAL_OCEAN_1;
public static final RegTestHost DEFAULT = LOCALHOST;
public static final String SERVER_IP = "188.226.179.109";
}

View file

@ -106,7 +106,7 @@ public class WalletService {
private final DownloadListener downloadListener = new DownloadListener();
private final WalletEventListener walletEventListener = new BitsquareWalletEventListener();
private RegTestHost regTestHost;
private final RegTestHost regTestHost;
private final TradeWalletService tradeWalletService;
private final AddressEntryList addressEntryList;
private final NetworkParameters params;
@ -175,9 +175,9 @@ 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.DIGITAL_OCEAN_1) {
if (regTestHost == RegTestHost.REG_TEST_SERVER) {
try {
walletAppKit.setPeerNodes(new PeerAddress(InetAddress.getByName("188.226.179.109"), params.getPort()));
walletAppKit.setPeerNodes(new PeerAddress(InetAddress.getByName(RegTestHost.SERVER_IP), params.getPort()));
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}

View file

@ -19,30 +19,36 @@ package io.bitsquare.p2p;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public interface BootstrapNodes {
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
int DEFAULT_PORT = 7366;
public class BootstrapNodes {
private static final Logger log = LoggerFactory.getLogger(BootstrapNodes.class);
Node DIGITAL_OCEAN_1 = Node.at("digitalocean1.bitsquare.io", "188.226.179.109", DEFAULT_PORT);
public static final int PORT = 7366;
public static final String DEFAULT_NODE_NAME = "default";
/**
* Alias to the default bootstrap node.
*/
Node DEFAULT = DIGITAL_OCEAN_1;
private static List<Node> bootstrapNodes = Arrays.asList(
Node.at(DEFAULT_NODE_NAME, "188.226.179.109", PORT),
Node.at(DEFAULT_NODE_NAME, "52.24.144.42", PORT),
Node.at(DEFAULT_NODE_NAME, "52.11.125.194", PORT)
);
/**
* A locally-running BootstrapNode instance.
* Typically used only for testing. Not included in results from {@link #all()}.
* Typically used only for testing. Not included in results from {@link #getAllBootstrapNodes()}.
*/
Node LOCALHOST = Node.at("localhost", "127.0.0.1", DEFAULT_PORT);
public static Node LOCALHOST = Node.at("localhost", "127.0.0.1", PORT);
/**
* All known public bootstrap nodes.
*/
static List<Node> all() {
return Arrays.asList(
DIGITAL_OCEAN_1
);
private static Node selectedNode = bootstrapNodes.get(new Random().nextInt(bootstrapNodes.size()));
public static List<Node> getAllBootstrapNodes() {
return bootstrapNodes;
}
public static Node getSelectedNode() {
return selectedNode;
}
}

View file

@ -21,6 +21,8 @@ import io.bitsquare.p2p.tomp2p.BootstrappedPeerBuilder;
import java.security.KeyPair;
import java.util.concurrent.Executor;
import javafx.beans.property.ReadOnlyIntegerProperty;
import rx.Observable;
@ -32,7 +34,9 @@ public interface ClientNode {
Node getBootstrapNodeAddress();
Observable<BootstrappedPeerBuilder.State> bootstrap(KeyPair keyPair);
Observable<BootstrappedPeerBuilder.State> bootstrap(int networkId, KeyPair keyPair);
ReadOnlyIntegerProperty numPeersProperty();
void setExecutor(Executor executor);
}

View file

@ -21,7 +21,12 @@ import com.google.common.base.Objects;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import net.tomp2p.peers.Number160;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -89,6 +94,18 @@ public final class Node {
return port;
}
public PeerAddress toPeerAddress() {
try {
return new PeerAddress(Number160.createHash(getName()),
InetAddress.getByName(getIp()),
getPort(),
getPort());
} catch (UnknownHostException e) {
log.error("toPeerAddress failed: " + e.getMessage());
return null;
}
}
@Override
public boolean equals(Object object) {
if (this == object)

View file

@ -17,6 +17,7 @@
package io.bitsquare.p2p.tomp2p;
import io.bitsquare.p2p.BootstrapNodes;
import io.bitsquare.p2p.Node;
import com.google.common.util.concurrent.SettableFuture;
@ -30,6 +31,9 @@ import java.net.UnknownHostException;
import java.security.KeyPair;
import java.util.Optional;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javafx.beans.property.ObjectProperty;
@ -113,7 +117,7 @@ public class BootstrappedPeerBuilder {
private KeyPair keyPair;
private final int port;
private final boolean useManualPortForwarding;
private final Node bootstrapNode;
private Node bootstrapNode;
private final String networkInterface;
private final SettableFuture<PeerDHT> settableFuture = SettableFuture.create();
@ -123,6 +127,8 @@ public class BootstrappedPeerBuilder {
private Peer peer;
private PeerDHT peerDHT;
private boolean retriedOtherBootstrapNode;
private Executor executor;
///////////////////////////////////////////////////////////////////////////////////////////
@ -156,7 +162,11 @@ public class BootstrappedPeerBuilder {
// Public methods
///////////////////////////////////////////////////////////////////////////////////////////
public SettableFuture<PeerDHT> start() {
public void setExecutor(Executor executor) {
this.executor = executor;
}
public SettableFuture<PeerDHT> start(int networkId) {
try {
DefaultEventExecutorGroup eventExecutorGroup = new DefaultEventExecutorGroup(20);
ChannelClientConfiguration clientConf = PeerBuilder.createDefaultChannelClientConfiguration();
@ -172,6 +182,7 @@ public class BootstrappedPeerBuilder {
if (useManualPortForwarding) {
peer = new PeerBuilder(keyPair)
.p2pId(networkId)
.channelClientConfiguration(clientConf)
.channelServerConfiguration(serverConf)
.ports(port)
@ -182,6 +193,7 @@ public class BootstrappedPeerBuilder {
}
else {
peer = new PeerBuilder(keyPair)
.p2pId(networkId)
.channelClientConfiguration(clientConf)
.channelServerConfiguration(serverConf)
.ports(port)
@ -274,9 +286,20 @@ public class BootstrappedPeerBuilder {
bootstrap();
}
else {
// All attempts failed. Give up...
handleError(State.RELAY_FAILED, "NAT traversal using relay mode failed " +
futureRelayNAT.failedReason());
if (!retriedOtherBootstrapNode && BootstrapNodes.getAllBootstrapNodes().size() > 1) {
log.warn("Bootstrap failed with bootstrapNode: " + bootstrapNode + ". We try again with another node.");
retriedOtherBootstrapNode = true;
Optional<Node> optional = BootstrapNodes.getAllBootstrapNodes().stream().filter(e -> !e.equals(bootstrapNode)).findAny();
if (optional.isPresent()) {
bootstrapNode = optional.get();
executor.execute(() -> discoverExternalAddress());
}
}
else {
// All attempts failed. Give up...
handleError(State.RELAY_FAILED, "NAT traversal using relay mode failed " +
futureRelayNAT.failedReason());
}
}
}
}

View file

@ -65,9 +65,9 @@ public class TomP2PModule extends P2PModule {
env.getProperty(USE_MANUAL_PORT_FORWARDING_KEY, boolean.class, false));
bind(Node.class).annotatedWith(Names.named(BOOTSTRAP_NODE_KEY)).toInstance(
Node.at(env.getProperty(BOOTSTRAP_NODE_NAME_KEY, BootstrapNodes.DEFAULT.getName()),
env.getProperty(BOOTSTRAP_NODE_IP_KEY, BootstrapNodes.DEFAULT.getIp()),
env.getProperty(BOOTSTRAP_NODE_PORT_KEY, int.class, BootstrapNodes.DEFAULT.getPort())
Node.at(env.getProperty(BOOTSTRAP_NODE_NAME_KEY, BootstrapNodes.getSelectedNode().getName()),
env.getProperty(BOOTSTRAP_NODE_IP_KEY, BootstrapNodes.getSelectedNode().getIp()),
env.getProperty(BOOTSTRAP_NODE_PORT_KEY, int.class, BootstrapNodes.getSelectedNode().getPort())
)
);
bindConstant().annotatedWith(Names.named(NETWORK_INTERFACE_KEY)).to(env.getProperty(NETWORK_INTERFACE_KEY, NETWORK_INTERFACE_UNSPECIFIED));

View file

@ -31,6 +31,7 @@ import java.security.KeyPair;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
@ -40,12 +41,10 @@ import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import net.tomp2p.connection.PeerConnection;
import net.tomp2p.connection.PeerException;
import net.tomp2p.dht.PeerDHT;
import net.tomp2p.peers.PeerAddress;
import net.tomp2p.peers.PeerStatusListener;
import net.tomp2p.peers.RTT;
import net.tomp2p.peers.PeerMapChangeListener;
import net.tomp2p.peers.PeerStatistic;
import org.jetbrains.annotations.NotNull;
@ -88,7 +87,11 @@ public class TomP2PNode implements ClientNode {
// Public methods
///////////////////////////////////////////////////////////////////////////////////////////
public Observable<BootstrappedPeerBuilder.State> bootstrap(KeyPair keyPair) {
public void setExecutor(Executor executor) {
bootstrappedPeerBuilder.setExecutor(executor);
}
public Observable<BootstrappedPeerBuilder.State> bootstrap(int networkId, KeyPair keyPair) {
bootstrappedPeerBuilder.setKeyPair(keyPair);
bootstrappedPeerBuilder.getState().addListener((ov, oldValue, newValue) -> {
@ -96,14 +99,39 @@ public class TomP2PNode implements ClientNode {
bootstrapStateSubject.onNext(newValue);
});
SettableFuture<PeerDHT> bootstrapFuture = bootstrappedPeerBuilder.start();
SettableFuture<PeerDHT> bootstrapFuture = bootstrappedPeerBuilder.start(networkId);
Futures.addCallback(bootstrapFuture, new FutureCallback<PeerDHT>() {
@Override
public void onSuccess(@Nullable PeerDHT peerDHT) {
if (peerDHT != null) {
TomP2PNode.this.peerDHT = peerDHT;
peerDHT.peerBean().addPeerStatusListener(new PeerStatusListener() {
BaseP2PService.getUserThread().execute(() -> numPeers.set(peerDHT.peerBean().peerMap().all().size()));
log.debug("Number of peers = " + peerDHT.peerBean().peerMap().all().size());
peerDHT.peerBean().peerMap().addPeerMapChangeListener(new PeerMapChangeListener() {
@Override
public void peerInserted(PeerAddress peerAddress, boolean b) {
BaseP2PService.getUserThread().execute(() -> numPeers.set(peerDHT.peerBean().peerMap().all().size()));
log.debug("peerInserted " + peerAddress);
log.debug("Number of peers = " + peerDHT.peerBean().peerMap().all().size());
}
@Override
public void peerRemoved(PeerAddress peerAddress, PeerStatistic peerStatistic) {
BaseP2PService.getUserThread().execute(() -> numPeers.set(peerDHT.peerBean().peerMap().all().size()));
log.debug("peerRemoved " + peerAddress);
log.debug("Number of peers = " + peerDHT.peerBean().peerMap().all().size());
}
@Override
public void peerUpdated(PeerAddress peerAddress, PeerStatistic peerStatistic) {
BaseP2PService.getUserThread().execute(() -> numPeers.set(peerDHT.peerBean().peerMap().all().size()));
log.debug("peerUpdated " + peerAddress);
log.debug("Number of peers = " + peerDHT.peerBean().peerMap().all().size());
}
});
/* peerDHT.peerBean().addPeerStatusListener(new PeerStatusListener() {
@Override
public boolean peerFailed(PeerAddress peerAddress, PeerException e) {
return false;
@ -114,7 +142,7 @@ public class TomP2PNode implements ClientNode {
BaseP2PService.getUserThread().execute(() -> numPeers.set(peerDHT.peerBean().peerMap().size()));
return false;
}
});
});*/
resultHandlers.stream().forEach(ResultHandler::handleResult);
bootstrapStateSubject.onCompleted();