fix(rewards-program): use Anchor-compatible 8-byte event discriminators#37
Open
fix(rewards-program): use Anchor-compatible 8-byte event discriminators#37
Conversation
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).
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
sha256("event:StructName")[..8], matching Anchor's conventionEVENT_DISCRIMINATOR_LENchanges from 9 to 16 bytes (8-byteEVENT_IX_TAG+ 8-byte discriminator)EventDiscriminatorsenum — each event struct now holds its own pre-computed 8-byte constantAudit finding: #5
Test plan
cargo test -p rewards-program --lib(405 unit tests)cargo test -p tests-rewards-program --lib(204 integration tests)