1/5
_Follow along with this video:_ --- ## What Happens When We Don't "Jump"? Now that we've accounted for each of our contract's function in our function dispatcher, we're done, right? Not exactly. Our code won't just stop executing if no valid `JUMPDEST` is found, it'll continue to the next operation (which in our case happens to be a `JUMPDEST`). We could easily imagine a scenario where `call data` is sent to our contract, no `function selector` matches are found, and arbitrary code is executed when we don't intend! We should protect against this by terminating the execution when no matches are found by our dispatcher. We can leverage the `REVERT` op code to do this. ::image{src='/formal-verification-1/25-revert/revert-1.png' style='width: 50%; height: auto;'} The revert op code takes two stack inputs, the **byte offset** and **byte size**. Both of these are used to return data from memory (like an error code) when the REVERT operation is executed. We haven't dealt with memory and have no errors so we won't worry about this, we'll simply pass 0s. Our Huff contract with REVERT implemented should look something like this. ```js #define macro MAIN() = takes(0) returns(0){ 0x00 calldataload 0xe0 shr // [function_selector] dup1 // [function_selector, function_selector] 0xcdfead2e // [0xcdfead2e, function_selector, function_selector] eq // [true/false, function_selector] updateJump // [updateHorseNumberProgramCounter, true/false, function_selector] jumpi // [function_selector] 0xe026c017 // [0xe026c017, function_selector] eq // [true/false] readJump // [readHorseNumberProgramCounter, true/false] jumpi // [] 0x00 // [0] 0x00 // [0,0] revert // [] updateJump: SET_NUMBER_OF_HORSES() readJump: GET_NUMBER_OF_HORSES() } #define macro SET_NUMBER_OF_HORSES() = takes(0) returns(0){} #define macro GET_NUMBER_OF_HORSES() = takes(0) returns(0){} ``` Now, if we head back to the [**evm.codes playground**](https://www.evm.codes/playground) with our new runtime bytecode (`huffc src/horseStoreV1/HorseStore.huff --bin-runtime`), we can send some garbage `call data` and step through the operations to see how the contract responds. ::image{src='/formal-verification-1/25-revert/revert-2.png' style='width: 50%; height: auto;'} It seems our revert works exactly as intended! Our revert code will terminate the execution if hit and return an error! Great Job!
A simple guide to reverting a function call in an EVM smart contract. This lesson covers the REVERT opcode, including how it takes two parameters, 'offset' and 'size', how to revert the current context execution, and how to revert the state changes.
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