Assertions
An assertion is a condition which must evaluate to the Boolean value of true and its purpose is to prevent undesirable computation when the condition is evaluated to false.
For example, a function may only work if the condition argument < 5 is true. We can use an assertion to enforce this condition by:
- Forcing a revert in the program when 5 <= argument
- Handling the exception with additional code
Handling exceptions may be done through if expressions therefore the following sections will take a look at how we can make the virtual machine revert (safely crash).
- assert: Checks if a- conditionis- trueotherwise reverts
- require: Checks if a- conditionis- trueotherwise logs a- valueand reverts
- revert: Reverts the virtual machine with the provided exit code
- assert_eq: Checks if- aand- bare equal otherwise reverts
- assert_ne: Checks if- aand- bare not equal otherwise reverts