mirror of
https://github.com/bisq-network/bisq.git
synced 2025-03-03 18:56:59 +01:00
Add Network id
This commit is contained in:
parent
09321c89b3
commit
5618f23654
28 changed files with 264 additions and 178 deletions
|
@ -14,6 +14,7 @@ public class ProgramArguments {
|
|||
public static final String NAME_KEY = "node.name";
|
||||
public static final String PORT_KEY = "node.port";
|
||||
|
||||
public static final String NETWORK_ID = "network.id";
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ProgramArguments.class);
|
||||
}
|
||||
|
|
|
@ -192,11 +192,13 @@ public class BitsquareEnvironment extends StandardEnvironment {
|
|||
setProperty(Storage.DIR_KEY, Paths.get(btcNetworkDir, "db").toString());
|
||||
setProperty(KeyStorage.DIR_KEY, Paths.get(btcNetworkDir, "keys").toString());
|
||||
setProperty(ProgramArguments.TOR_DIR, Paths.get(btcNetworkDir, "tor").toString());
|
||||
|
||||
setProperty(ProgramArguments.NETWORK_ID, String.valueOf(bitcoinNetwork.ordinal()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static String defaultUserDataDir() {
|
||||
public static String defaultUserDataDir() {
|
||||
if (Utilities.isWindows())
|
||||
return System.getenv("APPDATA");
|
||||
else if (Utilities.isOSX())
|
||||
|
|
|
@ -21,8 +21,10 @@ import io.bitsquare.app.Version;
|
|||
import io.bitsquare.p2p.messaging.MailboxMessage;
|
||||
|
||||
public abstract class DisputeMessage implements MailboxMessage {
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ public abstract class OfferMessage implements MailMessage {
|
|||
// That object is sent over the wire, so we need to take care of version compatibility.
|
||||
private static final long serialVersionUID = Version.NETWORK_PROTOCOL_VERSION;
|
||||
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
public final String offerId;
|
||||
|
||||
protected OfferMessage(String offerId) {
|
||||
|
@ -35,6 +36,6 @@ public abstract class OfferMessage implements MailMessage {
|
|||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ public abstract class TradeMessage implements MailMessage {
|
|||
// That object is sent over the wire, so we need to take care of version compatibility.
|
||||
private static final long serialVersionUID = Version.NETWORK_PROTOCOL_VERSION;
|
||||
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
public final String tradeId;
|
||||
|
||||
@Override
|
||||
|
@ -51,6 +52,6 @@ public abstract class TradeMessage implements MailMessage {
|
|||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,6 @@ import io.bitsquare.btc.AddressEntry;
|
|||
import io.bitsquare.btc.TradeWalletService;
|
||||
import io.bitsquare.btc.WalletService;
|
||||
import io.bitsquare.btc.listeners.BalanceListener;
|
||||
import io.bitsquare.common.crypto.KeyRing;
|
||||
import io.bitsquare.gui.common.model.ViewModel;
|
||||
import io.bitsquare.gui.components.BalanceTextField;
|
||||
import io.bitsquare.gui.components.BalanceWithConfirmationTextField;
|
||||
|
@ -126,9 +125,8 @@ class MainViewModel implements ViewModel {
|
|||
public MainViewModel(WalletService walletService, TradeWalletService tradeWalletService,
|
||||
ArbitratorManager arbitratorManager, P2PService p2PService, TradeManager tradeManager,
|
||||
OpenOfferManager openOfferManager, DisputeManager disputeManager, Preferences preferences,
|
||||
KeyRing keyRing, User user,
|
||||
AlertManager alertManager,
|
||||
WalletPasswordPopup walletPasswordPopup, BSFormatter formatter) {
|
||||
User user, AlertManager alertManager, WalletPasswordPopup walletPasswordPopup,
|
||||
BSFormatter formatter) {
|
||||
this.user = user;
|
||||
log.debug("in");
|
||||
this.walletService = walletService;
|
||||
|
|
|
@ -29,7 +29,7 @@ import io.bitsquare.gui.util.BSFormatter;
|
|||
import io.bitsquare.p2p.Address;
|
||||
import io.bitsquare.p2p.P2PService;
|
||||
import io.bitsquare.p2p.P2PServiceListener;
|
||||
import io.bitsquare.p2p.network.TorNetworkNode;
|
||||
import io.bitsquare.p2p.network.LocalhostNetworkNode;
|
||||
import io.bitsquare.p2p.seed.SeedNodesRepository;
|
||||
import io.bitsquare.user.Preferences;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
|
@ -68,13 +68,12 @@ public class NetworkSettingsView extends ActivatableViewAndModel<GridPane, Activ
|
|||
formatter) {
|
||||
this.walletService = walletService;
|
||||
this.preferences = preferences;
|
||||
this.bitcoinNetworkString = formatter.formatBitcoinNetwork(preferences.getBitcoinNetwork());
|
||||
BitcoinNetwork bitcoinNetwork = preferences.getBitcoinNetwork();
|
||||
this.bitcoinNetworkString = formatter.formatBitcoinNetwork(bitcoinNetwork);
|
||||
this.p2PService = p2PService;
|
||||
|
||||
if (p2PService.getNetworkNode() instanceof TorNetworkNode)
|
||||
this.seedNodeAddresses = seedNodesRepository.getTorSeedNodeAddresses();
|
||||
else
|
||||
this.seedNodeAddresses = seedNodesRepository.getLocalhostSeedNodeAddresses();
|
||||
boolean useLocalhost = p2PService.getNetworkNode() instanceof LocalhostNetworkNode;
|
||||
this.seedNodeAddresses = seedNodesRepository.geSeedNodeAddresses(useLocalhost, bitcoinNetwork.ordinal());
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
|
|
|
@ -9,6 +9,7 @@ public final class SealedAndSignedMessage implements MailboxMessage {
|
|||
// That object is sent over the wire, so we need to take care of version compatibility.
|
||||
private static final long serialVersionUID = Version.NETWORK_PROTOCOL_VERSION;
|
||||
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
public final SealedAndSigned sealedAndSigned;
|
||||
|
||||
public SealedAndSignedMessage(SealedAndSigned sealedAndSigned) {
|
||||
|
@ -22,6 +23,6 @@ public final class SealedAndSignedMessage implements MailboxMessage {
|
|||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,5 +52,8 @@ public class P2PModule extends AppModule {
|
|||
// use a fixed port as arbitrator use that for his ID
|
||||
Integer port = env.getProperty(ProgramArguments.PORT_KEY, int.class, 9999);
|
||||
bind(int.class).annotatedWith(Names.named(ProgramArguments.PORT_KEY)).toInstance(port);
|
||||
|
||||
Integer networkId = env.getProperty(ProgramArguments.NETWORK_ID, int.class, 1);
|
||||
bind(int.class).annotatedWith(Names.named(ProgramArguments.NETWORK_ID)).toInstance(networkId);
|
||||
}
|
||||
}
|
|
@ -42,7 +42,6 @@ import java.io.File;
|
|||
import java.math.BigInteger;
|
||||
import java.security.PublicKey;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
@ -69,7 +68,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
|||
private final CopyOnWriteArraySet<DecryptedMailListener> decryptedMailListeners = new CopyOnWriteArraySet<>();
|
||||
private final CopyOnWriteArraySet<DecryptedMailboxListener> decryptedMailboxListeners = new CopyOnWriteArraySet<>();
|
||||
private final CopyOnWriteArraySet<P2PServiceListener> p2pServiceListeners = new CopyOnWriteArraySet<>();
|
||||
private final Map<DecryptedMsgWithPubKey, ProtectedMailboxData> mailboxMap = new ConcurrentHashMap<>();
|
||||
private final Map<DecryptedMsgWithPubKey, ProtectedMailboxData> mailboxMap = new HashMap<>();
|
||||
private volatile boolean shutDownInProgress;
|
||||
private Address connectedSeedNode;
|
||||
private final Set<Address> authenticatedPeerAddresses = new HashSet<>();
|
||||
|
@ -91,6 +90,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
|||
@Named(ProgramArguments.PORT_KEY) int port,
|
||||
@Named(ProgramArguments.TOR_DIR) File torDir,
|
||||
@Named(ProgramArguments.USE_LOCALHOST) boolean useLocalhost,
|
||||
@Named(ProgramArguments.NETWORK_ID) int networkId,
|
||||
@Nullable EncryptionService encryptionService,
|
||||
KeyRing keyRing,
|
||||
@Named("storage.dir") File storageDir) {
|
||||
|
@ -103,20 +103,14 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
|||
this.keyRing = keyRing;
|
||||
this.storageDir = storageDir;
|
||||
|
||||
init();
|
||||
init(networkId);
|
||||
}
|
||||
|
||||
private void init() {
|
||||
private void init(int networkId) {
|
||||
Log.traceCall();
|
||||
// network
|
||||
Set<Address> seedNodeAddresses;
|
||||
if (useLocalhost) {
|
||||
networkNode = new LocalhostNetworkNode(port);
|
||||
seedNodeAddresses = seedNodesRepository.getLocalhostSeedNodeAddresses();
|
||||
} else {
|
||||
networkNode = new TorNetworkNode(port, torDir);
|
||||
seedNodeAddresses = seedNodesRepository.getTorSeedNodeAddresses();
|
||||
}
|
||||
networkNode = useLocalhost ? new LocalhostNetworkNode(port) : new TorNetworkNode(port, torDir);
|
||||
Set<Address> seedNodeAddresses = seedNodesRepository.geSeedNodeAddresses(useLocalhost, networkId);
|
||||
|
||||
// peer group
|
||||
peerGroup = new PeerGroup(networkNode, seedNodeAddresses);
|
||||
|
@ -564,6 +558,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
|||
Log.traceCall();
|
||||
checkAuthentication();
|
||||
|
||||
if (mailboxMap.containsKey(decryptedMsgWithPubKey)) {
|
||||
ProtectedMailboxData mailboxData = mailboxMap.get(decryptedMsgWithPubKey);
|
||||
if (mailboxData != null && mailboxData.expirablePayload instanceof ExpirableMailboxPayload) {
|
||||
checkArgument(mailboxData.receiversPubKey.equals(keyRing.getSignatureKeyPair().getPublic()),
|
||||
|
@ -571,6 +566,11 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
|||
removeMailboxData((ExpirableMailboxPayload) mailboxData.expirablePayload, mailboxData.receiversPubKey);
|
||||
mailboxMap.remove(decryptedMsgWithPubKey);
|
||||
log.trace("Removed successfully protectedExpirableData.");
|
||||
|
||||
}
|
||||
} else {
|
||||
log.warn("decryptedMsgWithPubKey not found in mailboxMap. That should never happen." +
|
||||
"\ndecryptedMsgWithPubKey={}\nmailboxMap={}", decryptedMsgWithPubKey, mailboxMap);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ public final class DecryptedMsgWithPubKey implements MailMessage {
|
|||
// That object is sent over the wire, so we need to take care of version compatibility.
|
||||
private static final long serialVersionUID = Version.NETWORK_PROTOCOL_VERSION;
|
||||
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
public final Message message;
|
||||
public final PublicKey signaturePubKey;
|
||||
|
||||
|
@ -36,7 +37,7 @@ public final class DecryptedMsgWithPubKey implements MailMessage {
|
|||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -35,7 +35,6 @@ public class Connection implements MessageListener {
|
|||
private static final int SOCKET_TIMEOUT = 30 * 60 * 1000; // 30 min.
|
||||
private InputHandler inputHandler;
|
||||
private volatile boolean isAuthenticated;
|
||||
private String connectionId;
|
||||
|
||||
public static int getMaxMsgSize() {
|
||||
return MAX_MSG_SIZE;
|
||||
|
@ -48,7 +47,7 @@ public class Connection implements MessageListener {
|
|||
private final String portInfo;
|
||||
private final String uid;
|
||||
private final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
|
||||
public final String objectId = super.toString().split("@")[1];
|
||||
public final String objectId;
|
||||
|
||||
// set in init
|
||||
private ObjectOutputStream objectOutputStream;
|
||||
|
@ -76,6 +75,8 @@ public class Connection implements MessageListener {
|
|||
this.messageListener = messageListener;
|
||||
this.connectionListener = connectionListener;
|
||||
|
||||
objectId = super.toString().split("@")[1];
|
||||
|
||||
Log.traceCall();
|
||||
uid = UUID.randomUUID().toString();
|
||||
if (socket.getLocalPort() == 0)
|
||||
|
@ -335,10 +336,6 @@ public class Connection implements MessageListener {
|
|||
'}';
|
||||
}
|
||||
|
||||
public String getConnectionId() {
|
||||
return connectionId;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// SharedSpace
|
||||
|
@ -379,12 +376,23 @@ public class Connection implements MessageListener {
|
|||
public void reportIllegalRequest(IllegalRequest illegalRequest) {
|
||||
Log.traceCall();
|
||||
log.warn("We got reported an illegal request " + illegalRequest);
|
||||
int prevCounter = illegalRequests.get(illegalRequest);
|
||||
if (prevCounter > illegalRequest.maxTolerance) {
|
||||
log.warn("We close connection as we received too many illegal requests.\n" + illegalRequests.toString());
|
||||
connection.shutDown(false);
|
||||
int violations;
|
||||
if (illegalRequests.contains(illegalRequest))
|
||||
violations = illegalRequests.get(illegalRequest);
|
||||
else
|
||||
violations = 0;
|
||||
|
||||
violations++;
|
||||
illegalRequests.put(illegalRequest, violations);
|
||||
|
||||
if (violations >= illegalRequest.maxTolerance) {
|
||||
log.warn("We close connection as we received too many invalid requests.\n" +
|
||||
"violations={}\n" +
|
||||
"illegalRequest={}\n" +
|
||||
"illegalRequests={}", violations, illegalRequest, illegalRequests.toString());
|
||||
shutDown(false);
|
||||
} else {
|
||||
illegalRequests.put(illegalRequest, ++prevCounter);
|
||||
illegalRequests.put(illegalRequest, ++violations);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -406,24 +414,24 @@ public class Connection implements MessageListener {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (!stopped) {
|
||||
stopped = true;
|
||||
connection.shutDown(false);
|
||||
}
|
||||
shutDown(false);
|
||||
}
|
||||
|
||||
public void shutDown(boolean sendCloseConnectionMessage) {
|
||||
Log.traceCall();
|
||||
if (!stopped) {
|
||||
stopped = true;
|
||||
connection.shutDown(sendCloseConnectionMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public synchronized Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public String getConnectionId() {
|
||||
return connection.getConnectionId();
|
||||
public String getConnectionInfo() {
|
||||
return connection.toString();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
|
@ -483,9 +491,9 @@ public class Connection implements MessageListener {
|
|||
Thread.currentThread().setName("InputHandler-" + portInfo);
|
||||
while (!stopped && !Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
log.trace("InputHandler waiting for incoming messages connection=" + sharedSpace.getConnectionId());
|
||||
log.trace("InputHandler waiting for incoming messages connection=" + sharedSpace.getConnectionInfo());
|
||||
Object rawInputObject = objectInputStream.readObject();
|
||||
log.info("New data arrived at inputHandler of connection=" + sharedSpace.getConnectionId()
|
||||
log.info("New data arrived at inputHandler of connection=" + sharedSpace.getConnectionInfo()
|
||||
+ " rawInputObject " + rawInputObject);
|
||||
|
||||
int size = ByteArrayUtils.objectToByteArray(rawInputObject).length;
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package io.bitsquare.p2p.network;
|
||||
|
||||
public enum IllegalRequest {
|
||||
// TODO check for needed allowed tolerance
|
||||
MaxSizeExceeded(1),
|
||||
NotAuthenticated(2),
|
||||
InvalidDataType(2),
|
||||
WrongNetworkId(2);
|
||||
NotAuthenticated(1),
|
||||
InvalidDataType(1),
|
||||
WrongNetworkId(1);
|
||||
|
||||
public final int maxTolerance;
|
||||
|
||||
|
|
|
@ -7,8 +7,10 @@ public final class CloseConnectionMessage implements Message {
|
|||
// That object is sent over the wire, so we need to take care of version compatibility.
|
||||
private static final long serialVersionUID = Version.NETWORK_PROTOCOL_VERSION;
|
||||
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@ import io.bitsquare.app.Version;
|
|||
import io.bitsquare.p2p.Message;
|
||||
|
||||
public abstract class AuthenticationMessage implements Message {
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@ import io.bitsquare.app.Version;
|
|||
import io.bitsquare.p2p.Message;
|
||||
|
||||
public abstract class MaintenanceMessage implements Message {
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,8 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
@ -29,8 +31,10 @@ public class SeedNode {
|
|||
private Set<Address> seedNodes;
|
||||
private P2PService p2PService;
|
||||
private boolean stopped;
|
||||
private final String defaultUserDataDir;
|
||||
|
||||
public SeedNode() {
|
||||
public SeedNode(String defaultUserDataDir) {
|
||||
this.defaultUserDataDir = defaultUserDataDir;
|
||||
Log.traceCall();
|
||||
}
|
||||
|
||||
|
@ -39,21 +43,23 @@ public class SeedNode {
|
|||
// API
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// args: myAddress (incl. port) BitcoinNetworkId maxConnections useLocalhost seedNodes (separated with |)
|
||||
// args: myAddress (incl. port) bitcoinNetworkId maxConnections useLocalhost seedNodes (separated with |)
|
||||
// 2. and 3. args are optional
|
||||
// eg. lmvdenjkyvx2ovga.onion:8001 0 20 false eo5ay2lyzrfvx2nr.onion:8002|si3uu56adkyqkldl.onion:8003
|
||||
// or when using localhost: localhost:8001 2 20 true localhost:8002|localhost:8003
|
||||
// BitcoinNetworkId: The id for the bitcoin network (Mainnet = 0, TestNet = 1, Regtest = 2)
|
||||
public void processArgs(String[] args) {
|
||||
Log.traceCall();
|
||||
try {
|
||||
if (args.length > 0) {
|
||||
String arg0 = args[0];
|
||||
checkArgument(arg0.contains(":") && arg0.split(":").length == 2 && arg0.split(":")[1].length() == 4, "Wrong program argument");
|
||||
checkArgument(arg0.contains(":") && arg0.split(":").length == 2 && arg0.split(":")[1].length() > 3, "Wrong program argument: " + arg0);
|
||||
mySeedNodeAddress = new Address(arg0);
|
||||
if (args.length > 1) {
|
||||
String arg1 = args[1];
|
||||
int networkId = Integer.parseInt(arg1);
|
||||
checkArgument(networkId > -1 && networkId < 3, "networkId out of scope (Mainnet = 0, TestNet = 1, Regtest = 2)");
|
||||
checkArgument(networkId > -1 && networkId < 3,
|
||||
"networkId out of scope (Mainnet = 0, TestNet = 1, Regtest = 2)");
|
||||
Version.NETWORK_ID = networkId;
|
||||
if (args.length > 2) {
|
||||
String arg2 = args[2];
|
||||
|
@ -71,27 +77,39 @@ public class SeedNode {
|
|||
}
|
||||
if (args.length > 4) {
|
||||
String arg4 = args[4];
|
||||
checkArgument(arg4.contains(":") && arg4.split(":").length > 1 && arg4.split(":")[1].length() > 3, "Wrong program argument");
|
||||
checkArgument(arg4.contains(":") && arg4.split(":").length > 1 && arg4.split(":")[1].length() > 3,
|
||||
"Wrong program argument");
|
||||
List<String> list = Arrays.asList(arg4.split("|"));
|
||||
seedNodes = new HashSet<>();
|
||||
list.forEach(e -> {
|
||||
checkArgument(e.contains(":") && e.split(":").length == 2 && e.split(":")[1].length() == 4, "Wrong program argument");
|
||||
checkArgument(e.contains(":") && e.split(":").length == 2 && e.split(":")[1].length() == 4,
|
||||
"Wrong program argument");
|
||||
seedNodes.add(new Address(e));
|
||||
});
|
||||
seedNodes.remove(mySeedNodeAddress);
|
||||
} else if (args.length > 5) {
|
||||
log.error("Too many program arguments." +
|
||||
"\nProgram arguments: myAddress (incl. port) BitcoinNetworkId maxConnections useLocalhost seedNodes (separated with |)");
|
||||
"\nProgram arguments: myAddress (incl. port) bitcoinNetworkId " +
|
||||
"maxConnections useLocalhost seedNodes (separated with |)");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
shutDown();
|
||||
}
|
||||
}
|
||||
|
||||
public void createAndStartP2PService() {
|
||||
createAndStartP2PService(null, null, mySeedNodeAddress, useLocalhost, seedNodes, null);
|
||||
createAndStartP2PService(null, null, mySeedNodeAddress, useLocalhost, Version.NETWORK_ID, seedNodes, null);
|
||||
}
|
||||
|
||||
public void createAndStartP2PService(EncryptionService encryptionService, KeyRing keyRing, Address mySeedNodeAddress, boolean useLocalhost, @Nullable Set<Address> seedNodes, @Nullable P2PServiceListener listener) {
|
||||
public void createAndStartP2PService(EncryptionService encryptionService,
|
||||
KeyRing keyRing,
|
||||
Address mySeedNodeAddress,
|
||||
boolean useLocalhost,
|
||||
int networkId,
|
||||
@Nullable Set<Address> seedNodes,
|
||||
@Nullable P2PServiceListener listener) {
|
||||
Log.traceCall();
|
||||
SeedNodesRepository seedNodesRepository = new SeedNodesRepository();
|
||||
if (seedNodes != null && !seedNodes.isEmpty()) {
|
||||
|
@ -100,8 +118,18 @@ public class SeedNode {
|
|||
else
|
||||
seedNodesRepository.setTorSeedNodeAddresses(seedNodes);
|
||||
}
|
||||
Path seedNodePath = Paths.get(defaultUserDataDir,
|
||||
"Bitsquare_seed_node_" + String.valueOf(mySeedNodeAddress.getFullAddress().replace(":", "_")));
|
||||
File storageDir = Paths.get(seedNodePath.toString(), "db").toFile();
|
||||
File torDir = Paths.get(seedNodePath.toString(), "tor").toFile();
|
||||
|
||||
p2PService = new P2PService(seedNodesRepository, mySeedNodeAddress.port, new File("bitsquare_seed_node_" + mySeedNodeAddress.port), useLocalhost, encryptionService, keyRing, new File("dummy"));
|
||||
if (storageDir.mkdirs())
|
||||
log.info("Created storageDir at " + storageDir.getAbsolutePath());
|
||||
if (torDir.mkdirs())
|
||||
log.info("Created torDir at " + torDir.getAbsolutePath());
|
||||
|
||||
p2PService = new P2PService(seedNodesRepository, mySeedNodeAddress.port, torDir,
|
||||
useLocalhost, networkId, encryptionService, keyRing, storageDir);
|
||||
p2PService.removeMySeedNodeAddressFromList(mySeedNodeAddress);
|
||||
p2PService.start(listener);
|
||||
}
|
||||
|
|
|
@ -4,33 +4,53 @@ import com.google.common.collect.Sets;
|
|||
import io.bitsquare.p2p.Address;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SeedNodesRepository {
|
||||
|
||||
|
||||
// mainnet use port 8000
|
||||
// testnet use port 8001
|
||||
// regtest use port 8002
|
||||
private Set<Address> torSeedNodeAddresses = Sets.newHashSet(
|
||||
// mainnet
|
||||
new Address("lmvdenjkyvx2ovga.onion:8000"),
|
||||
new Address("eo5ay2lyzrfvx2nr.onion:8000"),
|
||||
new Address("si3uu56adkyqkldl.onion:8000"),
|
||||
|
||||
// testnet
|
||||
new Address("lmvdenjkyvx2ovga.onion:8001"),
|
||||
new Address("eo5ay2lyzrfvx2nr.onion:8001"),
|
||||
new Address("si3uu56adkyqkldl.onion:8001"),
|
||||
|
||||
// regtest
|
||||
new Address("lmvdenjkyvx2ovga.onion:8002"),
|
||||
new Address("eo5ay2lyzrfvx2nr.onion:8002"),
|
||||
new Address("si3uu56adkyqkldl.onion:8003")
|
||||
new Address("si3uu56adkyqkldl.onion:8002")
|
||||
);
|
||||
|
||||
|
||||
private Set<Address> localhostSeedNodeAddresses = Sets.newHashSet(
|
||||
new Address("localhost:8001"),
|
||||
new Address("localhost:8002"),
|
||||
new Address("localhost:8003")
|
||||
// mainnet
|
||||
new Address("localhost:2000"),
|
||||
new Address("localhost:3000"),
|
||||
new Address("localhost:4000"),
|
||||
|
||||
// testnet
|
||||
new Address("localhost:2001"),
|
||||
new Address("localhost:3001"),
|
||||
new Address("localhost:4001"),
|
||||
|
||||
// regtest
|
||||
new Address("localhost:2002"),
|
||||
new Address("localhost:3002"),
|
||||
new Address("localhost:4002")
|
||||
);
|
||||
|
||||
public Set<Address> getTorSeedNodeAddresses() {
|
||||
return torSeedNodeAddresses;
|
||||
}
|
||||
|
||||
public Set<Address> geSeedNodeAddresses(boolean useLocalhost) {
|
||||
return useLocalhost ? localhostSeedNodeAddresses : torSeedNodeAddresses;
|
||||
}
|
||||
|
||||
public Set<Address> getLocalhostSeedNodeAddresses() {
|
||||
return localhostSeedNodeAddresses;
|
||||
public Set<Address> geSeedNodeAddresses(boolean useLocalhost, int networkId) {
|
||||
String networkIdAsString = String.valueOf(networkId);
|
||||
Set<Address> addresses = useLocalhost ? localhostSeedNodeAddresses : torSeedNodeAddresses;
|
||||
return addresses.stream()
|
||||
.filter(e -> String.valueOf(e.port).endsWith(networkIdAsString)).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public void setTorSeedNodeAddresses(Set<Address> torSeedNodeAddresses) {
|
||||
|
|
|
@ -28,10 +28,10 @@ import java.io.File;
|
|||
import java.math.BigInteger;
|
||||
import java.security.KeyPair;
|
||||
import java.security.PublicKey;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
// Run in UserThread
|
||||
|
@ -42,10 +42,10 @@ public class ProtectedExpirableDataStorage implements MessageListener {
|
|||
public static int CHECK_TTL_INTERVAL = 10 * 60 * 1000;
|
||||
|
||||
private final PeerGroup peerGroup;
|
||||
private final Map<BigInteger, ProtectedData> map = new ConcurrentHashMap<>();
|
||||
private final Map<BigInteger, ProtectedData> map = new HashMap<>();
|
||||
private final CopyOnWriteArraySet<HashMapChangedListener> hashMapChangedListeners = new CopyOnWriteArraySet<>();
|
||||
private ConcurrentHashMap<BigInteger, Integer> sequenceNumberMap = new ConcurrentHashMap<>();
|
||||
private final Storage<ConcurrentHashMap> storage;
|
||||
private HashMap<BigInteger, Integer> sequenceNumberMap = new HashMap<>();
|
||||
private final Storage<HashMap> storage;
|
||||
private final Timer timer = new Timer();
|
||||
private volatile boolean shutDownInProgress;
|
||||
|
||||
|
@ -65,7 +65,7 @@ public class ProtectedExpirableDataStorage implements MessageListener {
|
|||
|
||||
private void init() {
|
||||
Log.traceCall();
|
||||
ConcurrentHashMap<BigInteger, Integer> persisted = storage.initAndGetPersisted(sequenceNumberMap, "sequenceNumberMap");
|
||||
HashMap<BigInteger, Integer> persisted = storage.initAndGetPersisted(sequenceNumberMap, "sequenceNumberMap");
|
||||
if (persisted != null) {
|
||||
sequenceNumberMap = persisted;
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@ import io.bitsquare.app.Version;
|
|||
import io.bitsquare.p2p.Message;
|
||||
|
||||
public abstract class DataBroadcastMessage implements Message {
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
}
|
|
@ -7,11 +7,13 @@ public final class GetDataRequest implements Message {
|
|||
// That object is sent over the wire, so we need to take care of version compatibility.
|
||||
private static final long serialVersionUID = Version.NETWORK_PROTOCOL_VERSION;
|
||||
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
|
||||
public GetDataRequest() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import java.util.HashSet;
|
|||
public final class GetDataResponse implements Message {
|
||||
// That object is sent over the wire, so we need to take care of version compatibility.
|
||||
private static final long serialVersionUID = Version.NETWORK_PROTOCOL_VERSION;
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
|
||||
public final HashSet<ProtectedData> set;
|
||||
|
||||
|
@ -18,7 +19,7 @@ public final class GetDataResponse implements Message {
|
|||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -84,6 +84,7 @@ public class EncryptionServiceTests {
|
|||
|
||||
class TestMessage implements MailboxMessage {
|
||||
public String data = "test";
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
|
||||
public TestMessage(String data) {
|
||||
this.data = data;
|
||||
|
@ -96,6 +97,6 @@ class TestMessage implements MailboxMessage {
|
|||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -70,17 +70,18 @@ public class TestUtils {
|
|||
seedNodes.add(new Address("localhost:8002"));
|
||||
seedNodes.add(new Address("localhost:8003"));
|
||||
sleepTime = 100;
|
||||
seedNode = new SeedNode();
|
||||
seedNode = new SeedNode("test_dummy_dir");
|
||||
} else {
|
||||
seedNodes.add(new Address("3omjuxn7z73pxoee.onion:8001"));
|
||||
seedNodes.add(new Address("j24fxqyghjetgpdx.onion:8002"));
|
||||
seedNodes.add(new Address("45367tl6unwec6kw.onion:8003"));
|
||||
sleepTime = 10000;
|
||||
seedNode = new SeedNode();
|
||||
seedNode = new SeedNode("test_dummy_dir");
|
||||
}
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
seedNode.createAndStartP2PService(encryptionService, keyRing, new Address("localhost", port), useLocalhost, seedNodes, new P2PServiceListener() {
|
||||
seedNode.createAndStartP2PService(encryptionService, keyRing, new Address("localhost", port), useLocalhost, 2,
|
||||
seedNodes, new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
}
|
||||
|
@ -107,7 +108,9 @@ public class TestUtils {
|
|||
return seedNode;
|
||||
}
|
||||
|
||||
public static P2PService getAndAuthenticateP2PService(int port, EncryptionService encryptionService, KeyRing keyRing, boolean useLocalhost, Set<Address> seedNodes) throws InterruptedException {
|
||||
public static P2PService getAndAuthenticateP2PService(int port, EncryptionService encryptionService, KeyRing keyRing,
|
||||
boolean useLocalhost, Set<Address> seedNodes)
|
||||
throws InterruptedException {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
SeedNodesRepository seedNodesRepository = new SeedNodesRepository();
|
||||
if (seedNodes != null && !seedNodes.isEmpty()) {
|
||||
|
@ -117,7 +120,8 @@ public class TestUtils {
|
|||
seedNodesRepository.setTorSeedNodeAddresses(seedNodes);
|
||||
}
|
||||
|
||||
P2PService p2PService = new P2PService(seedNodesRepository, port, new File("seed_node_" + port), useLocalhost, encryptionService, keyRing, new File("dummy"));
|
||||
P2PService p2PService = new P2PService(seedNodesRepository, port, new File("seed_node_" + port), useLocalhost, 2,
|
||||
encryptionService, keyRing, new File("dummy"));
|
||||
p2PService.start(new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
|
|
|
@ -6,6 +6,7 @@ import io.bitsquare.p2p.messaging.MailboxMessage;
|
|||
import io.bitsquare.p2p.storage.data.ExpirablePayload;
|
||||
|
||||
public class MockMailboxMessage implements MailboxMessage, ExpirablePayload {
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
public String msg;
|
||||
public Address senderAddress;
|
||||
public long ttl;
|
||||
|
@ -17,7 +18,7 @@ public class MockMailboxMessage implements MailboxMessage, ExpirablePayload {
|
|||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -7,6 +7,7 @@ import io.bitsquare.p2p.storage.data.ExpirablePayload;
|
|||
public class MockMessage implements Message, ExpirablePayload {
|
||||
public String msg;
|
||||
public long ttl;
|
||||
private final int networkId = Version.NETWORK_ID;
|
||||
|
||||
public MockMessage(String msg) {
|
||||
this.msg = msg;
|
||||
|
@ -14,7 +15,7 @@ public class MockMessage implements Message, ExpirablePayload {
|
|||
|
||||
@Override
|
||||
public int networkId() {
|
||||
return Version.NETWORK_ID;
|
||||
return networkId;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -81,9 +81,10 @@ public class PeerGroupTest {
|
|||
seedNodes = new HashSet<>();
|
||||
Address address = new Address("localhost:8001");
|
||||
seedNodes.add(address);
|
||||
seedNode1 = new SeedNode();
|
||||
seedNode1 = new SeedNode("test_dummy_dir");
|
||||
latch = new CountDownLatch(2);
|
||||
seedNode1.createAndStartP2PService(null, null, address, useLocalhost, seedNodes, new P2PServiceListener() {
|
||||
seedNode1.createAndStartP2PService(null, null, address, useLocalhost, 2,
|
||||
seedNodes, new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
latch.countDown();
|
||||
|
@ -126,8 +127,8 @@ public class PeerGroupTest {
|
|||
|
||||
latch = new CountDownLatch(6);
|
||||
|
||||
seedNode1 = new SeedNode();
|
||||
seedNode1.createAndStartP2PService(null, null, address1, useLocalhost, seedNodes, new P2PServiceListener() {
|
||||
seedNode1 = new SeedNode("test_dummy_dir");
|
||||
seedNode1.createAndStartP2PService(null, null, address1, useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
latch.countDown();
|
||||
|
@ -157,8 +158,8 @@ public class PeerGroupTest {
|
|||
|
||||
Thread.sleep(500);
|
||||
|
||||
seedNode2 = new SeedNode();
|
||||
seedNode2.createAndStartP2PService(null, null, address2, useLocalhost, seedNodes, new P2PServiceListener() {
|
||||
seedNode2 = new SeedNode("test_dummy_dir");
|
||||
seedNode2.createAndStartP2PService(null, null, address2, useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
latch.countDown();
|
||||
|
@ -382,10 +383,10 @@ public class PeerGroupTest {
|
|||
}
|
||||
|
||||
private SeedNode getAndStartSeedNode(int port) throws InterruptedException {
|
||||
SeedNode seedNode = new SeedNode();
|
||||
SeedNode seedNode = new SeedNode("test_dummy_dir");
|
||||
|
||||
latch = new CountDownLatch(1);
|
||||
seedNode.createAndStartP2PService(null, null, new Address("localhost", port), useLocalhost, seedNodes, new P2PServiceListener() {
|
||||
seedNode.createAndStartP2PService(null, null, new Address("localhost", port), useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||
@Override
|
||||
public void onRequestingDataCompleted() {
|
||||
latch.countDown();
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package io.bitsquare.p2p.seed;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import io.bitsquare.app.BitsquareEnvironment;
|
||||
import io.bitsquare.app.Log;
|
||||
import io.bitsquare.common.UserThread;
|
||||
import org.bitcoinj.crypto.DRMWorkaround;
|
||||
|
@ -47,7 +48,7 @@ public class SeedNodeMain {
|
|||
UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory));
|
||||
UserThread.execute(() -> {
|
||||
try {
|
||||
seedNode = new SeedNode();
|
||||
seedNode = new SeedNode(BitsquareEnvironment.defaultUserDataDir());
|
||||
seedNode.processArgs(args);
|
||||
seedNode.createAndStartP2PService();
|
||||
} catch (Throwable t) {
|
||||
|
|
Loading…
Add table
Reference in a new issue