在 Corda 中实现可调度状态

3

我们如何在 Corda 中实现可调度状态?在我的情况下,我需要发出一个月的报表,所以可调度状态是否可以用于此目的?


这是一个有趣的问题,我正在处理类似的用例,但我还没有进行实际实现,不过,schedulableState 是你需要的。 - Stefano.Maffullo
1个回答

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中指定的偏移时间开始计划启动。

就是这样!


在我的情况下,我需要根据父级状态中的本金创建月度报表。因此,我的父状态将是可调度状态,它每个月都会安排一个流程来创建月度报表(子状态)。我是正确的吗? - Raghuram
完全正确。您需要实现可调度状态并将时间延迟设置为一个月。然后,当计时器触发时,将执行发出语句的流程。干杯! - Roger Willis
可调度状态是两个节点之间交易的一部分,该状态驻留在双方节点中。在这种情况下,调度程序流程将在两个节点中触发。但我希望它只在一个节点中触发。这可能吗? - Raghuram
在正常实现中,我们是否需要为网络初始化处理程序添加逻辑,在这种情况下,相同的流程逻辑必须在两侧执行? - Raghuram

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接