paritytech/parity-scale-codec

Marker trait `ConstEncodedLen`

Closed this issue · 3 comments

As companion for the MaxEncodedLen trait i propose a ConstEncodedLen marker that should be applied to all types with constant encoded length. Definition:

  • For all instances I of type T must I.encoded_len() be equal to T::max_encoded_len().

Eg. this holds for all integer types, but not for compact types and enums.

Why is this needed?

To guarantee that in-place memory operations are possible for a type. For example in the MQ pallet we have the ItemHeader type that assumes to be ConstEncodedLen without a way to express it. Now if someone puts Compact<u32> for the Size type to conserve memory, it will inevitably corrupt the storage when attempting to do an in-place storage operation.

PS: Maybe we can also extend the MaxEncodedLen trait with an optional generic? Like MaxEncodedLen<Len=ConstLen> or whatever.

bkchr commented

The MaxEncodedLen trait is defined in parity-scale-codec create.

Ups, I moved it.

Do you think this generally makes sense?

bkchr commented

I don't really get why you need it, but I can see the benefit of this trait. I mean it shouldn't "harm" that much by having it.