我们如何在corda中实现可调度状态?在我的情况下,我需要发一个月结单,那么可以使用schedulablestate吗?
答案 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
中指定的偏移时间开始。
那就是它!