Skip to content

Commit

Permalink
[REFACTOR] Remove duplicated code in refreshTTL
Browse files Browse the repository at this point in the history
The custom code to verify the refreshTTLMessage's signature and update
an entry isn't necessary. Just have the code construct an updated
ProtectedStorageEntry from the existing and new data, verify it,
and add it to the map.

This also allows the removal of the ProtectedStorageEntry APIs
that modify internal state.
  • Loading branch information
julianknutsen committed Nov 13, 2019
1 parent 454b2d7 commit 2c2a57e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
36 changes: 17 additions & 19 deletions p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@

import com.google.common.annotations.VisibleForTesting;

import org.apache.commons.lang3.StringUtils;

import java.security.KeyPair;
import java.security.PublicKey;

Expand Down Expand Up @@ -444,6 +442,7 @@ private void broadcastProtectedStorageEntry(ProtectedStorageEntry protectedStora
public boolean refreshTTL(RefreshOfferMessage refreshTTLMessage,
@Nullable NodeAddress sender,
boolean isDataOwner) {

ByteArray hashOfPayload = new ByteArray(refreshTTLMessage.getHashOfPayload());

if (!map.containsKey((hashOfPayload))) {
Expand All @@ -452,34 +451,33 @@ public boolean refreshTTL(RefreshOfferMessage refreshTTLMessage,
return false;
}

int sequenceNumber = refreshTTLMessage.getSequenceNumber();
ProtectedStorageEntry storedEntry = map.get(hashOfPayload);
ProtectedStorageEntry updatedEntry = new ProtectedStorageEntry(
storedEntry.getProtectedStoragePayload(),
storedEntry.getOwnerPubKey(),
refreshTTLMessage.getSequenceNumber(),
refreshTTLMessage.getSignature(),
this.clock);

if(!hasSequenceNrIncreased(sequenceNumber, hashOfPayload))
return false;

ProtectedStorageEntry storedData = map.get(hashOfPayload);
PublicKey ownerPubKey = storedData.getProtectedStoragePayload().getOwnerPubKey();
byte[] hashOfDataAndSeqNr = refreshTTLMessage.getHashOfDataAndSeqNr();
byte[] signature = refreshTTLMessage.getSignature();
// If we have seen a more recent operation for this payload, we ignore the current one
if(!hasSequenceNrIncreased(updatedEntry.getSequenceNumber(), hashOfPayload))
return false;

// Verify the RefreshOfferMessage is well formed and valid for the refresh operation
if (!checkSignature(ownerPubKey, hashOfDataAndSeqNr, signature))
// Verify the updated ProtectedStorageEntry is well formed and valid for update
if (!checkSignature(updatedEntry))
return false;

// Verify the Payload owner and the Entry owner for the stored Entry are the same
// TODO: This is also checked in the validation for the original add(), investigate if this can be removed
if (!checkIfStoredDataPubKeyMatchesNewDataPubKey(ownerPubKey, hashOfPayload))
if (!checkIfStoredDataPubKeyMatchesNewDataPubKey(updatedEntry.getOwnerPubKey(), hashOfPayload))
return false;

// This is a valid refresh, update the payload for it
log.debug("refreshDate called for storedData:\n\t" + StringUtils.abbreviate(storedData.toString(), 100));
storedData.refreshTTL(this.clock);
storedData.updateSequenceNumber(sequenceNumber);
storedData.updateSignature(signature);
printData("after refreshTTL");
// Update the hash map with the updated entry
map.put(hashOfPayload, updatedEntry);

// Record the latest sequence number and persist it
sequenceNumberMap.put(hashOfPayload, new MapValue(sequenceNumber, this.clock.millis()));
sequenceNumberMap.put(hashOfPayload, new MapValue(updatedEntry.getSequenceNumber(), this.clock.millis()));
sequenceNumberMapStorage.queueUpForSave(SequenceNumberMap.clone(sequenceNumberMap), 1000);

// Always broadcast refreshes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ProtectedStorageEntry implements NetworkPayload, PersistablePayload
private final ProtectedStoragePayload protectedStoragePayload;
private final byte[] ownerPubKeyBytes;
transient private final PublicKey ownerPubKey;
private int sequenceNumber;
private final int sequenceNumber;
private byte[] signature;
private long creationTimeStamp;

Expand Down Expand Up @@ -133,19 +133,12 @@ public void maybeAdjustCreationTimeStamp(Clock clock) {
creationTimeStamp = clock.millis();
}

public void refreshTTL(Clock clock) {
creationTimeStamp = clock.millis();
}

public void backDate() {
if (protectedStoragePayload instanceof ExpirablePayload)
creationTimeStamp -= ((ExpirablePayload) protectedStoragePayload).getTTL() / 2;
}

public void updateSequenceNumber(int sequenceNumber) {
this.sequenceNumber = sequenceNumber;
}

// TODO: only used in tests so find a better way to test and delete public API
public void updateSignature(byte[] signature) {
this.signature = signature;
}
Expand Down

0 comments on commit 2c2a57e

Please sign in to comment.