如何在Corda中实现原子多方交易? [英] How to achieve atomic multi-party transactions in Corda?
问题描述
比方说,我有 Parties A,B和Z
.并在A和Z之间指定 S1
,在B和Z之间指定 S2
.A不是S2的特权,B也不是S1的特权.
Let's say I have parties A, B, and Z
.
And states S1 between A and Z
, and S2 between B and Z
. A isn't privy to S2 nor B to S1.
我希望Z在原子上修改S1和S2,以便在进行事务处理"时,A和B都无法分别对S1或S2进行更新.
I want Z to modify S1 and S2 atomically, such that while the 'transaction' is in progress, neither A nor B can make updates to S1 or S2, respectively.
到目前为止,这个想法是:Z将首先在S1和S2上获得一个软锁,然后启动流程以修改A以S1为对手,以B为S2来修改S1.然后,释放S1和S2上的软锁.
So far the idea is: Z will first acquire a soft-lock on S1 and S2, and then launch the flows to modify S1 with A as the counterparty, and S2 with B as the counterparty. Then, release the soft-lock on S1 and S2.
我为状态写合同,以便修改"命令要求Z签名.
I write the contract for the state such that the Modify command requires Z to sign.
至关重要的是,如果A或B尝试修改S1或S2,如果Z在进行第一次交易时Z接收到该修改的接受者流,它将拒绝签名.但是我是否必须实施呢?仍然无法通过获取S1和S2上的软锁来阻止接受者流对S1和S2进行任何操作吗?
Crucially, if A or B attempts to modify S1 or S2, if Z receives the acceptor-flow for that modification while Z's first transaction is going on, it would just refuse to sign. But would I have to implement that at all? Wouldn't the acquisition of the soft-locks on S1 and S2 prevent the acceptor-flow from doing anything with S1 and S2 anyway?
还是有更好的方法?
推荐答案
在其他答案的基础上,这是一种使用软锁API防止节点一次进入涉及两种状态的事务的方法:
Building on the other answers, here's one way of using the soft-locking API to prevent a node from entering into transactions involving both states at once:
@InitiatingFlow
@StartableByRPC
class AtomicFlow(val stateRefToSpend: StateRef, val stateRefToLock: StateRef) : FlowLogic<Unit>() {
@Suspendable
override fun call() {
try {
serviceHub.vaultService.softLockReserve(runId.uuid, NonEmptySet.of(stateRefToSpend, stateRefToLock))
} catch (e: StatesNotAvailableException) {
logger.error("A transaction is already underway using S2.")
return
}
val stateToSpend = serviceHub.loadState(stateRefToSpend)
// Continue with transaction...
}
}
这篇关于如何在Corda中实现原子多方交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!