mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-06-24 23:00:36 -04:00
refactor p2pservice contructor
This commit is contained in:
parent
f9a31f4b8a
commit
cefcf5b3d8
8 changed files with 52 additions and 47 deletions
|
@ -44,9 +44,6 @@ import java.util.concurrent.TimeUnit;
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents our node in the P2P network
|
|
||||||
*/
|
|
||||||
public class P2PService implements SetupListener, MessageListener, ConnectionListener {
|
public class P2PService implements SetupListener, MessageListener, ConnectionListener {
|
||||||
private static final Logger log = LoggerFactory.getLogger(P2PService.class);
|
private static final Logger log = LoggerFactory.getLogger(P2PService.class);
|
||||||
|
|
||||||
|
@ -92,9 +89,10 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
||||||
@Named(ProgramArguments.TOR_DIR) File torDir,
|
@Named(ProgramArguments.TOR_DIR) File torDir,
|
||||||
@Named(ProgramArguments.USE_LOCALHOST) boolean useLocalhost,
|
@Named(ProgramArguments.USE_LOCALHOST) boolean useLocalhost,
|
||||||
@Named(ProgramArguments.NETWORK_ID) int networkId,
|
@Named(ProgramArguments.NETWORK_ID) int networkId,
|
||||||
|
@Named("storage.dir") File storageDir,
|
||||||
@Nullable EncryptionService encryptionService,
|
@Nullable EncryptionService encryptionService,
|
||||||
KeyRing keyRing,
|
@Nullable KeyRing keyRing
|
||||||
@Named("storage.dir") File storageDir) {
|
) {
|
||||||
Log.traceCall();
|
Log.traceCall();
|
||||||
this.seedNodesRepository = seedNodesRepository;
|
this.seedNodesRepository = seedNodesRepository;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
@ -107,6 +105,16 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
||||||
init(networkId, storageDir);
|
init(networkId, storageDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Used for seed node
|
||||||
|
public P2PService(SeedNodesRepository seedNodesRepository,
|
||||||
|
int port,
|
||||||
|
File torDir,
|
||||||
|
boolean useLocalhost,
|
||||||
|
int networkId,
|
||||||
|
File storageDir) {
|
||||||
|
this(seedNodesRepository, port, torDir, useLocalhost, networkId, storageDir, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
private void init(int networkId, File storageDir) {
|
private void init(int networkId, File storageDir) {
|
||||||
Log.traceCall();
|
Log.traceCall();
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,7 @@ import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
|
|
||||||
|
// authentication protocol:
|
||||||
// authentication example:
|
|
||||||
// node2 -> node1 AuthenticationRequest
|
// node2 -> node1 AuthenticationRequest
|
||||||
// node1: close connection
|
// node1: close connection
|
||||||
// node1 -> node2 AuthenticationResponse on new connection
|
// node1 -> node2 AuthenticationResponse on new connection
|
||||||
|
|
|
@ -23,7 +23,6 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
|
|
||||||
// Run in UserThread
|
|
||||||
public class PeerGroup implements MessageListener, ConnectionListener {
|
public class PeerGroup implements MessageListener, ConnectionListener {
|
||||||
private static final Logger log = LoggerFactory.getLogger(PeerGroup.class);
|
private static final Logger log = LoggerFactory.getLogger(PeerGroup.class);
|
||||||
|
|
||||||
|
@ -656,28 +655,31 @@ public class PeerGroup implements MessageListener, ConnectionListener {
|
||||||
} else {
|
} else {
|
||||||
newReportedPeers.remove(new ReportedPeer(getMyAddress(), new Date()));
|
newReportedPeers.remove(new ReportedPeer(getMyAddress(), new Date()));
|
||||||
|
|
||||||
//TODO if we have already peer, we mix date from old and new item
|
// In case we have a peers already we adjust the lastActivityDate by adjusting the date to the mid
|
||||||
//
|
// of the lastActivityDate of our already stored peer and the reported one
|
||||||
/* Map<Address, ReportedPeer> reportedPeersMap = new HashMap<>();
|
Map<Address, ReportedPeer> reportedPeersMap = new HashMap<>();
|
||||||
reportedPeers.stream().forEach(e -> reportedPeersMap.put(e.address, e));
|
reportedPeers.stream().forEach(e -> reportedPeersMap.put(e.address, e));
|
||||||
|
Set<ReportedPeer> newAdjustedReportedPeers = new HashSet<>();
|
||||||
HashSet<ReportedPeer> newAdjustedReportedPeers = new HashSet<>();
|
|
||||||
newReportedPeers.stream()
|
newReportedPeers.stream()
|
||||||
.forEach(e -> {
|
.forEach(e -> {
|
||||||
if()
|
if (reportedPeersMap.containsKey(e.address)) {
|
||||||
long adjustedTime = (e.lastActivityDate.getTime() +
|
long adjustedTime = (e.lastActivityDate.getTime() +
|
||||||
reportedPeersMap.get(e.address).lastActivityDate.getTime()) / 2;
|
reportedPeersMap.get(e.address).lastActivityDate.getTime()) / 2;
|
||||||
newAdjustedReportedPeers.add(new ReportedPeer(e.address,
|
newAdjustedReportedPeers.add(new ReportedPeer(e.address,
|
||||||
new Date(adjustedTime)));
|
new Date(adjustedTime)));
|
||||||
});*/
|
} else {
|
||||||
|
newAdjustedReportedPeers.add(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
this.reportedPeers.addAll(newReportedPeers);
|
this.reportedPeers.addAll(newAdjustedReportedPeers);
|
||||||
purgeReportedPeersIfExceeds();
|
purgeReportedPeersIfExceeds();
|
||||||
}
|
}
|
||||||
|
|
||||||
printReportedPeers();
|
printReportedPeers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO unit test
|
||||||
private void purgeReportedPeersIfExceeds() {
|
private void purgeReportedPeersIfExceeds() {
|
||||||
Log.traceCall();
|
Log.traceCall();
|
||||||
int size = reportedPeers.size();
|
int size = reportedPeers.size();
|
||||||
|
@ -685,9 +687,10 @@ public class PeerGroup implements MessageListener, ConnectionListener {
|
||||||
log.trace("We have more then {} reported peers. size={}. " +
|
log.trace("We have more then {} reported peers. size={}. " +
|
||||||
"We remove random peers from the reported peers list.", MAX_REPORTED_PEERS, size);
|
"We remove random peers from the reported peers list.", MAX_REPORTED_PEERS, size);
|
||||||
int diff = size - MAX_REPORTED_PEERS;
|
int diff = size - MAX_REPORTED_PEERS;
|
||||||
List<ReportedPeer> list = new LinkedList<>(getReportedNotConnectedPeerAddresses());
|
List<ReportedPeer> list = new ArrayList<>(getReportedNotConnectedPeerAddresses());
|
||||||
|
log.debug("Peers before sort " + list);
|
||||||
//TODO sort and remove oldest
|
list.sort((a, b) -> a.lastActivityDate.compareTo(b.lastActivityDate));
|
||||||
|
log.debug("Peers after sort " + list);
|
||||||
for (int i = 0; i < diff; i++) {
|
for (int i = 0; i < diff; i++) {
|
||||||
ReportedPeer toRemove = getAndRemoveRandomReportedPeer(list);
|
ReportedPeer toRemove = getAndRemoveRandomReportedPeer(list);
|
||||||
reportedPeers.remove(toRemove);
|
reportedPeers.remove(toRemove);
|
||||||
|
|
|
@ -3,8 +3,6 @@ package io.bitsquare.p2p.seed;
|
||||||
import io.bitsquare.app.Log;
|
import io.bitsquare.app.Log;
|
||||||
import io.bitsquare.app.Version;
|
import io.bitsquare.app.Version;
|
||||||
import io.bitsquare.common.UserThread;
|
import io.bitsquare.common.UserThread;
|
||||||
import io.bitsquare.common.crypto.KeyRing;
|
|
||||||
import io.bitsquare.crypto.EncryptionService;
|
|
||||||
import io.bitsquare.p2p.Address;
|
import io.bitsquare.p2p.Address;
|
||||||
import io.bitsquare.p2p.P2PService;
|
import io.bitsquare.p2p.P2PService;
|
||||||
import io.bitsquare.p2p.P2PServiceListener;
|
import io.bitsquare.p2p.P2PServiceListener;
|
||||||
|
@ -28,7 +26,7 @@ public class SeedNode {
|
||||||
|
|
||||||
private Address mySeedNodeAddress = new Address("localhost:8001");
|
private Address mySeedNodeAddress = new Address("localhost:8001");
|
||||||
private boolean useLocalhost = false;
|
private boolean useLocalhost = false;
|
||||||
private Set<Address> seedNodes;
|
private Set<Address> progArgSeedNodes;
|
||||||
private P2PService p2PService;
|
private P2PService p2PService;
|
||||||
private boolean stopped;
|
private boolean stopped;
|
||||||
private final String defaultUserDataDir;
|
private final String defaultUserDataDir;
|
||||||
|
@ -80,13 +78,13 @@ public class SeedNode {
|
||||||
checkArgument(arg4.contains(":") && arg4.split(":").length > 1 && arg4.split(":")[1].length() > 3,
|
checkArgument(arg4.contains(":") && arg4.split(":").length > 1 && arg4.split(":")[1].length() > 3,
|
||||||
"Wrong program argument");
|
"Wrong program argument");
|
||||||
List<String> list = Arrays.asList(arg4.split("|"));
|
List<String> list = Arrays.asList(arg4.split("|"));
|
||||||
seedNodes = new HashSet<>();
|
progArgSeedNodes = new HashSet<>();
|
||||||
list.forEach(e -> {
|
list.forEach(e -> {
|
||||||
checkArgument(e.contains(":") && e.split(":").length == 2 && e.split(":")[1].length() == 4,
|
checkArgument(e.contains(":") && e.split(":").length == 2 && e.split(":")[1].length() == 4,
|
||||||
"Wrong program argument");
|
"Wrong program argument");
|
||||||
seedNodes.add(new Address(e));
|
progArgSeedNodes.add(new Address(e));
|
||||||
});
|
});
|
||||||
seedNodes.remove(mySeedNodeAddress);
|
progArgSeedNodes.remove(mySeedNodeAddress);
|
||||||
} else if (args.length > 5) {
|
} else if (args.length > 5) {
|
||||||
log.error("Too many program arguments." +
|
log.error("Too many program arguments." +
|
||||||
"\nProgram arguments: myAddress (incl. port) bitcoinNetworkId " +
|
"\nProgram arguments: myAddress (incl. port) bitcoinNetworkId " +
|
||||||
|
@ -100,23 +98,21 @@ public class SeedNode {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createAndStartP2PService() {
|
public void createAndStartP2PService() {
|
||||||
createAndStartP2PService(null, null, mySeedNodeAddress, useLocalhost, Version.NETWORK_ID, seedNodes, null);
|
createAndStartP2PService(mySeedNodeAddress, useLocalhost, Version.NETWORK_ID, progArgSeedNodes, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createAndStartP2PService(EncryptionService encryptionService,
|
public void createAndStartP2PService(Address mySeedNodeAddress,
|
||||||
KeyRing keyRing,
|
|
||||||
Address mySeedNodeAddress,
|
|
||||||
boolean useLocalhost,
|
boolean useLocalhost,
|
||||||
int networkId,
|
int networkId,
|
||||||
@Nullable Set<Address> seedNodes,
|
@Nullable Set<Address> progArgSeedNodes,
|
||||||
@Nullable P2PServiceListener listener) {
|
@Nullable P2PServiceListener listener) {
|
||||||
Log.traceCall();
|
Log.traceCall();
|
||||||
SeedNodesRepository seedNodesRepository = new SeedNodesRepository();
|
SeedNodesRepository seedNodesRepository = new SeedNodesRepository();
|
||||||
if (seedNodes != null && !seedNodes.isEmpty()) {
|
if (progArgSeedNodes != null && !progArgSeedNodes.isEmpty()) {
|
||||||
if (useLocalhost)
|
if (useLocalhost)
|
||||||
seedNodesRepository.setLocalhostSeedNodeAddresses(seedNodes);
|
seedNodesRepository.setLocalhostSeedNodeAddresses(progArgSeedNodes);
|
||||||
else
|
else
|
||||||
seedNodesRepository.setTorSeedNodeAddresses(seedNodes);
|
seedNodesRepository.setTorSeedNodeAddresses(progArgSeedNodes);
|
||||||
}
|
}
|
||||||
Path seedNodePath = Paths.get(defaultUserDataDir,
|
Path seedNodePath = Paths.get(defaultUserDataDir,
|
||||||
"Bitsquare_seed_node_" + String.valueOf(mySeedNodeAddress.getFullAddress().replace(":", "_")));
|
"Bitsquare_seed_node_" + String.valueOf(mySeedNodeAddress.getFullAddress().replace(":", "_")));
|
||||||
|
@ -128,8 +124,7 @@ public class SeedNode {
|
||||||
if (torDir.mkdirs())
|
if (torDir.mkdirs())
|
||||||
log.info("Created torDir at " + torDir.getAbsolutePath());
|
log.info("Created torDir at " + torDir.getAbsolutePath());
|
||||||
|
|
||||||
p2PService = new P2PService(seedNodesRepository, mySeedNodeAddress.port, torDir,
|
p2PService = new P2PService(seedNodesRepository, mySeedNodeAddress.port, torDir, useLocalhost, networkId, storageDir);
|
||||||
useLocalhost, networkId, encryptionService, keyRing, storageDir);
|
|
||||||
p2PService.removeMySeedNodeAddressFromList(mySeedNodeAddress);
|
p2PService.removeMySeedNodeAddressFromList(mySeedNodeAddress);
|
||||||
p2PService.start(listener);
|
p2PService.start(listener);
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,7 @@ public class P2PServiceTest {
|
||||||
sleepTime = 1000;
|
sleepTime = 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
seedNode1 = TestUtils.getAndStartSeedNode(8001, encryptionService1, keyRing1, useLocalhost, seedNodes);
|
seedNode1 = TestUtils.getAndStartSeedNode(8001, useLocalhost, seedNodes);
|
||||||
p2PService1 = seedNode1.getP2PService();
|
p2PService1 = seedNode1.getP2PService();
|
||||||
p2PService2 = TestUtils.getAndAuthenticateP2PService(8002, encryptionService2, keyRing2, useLocalhost, seedNodes);
|
p2PService2 = TestUtils.getAndAuthenticateP2PService(8002, encryptionService2, keyRing2, useLocalhost, seedNodes);
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,7 @@ public class TestUtils {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SeedNode getAndStartSeedNode(int port, EncryptionService encryptionService, KeyRing keyRing, boolean useLocalhost, Set<Address> seedNodes) throws InterruptedException {
|
public static SeedNode getAndStartSeedNode(int port, boolean useLocalhost, Set<Address> seedNodes) throws InterruptedException {
|
||||||
SeedNode seedNode;
|
SeedNode seedNode;
|
||||||
|
|
||||||
if (useLocalhost) {
|
if (useLocalhost) {
|
||||||
|
@ -80,7 +80,7 @@ public class TestUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
seedNode.createAndStartP2PService(encryptionService, keyRing, new Address("localhost", port), useLocalhost, 2,
|
seedNode.createAndStartP2PService(new Address("localhost", port), useLocalhost, 2,
|
||||||
seedNodes, new P2PServiceListener() {
|
seedNodes, new P2PServiceListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onRequestingDataCompleted() {
|
public void onRequestingDataCompleted() {
|
||||||
|
|
|
@ -80,7 +80,7 @@ public class PeerGroupTest {
|
||||||
seedNodes.add(address);
|
seedNodes.add(address);
|
||||||
seedNode1 = new SeedNode("test_dummy_dir");
|
seedNode1 = new SeedNode("test_dummy_dir");
|
||||||
latch = new CountDownLatch(2);
|
latch = new CountDownLatch(2);
|
||||||
seedNode1.createAndStartP2PService(null, null, address, useLocalhost, 2,
|
seedNode1.createAndStartP2PService(address, useLocalhost, 2,
|
||||||
seedNodes, new P2PServiceListener() {
|
seedNodes, new P2PServiceListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onRequestingDataCompleted() {
|
public void onRequestingDataCompleted() {
|
||||||
|
@ -125,7 +125,7 @@ public class PeerGroupTest {
|
||||||
latch = new CountDownLatch(6);
|
latch = new CountDownLatch(6);
|
||||||
|
|
||||||
seedNode1 = new SeedNode("test_dummy_dir");
|
seedNode1 = new SeedNode("test_dummy_dir");
|
||||||
seedNode1.createAndStartP2PService(null, null, address1, useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
seedNode1.createAndStartP2PService(address1, useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onRequestingDataCompleted() {
|
public void onRequestingDataCompleted() {
|
||||||
latch.countDown();
|
latch.countDown();
|
||||||
|
@ -156,7 +156,7 @@ public class PeerGroupTest {
|
||||||
Thread.sleep(500);
|
Thread.sleep(500);
|
||||||
|
|
||||||
seedNode2 = new SeedNode("test_dummy_dir");
|
seedNode2 = new SeedNode("test_dummy_dir");
|
||||||
seedNode2.createAndStartP2PService(null, null, address2, useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
seedNode2.createAndStartP2PService(address2, useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onRequestingDataCompleted() {
|
public void onRequestingDataCompleted() {
|
||||||
latch.countDown();
|
latch.countDown();
|
||||||
|
@ -386,7 +386,7 @@ public class PeerGroupTest {
|
||||||
SeedNode seedNode = new SeedNode("test_dummy_dir");
|
SeedNode seedNode = new SeedNode("test_dummy_dir");
|
||||||
|
|
||||||
latch = new CountDownLatch(1);
|
latch = new CountDownLatch(1);
|
||||||
seedNode.createAndStartP2PService(null, null, new Address("localhost", port), useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
seedNode.createAndStartP2PService(new Address("localhost", port), useLocalhost, 2, seedNodes, new P2PServiceListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onRequestingDataCompleted() {
|
public void onRequestingDataCompleted() {
|
||||||
latch.countDown();
|
latch.countDown();
|
||||||
|
|
|
@ -64,7 +64,7 @@ public class ProtectedDataStorageTest {
|
||||||
|
|
||||||
storageSignatureKeyPair1 = keyRing1.getSignatureKeyPair();
|
storageSignatureKeyPair1 = keyRing1.getSignatureKeyPair();
|
||||||
encryptionService1 = new EncryptionService(keyRing1);
|
encryptionService1 = new EncryptionService(keyRing1);
|
||||||
networkNode1 = TestUtils.getAndStartSeedNode(8001, encryptionService1, keyRing1, useClearNet, seedNodes).getP2PService().getNetworkNode();
|
networkNode1 = TestUtils.getAndStartSeedNode(8001, useClearNet, seedNodes).getP2PService().getNetworkNode();
|
||||||
peerGroup1 = new PeerGroup(networkNode1, seedNodes);
|
peerGroup1 = new PeerGroup(networkNode1, seedNodes);
|
||||||
dataStorage1 = new ProtectedExpirableDataStorage(peerGroup1, new File("dummy"));
|
dataStorage1 = new ProtectedExpirableDataStorage(peerGroup1, new File("dummy"));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue