Corda合同中的动态规则

时间:2017-12-01 09:50:08

标签: corda

我希望国家合同中的条件取决于有关各方之间商定的规则。我不想对此规则进行硬编码,因为各方可能希望同意稍后更新规则。例如:

  //'y' is an agreed value between the participants.    
  if (inputThingState.amount > y) {
         "If thing amount is greater than y, then status must equal 1" using outputThingState.status == 1
   }

这样做的一种方法是拥有一个规则状态对象,该对象由相关方在先前的交易中提出并同意。然后将批准的Rule状态对象添加为当前事务的输入。因此条件如下:

if (inputThingState.amount > inputRuleState.y) {
     "If thing amount is greater than y, then status must equal 1" using outputThingState.status == 1
}

这似乎有效,但这意味着要使用RuleState对象,因此必须将其复制并添加为输出状态,以便可以再次使用它。它也可能导致问题,因为多个事务可能希望同时使用相同的RuleState。

有更优雅的方法来实现这一目标吗? (可能通过将批准的规则作为命令数据传递。)

1 个答案:

答案 0 :(得分:0)

实现此目的的一种方法是将规则放在命令中:

import net.corda.core.contracts.CommandData
import net.corda.core.contracts.Contract
import net.corda.core.contracts.ContractState
import net.corda.core.contracts.requireThat
import net.corda.core.identity.Party
import net.corda.core.transactions.LedgerTransaction

class MyState(val amount: Int, override val participants: List<Party>): ContractState

class MyCommand(val amount: Int) : CommandData

class MyStateContract: Contract {
    override fun verify(tx: LedgerTransaction) {
        requireThat {
            "There is one input state" using (tx.inputsOfType<MyState>().size == 1)
            "There is one command" using (tx.commandsOfType<MyCommand>().size == 1)
            val myState = tx.inputsOfType<MyState>().single()
            val myCommand = tx.commandsOfType<MyCommand>().single()
            "The amount of the state and the command match" using (myState.amount == myCommand.value.amount)
        }
    }
}

另一种方法是编写流程,以便拒绝签署任何不满足条件的事务。仅仅因为交易合同有效,并不意味着您有义务签署。只要规则发生变化,您就可以更新流程。