在corda中实现可调度状态

时间:2017-08-16 11:50:49

标签: corda

我们如何在corda中实现可调度状态?在我的情况下,我需要发一个月结单,那么可以使用schedulablestate吗?

1 个答案:

答案 0 :(得分:5)

您需要做很多事情。

首先,您的状态对象需要实现SchedulableState接口。它增加了一个额外的方法:

interface SchedulableState : ContractState {
    /**
     * Indicate whether there is some activity to be performed at some future point in time with respect to this
     * [ContractState], what that activity is and at what point in time it should be initiated.
     * This can be used to implement deadlines for payment or processing of financial instruments according to a schedule.
     *
     * The state has no reference to it's own StateRef, so supply that for use as input to any FlowLogic constructed.
     *
     * @return null if there is no activity to schedule.
     */
    fun nextScheduledActivity(thisStateRef: StateRef, flowLogicRefFactory: FlowLogicRefFactory): ScheduledActivity?
}

此接口需要实现名为nextScheduledActivity的方法,该方法返回可选的ScheduledActivity实例。 ScheduledActivity捕获每个节点将运行的FlowLogic实例,执行活动以及运行的时间由java.time.Instant描述。一旦您的状态实现此接口并由Vault跟踪,就可以在提交到Vault时查询下一个活动。示例:

class ExampleState(val initiator: Party,
                   val requestTime: Instant,
                   val delay: Long) : SchedulableState {
     override val contract: Contract get() = DUMMY_PROGRAM_ID
     override val participants: List<AbstractParty> get() = listOf(initiator)
     override fun nextScheduledActivity(thisStateRef: StateRef, flowLogicRefFactory: FlowLogicRefFactory): ScheduledActivity? {
         val responseTime = requestTime.plusSeconds(delay)
         val flowRef = flowLogicRefFactory.create(FlowToStart::class.java)
         return ScheduledActivity(flowRef, responseTime)
     }
 }

其次,计划开始的FlowLogic类(在这种情况下为FlowToStart)必须也注明@SchedulableFlow.例如。

@InitiatingFlow
@SchedulableFlow
class FlowToStart : FlowLogic<Unit>() {
    @Suspendable
    override fun call() {
        // Do stuff.
    }
}

现在,当ExampleState存储在保管库中时,FlowToStart将被调整为从ExampleState中指定的偏移时间开始。

那就是它!