mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-07-27 00:45:23 -04:00
Allow command-line configuration of local node id and port
This change does away with the notion of "clientPort" and replaces it, simply, with "port". There are only two ports we care about in Bitsquare: 1. The port that the local node (i.e. a Bitsquare UI running on your laptop) listens on. This value is now specified with `--port` 2. The port of the bootstrap node that the local node will connect to on its first run. This value is specified with `--bootstrap.node.port` So, for example, the following is a valid commandline invocation: java -jar bitsquare.jar --port 1234 --bootstrap.node.port=9876 Both of these values default to Node.DEFAULT_PORT (currently 7366) This commit also introduces the --id flag for configuring the ID of the local node.
This commit is contained in:
parent
cb0e214a28
commit
2ae5949448
6 changed files with 38 additions and 43 deletions
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
package io.bitsquare.app;
|
package io.bitsquare.app;
|
||||||
|
|
||||||
|
import io.bitsquare.network.Node;
|
||||||
|
|
||||||
import net.sourceforge.argparse4j.ArgumentParsers;
|
import net.sourceforge.argparse4j.ArgumentParsers;
|
||||||
import net.sourceforge.argparse4j.inf.ArgumentParserException;
|
import net.sourceforge.argparse4j.inf.ArgumentParserException;
|
||||||
import net.sourceforge.argparse4j.inf.Namespace;
|
import net.sourceforge.argparse4j.inf.Namespace;
|
||||||
|
@ -33,6 +35,12 @@ public class ArgumentParser {
|
||||||
.defaultHelp(true)
|
.defaultHelp(true)
|
||||||
.description("Bitsquare - The decentralized bitcoin exchange");
|
.description("Bitsquare - The decentralized bitcoin exchange");
|
||||||
|
|
||||||
|
// Args for local node config
|
||||||
|
parser.addArgument("--" + Node.ID_KEY)
|
||||||
|
.help("Local node ID");
|
||||||
|
parser.addArgument("--" + Node.PORT_KEY)
|
||||||
|
.help("Local port to listen on");
|
||||||
|
|
||||||
// Args for seed node config
|
// Args for seed node config
|
||||||
parser.addArgument("--" + BOOTSTRAP_NODE_ID_KEY)
|
parser.addArgument("--" + BOOTSTRAP_NODE_ID_KEY)
|
||||||
.help("Seed node ID");
|
.help("Seed node ID");
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
package io.bitsquare.app.cli;
|
package io.bitsquare.app.cli;
|
||||||
|
|
||||||
import io.bitsquare.app.ArgumentParser;
|
import io.bitsquare.app.ArgumentParser;
|
||||||
import io.bitsquare.network.BootstrapNodes;
|
|
||||||
import io.bitsquare.network.Node;
|
import io.bitsquare.network.Node;
|
||||||
|
|
||||||
import net.tomp2p.dht.PeerBuilderDHT;
|
import net.tomp2p.dht.PeerBuilderDHT;
|
||||||
|
@ -36,8 +35,6 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import net.sourceforge.argparse4j.inf.Namespace;
|
import net.sourceforge.argparse4j.inf.Namespace;
|
||||||
|
|
||||||
import static io.bitsquare.msg.tomp2p.TomP2PMessageModule.*;
|
|
||||||
|
|
||||||
public class SeedNode {
|
public class SeedNode {
|
||||||
private static final Logger log = LoggerFactory.getLogger(SeedNode.class);
|
private static final Logger log = LoggerFactory.getLogger(SeedNode.class);
|
||||||
|
|
||||||
|
@ -48,18 +45,12 @@ public class SeedNode {
|
||||||
ArgumentParser parser = new ArgumentParser();
|
ArgumentParser parser = new ArgumentParser();
|
||||||
Namespace namespace = parser.parseArgs(args);
|
Namespace namespace = parser.parseArgs(args);
|
||||||
|
|
||||||
Node defaultNode = BootstrapNodes.DIGITAL_OCEAN_1;
|
String id = namespace.getString(Node.ID_KEY);
|
||||||
String id = defaultNode.getId();
|
if (id == null)
|
||||||
int port = defaultNode.getPort();
|
throw new IllegalArgumentException(String.format("--%s option is required", Node.ID_KEY));
|
||||||
|
|
||||||
// Passed program args will override the properties of the default bootstrapNode
|
String portValue = namespace.getString(Node.PORT_KEY);
|
||||||
// So you can use the same id but different ports (e.g. running several nodes on one server with
|
int port = portValue != null ? Integer.valueOf(portValue) : Node.DEFAULT_PORT;
|
||||||
// different ports)
|
|
||||||
if (namespace.getString(BOOTSTRAP_NODE_ID_KEY) != null)
|
|
||||||
id = namespace.getString(BOOTSTRAP_NODE_ID_KEY);
|
|
||||||
|
|
||||||
if (namespace.getString(BOOTSTRAP_NODE_PORT_KEY) != null)
|
|
||||||
port = Integer.valueOf(namespace.getString(BOOTSTRAP_NODE_PORT_KEY));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Number160 peerId = Number160.createHash(id);
|
Number160 peerId = Number160.createHash(id);
|
||||||
|
|
|
@ -25,8 +25,6 @@ import com.google.inject.name.Names;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import net.tomp2p.connection.Ports;
|
|
||||||
|
|
||||||
import static io.bitsquare.msg.tomp2p.BootstrappedPeerFactory.*;
|
import static io.bitsquare.msg.tomp2p.BootstrappedPeerFactory.*;
|
||||||
import static io.bitsquare.network.BootstrapNodes.DEFAULT_BOOTSTRAP_NODE;
|
import static io.bitsquare.network.BootstrapNodes.DEFAULT_BOOTSTRAP_NODE;
|
||||||
|
|
||||||
|
@ -43,8 +41,8 @@ public class TomP2PMessageModule extends MessageModule {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doConfigure() {
|
protected void doConfigure() {
|
||||||
Integer randomPort = new Ports().tcpPort();
|
bind(int.class).annotatedWith(Names.named(Node.PORT_KEY)).toInstance(
|
||||||
bindConstant().annotatedWith(Names.named(TomP2PNode.CLIENT_PORT_KEY)).to(randomPort);
|
Integer.valueOf(properties.getProperty(Node.PORT_KEY, String.valueOf(Node.DEFAULT_PORT))));
|
||||||
bind(boolean.class).annotatedWith(Names.named(TomP2PNode.USE_DISK_STORAGE_KEY)).toInstance(false);
|
bind(boolean.class).annotatedWith(Names.named(TomP2PNode.USE_DISK_STORAGE_KEY)).toInstance(false);
|
||||||
bind(TomP2PNode.class).asEagerSingleton();
|
bind(TomP2PNode.class).asEagerSingleton();
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ package io.bitsquare.msg.tomp2p;
|
||||||
|
|
||||||
import io.bitsquare.msg.MessageBroker;
|
import io.bitsquare.msg.MessageBroker;
|
||||||
import io.bitsquare.msg.listeners.BootstrapListener;
|
import io.bitsquare.msg.listeners.BootstrapListener;
|
||||||
|
import io.bitsquare.network.Node;
|
||||||
import io.bitsquare.network.tomp2p.TomP2PPeer;
|
import io.bitsquare.network.tomp2p.TomP2PPeer;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.FutureCallback;
|
import com.google.common.util.concurrent.FutureCallback;
|
||||||
|
@ -79,11 +80,10 @@ public class TomP2PNode {
|
||||||
private static final Logger log = LoggerFactory.getLogger(TomP2PNode.class);
|
private static final Logger log = LoggerFactory.getLogger(TomP2PNode.class);
|
||||||
|
|
||||||
static final String USE_DISK_STORAGE_KEY = "useDiskStorage";
|
static final String USE_DISK_STORAGE_KEY = "useDiskStorage";
|
||||||
static final String CLIENT_PORT_KEY = "clientPort";
|
|
||||||
|
|
||||||
private KeyPair keyPair;
|
private KeyPair keyPair;
|
||||||
private String appName;
|
private String appName;
|
||||||
private final int clientPort;
|
private final int port;
|
||||||
private final Boolean useDiskStorage;
|
private final Boolean useDiskStorage;
|
||||||
private MessageBroker messageBroker;
|
private MessageBroker messageBroker;
|
||||||
|
|
||||||
|
@ -100,11 +100,11 @@ public class TomP2PNode {
|
||||||
@Inject
|
@Inject
|
||||||
public TomP2PNode(BootstrappedPeerFactory bootstrappedPeerFactory,
|
public TomP2PNode(BootstrappedPeerFactory bootstrappedPeerFactory,
|
||||||
@Named("appName") String appName,
|
@Named("appName") String appName,
|
||||||
@Named(CLIENT_PORT_KEY) int clientPort,
|
@Named(Node.PORT_KEY) int port,
|
||||||
@Named(USE_DISK_STORAGE_KEY) Boolean useDiskStorage) {
|
@Named(USE_DISK_STORAGE_KEY) Boolean useDiskStorage) {
|
||||||
this.bootstrappedPeerFactory = bootstrappedPeerFactory;
|
this.bootstrappedPeerFactory = bootstrappedPeerFactory;
|
||||||
this.appName = appName;
|
this.appName = appName;
|
||||||
this.clientPort = clientPort;
|
this.port = port;
|
||||||
this.useDiskStorage = useDiskStorage;
|
this.useDiskStorage = useDiskStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ public class TomP2PNode {
|
||||||
peerDHT.peerBean().keyPair(keyPair);
|
peerDHT.peerBean().keyPair(keyPair);
|
||||||
messageBroker = (message, peerAddress) -> {
|
messageBroker = (message, peerAddress) -> {
|
||||||
};
|
};
|
||||||
clientPort = -1;
|
port = Node.DEFAULT_PORT;
|
||||||
useDiskStorage = false;
|
useDiskStorage = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +139,7 @@ public class TomP2PNode {
|
||||||
bootstrappedPeerFactory.setStorage(storage);
|
bootstrappedPeerFactory.setStorage(storage);
|
||||||
setupTimerForIPCheck();
|
setupTimerForIPCheck();
|
||||||
|
|
||||||
ListenableFuture<PeerDHT> bootstrapComplete = bootstrap(clientPort);
|
ListenableFuture<PeerDHT> bootstrapComplete = bootstrap(port);
|
||||||
Futures.addCallback(bootstrapComplete, new FutureCallback<PeerDHT>() {
|
Futures.addCallback(bootstrapComplete, new FutureCallback<PeerDHT>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(@Nullable PeerDHT result) {
|
public void onSuccess(@Nullable PeerDHT result) {
|
||||||
|
|
|
@ -20,6 +20,8 @@ package io.bitsquare.network;
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
|
|
||||||
public final class Node {
|
public final class Node {
|
||||||
|
public static final String ID_KEY = "id";
|
||||||
|
public static final String PORT_KEY = "port";
|
||||||
public static final int DEFAULT_PORT = 7366;
|
public static final int DEFAULT_PORT = 7366;
|
||||||
|
|
||||||
private final String id;
|
private final String id;
|
||||||
|
|
|
@ -304,8 +304,7 @@ public class TomP2PTests {
|
||||||
assertEquals("pong", futureDirect.object());
|
assertEquals("pong", futureDirect.object());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Peer bootstrapDirectConnection(String clientId, int clientPort) {
|
private Peer bootstrapDirectConnection(String clientId, int port) {
|
||||||
final String id = clientId + clientPort;
|
|
||||||
Peer peer = null;
|
Peer peer = null;
|
||||||
try {
|
try {
|
||||||
Number160 peerId = Number160.createHash(clientId);
|
Number160 peerId = Number160.createHash(clientId);
|
||||||
|
@ -315,7 +314,7 @@ public class TomP2PTests {
|
||||||
cc.maxPermitsTCP(100);
|
cc.maxPermitsTCP(100);
|
||||||
cc.maxPermitsUDP(100);
|
cc.maxPermitsUDP(100);
|
||||||
peer = new PeerBuilder(peerId).bindings(getBindings()).channelClientConfiguration(cc).peerMap(pm)
|
peer = new PeerBuilder(peerId).bindings(getBindings()).channelClientConfiguration(cc).peerMap(pm)
|
||||||
.ports(clientPort).start();
|
.ports(port).start();
|
||||||
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
|
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
|
||||||
futureDiscover.awaitUninterruptibly();
|
futureDiscover.awaitUninterruptibly();
|
||||||
if (futureDiscover.isSuccess()) {
|
if (futureDiscover.isSuccess()) {
|
||||||
|
@ -347,12 +346,11 @@ public class TomP2PTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Peer bootstrapWithPortForwarding(String clientId, int clientPort) {
|
private Peer bootstrapWithPortForwarding(String clientId, int port) {
|
||||||
final String id = clientId + clientPort;
|
|
||||||
Peer peer = null;
|
Peer peer = null;
|
||||||
try {
|
try {
|
||||||
peer = new PeerBuilder(Number160.createHash(clientId)).bindings(getBindings()).behindFirewall()
|
peer = new PeerBuilder(Number160.createHash(clientId)).bindings(getBindings()).behindFirewall()
|
||||||
.ports(clientPort).start();
|
.ports(port).start();
|
||||||
|
|
||||||
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
|
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
|
||||||
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
|
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
|
||||||
|
@ -401,13 +399,11 @@ public class TomP2PTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Peer bootstrapInRelayMode(String clientId, int clientPort) {
|
private Peer bootstrapInRelayMode(String clientId, int port) {
|
||||||
final String id = clientId + clientPort;
|
|
||||||
|
|
||||||
Peer peer = null;
|
Peer peer = null;
|
||||||
try {
|
try {
|
||||||
peer = new PeerBuilder(Number160.createHash(clientId)).bindings(getBindings()).behindFirewall()
|
peer = new PeerBuilder(Number160.createHash(clientId)).bindings(getBindings()).behindFirewall()
|
||||||
.ports(clientPort).start();
|
.ports(port).start();
|
||||||
|
|
||||||
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
|
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
|
||||||
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
|
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
|
||||||
|
@ -444,19 +440,19 @@ public class TomP2PTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Peer bootstrapInUnknownMode(String clientId, int clientPort) {
|
private Peer bootstrapInUnknownMode(String clientId, int port) {
|
||||||
resolvedConnectionType = ConnectionType.DIRECT;
|
resolvedConnectionType = ConnectionType.DIRECT;
|
||||||
Peer peer = bootstrapDirectConnection(clientId, clientPort);
|
Peer peer = bootstrapDirectConnection(clientId, port);
|
||||||
if (peer != null)
|
if (peer != null)
|
||||||
return peer;
|
return peer;
|
||||||
|
|
||||||
resolvedConnectionType = ConnectionType.NAT;
|
resolvedConnectionType = ConnectionType.NAT;
|
||||||
peer = bootstrapWithPortForwarding(clientId, clientPort);
|
peer = bootstrapWithPortForwarding(clientId, port);
|
||||||
if (peer != null)
|
if (peer != null)
|
||||||
return peer;
|
return peer;
|
||||||
|
|
||||||
resolvedConnectionType = ConnectionType.RELAY;
|
resolvedConnectionType = ConnectionType.RELAY;
|
||||||
peer = bootstrapInRelayMode(clientId, clientPort);
|
peer = bootstrapInRelayMode(clientId, port);
|
||||||
if (peer != null)
|
if (peer != null)
|
||||||
return peer;
|
return peer;
|
||||||
else
|
else
|
||||||
|
@ -466,19 +462,19 @@ public class TomP2PTests {
|
||||||
return peer;
|
return peer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private PeerDHT getDHTPeer(String clientId, int clientPort) {
|
private PeerDHT getDHTPeer(String clientId, int port) {
|
||||||
Peer peer;
|
Peer peer;
|
||||||
if (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT) {
|
if (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT) {
|
||||||
peer = bootstrapDirectConnection(clientId, clientPort);
|
peer = bootstrapDirectConnection(clientId, port);
|
||||||
}
|
}
|
||||||
else if (FORCED_CONNECTION_TYPE == ConnectionType.NAT) {
|
else if (FORCED_CONNECTION_TYPE == ConnectionType.NAT) {
|
||||||
peer = bootstrapWithPortForwarding(clientId, clientPort);
|
peer = bootstrapWithPortForwarding(clientId, port);
|
||||||
}
|
}
|
||||||
else if (FORCED_CONNECTION_TYPE == ConnectionType.RELAY) {
|
else if (FORCED_CONNECTION_TYPE == ConnectionType.RELAY) {
|
||||||
peer = bootstrapInRelayMode(clientId, clientPort);
|
peer = bootstrapInRelayMode(clientId, port);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
peer = bootstrapInUnknownMode(clientId, clientPort);
|
peer = bootstrapInUnknownMode(clientId, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (peer == null)
|
if (peer == null)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue