aNDy
aNDy

Reputation: 5

Corda - Persistence State V/S Linear State

We have around 20 states that are related with one-one or one-many or many-many relationship. Some of the states are independent in nature. How do we select b/w Persistence state and Linear State for those states. Is there any guideline available. Any pointer will be helpful.

Thanks.

Upvotes: 0

Views: 441

Answers (2)

Adel Rustum
Adel Rustum

Reputation: 2548

  1. LinearState implements ContractState.

  2. Use LinearState when your state will evolve over time. As you know states in Corda are final, so to mimic an update of a state, you use LinearState which has a linearId, to update your state, you craft a transaction where the input is the state that you want to update, and the output is a new state which has the same linearId as the input but different values for the remaining attributes (i.e. the updated values). This way you can track the evolution of a stat by querying its linearId:

    // Query for all linear states associated with a linear ID:
    val linearStateCriteria = LinearStateQueryCriteria(linearId = listOf(linearId), 
    status = Vault.StateStatus.ALL)
    val vaultCriteria = VaultQueryCriteria(status = Vault.StateStatus.ALL)
    val results = vaultService.queryBy<LinearState>(linearStateCriteria and vaultCriteria)
    
  3. Since you have relations between your states, you might want to use state pointers; as the name suggests it's a pointer to a state, there are 2 types, fixed and linear pointers; the former is for states that don't evolve, the latter is for states that evolve so the pointer will always point to the latest version of the state. More on that in the below reference links:

Upvotes: 3

Peter Li
Peter Li

Reputation: 1032

Add on to what Adel said, based on the information you provided, it seems that you should look into QueryableState.

PersistenceState is a superclass for all mapped states exported to a schema that ensures the [StateRef] appears on the database row. (inside of the QueryableState interface. You can see it by Command + click on the QueryableState in IntelliJ)

And the point of QueryableState is to store everything in the database via your customized schemas.

So it seems you are dealing with a lot of relationships, it would up to you if you want to store them into database via schema or just store them in states. I don't see how you would signify these relationships via LinearStates.

Upvotes: 2

Related Questions