[Proposal] Fixed-size Union Molecule Extension

As per title, I’m wondering: would be possible to extend the spec with a Fixed-size Union?

To be precise, a Fixed-size Union is exactly like a standard Dynamic-size Union in it’s encoding and decoding, just with the additional constraint that all the choices are of the same fixed size. This way all the generated encoding is always of the same length for any of the choices. This a very general constraint and it can be always satisfied by padding the shorter choices. As a result, this fixed-size Union can be embedded transparently into a Struct, which is a small achievement in itself.

Without this little change, there are things that are unnecessarily complicated. For example, UDT use the initial 16 bytes of Cell Data to encode a Uint128LE amount. To define a molecule schema encompassing all the Cell Data it must be a struct, but at that point all the fields that can be put in this struct must be of fixed size. Hence the idea of a Fixed-size Union.

Alternatives are possible, sure, just not as nice:

  • The practitioner needs to encode and decode by splitting into pieces.
  • The practitioner has no way to represent this Cell Data as a molecule schema.

The change required to support this extension is really small, but allows so much more to be built on top with ease, while being molecule compliant.

Feel free to track the related Github Issue: [Proposal] Fixed-size Union Extension · Issue #99 · nervosnetwork/molecule · GitHub

3 Likes