Skip to content

Instantly share code, notes, and snippets.

@ashutoshmeher-r3
ashutoshmeher-r3 / ExplicitUpgrade.java
Last active August 1, 2019 07:29
Corda Explicit Upgrade Contract
public class SampleContractV2 implements UpgradedContract<SampleStateV1, SampleStateV2> {
// This is used to identify our contract when building a transaction.
public static final String ID = "corda.samples.upgrades.contracts.v2.SampleContractV2";
@Override
public void verify(LedgerTransaction tx) {
// Verify logic goes here
}
@NotNull
@NotNull
@Override
public AttachmentConstraint getLegacyContractConstraint() {
// Return the HashAttachment Constraint of the previous jar. Calculate the SHA256 hash of the jar file.
return new HashAttachmentConstraint(
SecureHash.parse("92F25AD232C099F6BD309B0E99E9C7DF6C62A59C8C8CEA96920E23338D1212E8")
);
}
public class SampleStateV2 implements ContractState {
private final String field1;
private final String field2;
// New field introduced in V2
private final String field3;
// New fields should be Nullable, so that older states issued with previous could be transacted with the newer version.
public SampleStateV2(String field1, String field2, @Nullable String field3) {
//Sample Flow version 2
@InitiatingFlow(version = 2)
@StartableByRPC
public class SampleFlow extends FlowLogic<SignedTransaction>{
private final Party counterparty;
private final String someData;
public SampleFlow(Party counterparty , String someData) {
this.counterparty = counterparty;
public class InsuranceSchemaV1 extends MappedSchema {
public InsuranceSchemaV1() {
super(InsuranceSchemaFamily.class, 1, ImmutableList.of(PersistentInsurance.class));
}
}
@BelongsToContract(InsuranceContract.class)
public class Insurance implements QueryableState {
private final String policyNumber;
private final long insuredValue;
private final int duration;
private final int premium;
private final Party insurer;
private final Party insuree;
@NotNull
@Override
public PersistentState generateMappedObject(@NotNull MappedSchema schema) {
// Check if the schema is supported by the state
if(schema instanceof InsuranceSchemaV1){
return new PersistentInsurance(
this.policyNumber,
this.insuredValue,
this.duration,
this.premium,
@Entity
@Table(name = "INSURANCE_DETAIL")
public class PersistentInsurance extends PersistentState implements Serializable {
@Column private final String policyNumber;
@Column private final Long insuredValue;
@Column private final Integer duration;
@Column private final Integer premium;
public PersistentInsurance() {
@CordaSerializable
public class VehicleDetail {
private final String registrationNumber;
private final String chasisNumber;
private final String make;
private final String model;
private final String variant;
private final String color;
private final String fuelType;
@Entity
@Table(name = "VEHICLE_DETAIL")
public class PersistentVehicle {
@Id private final UUID id;
@Column private final String registrationNumber;
@Column private final String chasisNumber;
@Column private final String make;
@Column private final String model;
@Column private final String variant;