Add comment

This commit is contained in:
Manfred Karrer 2015-11-11 01:57:13 +01:00
parent 1f3c2c1479
commit f4383afe9b

View file

@ -90,6 +90,11 @@ public class ProtectedExpirableDataStorage implements MessageListener {
private void removeExpiredEntries() { private void removeExpiredEntries() {
Log.traceCall(); Log.traceCall();
// The moment when an object becomes expired will not be synchrone in the network and we could
// get add messages after the object has expired. To avoid repeated additions of already expired
// object when we get it sent from new peers, we dont remove the sequence number from the map.
// That way a add message for an already expired data will fail because the sequence number
// is equal and not larger.
map.entrySet().stream() map.entrySet().stream()
.filter(entry -> entry.getValue().isExpired()) .filter(entry -> entry.getValue().isExpired())
.forEach(entry -> map.remove(entry.getKey())); .forEach(entry -> map.remove(entry.getKey()));
@ -138,19 +143,17 @@ public class ProtectedExpirableDataStorage implements MessageListener {
public boolean add(ProtectedData protectedData, @Nullable Address sender) { public boolean add(ProtectedData protectedData, @Nullable Address sender) {
Log.traceCall(); Log.traceCall();
ByteArray hashOfPayload = getHashAsByteArray(protectedData.expirablePayload); ByteArray hashOfPayload = getHashAsByteArray(protectedData.expirablePayload);
boolean containsKey = map.containsKey(hashOfPayload);
boolean result = checkPublicKeys(protectedData, true) boolean result = checkPublicKeys(protectedData, true)
&& checkSignature(protectedData); && checkSignature(protectedData)
if (containsKey) {
result &= checkIfStoredDataMatchesNewData(protectedData, hashOfPayload)
&& isSequenceNrValid(protectedData, hashOfPayload); && isSequenceNrValid(protectedData, hashOfPayload);
}
boolean containsKey = map.containsKey(hashOfPayload);
if (containsKey)
result &= checkIfStoredDataPubKeyMatchesNewDataPubKey(protectedData, hashOfPayload);
if (result) { if (result) {
map.put(hashOfPayload, protectedData); map.put(hashOfPayload, protectedData);
log.trace("Data added to our map and it will be broadcasted to our peers."); sequenceNumberMap.put(hashOfPayload, protectedData.sequenceNumber);
hashMapChangedListeners.stream().forEach(e -> e.onAdded(protectedData));
StringBuilder sb = new StringBuilder("\n\n------------------------------------------------------------\n"); StringBuilder sb = new StringBuilder("\n\n------------------------------------------------------------\n");
sb.append("Data set after addProtectedExpirableData:"); sb.append("Data set after addProtectedExpirableData:");
@ -161,8 +164,8 @@ public class ProtectedExpirableDataStorage implements MessageListener {
if (!containsKey) if (!containsKey)
broadcast(new AddDataMessage(protectedData), sender); broadcast(new AddDataMessage(protectedData), sender);
sequenceNumberMap.put(hashOfPayload, protectedData.sequenceNumber);
storage.queueUpForSave(); storage.queueUpForSave();
hashMapChangedListeners.stream().forEach(e -> e.onAdded(protectedData));
} else { } else {
log.trace("add failed"); log.trace("add failed");
} }
@ -178,7 +181,7 @@ public class ProtectedExpirableDataStorage implements MessageListener {
&& checkPublicKeys(protectedData, false) && checkPublicKeys(protectedData, false)
&& isSequenceNrValid(protectedData, hashOfPayload) && isSequenceNrValid(protectedData, hashOfPayload)
&& checkSignature(protectedData) && checkSignature(protectedData)
&& checkIfStoredDataMatchesNewData(protectedData, hashOfPayload); && checkIfStoredDataPubKeyMatchesNewDataPubKey(protectedData, hashOfPayload);
if (result) { if (result) {
@ -325,11 +328,10 @@ public class ProtectedExpirableDataStorage implements MessageListener {
return result; return result;
} }
private boolean checkIfStoredDataMatchesNewData(ProtectedData data, ByteArray hashOfData) { private boolean checkIfStoredDataPubKeyMatchesNewDataPubKey(ProtectedData data, ByteArray hashOfData) {
Log.traceCall(); Log.traceCall();
ProtectedData storedData = map.get(hashOfData); ProtectedData storedData = map.get(hashOfData);
boolean result = getHashAsByteArray(storedData.expirablePayload).equals(hashOfData) boolean result = storedData.ownerStoragePubKey.equals(data.ownerStoragePubKey);
&& storedData.ownerStoragePubKey.equals(data.ownerStoragePubKey);
if (!result) if (!result)
log.error("New data entry does not match our stored data. Consider it might be an attempt of fraud"); log.error("New data entry does not match our stored data. Consider it might be an attempt of fraud");