Skip to content

Add single-byte account discriminators via a SettlementAccount enum#64

Open
kaze-cow wants to merge 4 commits into
le-encoding-fixesfrom
discriminators
Open

Add single-byte account discriminators via a SettlementAccount enum#64
kaze-cow wants to merge 4 commits into
le-encoding-fixesfrom
discriminators

Conversation

@kaze-cow

@kaze-cow kaze-cow commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Prefixes every account this program owns with real storage with a 1-byte discriminator, mirroring how SettlementInstruction already identifies instructions.
  • New SettlementAccount enum (interface/src/lib.rs), following the same num_enum::TryFromPrimitive pattern as SettlementInstruction. Discriminators start at 128 and increment per account type, kept visually/numerically distinct from the instruction discriminators (0-4).
    • OrderAccount: discriminator 128, account grows 199 -> 200 bytes.
    • Settlement state PDA: new interface::data::state module, discriminator 129, account grows 0 -> 1 byte (written on Initialize).
  • An IDL's discriminator field can be any byte length — it doesn't have to be Anchor's own 8-byte sha256("account:...") convention — so a single byte is enough for IDL-driven tooling (e.g. Solscan) to identify the account type, while costing far less rent than an 8-byte discriminator would.

Builds on #63 (little-endian encoding) — based on that branch since decoding an account now also depends on its fields already being little-endian.

The hand-written IDL that documents this program (originally included here) now lives in its own separate draft PR, stacked on top of this one.

Test plan

  • just build-verified to rebuild the on-chain .so
  • cargo test --workspace — all tests pass, including new discriminator-rejection tests and SettlementAccount enum tests
  • just fmt-check passes

🤖 Generated with Claude Code

Prefixes every account this program owns with real storage with a
1-byte discriminator, mirroring how SettlementInstruction already
identifies instructions:

- New SettlementAccount enum (interface/src/lib.rs), following the same
  num_enum::TryFromPrimitive pattern as SettlementInstruction. Discriminators
  start at 128 and increment per account type, kept distinct from the
  instruction discriminators (0-4).
- OrderAccount: discriminator 128 (OrderAccount), account grows
  199 -> 200 bytes.
- Settlement state PDA: new interface::data::state module, discriminator
  129 (SettlementState), account grows 0 -> 1 byte (written on Initialize).

An Anchor-style IDL's `discriminator` field can be any byte length, not
just Anchor's own 8-byte sha256("account:...") convention, so a single
byte is enough for IDL-driven tooling (e.g. Solscan) to identify the
account type while costing far less rent than an 8-byte discriminator.

Also adds a hand-written Anchor-compatible IDL (programs/settlement/idl/
cow_settlement.json) describing the program's instructions/accounts/types.
Since this is a native/Pinocchio program rather than an Anchor program,
several spots can't be fully expressed in the IDL grammar (BeginSettle's
dynamically-shaped tail, order_pda's hashed PDA seed) and are documented
inline via `docs` fields instead.

Builds on the little-endian encoding from the previous PR.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@kaze-cow kaze-cow requested a review from a team as a code owner July 8, 2026 12:02
kaze-cow and others added 2 commits July 8, 2026 21:04
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The hand-written IDL doesn't need to land together with the code changes
it describes; splitting it out so it can be reviewed and iterated on
separately (and marked draft while we figure out how to validate it).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Comment on lines +182 to +184
if *discriminator != EncodedOrderAccount::DISCRIMINATOR {
return Err(ProgramError::InvalidAccountData);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check may be superfluous if we decide we want to go more efficient.

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