From cfc90a221b700c83849df9aaaa598c1af2b99666 Mon Sep 17 00:00:00 2001 From: Andras Slemmer Date: Fri, 9 Feb 2018 18:23:42 +0000 Subject: [PATCH] CORDA-1003: Fix duplicate detection on cache evict --- .../node/utilities/AppendOnlyPersistentMap.kt | 1 + .../persistence/DBTransactionStorageTests.kt | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/node/src/main/kotlin/net/corda/node/utilities/AppendOnlyPersistentMap.kt b/node/src/main/kotlin/net/corda/node/utilities/AppendOnlyPersistentMap.kt index 4912871b46..c6b875dbd9 100644 --- a/node/src/main/kotlin/net/corda/node/utilities/AppendOnlyPersistentMap.kt +++ b/node/src/main/kotlin/net/corda/node/utilities/AppendOnlyPersistentMap.kt @@ -57,6 +57,7 @@ abstract class AppendOnlyPersistentMapBase( // Depending on 'store' method, this may insert without checking key duplication or it may avoid inserting a duplicated key. val existingInDb = store(key, value) if (existingInDb != null) { // Always reuse an existing value from the storage of a duplicated key. + isUnique = false Optional.of(existingInDb) } else { Optional.of(value) diff --git a/node/src/test/kotlin/net/corda/node/services/persistence/DBTransactionStorageTests.kt b/node/src/test/kotlin/net/corda/node/services/persistence/DBTransactionStorageTests.kt index 5247788410..4c2ffc2843 100644 --- a/node/src/test/kotlin/net/corda/node/services/persistence/DBTransactionStorageTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/persistence/DBTransactionStorageTests.kt @@ -172,9 +172,22 @@ class DBTransactionStorageTests { assertEquals(expected, actual) } - private fun newTransactionStorage() { + @Test + fun `duplicates are detected when transaction is evicted from cache`() { + newTransactionStorage(cacheSizeBytesOverride = 0) + val transaction = newTransaction() database.transaction { - transactionStorage = DBTransactionStorage(NodeConfiguration.defaultTransactionCacheSize) + val firstInserted = transactionStorage.addTransaction(transaction) + val secondInserted = transactionStorage.addTransaction(transaction) + require(firstInserted) { "We inserted a fresh transaction" } + require(!secondInserted) { "Second time should be redundant" } + println("$firstInserted $secondInserted") + } + } + + private fun newTransactionStorage(cacheSizeBytesOverride: Long? = null) { + database.transaction { + transactionStorage = DBTransactionStorage(cacheSizeBytesOverride ?: NodeConfiguration.defaultTransactionCacheSize) } }