feat(scale): CompactEnum + JAM compact codec for >256-variant enums#184
Closed
pgherveou wants to merge 1 commit into
Closed
feat(scale): CompactEnum + JAM compact codec for >256-variant enums#184pgherveou wants to merge 1 commit into
pgherveou wants to merge 1 commit into
Conversation
The MessagePayload enum used scale-ts `Enum`, which writes the variant discriminant as a single byte, capping the protocol at 256 variants. We are at 130 and growing. Add `CompactEnum`, a drop-in replacement that encodes the discriminant as a SCALE-compact integer, lifting the cap into the u16 range while keeping single-byte tags for indices 0..=63. BREAKING: not wire-compatible with the u8 form. Index 0 still encodes as [0], but every other index differs (e.g. 76 -> [49, 1]), so both ends of the protocol must run this version.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The host-api
MessagePayloadenum is built with scale-tsEnum, whose discriminant is a single byte — capping the protocol at 256 variants (we're at 130, and indices 128/129 are already in use). This adds a compact-discriminant path without changing the meaning of the standardEnum.Design
Two distinct codecs rather than overloading
Enum:Enumu8CompactEnumcompactcompact(new) — the JAM general-natural-number codec (see jam-codec'sCompact). Byte-identical to au8for values0..=127, spilling into more bytes above that.CompactEnum(new) — tagged-union codec that encodes the discriminant withcompact, lifting the 256 cap. Used only by theMessagePayloadenvelope.Enumstays theu8codec and now rejects >256 variants at construction, mirroring jam-codec's compile-time guard.TODO before un-drafting
docs/design/host-api-protocol.mdto document the compact discriminant on the message envelope.u8encoding of indices 128/129.