Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for enum cases during contract updates #762

Merged
merged 4 commits into from
Apr 6, 2021

Conversation

SupunS
Copy link
Member

@SupunS SupunS commented Apr 5, 2021

Closes #757

Description

As in #757, the enum-case values are represented using a composite value, having a field called rawValue which stores the enum-case raw value. Hence serializing/deserializing, only the raw value gets serialized/deserialized, but not the actual enum-case name. So changing enum-cases can lead to type/value confusions.

This PR adds a validation to avoid such scenarios. With these changes, update to an enum need to make sure that:

  1. Order of the updated enum-cases must be the same as the existing enum.
  2. Updated enum must have at-least the same number of enum-cases as the existing one.

The above restrictions would mean:

  • Adding enum-cases at the end is allowed
  • Adding enum cases to middle/top is not allowed (violates 1)
  • Swapping enum-cases is not allowed (violates 1)
  • Removing new enum cases is not allowed (violates 2)

For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@SupunS SupunS marked this pull request as ready for review April 5, 2021 14:19
@SupunS SupunS requested a review from turbolent as a code owner April 5, 2021 14:19
@SupunS SupunS self-assigned this Apr 5, 2021
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding this @SupunS!

runtime/contract_update_validation.go Outdated Show resolved Hide resolved
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! just a couple minor suggestions

Comment on lines 393 to 395
declName string
expected int
found int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other errors export the fields so maybe do the same here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 51bb585

Comment on lines +1514 to +1519
fmt.Sprintf(
"mismatching enum case: expected `%s`, found `%s`",
expectedEnumCase,
foundEnumCase,
),
enumMismatchError.Error(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of comparing error messages literally, just check declName, expectedEnumCase, and foundEnumCase of the error directly, so changing the error message doesn't break the test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also wanted to assert the error message, to make sure they are consistent, and the to-string works with nested errors, etc.

Comment on lines 1527 to 1536
assert.Equal(
t,
fmt.Sprintf(
"missing cases in enum `%s`: expected %d or more, found %d",
declName,
expectedCaes,
foundCases,
),
extraFieldError.Error(),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of comparing error messages literally, just check declName, expectedCases, and foundCases of the error directly, so changing the error message doesn't break the test

runtime/contract_update_validation_test.go Outdated Show resolved Hide resolved
runtime/contract_update_validation.go Outdated Show resolved Hide resolved
@SupunS SupunS force-pushed the supun/enum-update-validation branch from b938b28 to 51bb585 Compare April 6, 2021 02:48
@SupunS SupunS merged commit dc28b98 into master Apr 6, 2021
@SupunS SupunS deleted the supun/enum-update-validation branch April 6, 2021 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ensure contract update validator handles enum cases correctly
2 participants