Skip to content

Commit

Permalink
Allow hash and proto hash interfaces to be pairable by adding a uuid.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Cirner committed Mar 19, 2021
1 parent e01d693 commit 1973768
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ package io.p8e.contracts
interface ContractHash {
fun getClasses(): Map<String, Boolean>
fun getHash(): String
}
// 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
}
7 changes: 6 additions & 1 deletion p8e-proto-internal/src/main/kotlin/io/p8e/proto/ProtoHash.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ package io.p8e.proto
interface ProtoHash {
fun getClasses(): Map<String, Boolean>
fun getHash(): String
}
// 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
}
18 changes: 9 additions & 9 deletions p8e-sdk/src/main/kotlin/io/p8e/ContractManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,16 @@ class ContractManager(
)
}

private fun <T: P8eContract> getContractHash(clazz: Class<T>): String {
private fun <T: P8eContract> getContractHash(clazz: Class<T>): 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 <T: P8eContract> newContractProto(contractClazz: Class<T>): Contracts.Contract.Builder =
Expand All @@ -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()
Expand Down Expand Up @@ -235,6 +233,7 @@ class ContractManager(
contractClazz,
contractClassExecutor(contractClazz)
).also { contract ->
// contract.envelope.contract.spec.dataLocation.ref.hash
invokerRole?.let { contract.satisfyParticipant(it, publicKey) }
}
}
Expand Down Expand Up @@ -322,15 +321,16 @@ class ContractManager(
}

fun <T: P8eContract> dehydrateSpec(clazz: Class<T>): 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)
}
Expand Down

0 comments on commit 1973768

Please sign in to comment.