一次交易中有多个合同

时间:2018-10-19 11:04:27

标签: kotlin corda

我要有一个输入国(状态A和合同A)和输出国(状态B和合同B)。我们可以将其合并到单个交易中吗? 另外,如何调用两个不同合同的验证方法?如果将示例与响应共享,将非常有帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

是的,这是可能的,并且整个交易都调用了两个合同。有效地,该节点运行:

ContractA().verify(transaction)
ContractB().verify(transaction)

因此,在编写两个verify方法时必须小心。通常,您将需要编写每个合同的verify方法,以便它仅查看与该合同相关的命令和状态。例如:

class ExampleContract : Contract {
    companion object {
        val ID = "com.example.ExampleContract"
    }

    interface ExampleCommands : CommandData {
        class Issue : ExampleCommands
        class Transfer : ExampleCommands
        class Exit: ExampleCommands
    }

    override fun verify(tx: LedgerTransaction) {
        val exampleCommands = tx.commandsOfType<ExampleCommands>()
        val exampleInputs = tx.inputsOfType<ExampleState>()
        val exampleOutputs = tx.outputsOfType<ExampleState>()

        exampleCommands.forEach { command ->
            when (command.value) {
                is ExampleCommands.Issue -> {
                    if (exampleInputs.isNotEmpty()) throw IllegalArgumentException("Issuance should have no inputs.")
                    if (exampleOutputs.isEmpty()) throw IllegalArgumentException("Issuance should have outputs.")
                    // TODO: More verification.
                }

                is ExampleCommands.Transfer -> {
                    if (exampleInputs.isEmpty()) throw IllegalArgumentException("Transfer should have inputs.")
                    if (exampleOutputs.isEmpty()) throw IllegalArgumentException("Transfer should have outputs.")
                    // TODO: More verification.
                }

                is ExampleCommands.Exit -> {
                    if (exampleInputs.isEmpty()) throw IllegalArgumentException("Exit should have inputs.")
                    if (exampleOutputs.isNotEmpty()) throw IllegalArgumentException("Exit should have no outputs.")
                    // TODO: More verification.
                }
            }
        }
    }
}