From 551eb5b648d1a0306b4f3f3f776bd6fe0d6c81c9 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 24 May 2016 18:55:55 +0200 Subject: [PATCH] Use creation data for TTL check --- .../bitsquare/p2p/storage/P2PDataStorage.java | 4 +++- .../ProtectedMailboxStorageEntry.java | 2 +- .../storageentry/ProtectedStorageEntry.java | 22 +++++++++++++------ .../p2p/storage/ProtectedDataStorageTest.java | 2 +- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/network/src/main/java/io/bitsquare/p2p/storage/P2PDataStorage.java b/network/src/main/java/io/bitsquare/p2p/storage/P2PDataStorage.java index c26c433fb0..246f2c80c6 100644 --- a/network/src/main/java/io/bitsquare/p2p/storage/P2PDataStorage.java +++ b/network/src/main/java/io/bitsquare/p2p/storage/P2PDataStorage.java @@ -40,6 +40,7 @@ import java.util.concurrent.TimeUnit; // Run in UserThread public class P2PDataStorage implements MessageListener, ConnectionListener { private static final Logger log = LoggerFactory.getLogger(P2PDataStorage.class); + /** * How many days to keep an entry before it is purged. */ @@ -55,6 +56,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener { private HashMap sequenceNumberMap = new HashMap<>(); private final Storage storage; + /////////////////////////////////////////////////////////////////////////////////////////// // Constructor /////////////////////////////////////////////////////////////////////////////////////////// @@ -237,7 +239,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener { if (result) { log.info("refreshDate called for storedData:\n\t" + StringUtils.abbreviate(storedData.toString(), 100)); - storedData.updateTimeStamp(); + storedData.refreshTTL(); storedData.updateSequenceNumber(sequenceNumber); storedData.updateSignature(signature); diff --git a/network/src/main/java/io/bitsquare/p2p/storage/storageentry/ProtectedMailboxStorageEntry.java b/network/src/main/java/io/bitsquare/p2p/storage/storageentry/ProtectedMailboxStorageEntry.java index 7239e0ed81..3370fc7e60 100644 --- a/network/src/main/java/io/bitsquare/p2p/storage/storageentry/ProtectedMailboxStorageEntry.java +++ b/network/src/main/java/io/bitsquare/p2p/storage/storageentry/ProtectedMailboxStorageEntry.java @@ -36,7 +36,7 @@ public class ProtectedMailboxStorageEntry extends ProtectedStorageEntry { try { in.defaultReadObject(); receiversPubKey = KeyFactory.getInstance(Sig.KEY_ALGO, "BC").generatePublic(new X509EncodedKeySpec(receiversPubKeyBytes)); - updateTimeStamp(); + checkCreationTimeStamp(); } catch (Throwable t) { log.warn("Exception at readObject: " + t.getMessage()); } diff --git a/network/src/main/java/io/bitsquare/p2p/storage/storageentry/ProtectedStorageEntry.java b/network/src/main/java/io/bitsquare/p2p/storage/storageentry/ProtectedStorageEntry.java index c75d5bfbae..ebd2ed0b61 100644 --- a/network/src/main/java/io/bitsquare/p2p/storage/storageentry/ProtectedStorageEntry.java +++ b/network/src/main/java/io/bitsquare/p2p/storage/storageentry/ProtectedStorageEntry.java @@ -26,14 +26,14 @@ public class ProtectedStorageEntry implements Payload { public int sequenceNumber; public byte[] signature; @VisibleForTesting - transient public long timeStamp; + public long creationTimeStamp; public ProtectedStorageEntry(StoragePayload storagePayload, PublicKey ownerPubKey, int sequenceNumber, byte[] signature) { this.storagePayload = storagePayload; this.ownerPubKey = ownerPubKey; this.sequenceNumber = sequenceNumber; this.signature = signature; - this.timeStamp = System.currentTimeMillis(); + this.creationTimeStamp = System.currentTimeMillis(); this.ownerPubKeyBytes = new X509EncodedKeySpec(this.ownerPubKey.getEncoded()).getEncoded(); } @@ -41,7 +41,7 @@ public class ProtectedStorageEntry implements Payload { try { in.defaultReadObject(); ownerPubKey = KeyFactory.getInstance(Sig.KEY_ALGO, "BC").generatePublic(new X509EncodedKeySpec(ownerPubKeyBytes)); - updateTimeStamp(); + checkCreationTimeStamp(); } catch (Throwable t) { log.warn("Exception at readObject: " + t.getMessage()); } @@ -51,8 +51,16 @@ public class ProtectedStorageEntry implements Payload { return storagePayload; } - public void updateTimeStamp() { - timeStamp = System.currentTimeMillis(); + public void checkCreationTimeStamp() { + // We don't allow creation date in the future, but we cannot be too strict as clocks are not synced + // The 0 test is needed to be backward compatible as creationTimeStamp (timeStamp) was transient before 0.4.7 + // TODO "|| creationTimeStamp == 0" can removed after we don't support 0.4.6 anymore + if (creationTimeStamp > System.currentTimeMillis() || creationTimeStamp == 0) + creationTimeStamp = System.currentTimeMillis(); + } + + public void refreshTTL() { + creationTimeStamp = System.currentTimeMillis(); } public void updateSequenceNumber(int sequenceNumber) { @@ -64,14 +72,14 @@ public class ProtectedStorageEntry implements Payload { } public boolean isExpired() { - return (System.currentTimeMillis() - timeStamp) > storagePayload.getTTL(); + return (System.currentTimeMillis() - creationTimeStamp) > storagePayload.getTTL(); } @Override public String toString() { return "ProtectedStorageEntry{" + "expirablePayload=" + storagePayload + - ", timeStamp=" + timeStamp + + ", creationTimeStamp=" + creationTimeStamp + ", sequenceNumber=" + sequenceNumber + ", ownerPubKey.hashCode()=" + (ownerPubKey != null ? ownerPubKey.hashCode() : "null") + ", signature.hashCode()=" + (signature != null ? Arrays.toString(signature).hashCode() : "null") + diff --git a/network/src/test/java/io/bitsquare/p2p/storage/ProtectedDataStorageTest.java b/network/src/test/java/io/bitsquare/p2p/storage/ProtectedDataStorageTest.java index 6f201853c6..90345033f0 100644 --- a/network/src/test/java/io/bitsquare/p2p/storage/ProtectedDataStorageTest.java +++ b/network/src/test/java/io/bitsquare/p2p/storage/ProtectedDataStorageTest.java @@ -113,7 +113,7 @@ public class ProtectedDataStorageTest { public void testTTL() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException, NoSuchProviderException { mockData.ttl = (int) (P2PDataStorage.CHECK_TTL_INTERVAL_SEC * 1.5); ProtectedStorageEntry data = dataStorage1.getProtectedData(mockData, storageSignatureKeyPair1); - log.debug("data.date " + data.timeStamp); + log.debug("data.date " + data.creationTimeStamp); Assert.assertTrue(dataStorage1.add(data, null, null, true)); log.debug("test 1"); Assert.assertEquals(1, dataStorage1.getMap().size());