Skip to content

Commit

Permalink
[PR COMMENTS] Don't store Clock in ProtectedStorageEntry
Browse files Browse the repository at this point in the history
Just have the methods that use it take one as a parameter.
  • Loading branch information
julianknutsen committed Nov 7, 2019
1 parent 4a579d2 commit c66f582
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void removeExpiredEntries() {
Map<ByteArray, ProtectedStorageEntry> temp = new HashMap<>(map);
Set<ProtectedStorageEntry> toRemoveSet = new HashSet<>();
temp.entrySet().stream()
.filter(entry -> entry.getValue().isExpired())
.filter(entry -> entry.getValue().isExpired(this.clock))
.forEach(entry -> {
ByteArray hashOfPayload = entry.getKey();
ProtectedStorageEntry protectedStorageEntry = map.get(hashOfPayload);
Expand Down Expand Up @@ -292,7 +292,7 @@ public void onDisconnect(CloseConnectionReason closeConnectionReason, Connection
// TODO investigate what causes the disconnections.
// Usually the are: SOCKET_TIMEOUT ,TERMINATED (EOFException)
protectedStorageEntry.backDate();
if (protectedStorageEntry.isExpired()) {
if (protectedStorageEntry.isExpired(this.clock)) {
log.info("We found an expired data entry which we have already back dated. " +
"We remove the protectedStoragePayload:\n\t" + Utilities.toTruncatedString(protectedStorageEntry.getProtectedStoragePayload(), 100));
doRemoveProtectedExpirableData(protectedStorageEntry, hashOfPayload);
Expand Down Expand Up @@ -474,7 +474,7 @@ public boolean refreshTTL(RefreshOfferMessage refreshTTLMessage,

// 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();
storedData.refreshTTL(this.clock);
storedData.updateSequenceNumber(sequenceNumber);
storedData.updateSignature(signature);
printData("after refreshTTL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class ProtectedStorageEntry implements NetworkPayload, PersistablePayload
private int sequenceNumber;
private byte[] signature;
private long creationTimeStamp;
transient private final Clock clock;

public ProtectedStorageEntry(ProtectedStoragePayload protectedStoragePayload,
PublicKey ownerPubKey,
Expand Down Expand Up @@ -74,9 +73,8 @@ protected ProtectedStorageEntry(ProtectedStoragePayload protectedStoragePayload,
this.sequenceNumber = sequenceNumber;
this.signature = signature;
this.creationTimeStamp = creationTimeStamp;
this.clock = clock;

maybeAdjustCreationTimeStamp();
maybeAdjustCreationTimeStamp(clock);
}

///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -129,14 +127,14 @@ public static ProtectedStorageEntry fromProto(protobuf.ProtectedStorageEntry pro
// API
///////////////////////////////////////////////////////////////////////////////////////////

public void maybeAdjustCreationTimeStamp() {
public void maybeAdjustCreationTimeStamp(Clock clock) {
// We don't allow creation date in the future, but we cannot be too strict as clocks are not synced
if (creationTimeStamp > this.clock.millis())
creationTimeStamp = this.clock.millis();
if (creationTimeStamp > clock.millis())
creationTimeStamp = clock.millis();
}

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

public void backDate() {
Expand All @@ -152,8 +150,8 @@ public void updateSignature(byte[] signature) {
this.signature = signature;
}

public boolean isExpired() {
public boolean isExpired(Clock clock) {
return protectedStoragePayload instanceof ExpirablePayload &&
(this.clock.millis() - creationTimeStamp) > ((ExpirablePayload) protectedStoragePayload).getTTL();
(clock.millis() - creationTimeStamp) > ((ExpirablePayload) protectedStoragePayload).getTTL();
}
}

0 comments on commit c66f582

Please sign in to comment.