7

The question should be straightforward.

1 Answer 1

7

The discriminator is defined by the first 8 bytes of the Sha256 hash of the account's Rust identifier--i.e., the struct type name--and ensures no account can be substituted for another. It lets Anchor know what type of account it should deserialize the data as.

example:

Say a Program has two types of accounts that are owned by itself (Account A and Account B). Both have identical fields. Instruction foo is meant to take Account A, but a user passes Account B, which is still owned by the program and has the same fields. How will the program know to throw an Error?

Using the discriminator the program will know which account is which, solving this issue.

  • If you say foo: Account<'info, A>, anchor will ensure that it really is an A account.
  • Anchor adds the bytes automatically whenever you use the Account type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.