From 1973768c1b9dfd70651068f85605368702887f6a Mon Sep 17 00:00:00 2001 From: Stephen Cirner Date: Fri, 19 Mar 2021 16:31:42 -0400 Subject: [PATCH] Allow hash and proto hash interfaces to be pairable by adding a uuid. --- .../kotlin/io/p8e/contracts/ContractHash.kt | 7 ++++++- .../src/main/kotlin/io/p8e/proto/ProtoHash.kt | 7 ++++++- .../src/main/kotlin/io/p8e/ContractManager.kt | 18 +++++++++--------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/p8e-contract-base/src/main/kotlin/io/p8e/contracts/ContractHash.kt b/p8e-contract-base/src/main/kotlin/io/p8e/contracts/ContractHash.kt index 8df8989..2df1874 100644 --- a/p8e-contract-base/src/main/kotlin/io/p8e/contracts/ContractHash.kt +++ b/p8e-contract-base/src/main/kotlin/io/p8e/contracts/ContractHash.kt @@ -3,4 +3,9 @@ package io.p8e.contracts interface ContractHash { fun getClasses(): Map fun getHash(): String -} \ No newline at end of file + // Provides a means to pair ContractHash to ProtoHash implementations so that the pairing can be preferred + // when resolution takes place during ContractManager::dehydrateSpec. Fully qualified contract names in + // ContractHash::getClasses must be unique, but fully qualified proto names in ProtoHash::getClasses will often + // collide. + fun getUuid(): String +} diff --git a/p8e-proto-internal/src/main/kotlin/io/p8e/proto/ProtoHash.kt b/p8e-proto-internal/src/main/kotlin/io/p8e/proto/ProtoHash.kt index 5a0997d..a3dce4f 100644 --- a/p8e-proto-internal/src/main/kotlin/io/p8e/proto/ProtoHash.kt +++ b/p8e-proto-internal/src/main/kotlin/io/p8e/proto/ProtoHash.kt @@ -3,4 +3,9 @@ package io.p8e.proto interface ProtoHash { fun getClasses(): Map fun getHash(): String -} \ No newline at end of file + // Provides a means to pair ContractHash to ProtoHash implementations so that the pairing can be preferred + // when resolution takes place during ContractManager::dehydrateSpec. Fully qualified contract names in + // ContractHash::getClasses must be unique, but fully qualified proto names in ProtoHash::getClasses will often + // collide. + fun getUuid(): String +} diff --git a/p8e-sdk/src/main/kotlin/io/p8e/ContractManager.kt b/p8e-sdk/src/main/kotlin/io/p8e/ContractManager.kt index 89b9b52..161fb75 100644 --- a/p8e-sdk/src/main/kotlin/io/p8e/ContractManager.kt +++ b/p8e-sdk/src/main/kotlin/io/p8e/ContractManager.kt @@ -164,18 +164,16 @@ class ContractManager( ) } - private fun getContractHash(clazz: Class): String { + private fun getContractHash(clazz: Class): ContractHash { return contractHashes.find { it.getClasses()[clazz.name] == true }.orThrow { IllegalStateException("Unable to find ContractHash instance to match ${clazz.name}, please verify you are running a Provenance bootstrapped JAR.") } - .getHash() } - private fun getProtoHash(clazz: Class<*>): String { + private fun getProtoHash(contractHash: ContractHash, clazz: Class<*>): ProtoHash { return protoHashes.find { - it.getClasses()[clazz.name] == true + it.getUuid() == contractHash.getUuid() && it.getClasses()[clazz.name] == true }.orThrow { IllegalStateException("Unable to find ProtoHash instance to match ${clazz.name}, please verify you are running a Provenance bootstrapped JAR.") } - .getHash() } private fun newContractProto(contractClazz: Class): Contracts.Contract.Builder = @@ -185,7 +183,7 @@ class ContractManager( contractClazz.name, ProtoUtil.locationBuilderOf( contractClazz.name, - ProvenanceReference.newBuilder().setHash(getContractHash(contractClazz)).build() + ProvenanceReference.newBuilder().setHash(getContractHash(contractClazz).getHash()).build() ), FACT ).build() @@ -235,6 +233,7 @@ class ContractManager( contractClazz, contractClassExecutor(contractClazz) ).also { contract -> + // contract.envelope.contract.spec.dataLocation.ref.hash invokerRole?.let { contract.satisfyParticipant(it, publicKey) } } } @@ -322,15 +321,16 @@ class ContractManager( } fun dehydrateSpec(clazz: Class): ContractSpec { + val contractHash = getContractHash(clazz) val protoHash = clazz.methods .find { it.returnType != null && Message::class.java.isAssignableFrom(it.returnType) } ?.returnType - ?.let { getProtoHash(it) } + ?.let { getProtoHash(contractHash, it) } .orThrow { IllegalStateException("Unable to find hash for proto JAR for return types on ${clazz.name}") } - val contractRef = ProvenanceReference.newBuilder().setHash(getContractHash(clazz)).build() - val protoRef = ProvenanceReference.newBuilder().setHash(protoHash).build() + val contractRef = ProvenanceReference.newBuilder().setHash(contractHash.getHash()).build() + val protoRef = ProvenanceReference.newBuilder().setHash(protoHash.getHash()).build() return ContractSpecMapper.dehydrateSpec(clazz.kotlin, contractRef, protoRef) }