1/5
_Follow along with this video:_ --- ### Converting To Fuzzing If you've taken a look at the **[GitHub repo](https://github.com/Cyfrin/1-horse-store-s23)** associated with this course, you'll see we're doing things a little bit differently. Looking at the differential testing in the repo, things are much more formal. ```js // SPDX-License-Identifier: MIT pragma solidity 0.8.20; import {Base_TestV1, HorseStore, HorseStoreYul} from "./Base_TestV1.t.sol"; import {HuffDeployer} from "foundry-huff/HuffDeployer.sol"; contract HorseStoreTestV1 is Base_TestV1 { function setUp() public override { super.setUp(); horseStoreHuff = HorseStore(HuffDeployer.config().deploy(horseStoreLocation)); } function testReadHuffValue() public { uint256 initialValue = horseStoreHuff.readNumberOfHorses(); assertEq(initialValue, 0); } function testStoreAndReadHorseNumberHuff() public { uint256 numberOfHorses = 77; horseStoreHuff.updateHorseNumber(numberOfHorses); assertEq(horseStoreHuff.readNumberOfHorses(), numberOfHorses); } function testStoreAndReadHorseNumberYul() public { uint256 numberOfHorses = 77; horseStoreYul.updateHorseNumber(numberOfHorses); assertEq(horseStoreYul.readNumberOfHorses(), numberOfHorses); } function testCompareHorseStores(uint256 randomNumberToStore) public { horseStoreSol.updateHorseNumber(randomNumberToStore); horseStoreHuff.updateHorseNumber(randomNumberToStore); horseStoreYul.updateHorseNumber(randomNumberToStore); assertEq(horseStoreSol.readNumberOfHorses(), randomNumberToStore); assertEq(horseStoreHuff.readNumberOfHorses(), randomNumberToStore); assertEq(horseStoreYul.readNumberOfHorses(), randomNumberToStore); } } ``` A couple things stand out: 1. We're deploying each of our implementation versions, and comparing them all to assure they're equal 2. We're using fuzzing rather than unit tests How you ultimately approach your test suite is up to you, but let's add fuzzing to what we've built out to assure our tests are thorough and secure. This adjustment is very easy. Our `testWriteValue` test just needs to take a fuzzing parameter instead of declaring a value for `numberOfHorses` ```js function testWriteValue(uint256 numberOfHorses) public { horseStore.updateHorseNumber(numberOfHorses); assertEq(horseStore.readNumberOfHorses(), numberOfHorses); } ``` Boom. All there is to it to convert this simple function into a fuzz test. ### Risks of Low Level Code Working with op codes is POWERFUL, but as cliche as it is - *with great power, comes great responsibility*. Breaking the functionality of code written in such a low level is very easy. Any of our `PUSH` op codes, even if off by a single digit will cause things to likely fail, the precision of capturing the correct offset, or referencing the correct storage slots is paramount when writing in low level code. Its for this reason that if you choose to write in something low level like Assembly or Huff, you should back up you logic with robust fuzz testing and even formal verification tests to assure your Huff implementation acts precisely how it should. Don't worry, we'll absolutely be going over formal verification later in the course 😉
A comprehensive guide to updating tests to Fuzz Tests. This lesson explores the process of implementing fuzz testing in Solidity projects, demonstrating how to leverage the power of fuzz testing to identify subtle vulnerabilities in low-level assembly code and ensure the consistency of your Huff and Solidity contracts.
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