From 9c07e67100c8f50f8f585a3430ba2b83a624c4d5 Mon Sep 17 00:00:00 2001 From: Chris Rankin Date: Sat, 31 Mar 2018 14:52:08 +0100 Subject: [PATCH] ENT-1463: Instantiate the contract class as part of contract verification. (#660) * Instantiating the contract class should be part of contract verification. We should not instantiate it while building LedgerTransaction. * Also catch any exceptions from instantiating the contract. --- .../net/corda/core/transactions/LedgerTransaction.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/transactions/LedgerTransaction.kt b/core/src/main/kotlin/net/corda/core/transactions/LedgerTransaction.kt index 5997ce891f..773741dd53 100644 --- a/core/src/main/kotlin/net/corda/core/transactions/LedgerTransaction.kt +++ b/core/src/main/kotlin/net/corda/core/transactions/LedgerTransaction.kt @@ -61,19 +61,17 @@ data class LedgerTransaction @JvmOverloads constructor( } private companion object { - private fun createContractFor(className: ContractClassName, classLoader: ClassLoader?): Try { + private fun contractClassFor(className: ContractClassName, classLoader: ClassLoader?): Try> { return Try.on { (classLoader ?: this::class.java.classLoader) .loadClass(className) .asSubclass(Contract::class.java) - .getConstructor() - .newInstance() } } } - private val contracts: Map> = (inputs.map { it.state } + outputs) - .map { it.contract to createContractFor(it.contract, it.data::class.java.classLoader) }.toMap() + private val contracts: Map>> = (inputs.map { it.state } + outputs) + .map { it.contract to contractClassFor(it.contract, it.data::class.java.classLoader) }.toMap() val inputStates: List get() = inputs.map { it.state.data } @@ -135,11 +133,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) } } }