Skip to content

Commit

Permalink
Don't add data if it's been removed recently
Browse files Browse the repository at this point in the history
  • Loading branch information
freimair committed Apr 1, 2019
1 parent 86bd5a1 commit 26cad94
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import java.security.KeyPair;
import java.security.PublicKey;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -120,6 +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<>();


///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -349,6 +351,13 @@ 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 @@ -576,6 +585,13 @@ 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);
}

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 26cad94

Please sign in to comment.