Warm tip: This article is reproduced from serverfault.com, please click

Corda: Update and consume ContractState in a single transaction

发布于 2020-12-21 13:17:04

Problem

Consider the following design problem in Corda. Suppose that I have a ContractState, say Order, which carries an explicit status, for instance its either ALIVE or CANCELED. The latter status relates to finished orders. I would like to have a transaction which takes a single, ALIVE input state Order and consumes it while changing its status to CANCELED. Can I have an atomic transition accomplishing this task? In other words, is it possible to record the reason why a state was consumed?

Questioner
Maciej Bendkowski
Viewed
0
Adel Rustum 2020-12-21 23:09:46
  • Anytime a state is added as an input to a transaction, it is considered consumed.
  • Let's say Order has 3 attributes: Order(linearId, status, reason).
  • To mimic updates in Corda, your transaction will take as an input the state to be updated; and produce a new state that has the same linearId but with different values for the remaining attributes. This way all states in your vault that have the same linearId are considered different versions of the same state, there will be only one UNCONSUMED version; which is the latest version.
  • Order(123, ALIVE, null) ---Update Tx---> Order(123, CANCELED, "No longer needed").
  • You might also consider not creating an output if the state is "canceled"; you just consume it and not create an output; meaning it no longer exists on the ledger and Corda doesn't track it: Order(123, ALIVE, null) ---Cancel Tx---> no output.