How to spend UTXO in corda - corda

I learned how to create output state by corda.
However, I can't find documentation about how to consume those created outputs.
How can I specify outputs I want to consume as inputs in Flow code?

Usually it's like this:
Your state is a LinearState that has some UUID attribute as its linearId.
The flow that wants to consume that state has a constructor that takes as an input parameter that linearId.
Inside the flow you query the vault of the node to get that state by its linearId; the query returns a StateAndRef of your state.
You add that StateAndRef as an input to your transaction.
Let's say you're trying to update that state, so you add to the transaction an output; that output has the same value for linearId; but you can modify any other attribute. So this way you mimic an update, because states in Corda are final; so to update one, you consume it, then you create an output that has the same linearId but different values for the remaining attributes. This allows you to track the history of that state by its linearId.
This example shows the above steps, you'll see that the flow constructor takes the Id of the proposal, queries the vault for that proposal; then updates its amount by creating a new proposal with the same Id but different amount.

Related

Corda: Update and consume ContractState in a single transaction

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?
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.

Are fungible states automatically merged on Corda?

I want to create a custom fungible asset on corda.. It is unclear to me how states that implement FungibleAsset are handled internally. Say I receive ten 1 dollar CoinStates from a transaction, are those merged to one 10 dollar Coinstate which I then use as the one and only input state if I wanted to make a payment myself?
basically I want something like the Cash.generateSpend which unfortunately is not very well documented
My approach so far for a transferFlow:
I have a function that does a vault query and should return some parties balance.. (this is assuming that the balance amount is internally merged to one state)
then using this balance as Input state I have a check if the input is equal the amount to pay in which case I will simply create one output state of the same amount but a new owner, otherwise I create another output state with the amount of change and myself as the owner
This is the function so far...
I have a function that does a vault query and should return some parties balance..
StateAndRef<CurrencyState> getBalaceOfIdentiy(AbstractParty id) throws FlowException {
QueryCriteria queryCriteria = new QueryCriteria.FungibleAssetQueryCriteria(null,ImmutableList.of(id),null, null,
null, null, null
);
// this assumes states are merged internally
List<StateAndRef<CurrencyState>> balanceOfID = getServiceHub().getVaultService().queryBy(CurrencyState.class, queryCriteria).getStates();
return balanceOfID.get(0);
}
Corda follows UTXO model. I'm not sure which version of Corda you are on but if you'll look closely enough you'll find that it would call OnLedgerAsset.generateSpend at some point of time and that does the job for you. and can be statically used as it is annotated with #JvmStatic.

Corda is it possible to issue and consume state in same flow

I am trying to do simple fund transfer from one account to another account using simple state. 2 Flows have been created one for issue of transfer request with cash transfer and flow created to just consumed that transaction. My question is, is it possible to transfer and consume state in one flow ?
As per my opinion transaction must be consumed after transfer but also want to show it on UI.
Corda RPCQuery allowed to bring information of unconsumed states only, if I consume above transaction, is there way to show consumed transactions last state?
CordaRPCOps allows you to query for unconsumed states, consumed states, or both. Here's a simple way of querying for all states:
val criteria = QueryCriteria.VaultQueryCriteria(Vault.StateStatus.ALL)
val results = proxy.vaultQueryBy<ContractState>(criteria)
To show the last consumed state, you could retrieve all the consumed states in descending order of consumption and grab the first one, as follows:
val criteria = QueryCriteria.VaultQueryCriteria(Vault.StateStatus.UNCONSUMED)
val sortColumn = Sort.SortColumn(SortAttribute.Standard(Sort.VaultStateAttribute.CONSUMED_TIME), Sort.Direction.DESC)
val sorting = Sort(listOf(sortColumn))
val results = proxy.vaultQueryBy<Obligation>(criteria, sorting = sorting)
val lastConsumedState = results.states[0]
From any flow, you need to call FinalityFlow, in order to notarize
and record the transaction in individual parties' vault. So I think
after the issuance (or transfer), you need to call FinalityFlow
first. Only then you can use the issued state as input for the new
transaction.
The notary is responsible for avoiding the double spend
of the input state in any transaction. So you can not use any newly
issued state (as input to new transaction), until the notary is aware
of it.
Thus I think in your case, you will need to call FinalityFlow twice, once after each transaction (i.e. issuance & consumption).

Corda IsRelevant() work around?

