【问题标题】:Multiple contracts in a single transaction单笔交易中的多个合约
【发布时间】:2018-10-19 20:33:08
【问题描述】:

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

【问题讨论】:

    标签: kotlin corda


    【解决方案1】:

    是的,这是可能的,并且在整个交易过程中都会调用两个合约。实际上,节点运行:

    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.
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答。这很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2016-03-17
    • 2012-02-18
    • 2013-10-28
    • 1970-01-01
    • 2014-03-05
    • 2021-04-22
    相关资源
    最近更新 更多