【问题标题】:In Corda, how can one party share with another an existing state in their vault?在 Corda 中,一方如何与另一方共享其保险库中的现有状态?
【发布时间】:2018-11-16 21:45:45
【问题描述】:

假设有两个节点,Alice 和 Bob。爱丽丝有一个她想让鲍勃知道的状态。 Alice 如何将状态发送给 Bob 并让他将其存储在他的保险库中?

【问题讨论】:

    标签: corda


    【解决方案1】:

    您将需要一个发起者和一个响应者流:

    • 发起方将从其保管库中检索状态,检索创建此状态的交易,并将交易发送给交易对手进行记录
    • 响应者将记录交易,存储其所有状态

    发起者流程

    @InitiatingFlow
    @StartableByRPC
    public class Initiator extends FlowLogic<Void> {
        private final UUID stateId;
        private final Party otherParty;
    
        private final ProgressTracker progressTracker = new ProgressTracker();
    
        public Initiator(UUID stateId, Party otherParty) {
            this.stateId = stateId;
            this.otherParty = otherParty;
        }
    
        @Override
        public ProgressTracker getProgressTracker() {
            return progressTracker;
        }
    
        @Suspendable
        @Override
        public Void call() throws FlowException {
            // Find the correct state.
            LinearStateQueryCriteria criteria = new LinearStateQueryCriteria(null, Collections.singletonList(stateId));
            Vault.Page<IOUState> queryResults = getServiceHub().getVaultService().queryBy(IOUState.class, criteria);
            if (queryResults.getStates().size() != 1)
                throw new IllegalStateException("Not exactly one match for the provided ID.");
            StateAndRef<IOUState> stateAndRef = queryResults.getStates().get(0);
    
            // Find the transaction that created this state.
            SecureHash creatingTransactionHash = stateAndRef.getRef().getTxhash();
            SignedTransaction creatingTransaction = getServiceHub().getValidatedTransactions().getTransaction(creatingTransactionHash);
    
            // Send the transaction to the counterparty.
            final FlowSession counterpartySession = initiateFlow(otherParty);
            subFlow(new SendTransactionFlow(counterpartySession, creatingTransaction));
    
            return null;
        }
    }
    

    响应者流程

    @InitiatedBy(Initiator.class)
    public class Responder extends FlowLogic<Void> {
        private final FlowSession counterpartySession;
    
        public Responder(FlowSession counterpartySession) {
            this.counterpartySession = counterpartySession;
        }
    
        @Suspendable
        @Override
        public Void call() throws FlowException {
            // Receive the transaction and store all its states.
            // If we don't pass `ALL_VISIBLE`, only the states for which the node is one of the `participants` will be stored.
            subFlow(new ReceiveTransactionFlow(counterpartySession, true, StatesToRecord.ALL_VISIBLE));
    
            return null;
        }
    }
    

    【讨论】:

    • 除了 Bob 之外 Alice 和其他参与者参与的那些 txn 呢?从上面的代码中,我可以看到 bob 会得到这些 txn 信息,这在某些情况下似乎不太好。
    • 理论上,您可以只发送状态,但 Bob 必须相信状态是有效的,并且无法将其存储在他的保险库中。未来,新交所可能会阻止节点看到与他们无关的部分交易。
    猜你喜欢
    • 2021-09-30
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 2020-12-19
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多