Merge pull request #2903 from corda/chrisr3-verify-contract

ENT-1463: Instantiate the contract class as part of contract verification.
This commit is contained in:
Chris Rankin 2018-04-03 10:16:16 +01:00 committed by GitHub
commit 87836361ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -51,19 +51,17 @@ data class LedgerTransaction @JvmOverloads constructor(
}
private companion object {
private fun createContractFor(className: ContractClassName, classLoader: ClassLoader?): Try<Contract> {
private fun contractClassFor(className: ContractClassName, classLoader: ClassLoader?): Try<Class<out Contract>> {
return Try.on {
(classLoader ?: this::class.java.classLoader)
.loadClass(className)
.asSubclass(Contract::class.java)
.getConstructor()
.newInstance()
}
}
}
private val contracts: Map<ContractClassName, Try<Contract>> = (inputs.map { it.state } + outputs)
.map { it.contract to createContractFor(it.contract, it.data::class.java.classLoader) }.toMap()
private val contracts: Map<ContractClassName, Try<Class<out Contract>>> = (inputs.map { it.state } + outputs)
.map { it.contract to contractClassFor(it.contract, it.data::class.java.classLoader) }.toMap()
val inputStates: List<ContractState> get() = inputs.map { it.state.data }
@ -125,11 +123,11 @@ data class LedgerTransaction @JvmOverloads constructor(
when (result) {
is Try.Failure -> throw TransactionVerificationException.ContractCreationError(id, key, result.exception)
is Try.Success -> {
val contract = result.value
try {
val contract = result.value.newInstance()
contract.verify(this)
} catch (e: Throwable) {
throw TransactionVerificationException.ContractRejection(id, contract, e)
throw TransactionVerificationException.ContractRejection(id, result.value.name, e)
}
}
}