我正在尝试与ScheduledFlow中的另一个节点启动流会话。下面是Scheduledstate的定义:
data class State(val a: Party,
val b: Party,
val instant: Instant,
val status: Status,
override val linearId: UniqueIdentifier = UniqueIdentifier(),
override val participants: List<AbstractParty> = listOf(a, b))
: LinearState, SchedulableState {
private val scheduledTime = instant
override fun nextScheduledActivity(thisStateRef: StateRef, flowLogicRefFactory: FlowLogicRefFactory): ScheduledActivity? {
if (status != Status.COMPLETED) {
return null
} else {
return ScheduledActivity(flowLogicRefFactory.create("com.example.flows.StartFlow", thisStateRef), scheduledTime)
}
}}
以下是StartFlow的定义:
@InitiatingFlow
@SchedulableFlow
class StartFlow(val ref: StateRef): FlowLogic<SignedTransaction?>(){
@Suspendable
override fun call(): SignedTransaction? {
val notary = serviceHub.networkMapCache.notaryIdentities[0]
val stateAndRef = serviceHub.loadState(stateRef = ref)
val state = stateAndRef.data as State
if (state.status != State.COMPLETED) {
throw IllegalArgumentException("Cannot initiate transfer of ownership")
}
// Role decider:
val parties = state.participants.sortedBy { it.owningKey.toBase58String() }
if (ourIdentity == parties[0]) {
val tx = TransactionBuilder(notary = notary)
// add input states
tx.addInputState(stateAndRef)
// add output states
tx.addOutputState(state.copy(status = Status.TRANSFERRED), ContractA.CONTRACT_ID)
val command = Command(ContractA.Commands.Command1(), listOf(state.a.owningKey, state.b.owningKey))
tx.addCommand(command)
tx.verify(serviceHub)
val partSignedTx = serviceHub.signInitialTransaction(tx)
val counterparty = serviceHub.identityService.wellKnownPartyFromAnonymous(parties[1]) ?: throw IllegalStateException("Cannot resolve responding party")
val counterPartySession = initiateFlow(counterparty)
val fullySignedTx = subFlow(CollectSignaturesFlow(partSignedTx, setOf(counterPartySession)))
return subFlow(FinalityFlow(fullySignedTx))
}
return null
}
}
在测试上述流程时,按如下方式创建State对象:
State(a, b, Instant.now().plus(10), Status.COMPLETED)
StartFlow无法启动与对方的会话,并且代码卡在那里。
如果国家构造为
State(a, b, Instant.now(), Status.COMPLETED)
StartFlow能够发起与交易对手的会话,并且一切正常。
这里可能是什么问题?
答案 0 :(得分:1)
正如Nitesh报告的那样,条件检查对于决定角色不正确。 ourIdentity == parties[0]
应该是ourIdentity.owingkey == parties[0].owningkey
。
这是因为parties[0]
的类型为AbstractParty
。 ourIdentity
的类型为Party
。 AbstractParty
将覆盖equals
,如下所示:
override fun equals(other: Any?): Boolean =
other === this ||
other is AbstractParty && other.owningKey == owningKey
因此这两个对象不相等。