From a60ccded75fae08400b64124622904cc8882e66e Mon Sep 17 00:00:00 2001 From: Andrius Dagys Date: Tue, 1 Aug 2017 11:42:36 +0100 Subject: [PATCH] Minor: Add a test to ensure "identical" issue transactions have different ids (#1145) * Add a test to ensure identical issue transactions have different ids * Fix a PMT test --- .../net/corda/core/contracts/Structures.kt | 1 + .../corda/core/contracts/TransactionTests.kt | 20 +++++++++++++++++++ .../core/crypto/PartialMerkleTreeTest.kt | 17 ++++++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/contracts/Structures.kt b/core/src/main/kotlin/net/corda/core/contracts/Structures.kt index 52e449768c..fc587ab8e8 100644 --- a/core/src/main/kotlin/net/corda/core/contracts/Structures.kt +++ b/core/src/main/kotlin/net/corda/core/contracts/Structures.kt @@ -447,6 +447,7 @@ fun JarInputStream.extractFile(path: String, outputTo: OutputStream) { */ @CordaSerializable class PrivacySalt(bytes: ByteArray) : OpaqueBytes(bytes) { + /** Constructs a salt with a randomly-generated 32 byte value. */ constructor() : this(secureRandomBytes(32)) init { diff --git a/core/src/test/kotlin/net/corda/core/contracts/TransactionTests.kt b/core/src/test/kotlin/net/corda/core/contracts/TransactionTests.kt index b8f31c76eb..984d3b2f91 100644 --- a/core/src/test/kotlin/net/corda/core/contracts/TransactionTests.kt +++ b/core/src/test/kotlin/net/corda/core/contracts/TransactionTests.kt @@ -15,6 +15,7 @@ import org.junit.Test import java.security.KeyPair import kotlin.test.assertEquals import kotlin.test.assertFailsWith +import kotlin.test.assertNotEquals class TransactionTests : TestDependencyInjectionBase() { private fun makeSigned(wtx: WireTransaction, vararg keys: KeyPair, notarySig: Boolean = true): SignedTransaction { @@ -152,4 +153,23 @@ class TransactionTests : TestDependencyInjectionBase() { assertFailsWith { buildTransaction() } } + + @Test + fun `transactions with identical contents must have different ids`() { + val outputState = TransactionState(DummyContract.SingleOwnerState(0, ALICE), DUMMY_NOTARY) + fun buildTransaction() = WireTransaction( + inputs = emptyList(), + attachments = emptyList(), + outputs = listOf(outputState), + commands = listOf(dummyCommand(DUMMY_KEY_1.public, DUMMY_KEY_2.public)), + notary = null, + timeWindow = null, + privacySalt = PrivacySalt() // Randomly-generated – used for calculating the id + ) + + val issueTx1 = buildTransaction() + val issueTx2 = buildTransaction() + + assertNotEquals(issueTx1.id, issueTx2.id) + } } diff --git a/core/src/test/kotlin/net/corda/core/crypto/PartialMerkleTreeTest.kt b/core/src/test/kotlin/net/corda/core/crypto/PartialMerkleTreeTest.kt index cebc33a218..e4547b9e5a 100644 --- a/core/src/test/kotlin/net/corda/core/crypto/PartialMerkleTreeTest.kt +++ b/core/src/test/kotlin/net/corda/core/crypto/PartialMerkleTreeTest.kt @@ -127,8 +127,11 @@ class PartialMerkleTreeTest : TestDependencyInjectionBase() { @Test fun `same transactions with different notaries have different ids`() { - val wtx1 = makeSimpleCashWtx(DUMMY_NOTARY) - val wtx2 = makeSimpleCashWtx(MEGA_CORP) + // We even use the same privacySalt, and thus the only difference between the two transactions is the notary party. + val privacySalt = PrivacySalt() + val wtx1 = makeSimpleCashWtx(DUMMY_NOTARY, privacySalt) + val wtx2 = makeSimpleCashWtx(MEGA_CORP, privacySalt) + assertEquals(wtx1.privacySalt, wtx2.privacySalt) assertNotEquals(wtx1.id, wtx2.id) } @@ -235,14 +238,20 @@ class PartialMerkleTreeTest : TestDependencyInjectionBase() { hm1.serialize() } - private fun makeSimpleCashWtx(notary: Party, timeWindow: TimeWindow? = null, attachments: List = emptyList()): WireTransaction { + private fun makeSimpleCashWtx( + notary: Party, + privacySalt: PrivacySalt = PrivacySalt(), + timeWindow: TimeWindow? = null, + attachments: List = emptyList() + ): WireTransaction { return WireTransaction( inputs = testTx.inputs, attachments = attachments, outputs = testTx.outputs, commands = testTx.commands, notary = notary, - timeWindow = timeWindow + timeWindow = timeWindow, + privacySalt = privacySalt ) } }