mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 09:52:23 +01:00
Replace persistedPeers with peerList
This commit is contained in:
parent
25bfe2d6ad
commit
25526750a8
@ -43,6 +43,7 @@ import javax.inject.Named;
|
|||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@ -108,9 +109,6 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
|
|||||||
|
|
||||||
// Persistable peerList
|
// Persistable peerList
|
||||||
private final PeerList peerList = new PeerList();
|
private final PeerList peerList = new PeerList();
|
||||||
// Peers we had persisted TODO use peerList instead
|
|
||||||
@Getter
|
|
||||||
private final Set<Peer> persistedPeers = new HashSet<>();
|
|
||||||
// Peers we got reported from other peers
|
// Peers we got reported from other peers
|
||||||
@Getter
|
@Getter
|
||||||
private final Set<Peer> reportedPeers = new HashSet<>();
|
private final Set<Peer> reportedPeers = new HashSet<>();
|
||||||
@ -188,7 +186,7 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
|
|||||||
public void readPersisted() {
|
public void readPersisted() {
|
||||||
PeerList persisted = persistenceManager.getPersisted();
|
PeerList persisted = persistenceManager.getPersisted();
|
||||||
if (persisted != null) {
|
if (persisted != null) {
|
||||||
this.persistedPeers.addAll(persisted.getList());
|
peerList.setAll(persisted.getList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,11 +249,16 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
|
|||||||
|
|
||||||
public Set<Peer> getAllPeers() {
|
public Set<Peer> getAllPeers() {
|
||||||
Set<Peer> allPeers = new HashSet<>(getLivePeers());
|
Set<Peer> allPeers = new HashSet<>(getLivePeers());
|
||||||
allPeers.addAll(persistedPeers);
|
allPeers.addAll(peerList.getList());
|
||||||
allPeers.addAll(reportedPeers);
|
allPeers.addAll(reportedPeers);
|
||||||
return allPeers;
|
return allPeers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Collection<Peer> getPersistedPeers() {
|
||||||
|
return peerList.getList();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// ConnectionListener implementation
|
// ConnectionListener implementation
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -507,7 +510,7 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
|
|||||||
reportedPeers.addAll(reportedPeersToAdd);
|
reportedPeers.addAll(reportedPeersToAdd);
|
||||||
purgeReportedPeersIfExceeds();
|
purgeReportedPeersIfExceeds();
|
||||||
|
|
||||||
persistedPeers.addAll(reportedPeersToAdd);
|
peerList.getList().addAll(reportedPeersToAdd);
|
||||||
purgePersistedPeersIfExceeds();
|
purgePersistedPeersIfExceeds();
|
||||||
requestPersistence();
|
requestPersistence();
|
||||||
|
|
||||||
@ -582,8 +585,8 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
|
|||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
private boolean removePersistedPeer(Peer persistedPeer) {
|
private boolean removePersistedPeer(Peer persistedPeer) {
|
||||||
if (persistedPeers.contains(persistedPeer)) {
|
if (peerList.getList().contains(persistedPeer)) {
|
||||||
persistedPeers.remove(persistedPeer);
|
peerList.getList().remove(persistedPeer);
|
||||||
requestPersistence();
|
requestPersistence();
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
@ -592,7 +595,6 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void requestPersistence() {
|
private void requestPersistence() {
|
||||||
peerList.setAll(persistedPeers);
|
|
||||||
persistenceManager.requestPersistence();
|
persistenceManager.requestPersistence();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -603,27 +605,27 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Optional<Peer> getOptionalPersistedPeer(NodeAddress nodeAddress) {
|
private Optional<Peer> getOptionalPersistedPeer(NodeAddress nodeAddress) {
|
||||||
return persistedPeers.stream()
|
return peerList.getList().stream()
|
||||||
.filter(e -> e.getNodeAddress().equals(nodeAddress))
|
.filter(e -> e.getNodeAddress().equals(nodeAddress))
|
||||||
.findAny();
|
.findAny();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeTooOldPersistedPeers() {
|
private void removeTooOldPersistedPeers() {
|
||||||
Set<Peer> persistedPeersToRemove = persistedPeers.stream()
|
Set<Peer> persistedPeersToRemove = peerList.getList().stream()
|
||||||
.filter(reportedPeer -> new Date().getTime() - reportedPeer.getDate().getTime() > MAX_AGE)
|
.filter(reportedPeer -> new Date().getTime() - reportedPeer.getDate().getTime() > MAX_AGE)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
persistedPeersToRemove.forEach(this::removePersistedPeer);
|
persistedPeersToRemove.forEach(this::removePersistedPeer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void purgePersistedPeersIfExceeds() {
|
private void purgePersistedPeersIfExceeds() {
|
||||||
int size = persistedPeers.size();
|
int size = peerList.getList().size();
|
||||||
int limit = MAX_PERSISTED_PEERS;
|
int limit = MAX_PERSISTED_PEERS;
|
||||||
if (size > limit) {
|
if (size > limit) {
|
||||||
log.trace("We have already {} persisted peers which exceeds our limit of {}." +
|
log.trace("We have already {} persisted peers which exceeds our limit of {}." +
|
||||||
"We remove random peers from the persisted peers list.", size, limit);
|
"We remove random peers from the persisted peers list.", size, limit);
|
||||||
int diff = size - limit;
|
int diff = size - limit;
|
||||||
List<Peer> list = new ArrayList<>(persistedPeers);
|
List<Peer> list = new ArrayList<>(peerList.getList());
|
||||||
// we dont use sorting by lastActivityDate to avoid attack vectors and keep it more random
|
// we don't use sorting by lastActivityDate to avoid attack vectors and keep it more random
|
||||||
for (int i = 0; i < diff; i++) {
|
for (int i = 0; i < diff; i++) {
|
||||||
if (!list.isEmpty()) {
|
if (!list.isEmpty()) {
|
||||||
Peer toRemove = list.remove(new Random().nextInt(list.size()));
|
Peer toRemove = list.remove(new Random().nextInt(list.size()));
|
||||||
|
Loading…
Reference in New Issue
Block a user