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
This commit is contained in:
Andrius Dagys 2017-08-01 11:42:36 +01:00 committed by GitHub
parent f6e05aeae9
commit a60ccded75
3 changed files with 34 additions and 4 deletions

View File

@ -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 {

View File

@ -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<TransactionVerificationException.NotaryChangeInWrongTransactionType> { 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)
}
}

View File

@ -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<SecureHash> = emptyList()): WireTransaction {
private fun makeSimpleCashWtx(
notary: Party,
privacySalt: PrivacySalt = PrivacySalt(),
timeWindow: TimeWindow? = null,
attachments: List<SecureHash> = emptyList()
): WireTransaction {
return WireTransaction(
inputs = testTx.inputs,
attachments = attachments,
outputs = testTx.outputs,
commands = testTx.commands,
notary = notary,
timeWindow = timeWindow
timeWindow = timeWindow,
privacySalt = privacySalt
)
}
}