使用不同类型的状态作为输入和输出时,事务验证失败

时间:2018-01-31 12:10:06

标签: corda

我正在尝试创建一个corda事务,其中我正在使用一种类型的输入状态,同时创建另一种类型的状态作为输出但是在验证时输入和输出状态正在使用不同合同中的相同命令进行评估,因此验证部分失败了。是否有任何方法可以使用第一个状态的命令评估输入合同,而输出状态将使用另一个合同进行验证。

在这两份合同中,我都是这样做的:

val command = tx.commands.requireSingleCommand<Commands>()
when (command.value) {
    is Commands.Issue -> verifyIssue(tx, setOfSigners)
    is Commands.Transfer -> verifyTransfer(tx, setOfSigners)
    else -> throw IllegalArgumentException("Unrecognised command")
}

这是日志文件中的输出:

[WARN ] 2018-01-31T12:07:53,963Z [Node thread] flow.[51120088-7c8c-457b-b143-966b85e788cb].run - Flow finished with exception
net.corda.core.contracts.TransactionVerificationException$ContractRejection: Contract verification failed: Required com.template.AssetContract.Commands command, contract: com.template.AssetContract@53cc722e, transaction: 6FBE6CB29E2385C81683ED25C0A5CE21E07A18BE9DFD74AD47C45223138B35E6

1 个答案:

答案 0 :(得分:2)

您需要编写每份合约,以便它只对某些类型的州施加条件。例如,假设您有两种状态类型 - XStateYState - 以及两种类型的合同 - XContractYContract。然后可以按如下方式编写合同:

class XContract : Contract {
    override fun verify(tx: LedgerTransaction) {
        val inputXStates = tx.inputsOfType<XState>()
        val outputXStates = tx.outputsOfType<XState>()

        requireThat {
            "All XState inputs are unapproved." using (inputXStates.all { it.approved == false })
            "All XState outputs are approved" using (outputXStates.all { it.approved == true })
        }
    }
}

class YContract : Contract {
    override fun verify(tx: LedgerTransaction) {
        val inputYStates = tx.inputsOfType<YState>()
        val outputYStates = tx.outputsOfType<YState>()

        requireThat {
            "All YState inputs are unaudited." using (inputYStates.all { it.audited == false })
            "All YState outputs are audited" using (outputYStates.all { it.audited == true })
        }
    }
}