From 837b4dffb339748a449749475efb7fe8ffc1a4db Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Mon, 18 Mar 2019 12:03:26 +0100 Subject: [PATCH] Refactor Capabilities for Peer --- .../network/RepublishGovernanceDataHandler.java | 6 +++--- .../main/java/bisq/network/p2p/P2PService.java | 4 ++-- .../java/bisq/network/p2p/peers/PeerManager.java | 6 +++--- .../network/p2p/peers/peerexchange/Peer.java | 16 ++++++++-------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/bisq/core/dao/governance/blindvote/network/RepublishGovernanceDataHandler.java b/core/src/main/java/bisq/core/dao/governance/blindvote/network/RepublishGovernanceDataHandler.java index 57e4649438..569796343b 100644 --- a/core/src/main/java/bisq/core/dao/governance/blindvote/network/RepublishGovernanceDataHandler.java +++ b/core/src/main/java/bisq/core/dao/governance/blindvote/network/RepublishGovernanceDataHandler.java @@ -168,17 +168,17 @@ public final class RepublishGovernanceDataHandler { Capabilities required = new Capabilities(Capability.DAO_FULL_NODE); List 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()) { diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index c6052ed76f..06c139c0d4 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -619,10 +619,10 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis // it from old versions, so we filter those. Optional 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. " + diff --git a/p2p/src/main/java/bisq/network/p2p/peers/PeerManager.java b/p2p/src/main/java/bisq/network/p2p/peers/PeerManager.java index e650673586..87b9d94048 100644 --- a/p2p/src/main/java/bisq/network/p2p/peers/PeerManager.java +++ b/p2p/src/main/java/bisq/network/p2p/peers/PeerManager.java @@ -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 allPeers = new HashSet<>(getPersistedPeers()); allPeers.addAll(getReportedPeers()); Optional 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); diff --git a/p2p/src/main/java/bisq/network/p2p/peers/peerexchange/Peer.java b/p2p/src/main/java/bisq/network/p2p/peers/peerexchange/Peer.java index e1a7971223..ecce7a7615 100644 --- a/p2p/src/main/java/bisq/network/p2p/peers/peerexchange/Peer.java +++ b/p2p/src/main/java/bisq/network/p2p/peers/peerexchange/Peer.java @@ -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); }