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.
This commit is contained in:
Chris Rankin 2018-03-31 14:52:08 +01:00 committed by GitHub
parent 10e97f4919
commit 9c07e67100
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -61,19 +61,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 }
@ -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)
}
}
}