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 acondition
istrue
otherwise revertsrequire
: Checks if acondition
istrue
otherwise logs avalue
and revertsrevert
: Reverts the virtual machine with the provided exit codeassert_eq
: Checks ifa
andb
are equal otherwise revertsassert_ne
: Checks ifa
andb
are not equal otherwise reverts