Skip to content

fix(rewards-program): use Anchor-compatible 8-byte event discriminators#37

Open
dev-jodee wants to merge 2 commits intomainfrom
fix/event-discriminators-anchor-compat
Open

fix(rewards-program): use Anchor-compatible 8-byte event discriminators#37
dev-jodee wants to merge 2 commits intomainfrom
fix/event-discriminators-anchor-compat

Conversation

@dev-jodee
Copy link
Copy Markdown
Collaborator

Summary

  • Replaces single-byte event discriminators with 8-byte constants derived from sha256("event:StructName")[..8], matching Anchor's convention
  • EVENT_DISCRIMINATOR_LEN changes from 9 to 16 bytes (8-byte EVENT_IX_TAG + 8-byte discriminator)
  • Removes the EventDiscriminators enum — each event struct now holds its own pre-computed 8-byte constant
  • Updates all 18 event files and their unit tests (byte offset shift: data starts at byte 16 instead of byte 9)

⚠️ Breaking change: Event wire format changes. Indexers and parsers consuming these events must update their discriminator matching logic.

Audit finding: #5

Test plan

  • cargo test -p rewards-program --lib (405 unit tests)
  • cargo test -p tests-rewards-program --lib (204 integration tests)

Replace single-byte event discriminators with 8-byte constants derived
from sha256("event:StructName")[..8], matching Anchor's convention.

This is a breaking change to the event wire format. Event data now
starts at byte 16 (8-byte EVENT_IX_TAG + 8-byte discriminator) instead
of byte 9 (8-byte EVENT_IX_TAG + 1-byte discriminator).

Previously, standard Anchor event parsers couldn't decode these events
because they expected 8-byte discriminators.
Replace hardcoded 8-byte discriminator arrays in each event struct
with calls to a new const fn event_discriminator(name) that computes
sha256("event:" + name)[..8] at compile time via const-crypto's
re-exported sha2-const-stable.

Adding a new event now just requires:
    const DISCRIMINATOR: [u8; 8] = event_discriminator(b"FooEvent");

instead of manually computing sha256 offline and pasting the bytes.
Fixes OptInEvent/OptOutEvent discriminators to match their actual
struct names (were previously derived from the old enum variant
names OptedIn/OptedOut).
@dev-jodee dev-jodee requested a review from amilz April 20, 2026 13:32
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