Corda - SignTransactionFlow 和 sendAndReceive

Corda - SignTransactionFlow and sendAndReceive

我有两方(A 和 B)。 A 使用 SignTransactionFlow 向 B 发送一个 Corda 状态,因此也获得了该交易的交易对手签名。

是否可以在不使用 SignTransactionFlow 而使用 sendAndReceive 调用的情况下共享 Corda 状态? 如果是这样,通过 sendAndReceive 接收 Corda 状态的交易对手是否能够使用该状态?

是的,可以使用 send()receive() 发送状态。这是我测试过的代码示例,看它是否有效:

class TestContract : Contract{
    companion object{
        @JvmStatic
        val ID = "package net.corda.sample.TestContract"
    }
    override fun verify(tx: LedgerTransaction) {

    }
}
@BelongsToContract(TestContract::class)
class TestState(val owner : Party, val value : String) : ContractState {
    override val participants: List<AbstractParty>
        get() = listOf(owner)
}


@InitiatingFlow
@StartableByRPC
class receiveStateFlow(private val counterparty: Party) : FlowLogic<Unit>() {
    override val progressTracker = ProgressTracker()
    val log = loggerFor<receiveStateFlow>()

    @Suspendable
    override fun call() {
        val counterpartySession = initiateFlow(counterparty)
        val counterpartyData = counterpartySession.sendAndReceive<TestState>("hello")
        counterpartyData.unwrap { msg ->
            log.warn(msg.value)
            assert((msg.participants.first()) == counterparty)
        }
    }
}

@InitiatedBy(receiveStateFlow::class)
class sendStateFlow(private val counterpartySession: FlowSession) : FlowLogic<Unit>() {
    @Suspendable
    override fun call() {
        val counterpartyData = counterpartySession.receive<String>()
        counterpartyData.unwrap { msg ->
            assert(msg == "hello")
        }
        val newState = TestState(serviceHub.myInfo.legalIdentities.first(), "someValue")
        counterpartySession.send(newState)
    }
}

但是,此方法不会消耗任何状态。这只有在您将这些状态用作 Transaction 的输出时才会发生。因此,接收状态的一方应该使用这些确切的状态作为同一流程中 TransactionBuilder 的输出,并继续签署和完成交易。