在Corda中实现可调度状态 [英] Implementing schedulable states in corda

查看:58
本文介绍了在Corda中实现可调度状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在Corda中实现可调度状态?就我而言,我需要发布月度报表,因此可以使用schedulablestate吗?

How do we implement schedulable state in corda? In my case i need to issue a monthly statement, so can schedulablestate be used for this?

推荐答案

有很多事情你需要做。

There are a number of things you need to do.

首先,您的状态对象需要实现 SchedulableState 接口。它添加了另一种方法:

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

此接口要求使用名为 nextScheduledActivity 要实现,它将返回一个可选的 ScheduledActivity 实例。 ScheduledActivity 捕获每个节点将要运行的 FlowLogic 实例,以执行该活动以及何时运行由一个实例描述。 java.time.Instant 。一旦您的状态实现了此接口并被Vault跟踪,则可以在提交到Vault时查询下一个活动。示例:

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

第二, FlowLogic 类计划开始的时间(在这种情况下为 FlowToStart )也必须使用 @SchedulableFlow进行注释。例如

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

现在,当 ExampleState 存储在库中, FlowToStart 将安排在 ExampleState

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

就是这样!

这篇关于在Corda中实现可调度状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