From a2537d59f54d9a4d58bdca964dee5b7972cef0a7 Mon Sep 17 00:00:00 2001 From: Adel El-Beik Date: Wed, 9 Oct 2024 17:16:04 +0100 Subject: [PATCH] ENT-12291: Removed from rotated keys from public api. --- .../corda/core/internal/ServiceHubCoreInternal.kt | 3 +++ .../net/corda/core/internal/TransactionUtils.kt | 15 +++++++++++++++ .../main/kotlin/net/corda/core/node/ServiceHub.kt | 6 ------ .../corda/core/transactions/TransactionBuilder.kt | 5 +++-- .../corda/core/transactions/WireTransaction.kt | 4 ++-- .../kotlin/net/corda/testing/node/MockServices.kt | 3 ++- .../main/kotlin/net/corda/testing/dsl/TestDSL.kt | 2 +- 7 files changed, 26 insertions(+), 12 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/internal/ServiceHubCoreInternal.kt b/core/src/main/kotlin/net/corda/core/internal/ServiceHubCoreInternal.kt index 4b7d856699..5f90afa5b7 100644 --- a/core/src/main/kotlin/net/corda/core/internal/ServiceHubCoreInternal.kt +++ b/core/src/main/kotlin/net/corda/core/internal/ServiceHubCoreInternal.kt @@ -2,6 +2,7 @@ package net.corda.core.internal import co.paralleluniverse.fibers.Suspendable import net.corda.core.DeleteForDJVM +import net.corda.core.contracts.RotatedKeys import net.corda.core.internal.notary.NotaryService import net.corda.core.node.ServiceHub import net.corda.core.node.StatesToRecord @@ -24,6 +25,8 @@ interface ServiceHubCoreInternal : ServiceHub { fun createTransactionsResolver(flow: ResolveTransactionsFlow): TransactionsResolver val attachmentsClassLoaderCache: AttachmentsClassLoaderCache + + val rotatedKeys: RotatedKeys } interface TransactionsResolver { diff --git a/core/src/main/kotlin/net/corda/core/internal/TransactionUtils.kt b/core/src/main/kotlin/net/corda/core/internal/TransactionUtils.kt index 7bdfec76be..299a898267 100644 --- a/core/src/main/kotlin/net/corda/core/internal/TransactionUtils.kt +++ b/core/src/main/kotlin/net/corda/core/internal/TransactionUtils.kt @@ -8,6 +8,7 @@ import net.corda.core.crypto.algorithm import net.corda.core.crypto.internal.DigestAlgorithmFactory import net.corda.core.flows.FlowLogic import net.corda.core.identity.Party +import net.corda.core.node.ServiceHub import net.corda.core.node.ServicesForResolution import net.corda.core.serialization.* import net.corda.core.transactions.* @@ -16,6 +17,20 @@ import java.io.ByteArrayOutputStream import java.security.PublicKey import kotlin.reflect.KClass + +fun ServiceHub.retrieveRotatedKeys(): RotatedKeys { + if (this is ServiceHubCoreInternal) { + return this.rotatedKeys + } + var clazz: Class<*> = javaClass + while (true) { + if (clazz.name == "net.corda.testing.node.MockServices") { + return clazz.getDeclaredMethod("getRotatedKeys").apply { isAccessible = true }.invoke(this) as RotatedKeys + } + clazz = clazz.superclass ?: throw ClassCastException("${javaClass.name} is not a ServiceHub") + } +} + /** Constructs a [NotaryChangeWireTransaction]. */ class NotaryChangeTransactionBuilder(val inputs: List, val notary: Party, diff --git a/core/src/main/kotlin/net/corda/core/node/ServiceHub.kt b/core/src/main/kotlin/net/corda/core/node/ServiceHub.kt index fa9a7baf34..612e341a6f 100644 --- a/core/src/main/kotlin/net/corda/core/node/ServiceHub.kt +++ b/core/src/main/kotlin/net/corda/core/node/ServiceHub.kt @@ -171,12 +171,6 @@ interface ServiceHub : ServicesForResolution { */ val transactionVerifierService: TransactionVerifierService - /** - * INTERNAL. DO NOT USE. - * @suppress - */ - val rotatedKeys: RotatedKeys - /** * A [Clock] representing the node's current time. This should be used in preference to directly accessing the * clock so the current time can be controlled during unit testing. diff --git a/core/src/main/kotlin/net/corda/core/transactions/TransactionBuilder.kt b/core/src/main/kotlin/net/corda/core/transactions/TransactionBuilder.kt index cbe925b08b..afc2b8d3e4 100644 --- a/core/src/main/kotlin/net/corda/core/transactions/TransactionBuilder.kt +++ b/core/src/main/kotlin/net/corda/core/transactions/TransactionBuilder.kt @@ -535,6 +535,7 @@ open class TransactionBuilder( val resolvedOutputStates = outputStates.map { val outputConstraint = it.constraint + if (outputConstraint in automaticConstraints) { it.copy(constraint = defaultOutputConstraint) } else { @@ -553,9 +554,9 @@ open class TransactionBuilder( } private fun getRotatedKeys(services: ServiceHub?): RotatedKeys { - return services?.rotatedKeys ?: CordaRotatedKeys.keys.also { + return services?.let { services.retrieveRotatedKeys() } ?: CordaRotatedKeys.keys.also { log.warn("WARNING: You must pass in a ServiceHub reference to TransactionBuilder to resolve " + - "rotated keys defined in configuration. If you are writing a unit test then pass in a " + + "state pointers outside of flows. If you are writing a unit test then pass in a " + "MockServices instance.") } } diff --git a/core/src/main/kotlin/net/corda/core/transactions/WireTransaction.kt b/core/src/main/kotlin/net/corda/core/transactions/WireTransaction.kt index 37b7c5a397..6c33af9cc1 100644 --- a/core/src/main/kotlin/net/corda/core/transactions/WireTransaction.kt +++ b/core/src/main/kotlin/net/corda/core/transactions/WireTransaction.kt @@ -28,6 +28,7 @@ import net.corda.core.serialization.DeprecatedConstructorForDeserialization import net.corda.core.serialization.SerializationFactory import net.corda.core.serialization.SerializedBytes import net.corda.core.serialization.internal.AttachmentsClassLoaderCache +import net.corda.core.serialization.internal.AttachmentsClassLoaderForRotatedKeysOnlyImpl import net.corda.core.serialization.serialize import net.corda.core.utilities.OpaqueBytes import java.security.PublicKey @@ -166,8 +167,7 @@ class WireTransaction(componentGroups: List, val privacySalt: Pr { stateRef -> resolveStateRef(stateRef)?.serialize() }, { null }, Attachment::isUploaderTrusted, - null - // TODO : elbad01 : does rotated keys need to be passed here, or we use attachment class loader cache + attachmentsClassLoaderCache = AttachmentsClassLoaderForRotatedKeysOnlyImpl() ) } diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt index 95adc20f18..606293ef03 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt @@ -3,6 +3,7 @@ package net.corda.testing.node import com.google.common.collect.MutableClassToInstanceMap import net.corda.core.contracts.Attachment import net.corda.core.contracts.ContractClassName +import net.corda.core.contracts.CordaRotatedKeys import net.corda.core.contracts.RotatedKeys import net.corda.core.contracts.StateRef import net.corda.core.cordapp.CordappProvider @@ -457,10 +458,10 @@ open class MockServices private constructor( override val cordappProvider: CordappProvider get() = mockCordappProvider override var networkParametersService: NetworkParametersService = MockNetworkParametersStorage(initialNetworkParameters) override val diagnosticsService: DiagnosticsService = NodeDiagnosticsService() + var rotatedKeys: RotatedKeys = CordaRotatedKeys.keys protected val servicesForResolution: ServicesForResolution get() = ServicesForResolutionImpl(identityService, attachments, cordappProvider, networkParametersService, validatedTransactions) - override val rotatedKeys: RotatedKeys = RotatedKeys() internal fun makeVaultService(schemaService: SchemaService, database: CordaPersistence, cordappLoader: CordappLoader): VaultServiceInternal { return NodeVaultService( diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/dsl/TestDSL.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/dsl/TestDSL.kt index 72ff0e541e..07b3ef1a3b 100644 --- a/testing/test-utils/src/main/kotlin/net/corda/testing/dsl/TestDSL.kt +++ b/testing/test-utils/src/main/kotlin/net/corda/testing/dsl/TestDSL.kt @@ -105,7 +105,7 @@ data class TestTransactionDSLInterpreter private constructor( ThreadFactoryBuilder().setNameFormat("flow-external-operation-thread").build() ) - override val rotatedKeys: RotatedKeys = ledgerInterpreter.services.rotatedKeys + override val rotatedKeys: RotatedKeys = (ledgerInterpreter.services as? ServiceHubCoreInternal)?.rotatedKeys ?: CordaRotatedKeys.keys override val attachmentTrustCalculator: AttachmentTrustCalculator = ledgerInterpreter.services.attachments.let {