Implementing schedulable states in corda [closed]

喜夏-厌秋 提交于 2019-12-06 10:18:38

There are a number of things you need to do.

Firstly, your state object needs to implement the SchedulableState interface. It adds an additional method:

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?
}

This interface requires a method named nextScheduledActivity to be implemented which returns an optional ScheduledActivity instance. ScheduledActivity captures what FlowLogic instance each node will run, to perform the activity, and when it will run is described by a java.time.Instant. Once your state implements this interface and is tracked by the vault, it can expect to be queried for the next activity when committed to the vault. Example:

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)
     }
 }

Secondly, the FlowLogic class which is schedulted to start (in this case FlowToStart) must be also annotated with @SchedulableFlow. E.g.

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

Now, when ExampleState is stored in the vault, the FlowToStart will be schedulted to start at the offset time specified in the ExampleState.

That's it!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!