There were some changes in the API for 1.0 that had removed isRelevant(). What are the best workarounds for this?
Given a use case: If there are 100 parties that want to see this queryable state and all updates pertaining to it (but read-only and doesn't need to sign), do I need to add them all to the Participants list? The "observer" role doesn't exist yet? Is there an alsoInformed or something similar for the use case of just seeing static reference data?
The general function here is a shared linear queryable state where the issuer has the master to change/update which would propagate to all parties that want to "subscribe" to these changes. I believe this might work with a broadcast to a "club", but I don't think clubs exist yet or if they're dynamic groupings of the network map.
I'll go into a bit of background before answering... The concept of relevancy still exists in the platform. As you know, in Corda there are two data persistence stores; the storage service and the vault.
The storage service
The storage service is a key -> value store that persists data such as:
Serialised flow state machines
Attachments
Transactions
The storage service is great for storing large amounts of serialised data that can be indexed and retrieved by hash. However, it is awkward if one wishes to search for data within one of the stored objects. E.g. one cannot easily search for transaction output states of a specific type when using the transaction store. The approach would be to iterate through all transactions, deserialise them one by one, and filter by output type. It's cumbersome and not very efficient. This is why the vault and the concept of relevancy exists!
The vault
The vault exists to store state objects, as opposed to transactions. There is a master states table where the state reference, transaction id (that generated the output state) and some other meta data such as whether the state is consumed (or not), is stored. There's also a table for LinearStates and a table for OwnableStates. Also, if one wishes to add an ORM to their state, a database table is created for each type of state object reflecting the ORM definition. These properties can then be queried to pull out states from the vault that meet specific queries, e.g. "Any obligation states over £1000 with Alice as the lender that have not yet been consumed". That's the power of the vault!
Relevancy
Now, it is the case that not all transactions a node receives produce states that are relevant to that node. An example would be a payment vs payment transaction where Alice sends dollars to Bob and Bob sends pounds to Alice. As Bob now owns the dollars Alice previously owned, those dollars are now not relevant for Alice. As such, Alice shouldn't record the output state representing those dollars as she does not hold the right and obligations to those dollars. What Alice does do is to mark the old dollar state as consumed, thus it will now not count towards her total dollars balance and cannot be used as an input in another transaction (as it has already been spent).
How relevancy works in Corda
So, when a node receives a new transaction, it intersects the public keys defined in the participants property of each output state with all the public keys that the VaultService is aware of. If the resultant set fora particular state is not empty, then the state is relevant for the node. Simple.
What this means is that if a node receives a transaction where their public keys are not listed in an output states' participants field, then they will not store that output state in the vault. However, they will store the transaction in the transaction store, and it can still be queried.
The concept of relevancy for OwnableStates is simple, one either owns it or they don't. The concept for LinearStates that represent multi-lateral agreements is more complex. In versions M14 and below, one could override the functionality of isRelevant in a LinearState, however in V1 this has been removed in favour of an easier approach which just compares the participants keys to the vault keys (as described above).
Implications of the V1 approach to relevancy
As the OP notes, in V1, there will be the concept of transaction observers, where nodes that were not participants of a state can still store the state in their vault and query it as a "third party" state. I.e. it cannot be consumed or contribute to balance totals but it can be queried. In the meantime, we will have to work around the absence of that feature and the options are:
For LinearStates, add all intended observers to the participants list. Then, add an additional property to the state object called something like consumers that just contains the list of participants that can consume this state in a valid transaction, i.e. sign a transaction containing it. The contract for that state will then compare those public keys in the commands to those in the consumers list. This way all the observers will still store the state in their vaults. The FinalityFlow will broadcast this transaction to all participants. You can use randomly generated public keys if you don't want the observers to be known to other participants.
For OwnableStates, like Cash, there can only be one identity in participants, the owner. So the approach would be to use the FinalityFlow to send the transaction to a set of observers, then those observers would have to get the output states directly from the transaction. Cumbersome but temporary as we are working on transaction observers at this moment: https://r3-cev.atlassian.net/browse/CORDA-663.
Just a strawman of what I understood if this were to be in code.
i.e using obligation cordapp example
Obligation State
val consumers: List<AbstractParty> = listOf(lender, borrower)
override val participants: List<AbstractParty> get() = listOf(lender, borrower, extraActor)
Contract code verify
override fun verify(tx: LedgerTransaction){
val command = tx.commands.requireSingleCommand<Commands>()
when (command.value) {
is Obligation.Issue -> requireThat {
"The signers should only be the consumers for the obligation" using
(command.signers.toSet() == obligation.consumers.map { it.owningKey }.toSet())
}
Add Command specifying the signers need only be consumers during TX creation
val utx = TransactionBuilder(notary = notary)
.addOutputState(state, OBLIGATION_CONTRACT_ID)
.addCommand(Obligation.Issue(), state.consumers.map { it.owningKey })
.setTimeWindow(serviceHub.clock.instant(), 30.seconds)
In this way, the first tx allows the extraActor to commit the state into the ledger without signing. In a future tx proposal, the extraActor here can query its state table and propose a change of lifecycle in the state using a different command, whereby this time it may require all participants (if need be) to sign the command. i.e Obligation.DoSomethingExtra command with all participant signing (command.signers.toSet() == obligation.participants.map { it.owningKey }.toSet())

Best approach to get current state from ledger

Given that a list of states is shown on a web interface, and there's an "update" option on that interface (and consequently, I need to implement an amendment flow), what would be the best approach to get the StateAndRef of the current state?
Store the transaction ID from the create command and use it to get the current StateAndRef
By the linearID of the current state
It doesn't matter.
Thanks
Maka
For a LinearState, I'd recommend retrieving the latest version of the state you want to spend using its linearId:
val queryCriteria = QueryCriteria.LinearStateQueryCriteria(linearId = listOf(linearId))
val iouStateAndRef = serviceHub.vaultQueryService.queryBy<IOUState>(queryCriteria).states.single()
The first line creates a query object to retrieve the state by linearId. By default, queries only retrieve the latest version (the "head") of the state chain. The second line uses the query to retrieve the object from the vault.
See https://docs.corda.net/api-vault-query.html for details.

Resources