Finality Flow不向extraRecipients发送事务

时间:2018-03-23 16:23:59

标签: corda

  • Corda 2.0
  • JDK 1.8.0_162

我正在尝试调试FinalityFlow中的不一致行为。与Mock和Real节点中的不同结果不一致。

真节点上的程序

我正在尝试通过其中一个替代FinalityFlow构造函数将事务发送到另一个节点:

constructor(transaction: SignedTransaction, extraParticipants: Set<Party>) : this(transaction, extraParticipants, tracker())

我通过RPC与我的节点通信。该过程首先通过其名称检索其他节点的Party,例如。 O=PartyA,L=London,C=GB

val extraRecipientParties = myExtraRecipientsStringList.map { rpcOps.wellKnownPartyFromX500Name(CordaX500Name.build(X500Principal(it)))!! }

然后,rpcOps调用负责创建状态的流程:

val flow = rpcOps.startFlow(::CreateStateFlow, other, arguments, extraRecipientParties)
val result = flow.returnValue.getOrThrow()
val newState = result.tx.outRef<MyStateClass>(0)

CreateStateFlow 非常标准:

@StartableByRPC
class CreateStateFlow(
        val s: String,
        val p: String,
        val o: String,
        val extraParticipants: List<Party>
) : FlowLogic<SignedTransaction>() {

    constructor(s: String, p: String, o: String): this(s, p, o, emptyList())


    @Suspendable
    override fun call() : SignedTransaction {
        val notary = serviceHub.networkMapCache.notaryIdentities.first()

        val newState = MyStateClass(ourIdentity, s, p, o, extraRecipients=extraParticipants)
        val command = Command(TripleContract.Create(), listOf(ourIdentity.owningKey))
        val outputState = StateAndContract(newState, TripleContract.CONTRACT_REF)

        val utx = TransactionBuilder(notary=notary).withItems(
                command,
                outputState
        )

        val stx = serviceHub.signInitialTransaction(builder=utx, signingPubKeys=listOf(ourIdentity.owningKey))

        if (newState.extraRecipients.isEmpty()) {
            return subFlow(FinalityFlow(stx))
        }

        return subFlow(FinalityFlow(stx, newState.extraRecipients.toSet() ))

    }
}

我的期望是,现在,在extraRecipients变量中各方拥有的任何节点上,我应该能够通过查询保险库找到newState。 实际上,当我在Mock节点上测试它时,情况确实如此,但是当rpc调用时

rpcOps.vaultQueryBy<MyStateClass>().states --> returns an empty list

对模拟节点进行测试

@Test
fun `FinalityFlow used to federate a transaction`(){
    val partyAString = node1.info.legalIdentities.first().name.toString()
    val aStringX500Name = CordaX500Name.build(X500Principal(partyAString))
    val node2FindPartyA = node2.rpcOps.wellKnownPartyFromX500Name(aStringX500Name)!!
    assert(node1.info.legalIdentities.contains(node2FindPartyA))

    val executingFlow = node2.start(CreateStateFlow("fo", "boo", "bar", listOf(node2FindPartyA)))
    val flowResult = executingFlow.getOrThrow()
    val stateInNode2 = flowResult.tx.outRef<MyStateClass>(0)
    val stateInNode1 = node1.database.transaction {
        node1.services.loadState(stateInNode2.ref)
    }
    assert(stateInNode1.data == stateInNode2.state.data)

编辑: 的 MyStateClass.kt

data class MyStateClass(
        val owner: Party,
        val s: String,
        val p: String,
        val o: String,
        val extraRecipients: List<Party>,
        val lastEditor: AbstractParty = owner,
        override val participants: List<AbstractParty> = listOf(owner),
        override val linearId: UniqueIdentifier = UniqueIdentifier()
) : LinearState, QueryableState {

    object MyStateSchemaV1 : MappedSchema(MyStateClass::class.java, 1, listOf(MyStateEntity::class.java)) {
        @Entity
        @Table(name = "my-state")
        class MyStateEntity(state: MyStateClass) : PersistentState() {

            @Column @Lob
            var owner: ByteArray = state.owner.owningKey.encoded

            @Column
            var s: String = state.s

            @Column
            var p: String = state.p

            @Column
            var o: String = state.o

            @Column @ElementCollection
            var extra_recipients: Set<ByteArray> = state.extraRecipients.map { it.owningKey.encoded }.toSet()

            @Column @ElementCollection
            var participants: Set<ByteArray> = state.participants.map { it.owningKey.encoded }.toSet()

            @Column @Lob
            var last_editor: ByteArray = state.owner.owningKey.encoded

            @Column
            var linear_id: String = state.linearId.id.toString()
        }

    }

    override fun supportedSchemas(): Iterable<MappedSchema> = listOf(MyStateSchemaV1)
    override fun generateMappedObject(schema: MappedSchema): PersistentState = MyStateSchemaV1.MyStateEntity(this)

}

1 个答案:

答案 0 :(得分:3)

虽然您引入了一个新变量val extraRecipients: List<Party>,但您的参与者仅在所有者身上override val participants: List<AbstractParty> = listOf(owner),因此只有所有者方才能在保险库中拥有该状态。

FinalityFlow中的extraRecipients不会将状态存储在Vault中(状态存储),但它们会将已公证的事务的副本存储在事务存储中。

loadState函数的定义是Given a [StateRef] loads the referenced transaction and looks up the specified output [ContractState].因为节点1在最终流程中被添加为事务的额外接收者(将其视为电子邮件的cc-ed收件人),当被问到时到loadState,它能够从事务存储中推断出状态,因为它包含输入,命令,输出等。所以在这里你已经证明交易是在{{1}期间发送给其他方的。 }。

FinalityFlow上,它实际上是从节点状态库查询状态 - 而不是事务存储,因此返回一个空列表。

如果您希望extraRecipients存储州,则需要将其添加到州的rpcOps.vaultQueryBy<MyStateClass>().states字段中,或使用participants概念here