1/5
--- ### Setting Up the Halmos Test Environment First, let’s navigate to our working directory named “invariant break formal verification” and begin by creating a new file titled `HalmoseTest.t.sol`. This file will contain all the code required for using Halmos to formally verify the robustness of our code. ### Writing the Code We'll start by importing necessary components. First, integrate the standard test contract from `forge-std` by adding: ```solidity import {Test} from "forge-std/Test.sol"; ``` Next, we need to bring in the specific contract we wish to verify, named `formalVerificationCatches.sol`, which is located in a nested directory structure: ```solidity import {FormalVerificationCatches} "../../../src/invariant-break/FormalVerificationCatches.sol"; ``` ### Initializing the Verification Contract Create a setup function within your `HalmosTest` contract to deploy the verification contract, like so: ```solidity function setup() public { fvc = new formalVerificationCatches(); } ``` ### Writing the Test Function In Halmos, assertions are key, with the tool focusing entirely on these statements to conduct verification. Therefore, structure your test to include necessary assertions: ```solidity function testHellFuncDoesntRevert(uint128 num) public { (bool success, ) = address(FVC).staticcall(abi.encodeWithSelector(FVC.hellFunc.selector, num)); assert(success); } ``` ### Setting Up Halmos To utilize Halmos, ensure you have Python installed as it is required for the installation. If you haven’t installed Python yet, this would be a good starting point. Once Python is ready, install Halmos using pip: ```bash pip install halmos ``` Alternatively, for a more isolated setup, consider using Pipx to install Halmos: ```bash pipx install halmos ``` ### Running the Halmos Test Once everything is set up, you can execute the Halmos command on the test, which is designed to simulate and verify the function without causing reverts. This process converts the function into a mathematical expression and seeks to prove or disprove the specified conditions: ```bash halmos test HalmosTest.sol ``` ### Reviewing the Output and Adjustments After running the test, Halmos might provide insights such as counterexamples where the function might fail. These can be used to refine the tests or understand better where the code might not meet the desired conditions. ```bash forge test --mt ``` ### Conclusion This guide should help you set up and run a basic Halmos test, illustrating the transition from Foundry-based tests to formal verification using Halmos. As with any tool, familiarity will grow with use, helping to uncover more about its capabilities and limitations.
A comprehensive guide to symbolic testing with Halmos. This lesson provides you with the basics of setting up your Halmos testing environment, writing a simple Halmos test, and using Halmos to help you prove or disprove your assertions.
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 January 17, 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 January 17, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar