Hayk Kerobyan
Hayk Kerobyan

Reputation: 87

Non-backwards compatible state upgrade in Corda using SignatureConstraint

I want to perform a non-backwards compatible state upgrade using SignatureConstraint. If it were a backwards compatible change, for example adding a property, I'd just added a nullable property in the state and that would work. However I don't have any idea how should I act in the following scenarios:

Scenario1: A new non-null field is added to the state

Scenario2: A field was removed from state

Scenario3: A field was modified in the state. E.g. a field of type Date transformed into an object which contains that date and some other fields.

Scenario4: A field in the state was renamed.

The problem is that explicit upgrade does not support SignatureConstraint and I get the following error message Legacy contract does not satisfy the upgraded contract's constraint, so I need to find a solution for implicity upgrade.

Upvotes: 0

Views: 97

Answers (2)

Hayk Kerobyan
Hayk Kerobyan

Reputation: 87

As a workaround I made the incompatible change to become a compatible one. Here is how it works.

I've created a state which has propertiesV1 object. This object includes all the fields that CompanyState should.

@CordaSerializable
@BelongsToContract(CompanyContract::class)
data class CompanyState(
        override val linearId: UniqueIdentifier,
        val propertiesV1: CompanyV1?
) : LinearState

Now when I need to make an incompatible change in the properties, I just add another version of the object to the state.

@CordaSerializable
@BelongsToContract(CompanyContract::class)
data class CompanyState(
        override val linearId: UniqueIdentifier,
        val propertiesV1: CompanyV1?,
        val propertiesV2: CompanyV2?
) : LinearState

Neither contract, nor flows are changed. They are just being updated to handle propertiesV2 field.

Upvotes: 0

Ashutosh Meher
Ashutosh Meher

Reputation: 1841

ContractUpgradeFlow doesn't support the upgrade of state with SignatureConstraint. However, the flexibility of Signature Constraint allows you to add any CorDapps as long as it's signed by the same key. You could easily write a simple flow to mimic an ExplicitUpgrade for the scenario you mentioned.

Here is what you could do:

  • Add both the corDapps jar files (old and updated) in the nodes cordapps folder.
  • Write another cordapp with a flow that consumes your existing state and outputs the new state (the upgraded one).
  • Add this flow jar to the nodes cordapps folder.
  • Execute the new flow to consume the older states and output the upgraded state.

Points to Note:

  • Make sure to have the correct set of signers to avoid incorrect spending of the states.
  • This is just an overall idea. The actual way of doing this might get a little complicated depending on the contract rules for your Exit transaction of the state.
  • I would rather add a new upgrade command to cater to this scenario.

You could have got the overall idea and do the tweaking at your end to perform the upgrade of your usecase. Hope this helps!

Upvotes: 1

Related Questions