From 4fef01a5b0f11770afa86efbf04e3e4a2e945fed Mon Sep 17 00:00:00 2001 From: Jose Coll Date: Tue, 22 Aug 2023 16:03:13 +0100 Subject: [PATCH] Clean-up. --- .../coretests/flows/FinalityFlowTests.kt | 58 +++++++++---------- .../DBTransactionStorageLedgerRecovery.kt | 3 +- ...DBTransactionStorageLedgerRecoveryTests.kt | 20 ++----- 3 files changed, 33 insertions(+), 48 deletions(-) diff --git a/core-tests/src/test/kotlin/net/corda/coretests/flows/FinalityFlowTests.kt b/core-tests/src/test/kotlin/net/corda/coretests/flows/FinalityFlowTests.kt index 644ea2013a..da7a128e6f 100644 --- a/core-tests/src/test/kotlin/net/corda/coretests/flows/FinalityFlowTests.kt +++ b/core-tests/src/test/kotlin/net/corda/coretests/flows/FinalityFlowTests.kt @@ -51,8 +51,6 @@ import net.corda.finance.test.flows.CashIssueWithObserversFlow import net.corda.finance.test.flows.CashPaymentWithObserversFlow import net.corda.node.services.persistence.DBTransactionStorage import net.corda.node.services.persistence.DBTransactionStorageLedgerRecovery -import net.corda.node.services.persistence.DBTransactionStorageLedgerRecovery.DBReceiverDistributionRecord -import net.corda.node.services.persistence.DBTransactionStorageLedgerRecovery.DBSenderDistributionRecord import net.corda.node.services.persistence.HashedDistributionList import net.corda.node.services.persistence.ReceiverDistributionRecord import net.corda.node.services.persistence.SenderDistributionRecord @@ -76,8 +74,8 @@ import net.corda.testing.node.internal.enclosedCordapp import net.corda.testing.node.internal.findCordapp import org.assertj.core.api.Assertions.assertThat import org.junit.After +import org.junit.Assert.assertNotNull import org.junit.Test -import org.junit.jupiter.api.assertThrows import java.sql.SQLException import java.util.Random import kotlin.test.assertEquals @@ -358,10 +356,12 @@ class FinalityFlowTests : WithFinality { assertEquals(StatesToRecord.ALL_VISIBLE, this[0].statesToRecord) assertEquals(BOB_NAME.hashCode().toLong(), this[0].peerPartyId) } - val rdr = getReceiverRecoveryData(stx.id, bobNode, aliceNode).apply { - assertEquals(StatesToRecord.ONLY_RELEVANT, this?.statesToRecord) - assertEquals(aliceNode.info.singleIdentity().name.hashCode().toLong(), this?.initiatorPartyId) - assertEquals(mapOf(BOB_NAME.hashCode().toLong() to StatesToRecord.ALL_VISIBLE), this?.peersToStatesToRecord) + val rdr = getReceiverRecoveryData(stx.id, bobNode).apply { + assertNotNull(this) + val hashedDL = HashedDistributionList.decrypt(this!!.encryptedDistributionList.bytes, aliceNode.internals.encryptionService) + assertEquals(StatesToRecord.ONLY_RELEVANT, hashedDL.senderStatesToRecord) + assertEquals(aliceNode.info.singleIdentity().name.hashCode().toLong(), this.initiatorPartyId) + assertEquals(mapOf(BOB_NAME.hashCode().toLong() to StatesToRecord.ALL_VISIBLE), hashedDL.peerHashToStatesToRecord) } validateSenderAndReceiverTimestamps(sdrs, rdr!!) } @@ -391,14 +391,16 @@ class FinalityFlowTests : WithFinality { assertEquals(StatesToRecord.ALL_VISIBLE, this[1].statesToRecord) assertEquals(CHARLIE_NAME.hashCode().toLong(), this[1].peerPartyId) } - val rdr = getReceiverRecoveryData(stx.id, bobNode, aliceNode).apply { - assertEquals(StatesToRecord.ONLY_RELEVANT, this?.statesToRecord) - assertEquals(aliceNode.info.singleIdentity().name.hashCode().toLong(), this?.initiatorPartyId) + val rdr = getReceiverRecoveryData(stx.id, bobNode).apply { + assertNotNull(this) + val hashedDL = HashedDistributionList.decrypt(this!!.encryptedDistributionList.bytes, aliceNode.internals.encryptionService) + assertEquals(StatesToRecord.ONLY_RELEVANT, hashedDL.senderStatesToRecord) + assertEquals(aliceNode.info.singleIdentity().name.hashCode().toLong(), this.initiatorPartyId) // note: Charlie assertion here is using the hinted StatesToRecord value passed to it from Alice assertEquals(mapOf( BOB_NAME.hashCode().toLong() to StatesToRecord.ONLY_RELEVANT, CHARLIE_NAME.hashCode().toLong() to StatesToRecord.ALL_VISIBLE - ), distList.peerHashToStatesToRecord) + ), hashedDL.peerHashToStatesToRecord) } validateSenderAndReceiverTimestamps(sdrs, rdr!!) @@ -417,12 +419,12 @@ class FinalityFlowTests : WithFinality { assertEquals(2, this.size) assertEquals(this[0].timestamp, this[1].timestamp) } - getReceiverRecoveryData(stx3.id, bobNode, aliceNode).apply { - assertThat(getReceiverRecoveryData(stx3.id, bobNode, aliceNode)).isNotNull + getReceiverRecoveryData(stx3.id, bobNode).apply { + assertThat(this).isNotNull assertEquals(senderDistributionRecords[0].timestamp, this!!.timestamp) } - getReceiverRecoveryData(stx3.id, charlieNode, aliceNode).apply { - assertThat(getReceiverRecoveryData(stx3.id, charlieNode, aliceNode)).isNotNull + getReceiverRecoveryData(stx3.id, charlieNode).apply { + assertThat(this).isNotNull assertEquals(senderDistributionRecords[0].timestamp, this!!.timestamp) } } @@ -451,10 +453,12 @@ class FinalityFlowTests : WithFinality { assertEquals(StatesToRecord.ONLY_RELEVANT, this[0].statesToRecord) assertEquals(BOB_NAME.hashCode().toLong(), this[0].peerPartyId) } - val rdr = getReceiverRecoveryData(stx.id, bobNode, aliceNode).apply { - assertEquals(StatesToRecord.ONLY_RELEVANT, this?.statesToRecord) - assertEquals(aliceNode.info.singleIdentity().name.hashCode().toLong(), this?.initiatorPartyId) - assertEquals(mapOf(BOB_NAME.hashCode().toLong() to StatesToRecord.ONLY_RELEVANT), this?.peersToStatesToRecord) + val rdr = getReceiverRecoveryData(stx.id, bobNode).apply { + assertNotNull(this) + val hashedDL = HashedDistributionList.decrypt(this!!.encryptedDistributionList.bytes, aliceNode.internals.encryptionService) + assertEquals(StatesToRecord.ONLY_RELEVANT, hashedDL.senderStatesToRecord) + assertEquals(aliceNode.info.singleIdentity().name.hashCode().toLong(), this.initiatorPartyId) + assertEquals(mapOf(BOB_NAME.hashCode().toLong() to StatesToRecord.ONLY_RELEVANT), hashedDL.peerHashToStatesToRecord) } validateSenderAndReceiverTimestamps(sdr, rdr!!) } @@ -466,24 +470,16 @@ class FinalityFlowTests : WithFinality { DBTransactionStorageLedgerRecovery.DBSenderDistributionRecord::class.java ).setParameter("transactionId", id.toString()).resultList } - return fromDb.map { it.toSenderDistributionRecord() }.also { println("SenderDistributionRecord\n$it") } + return fromDb.map { it.toSenderDistributionRecord() } } - private fun getReceiverRecoveryData(txId: SecureHash, receiver: TestStartedNode, sender: TestStartedNode): ReceiverDistributionRecord? { - val fromDb = receiver.database.transaction { + private fun getReceiverRecoveryData(txId: SecureHash, receiver: TestStartedNode): ReceiverDistributionRecord? { + return receiver.database.transaction { session.createQuery( "from ${DBTransactionStorageLedgerRecovery.DBReceiverDistributionRecord::class.java.name} where txId = :transactionId", DBTransactionStorageLedgerRecovery.DBReceiverDistributionRecord::class.java ).setParameter("transactionId", txId.toString()).resultList - }.singleOrNull() - - // The receiver should not be able to decrypt the distribution list - assertThrows { - fromDb?.toReceiverDistributionRecord(receiver.internals.encryptionService) - } - - // Only the sender can - return fromDb?.toReceiverDistributionRecord(sender.internals.encryptionService) + }.singleOrNull()?.toReceiverDistributionRecord() } @StartableByRPC diff --git a/node/src/main/kotlin/net/corda/node/services/persistence/DBTransactionStorageLedgerRecovery.kt b/node/src/main/kotlin/net/corda/node/services/persistence/DBTransactionStorageLedgerRecovery.kt index db1c176f0a..69df1fe9b3 100644 --- a/node/src/main/kotlin/net/corda/node/services/persistence/DBTransactionStorageLedgerRecovery.kt +++ b/node/src/main/kotlin/net/corda/node/services/persistence/DBTransactionStorageLedgerRecovery.kt @@ -101,8 +101,7 @@ class DBTransactionStorageLedgerRecovery(private val database: CordaPersistence, receiverStatesToRecord = receiverStatesToRecord ) @VisibleForTesting - fun toReceiverDistributionRecord(encryptionService: EncryptionService): ReceiverDistributionRecord { - val hashedDL = HashedDistributionList.decrypt(this.distributionList, encryptionService) + fun toReceiverDistributionRecord(): ReceiverDistributionRecord { return ReceiverDistributionRecord( SecureHash.parse(this.txId), this.compositeKey.peerPartyId, diff --git a/node/src/test/kotlin/net/corda/node/services/persistence/DBTransactionStorageLedgerRecoveryTests.kt b/node/src/test/kotlin/net/corda/node/services/persistence/DBTransactionStorageLedgerRecoveryTests.kt index 1ffd27e736..3c81be2ab8 100644 --- a/node/src/test/kotlin/net/corda/node/services/persistence/DBTransactionStorageLedgerRecoveryTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/persistence/DBTransactionStorageLedgerRecoveryTests.kt @@ -331,7 +331,7 @@ class DBTransactionStorageLedgerRecoveryTests { session.createQuery( "from ${DBTransactionStorage.DBTransaction::class.java.name} where txId = :transactionId", DBTransactionStorage.DBTransaction::class.java - ).setParameter("transactionId", id.toString()).resultList + ).setParameter("transactionId", txId.toString()).resultList } assertEquals(1, fromDb.size) return fromDb[0] @@ -355,12 +355,12 @@ class DBTransactionStorageLedgerRecoveryTests { private fun readReceiverDistributionRecordFromDB(txId: SecureHash): ReceiverDistributionRecord { val fromDb = database.transaction { session.createQuery( - "from ${DBTransactionStorageLedgerRecovery.DBReceiverDistributionRecord::class.java.name} where txId = :transactionId", - DBTransactionStorageLedgerRecovery.DBReceiverDistributionRecord::class.java - ).setParameter("transactionId", id.toString()).resultList + "from ${DBReceiverDistributionRecord::class.java.name} where txId = :transactionId", + DBReceiverDistributionRecord::class.java + ).setParameter("transactionId", txId.toString()).resultList } assertEquals(1, fromDb.size) - return fromDb[0].toReceiverDistributionRecord(encryptionService) + return fromDb[0].toReceiverDistributionRecord() } private fun newTransactionRecovery(cacheSizeBytesOverride: Long? = null, clock: CordaClock = SimpleClock(Clock.systemUTC())) { @@ -415,15 +415,5 @@ class DBTransactionStorageLedgerRecoveryTests { private fun notarySig(txId: SecureHash) = DUMMY_NOTARY.keyPair.sign(SignableData(txId, SignatureMetadata(1, Crypto.findSignatureScheme(DUMMY_NOTARY.publicKey).schemeNumberID))) - - private fun SenderDistributionList.toWire(): ByteArray { - val hashedPeersToStatesToRecord = this.peersToStatesToRecord.mapKeys { (peer) -> partyInfoCache.getPartyIdByCordaX500Name(peer) } - val hashedDistributionList = HashedDistributionList( - this.senderStatesToRecord, - hashedPeersToStatesToRecord, - HashedDistributionList.PublicHeader(now()) - ) - return hashedDistributionList.encrypt(encryptionService) - } }