在corda中实现可调度状态

Rag*_*ram 2 corda

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

Rog*_*lis 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?
}
Run Code Online (Sandbox Code Playgroud)

此接口需要一个名为nextScheduledActivity要实现的方法,该方法返回一个可选ScheduledActivity实例.ScheduledActivity捕获FlowLogic每个节点将运行的实例,执行活动以及何时运行将由a描述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)
     }
 }
Run Code Online (Sandbox Code Playgroud)

其次,计划开始的FlowLogic类(在这种情况下FlowToStart)也必须用@SchedulableFlow.Eg 注释

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

现在,当ExampleState存储在库中时,FlowToStart将调度以在指定的偏移时间开始ExampleState.

而已!