0/5
--- ### Understanding hellFunc Function Behavior In this section, we will explore the use of the `hellFunc` function and how we can implement a simple invariant to ensure the function behaves as expected within our program. #### Calling the hellFunc Function Initially, you can invoke the `hellFunc` function by passing a numerical value, and expect a `uint256` response from it. This call is illustrated in the example: ```js hellFunc(number); ``` Here, the function should return a `uint256` response which we can then check against our invariant. #### Asserting an Invariant If our invariant condition stipulates that the response must always be zero, we can use the assert statement to enforce this rule. The code snippet would look something like this: ```js assert(response == 0); ``` This asserts that the response from `hellFunc` should always equal zero, forming a simple invariant for the function's expected behavior. #### Handling Reversions However, the primary invariant we are focusing on is ensuring that the `hellFunc` function does not revert under any circumstances. To test this, we use a modified function call: ```js hellFunc!@withrevert(number); ``` Alongside this function call, we implement an assertion to check that the function does not cause a revert: ```js assert(lastReverted == false); ``` Here, `lastReverted` acts as a keyword in Certora, a tool we are using. It updates to reflect whether the last executed Solidity function caused a revert or not. #### Ensuring No Reverts Given our invariant—that `hellFunc` must never revert—we structure our testing to confirm this behavior reliably. By monitoring the `lastReverted` keyword after invoking the function, we can assure that our condition is met.ß Now, it's time for Certora to perform its analysis and transform our invariant checking into a rigorous mathematical verification.
A simple guide to using invariants in Certora. The lesson covers the key elements of an invariant as it relates to Solidity smart contracts. It walks through how to use Certora to ensure a smart contract never reverts when being run.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on May 28, 2025
Solidity Developer
Assembly and Formal VerificationDuration: 30min
Duration: 4h 38min
Duration: 3h 57min
Duration: 1h 56min
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on May 28, 2025