Skip to content

Lock persisted enum variant indices via custom serde impls - #2026

Closed
tylerhawkes wants to merge 2 commits into
openmls:mainfrom
xmtp:refactor-serde-variant-impls
Closed

tylerhawkes wants to merge 2 commits into
openmls:mainfrom
xmtp:refactor-serde-variant-impls

Conversation

@tylerhawkes

Copy link
Copy Markdown
Contributor

After #2023 the five enum variant indices are pinned by source-file order
alone, which is fragile: serde's derive macro assigns indices via
iter().enumerate(), so a future PR that inserts a variant mid-enum
silently re-breaks bincode/postcard the same way 0.7.x → 0.8.x did.

This replaces the derived Serialize/Deserialize on CredentialType,
ExtensionType, Extension, ProposalType, and Proposal with
hand-written impls that pin each variant's variant_index as a numeric
literal at the call site.

Wire bytes are byte-identical to the previous positional encoding — the
same serialize_*_variant calls, just with the indices spelled out — and
the compiler's exhaustiveness check on the new Serialize match now
forces anyone adding a variant to make a deliberate choice about its
index.

Stacked on top of #2023; the diff will include those commits until #2023
lands, head commit is the only new content. Drop this one if the size
isn't worth the safety — #2023 stands without it.

Split from #2021.

`ProposalType`, `Proposal`, `Extension`, `ExtensionType`, and `CredentialType`
all gained variants since openmls-0.7.1 in positions that shifted the
serde variant index. Self-describing formats (JSON, CBOR) deserialize enum
variants by name and are unaffected, but non-self-describing formats like
bincode and postcard decode the variant tag as a positional `u32`. A
persisted `Proposal::Custom`, `Proposal::SelfRemove`, `Extension::Unknown`,
`ExtensionType::Unknown`, or `CredentialType::Other` written by openmls-0.7.x
deserializes as some other variant on the current main branch, silently
corrupting state.

Restore the 0.7.x variant order and append all new variants (`Grease`,
`AppEphemeral`, `AppDataUpdate`, `AppDataDictionary`) at the end of each
enum. For the removed `AppAck` variant on `Proposal` and `ProposalType`,
introduce a hidden `_AppAck` placeholder at index 7 (with
`#[serde(rename = "AppAck")]` to preserve the JSON variant name) so
subsequent variant indices stay stable. `AppAck` was never produced by
the library so no real persisted data round-trips through this variant.

Also extends the variant-order comment on the four enums with
feature-gated variants (`ProposalType`, `Proposal`, `Extension`,
`ExtensionType`) with a note that new non-optional variants must not be
added after a `#[cfg(feature = ...)]` variant — toggling the feature
would otherwise shift every following index for non-self-describing
formats.

Match arms in `messages/codec.rs`, `messages/proposals_in.rs`, and
`group/mls_group/proposal_store.rs` are updated to cover the new
`_AppAck` variant (`unreachable!()`, since the placeholder is never
produced at runtime).
The five persisted enums fixed by the previous commit (`CredentialType`,
`ExtensionType`, `Extension`, `ProposalType`, `Proposal`) currently rely on
the *declaration order* in source to determine their serde `variant_index`,
because serde's derive macro uses `iter().enumerate()` to assign indices
positionally — Rust's explicit discriminant syntax (`Basic = 1`) is ignored
by the derive.

That convention is fragile: a future PR that adds a variant in the middle of
one of these enums silently shifts every following index and reintroduces
the bincode/postcard wire-format corruption this branch just fixed. The
documentation comment helps, but a one-line review miss is all it takes.

Replace the derived `Serialize`/`Deserialize` impls with hand-written ones
that pin each variant to an explicit `variant_index` via
`serialize_unit_variant` / `serialize_newtype_variant` /
`serialize_tuple_variant`. The wire bytes are byte-identical to the previous
positional encoding — these impls produce the same calls the derive used to
emit, just with the indices spelled out as numeric literals at the call
site. The matching `Deserialize` impls use the standard
`deserialize_enum` + `EnumAccess` + variant-identifier visitor pattern,
accepting both u64 indices (for bincode/postcard) and variant name strings
(for JSON/CBOR).

The `_AppAck` placeholder on `Proposal` and `ProposalType` keeps its
historical `"AppAck"` JSON name via the explicit string passed to
`serialize_unit_variant`, replacing the previous `#[serde(rename = "AppAck")]`
attribute on the variant.

Future maintainers adding a variant to any of these enums must pick a new,
never-reused index and add it to both the `Serialize` match and the
variant-identifier visitor. The compiler's exhaustiveness check enforces
that omissions are caught.
@wysiwys

wysiwys commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Closing this PR as addressed by #2034

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants