mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 09:52:23 +01:00
Refactor Capabilities for Peer
This commit is contained in:
parent
7a03aba02e
commit
837b4dffb3
@ -168,17 +168,17 @@ public final class RepublishGovernanceDataHandler {
|
||||
Capabilities required = new Capabilities(Capability.DAO_FULL_NODE);
|
||||
|
||||
List<Peer> list = peerManager.getLivePeers(null).stream()
|
||||
.filter(peer -> peer.containsAll(required))
|
||||
.filter(peer -> peer.getCapabilities().containsAll(required))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (list.isEmpty())
|
||||
list = peerManager.getReportedPeers().stream()
|
||||
.filter(peer -> peer.containsAll(required))
|
||||
.filter(peer -> peer.getCapabilities().containsAll(required))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (list.isEmpty())
|
||||
list = peerManager.getPersistedPeers().stream()
|
||||
.filter(peer -> peer.containsAll(required))
|
||||
.filter(peer -> peer.getCapabilities().containsAll(required))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (!list.isEmpty()) {
|
||||
|
@ -619,10 +619,10 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
|
||||
// it from old versions, so we filter those.
|
||||
Optional<Peer> optionalPeer = allPeers.stream()
|
||||
.filter(peer -> peer.getNodeAddress().equals(peersNodeAddress))
|
||||
.filter(peer -> peer.hasCapabilities())
|
||||
.filter(peer -> peer.getCapabilities().hasCapabilities())
|
||||
.findAny();
|
||||
if (optionalPeer.isPresent()) {
|
||||
boolean result = optionalPeer.get().containsAll(((CapabilityRequiringPayload) message).getRequiredCapabilities());
|
||||
boolean result = optionalPeer.get().getCapabilities().containsAll(((CapabilityRequiringPayload) message).getRequiredCapabilities());
|
||||
|
||||
if (!result)
|
||||
log.warn("We don't send the message because the peer does not support the required capability. " +
|
||||
|
@ -176,7 +176,7 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
|
||||
PeerList persistedPeerList = storage.initAndGetPersistedWithFileName("PeerList", 1000);
|
||||
if (persistedPeerList != null) {
|
||||
long peersWithNoCapabilitiesSet = persistedPeerList.getList().stream()
|
||||
.filter(e -> e.hasCapabilities())
|
||||
.filter(e -> e.getCapabilities().hasCapabilities())
|
||||
.mapToInt(e -> 1)
|
||||
.count();
|
||||
if (peersWithNoCapabilitiesSet > 100) {
|
||||
@ -668,10 +668,10 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
|
||||
Set<Peer> allPeers = new HashSet<>(getPersistedPeers());
|
||||
allPeers.addAll(getReportedPeers());
|
||||
Optional<Peer> ourPeer = allPeers.stream().filter(peer -> peer.getNodeAddress().equals(connection.getPeersNodeAddressOptional().get()))
|
||||
.filter(peer -> !peer.hasCapabilities())
|
||||
.filter(peer -> !peer.getCapabilities().hasCapabilities())
|
||||
.findAny();
|
||||
if(ourPeer.isPresent())
|
||||
supportedCapabilities = new Capabilities(ourPeer.get());
|
||||
supportedCapabilities = new Capabilities(ourPeer.get().getCapabilities());
|
||||
}
|
||||
Peer peer = new Peer(connection.getPeersNodeAddressOptional().get(), supportedCapabilities);
|
||||
connection.addWeakCapabilitiesListener(peer);
|
||||
|
@ -21,6 +21,7 @@ import bisq.network.p2p.NodeAddress;
|
||||
import bisq.network.p2p.network.SupportedCapabilitiesListener;
|
||||
|
||||
import bisq.common.app.Capabilities;
|
||||
import bisq.common.app.HasCapabilities;
|
||||
import bisq.common.proto.network.NetworkPayload;
|
||||
import bisq.common.proto.persistable.PersistablePayload;
|
||||
|
||||
@ -28,7 +29,6 @@ import io.bisq.generated.protobuffer.PB;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -36,10 +36,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@Getter
|
||||
@EqualsAndHashCode(exclude = {"date"}, callSuper = true)
|
||||
// failedConnectionAttempts is transient and therefore excluded anyway
|
||||
@Slf4j
|
||||
public final class Peer extends Capabilities implements NetworkPayload, PersistablePayload, SupportedCapabilitiesListener {
|
||||
public final class Peer implements HasCapabilities, NetworkPayload, PersistablePayload, SupportedCapabilitiesListener {
|
||||
private static final int MAX_FAILED_CONNECTION_ATTEMPTS = 5;
|
||||
|
||||
private final NodeAddress nodeAddress;
|
||||
@ -48,9 +46,10 @@ public final class Peer extends Capabilities implements NetworkPayload, Persista
|
||||
|
||||
@Setter
|
||||
transient private int failedConnectionAttempts = 0;
|
||||
private Capabilities capabilities;
|
||||
|
||||
public Peer(NodeAddress nodeAddress, @Nullable Capabilities supportedCapabilities) {
|
||||
this(nodeAddress, new Date().getTime(), supportedCapabilities);
|
||||
this(nodeAddress, new Date().getTime(), supportedCapabilities == null ? new Capabilities() : supportedCapabilities);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
@ -58,9 +57,10 @@ public final class Peer extends Capabilities implements NetworkPayload, Persista
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private Peer(NodeAddress nodeAddress, long date, Capabilities supportedCapabilities) {
|
||||
super(supportedCapabilities);
|
||||
super();
|
||||
this.nodeAddress = nodeAddress;
|
||||
this.date = date;
|
||||
this.capabilities = supportedCapabilities;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -68,7 +68,7 @@ public final class Peer extends Capabilities implements NetworkPayload, Persista
|
||||
return PB.Peer.newBuilder()
|
||||
.setNodeAddress(nodeAddress.toProtoMessage())
|
||||
.setDate(date)
|
||||
.addAllSupportedCapabilities(Capabilities.toIntList(this))
|
||||
.addAllSupportedCapabilities(Capabilities.toIntList(getCapabilities()))
|
||||
.build();
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@ public final class Peer extends Capabilities implements NetworkPayload, Persista
|
||||
@Override
|
||||
public void onChanged(Capabilities supportedCapabilities) {
|
||||
if (supportedCapabilities.hasCapabilities())
|
||||
set(supportedCapabilities);
|
||||
capabilities.set(supportedCapabilities);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user