blockchain: Make zero val threshold tuple invalid. #3080
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.
This requires #3079.This makes the zero value for a threshold state tuple act as the invalid state and choice instead of using magic sentinel values.
In practice, callers should be checking the error of any methods before using any other returned values, however, the code typically tries to adopt a defense in depth model where it makes sure unusable/invalid values are also returned in error cases to help make it obvious when a caller incorrectly uses the returned value without checking the error.
It accomplishes this by reording the threshold states so that the invalid state is 0 and converting the choice field to a pointer to the relevant choice as opposed to a choice index. That way the zero value for the overall type is the invalid state and nil the choice, exactly as expected.
In addition to being more ergonomic and inline with typical Go code, it has a few additional benefits:
Closes #3078.