2

Say I have instances of this enum stored on-chain:

enum Enum {
    V0,
    V1,
}

And suppose now that I change the enum in some way:

enum Enum {
    V0,
    V2,
    V1,
}

This is what the substrate docs say about the encoding of enums:

A fixed number of variants, each mutually exclusive and potentially implying a further value or series of values. Encoded as the first byte identifying the index of the variant that the value is. Any further bytes are used to encode any data that the variant implies. Thus, no more than 256 variants are supported.

So I would expect that a storage migration is required almost everytime the enum is changed (there are exceptions like adding a variant to the end of the enum under certain circumstances). Otherwise, entries which previously are Enum::V1 are now Enum::V2. Is this correct?

If so, does this also pertain to the Error and Event enums, or are these special cases?

1 Answer 1

2

A storage migration is needed whenever the encoded representation of the storage changes.

In the case of an enum, it is represented as one byte.

In the original enum:

Enum::V0 == 0x00
Enum::V1 == 0x01

In the new enum:

Enum::V0 == 0x00
Enum::V2 == 0x01
Enum::V1 == 0x02

So in this case, yes, you would need to write a migration which would update all 0x01 to 0x02. But if you updated your enum to:

enum Enum {
    V0,
    V1,
    V2,
}

Then you wouldn't need to run a migration since V0 and V1 still encode the same.

Basically, your blockchain storage it just bytes. If your data structure can parse those bytes correctly, you don't need a migration. If your data structure changes what the bytes means, then yes you need a migration.

Error and Event enums are not normally placed in storage, so generally no, you do not need to write storage migrations. If they are put in storage, then yes you will, but then I will also suggest you don't put them in storage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.