Skip to content

feat(scale): CompactEnum + JAM compact codec for >256-variant enums#184

Closed
pgherveou wants to merge 1 commit into
mainfrom
feat/compact-u16-discriminant
Closed

feat(scale): CompactEnum + JAM compact codec for >256-variant enums#184
pgherveou wants to merge 1 commit into
mainfrom
feat/compact-u16-discriminant

Conversation

@pgherveou

@pgherveou pgherveou commented May 27, 2026

Copy link
Copy Markdown
Collaborator

Summary

The host-api MessagePayload enum is built with scale-ts Enum, 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 standard Enum.

Design

Two distinct codecs rather than overloading Enum:

codec discriminant matches jam-codec? cap
Enum u8 yes 256 (now enforced)
CompactEnum JAM compact only for index 0..=127 none
  • compact (new) — the JAM general-natural-number codec (see jam-codec's Compact). Byte-identical to a u8 for values 0..=127, spilling into more bytes above that.
  • CompactEnum (new) — tagged-union codec that encodes the discriminant with compact, lifting the 256 cap. Used only by the MessagePayload envelope.
  • Enum stays the u8 codec and now rejects >256 variants at construction, mirroring jam-codec's compile-time guard.

TODO before un-drafting

  • Update docs/design/host-api-protocol.md to document the compact discriminant on the message envelope.
  • Confirm whether any deployed peer relies on the u8 encoding of indices 128/129.

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.
@pgherveou pgherveou closed this May 27, 2026
@pgherveou pgherveou changed the title feat(host-api): encode protocol enum discriminant as Compact<u16> feat(scale): CompactEnum + JAM compact codec for >256-variant enums May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant