You should use assert
to help you find invariants which have been violated.
You use assert
to help you catch when the impossible happens.
Solidity also has an SMTChecker which can prove that your invariants are true:
Solidity implements a formal verification approach based on SMT (Satisfiability Modulo Theories) and
Horn solving.
The SMTChecker module automatically tries to prove that the code
satisfies the specification given by require
and assert
statements. That is, it considers require
statements as
assumptions and tries to prove that the conditions inside assert
statements are always true. If an assertion failure is found, a
counterexample may be given to the user showing how the assertion can
be violated. If no warning is given by the SMTChecker for a property,
it means that the property is safe.
The SMTChecker Tutorial provides a good example of assert
that is too lengthy to include here: highly recommend to read it.
Since assert
should never be false, you do not have to worry about consuming all the user's gas.
If the SMTChecker gets you excited to start using assert
, that's a very good thing. But remember they are for invariants, as the Solidity docs mention:
Assert should only be used to test for internal errors, and to check
invariants. Properly functioning code should never create a Panic, not
even on invalid external input. If this happens, then there is a bug
in your contract which you should fix. Language analysis tools can
evaluate your contract to identify the conditions and function calls
which will cause a Panic.