From 38d1f4792b0cd98f6ebd7d11f1e06bccbe9dd44f Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Mon, 4 Nov 2019 19:59:05 -0500 Subject: [PATCH] Limit max. nr. of PersistableNetworkPayload and ProtectedStorageEntry to 5000 To avoid that seed nodes get overloaded with requests for too many PersistableNetworkPayload and ProtectedStorageEntry data we limit nr. of entries to max 5000 each. --- .../peers/getdata/GetDataRequestHandler.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/peers/getdata/GetDataRequestHandler.java b/p2p/src/main/java/bisq/network/p2p/peers/getdata/GetDataRequestHandler.java index d6e2259b510..920c152d3a3 100644 --- a/p2p/src/main/java/bisq/network/p2p/peers/getdata/GetDataRequestHandler.java +++ b/p2p/src/main/java/bisq/network/p2p/peers/getdata/GetDataRequestHandler.java @@ -41,6 +41,7 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; @@ -50,6 +51,7 @@ @Slf4j public class GetDataRequestHandler { private static final long TIMEOUT = 90; + private static final int MAX_ENTRIES = 10000; /////////////////////////////////////////////////////////////////////////////////////////// @@ -138,13 +140,22 @@ private Set getFilteredPersistableNetworkPayload(GetD Connection connection) { final Set tempLookupSet = new HashSet<>(); Set excludedKeysAsByteArray = P2PDataStorage.ByteArray.convertBytesSetToByteArraySet(getDataRequest.getExcludedKeys()); - - return dataStorage.getAppendOnlyDataStoreMap().entrySet().stream() + AtomicInteger maxSize = new AtomicInteger(MAX_ENTRIES / 2); + Set result = dataStorage.getAppendOnlyDataStoreMap().entrySet().stream() .filter(e -> !excludedKeysAsByteArray.contains(e.getKey())) + .filter(e -> maxSize.decrementAndGet() > 0) .map(Map.Entry::getValue) - .filter(payload -> (connection.noCapabilityRequiredOrCapabilityIsSupported(payload))) - .filter(payload -> tempLookupSet.add(new P2PDataStorage.ByteArray(payload.getHash()))) + .filter(connection::noCapabilityRequiredOrCapabilityIsSupported) + .filter(payload -> { + boolean notContained = tempLookupSet.add(new P2PDataStorage.ByteArray(payload.getHash())); + return notContained; + }) .collect(Collectors.toSet()); + if (maxSize.get() <= 0) { + log.warn("The peer request caused too much PersistableNetworkPayload entries to get delivered. We limited the entries for the response to {} entries", MAX_ENTRIES / 2); + } + log.info("PersistableNetworkPayload set contains {} entries ", result.size()); + return result; } private Set getFilteredProtectedStorageEntries(GetDataRequest getDataRequest, @@ -152,11 +163,17 @@ private Set getFilteredProtectedStorageEntries(GetDataReq final Set filteredDataSet = new HashSet<>(); final Set lookupSet = new HashSet<>(); + AtomicInteger maxSize = new AtomicInteger(MAX_ENTRIES / 2); Set excludedKeysAsByteArray = P2PDataStorage.ByteArray.convertBytesSetToByteArraySet(getDataRequest.getExcludedKeys()); Set filteredSet = dataStorage.getMap().entrySet().stream() .filter(e -> !excludedKeysAsByteArray.contains(e.getKey())) + .filter(e -> maxSize.decrementAndGet() > 0) .map(Map.Entry::getValue) .collect(Collectors.toSet()); + if (maxSize.get() <= 0) { + log.warn("The peer request caused too much ProtectedStorageEntry entries to get delivered. We limited the entries for the response to {} entries", MAX_ENTRIES / 2); + } + log.info("getFilteredProtectedStorageEntries " + filteredSet.size()); for (ProtectedStorageEntry protectedStorageEntry : filteredSet) { final ProtectedStoragePayload protectedStoragePayload = protectedStorageEntry.getProtectedStoragePayload(); @@ -171,11 +188,13 @@ private Set getFilteredProtectedStorageEntries(GetDataReq doAdd = true; } if (doAdd) { - if (lookupSet.add(protectedStoragePayload.hashCode())) + boolean notContained = lookupSet.add(protectedStoragePayload.hashCode()); + if (notContained) filteredDataSet.add(protectedStorageEntry); } } + log.info("ProtectedStorageEntry set contains {} entries ", filteredDataSet.size()); return filteredDataSet; }