【问题标题】:Marking a state as consumed without changing its contents in Corda在 Corda 中将状态标记为已使用而不更改其内容
【发布时间】:2018-09-17 06:27:56
【问题描述】:

我正在研究一个简单的用例,在该用例中,我需要将状态作为事务中的输入并生成新的输出状态。但我希望状态的内容是一样的。我只想将输入状态标记为已使用并生成具有相同内容的新输出状态。我正在编写的 Cordapp 是用 Java 编写的。

如何在 Corda 中做到这一点?

【问题讨论】:

    标签: java blockchain rpc corda


    【解决方案1】:

    为此,您需要执行三个步骤:

    1. 检索您要使用的输入状态
    2. 创建一个作为输入状态副本的输出状态
    3. 将它们都添加到事务构建器中

    这是一个代表义务的状态的示例:

    // Retrieve the state using its linear ID.
    QueryCriteria queryCriteria = new QueryCriteria.LinearStateQueryCriteria(
            null,
            ImmutableList.of(linearId),
            Vault.StateStatus.UNCONSUMED,
            null);
    
    List<StateAndRef<Obligation>> obligations = getServiceHub().getVaultService().queryBy(Obligation.class, queryCriteria).getStates();
    if (obligations.size() != 1) {
        throw new FlowException(String.format("Obligation with id %s not found.", linearId));
    }
    StateAndRef<Obligation> inputStateAndRef = obligations.get(0);
    Obligation input = inputStateAndRef.getState().getData();
    
    // Create the new output state.
    Obligation output = new Obligation(input.getAmount(), input.getLender(), input.getBorrower(), input.getPaid(), input.getLinearId());
    
    // Creating the transaction builder (don't forget to add a command!)
    final TransactionBuilder builder = new TransactionBuilder(notary)
            .addInputState(inputStateAndRef)
            .addOutputState(output, OBLIGATION_CONTRACT_ID);
    

    【讨论】:

    • 下面是否还有一些元数据发生了变化,表明状态发生了变化?比如时间戳或某种参考?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多