【发布时间】:2018-10-19 20:33:08
【问题描述】:
我有一个输入状态(状态 A 和合同 A)和输出状态(状态 B 和合同 B)的要求。我们可以将其合并到一个事务中吗? 另外,两个不同合约的验证方法是如何调用的?如果与响应共享示例,那将非常有帮助。谢谢。
【问题讨论】:
我有一个输入状态(状态 A 和合同 A)和输出状态(状态 B 和合同 B)的要求。我们可以将其合并到一个事务中吗? 另外,两个不同合约的验证方法是如何调用的?如果与响应共享示例,那将非常有帮助。谢谢。
【问题讨论】:
是的,这是可能的,并且在整个交易过程中都会调用两个合约。实际上,节点运行:
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.
}
}
}
}
}
【讨论】: