Skip to content

Commit

Permalink
Refined recently removed data filter
Browse files Browse the repository at this point in the history
  • Loading branch information
freimair committed Apr 1, 2019
1 parent 26cad94 commit 956931d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ public void onMessage(NetworkEnvelope networkEnvelope, Connection connection) {

List<NetworkPayload> processDelayedItems = new ArrayList<>();
dataSet.stream().forEach(e -> {
if(dataStorage.isRecentlyRemoved(e))
return;

if (e.getProtectedStoragePayload() instanceof LazyProcessedPayload) {
processDelayedItems.add(e);
} else {
Expand Down
23 changes: 10 additions & 13 deletions p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers

private final Set<AppendOnlyDataStoreListener> appendOnlyDataStoreListeners = new CopyOnWriteArraySet<>();
private final Set<ProtectedDataStoreListener> protectedDataStoreListeners = new CopyOnWriteArraySet<>();
private List<ByteArray> recentlyRemovedMessages = new ArrayList<>();
private Map<Long, ByteArray> recentlyRemovedMessages = new ConcurrentHashMap<>();


///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -351,13 +351,6 @@ public boolean addProtectedStorageEntry(ProtectedStorageEntry protectedStorageEn
&& checkSignature(protectedStorageEntry)
&& sequenceNrValid;

synchronized (recentlyRemovedMessages) {
if (recentlyRemovedMessages.contains(hashOfPayload)) {
log.trace("We ignore this message as we received a remove command for this very data only recently");
return false;
}
}

boolean containsKey = map.containsKey(hashOfPayload);
if (containsKey)
result = result && checkIfStoredDataPubKeyMatchesNewDataPubKey(protectedStorageEntry.getOwnerPubKey(), hashOfPayload);
Expand Down Expand Up @@ -447,6 +440,10 @@ public boolean refreshTTL(RefreshOfferMessage refreshTTLMessage, @Nullable NodeA
}
}

public boolean isRecentlyRemoved(ProtectedStorageEntry e) {
return recentlyRemovedMessages.values().contains(get32ByteHashAsByteArray(e));
}

public boolean remove(ProtectedStorageEntry protectedStorageEntry, @Nullable NodeAddress sender, boolean isDataOwner) {
final ProtectedStoragePayload protectedStoragePayload = protectedStorageEntry.getProtectedStoragePayload();
ByteArray hashOfPayload = get32ByteHashAsByteArray(protectedStoragePayload);
Expand Down Expand Up @@ -586,11 +583,11 @@ public void removeProtectedDataStoreListener(ProtectedDataStoreListener listener
private void doRemoveProtectedExpirableData(ProtectedStorageEntry protectedStorageEntry, ByteArray hashOfPayload) {
map.remove(hashOfPayload);

synchronized (recentlyRemovedMessages) {
recentlyRemovedMessages.add(hashOfPayload);
while (recentlyRemovedMessages.size() > 500)
recentlyRemovedMessages.remove(0);
}
// add to the list of recently removed messages
recentlyRemovedMessages.put(System.currentTimeMillis(), hashOfPayload);

// clean the list by removing all recently removed messages which have been removed in the last minute
recentlyRemovedMessages.keySet().removeIf(timestamp -> timestamp < System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(1));

log.trace("Data removed from our map. We broadcast the message to our peers.");
hashMapChangedListeners.stream().forEach(e -> e.onRemoved(protectedStorageEntry));
Expand Down

0 comments on commit 956931d

Please sign in to comment.