我们如何在 Corda 中实现可调度状态?在我的情况下,我需要发出一个月的报表,所以可调度状态是否可以用于此目的?
我们如何在 Corda 中实现可调度状态?在我的情况下,我需要发出一个月的报表,所以可调度状态是否可以用于此目的?
有几件事情你需要做。
首先,你的状态对象需要实现 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
中指定的偏移时间开始计划启动。
就是这样!