idl: Check ambiguous discriminators #3157
Merged
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.
Problem
The ability to set custom discriminators means the user can also set discriminators that are ambiguous. For example:
In this example, there is no way to reliably decide the expected account type from a given account info's data. For this reason, Anchor should not allow compilation of this program.
However, this results in another problem — definitions of accounts, events, and instructions don't have access to other definitions' discriminators, as proc-macros are stateless. To solve this, ambiguous discriminators should be checked during the IDL generation process, since we have access to all discriminator values (and introducing another
idl-build
-like feature just to check discriminators would increase the already long build times even more).Summary of changes
Check potential discriminator collisisons during the IDL generation step.
Note: This PR is part of a greater effort explained in #3097.