将状态标记为已使用而不更改其在Corda中的内容 [英] Marking a state as consumed without changing its contents in Corda
本文介绍了将状态标记为已使用而不更改其在Corda中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究一个简单的用例,其中需要将状态作为事务中的输入并生成新的输出状态.但是我希望状态的内容是相同的.我只想将输入状态标记为已消耗,并生成具有相同内容的新输出状态.我正在编写的Cordapp使用Java.
I am working on a simple use case in which I need to take a state as an input in a transaction and generate a new output state. But I want the contents of the state to be the same. I just want to mark the input state as consumed and generate a new output state with the same contents. The Cordapp I am writing is in Java.
我该如何在Corda中做到这一点?
How can i do that in Corda?
推荐答案
为此,您需要执行三个步骤:
To do this, you need to perform three steps:
- 检索要使用的输入状态
- 将输出状态设为输入状态的副本
- 将它们都添加到交易生成器中
这是在代表义务的状态下执行此操作的示例:
Here's an example of doing this for a state representing an obligation:
// 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);
这篇关于将状态标记为已使用而不更改其在Corda中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文