Foundry/Forge enables fuzzing of Solidity smart contracts through property-based testing, where random inputs are generated to verify contract assertions; while Foundry provides basic fuzzing capabilities with configurable options like runs and seed for reproducibility, Echidna excels at invariant testing by generating sequences of transactions to verify global contract properties over time, though implementing this in Foundry requires more manual effort.
Fuzzing Solidity Smart Contracts Using Foundry: A Practical Guide
Added:hi guys so today's video is about uh fuzzing solidity smart contracts using laundry so Foundry um go to a specific mode for fuzzing it's part of the testes sub comment and I want to show you a bit more about how it work and how you can leverage some option to play with that basically so first of all you have a tiny should set as usual on the link below you need to install Foundry nothing fancy there you will get access to the four command after that so let's do a third in it and we're gonna call that a contour 2. that's the basic uh it's like the basic yellow wall for four so we can go inside the Contour number two inside this one let me update that Contour two we have multiple stuffs first of all we have files that have been generated the Contour that solve really basic contract a public number or we can set the number and we can increment this number and then what is interesting is the test folder and inside this one you can see the contour.c.sol we are importing the test um object and the Contour object in that case that is public we create we have the setup function really important the name that will create a contour contract and set the number so it's like the Constructor of that that's basically what you want to put inside this function and then you have a bunch of testing methods testing functions that will test some properties in that case like you increment so the number is actually equal to one you set the number to x so if you are cutting numbers you should get X and so on and so so we have multiple uh commands that can be used first of all you have to forge build to build the project so everything is working you have the four uh tested so it will run all the testes we saw previously what you can see right there is that since we have an argument you will actually start to do some fuzzing and in that case it will do 256 runs officing so it's really nothing in that case it's not really doing just some very short fuzzing nothing fancy and what I want to show you is oh actually it will really first the project that it should be something um really uh that you should do so um if we take a look at the code again we have these testes that take one uh you in 256 right there and the fuzzing mechanism of boundary will generate some random value for that and verify that this assertion is actually always correct so that's what we call property testing and that's something that is as you can see basically by default on on Chromebook so that's really good if we want to do more fuzzing we can actually specify some option like this one you can modify the foundry.tommer like that and you can specify okay for the first thing I want this amount of runs let's put 15 000 and I want for example this seed so it will be the number that will be used as a basis for random generation so if I'm putting 12 uh 34 um all the inputs that will be generated my my first thing um Campaign Will basically be reproductable and will always be the same so that's also really interesting to have something like that you can just run a Forge test again and as you can see already finished the 15 000 execution and it will just do that again and again so that's really good that's one of those options that are interesting there is a multiple of them and you can see that on the first testing section of The Foundry book really interesting there is also um as I mentioned um right there sorry yeah this one there is some option uh like the third option on The Foundry tunnel I mentioned uh this one so you have runs you have seed you have a bunch of them I I put all the links on the description as usual you also have The Foundry sheet cut so those staff allow you to assert and and force some stuff by uh by the the for The Foundry execution online in that case so you can create an address like this one you can assume um that the feather will consider it as a rejection like um you assume that the Contour will be different than this specific value you can Bond some value that have been generated and so on and so on you can even write logs there is plenty of them again uh all everything is on the book you can just search for like let's say vm.assume and you will get right there she could reference assume and you will get an example of that so really really good The Foundry book is actually the best resources for you in that case so that's some really good stuff and there is some extra links for for you to take a look at then we have the good question of uh is etchina uh better than four or more specific Foundry and also those foundries report in variant testing so that's something I want to mention because actually Paul made the suite but like the benefit of hatchimal over Foundry over Ford and I basically link so that's facing Maps I basically link the the tab I've made for the etrcc conference this year um I mean last year 2022 and I basically made a huge overview of the different feature of all the feather and what I my final result was basically like was um bitter in a lot of different features but especially China was supporting the generation of sequence of cores what basically it um sorry Foundry call the invariant testing that's mean um over the time after modification of the state of the contract after let's say in that case set number and then another call about a set number and then another call about um increment and so on and so on you verify some assertion that are basically Global to the contract one good example let's say you have um Ania C20 you have a maximum supply of tokens that have been emitted um one of the assumptions you want to verify all the time whatever the state of the contract is that basically this Supply the total supply of your token never increase for example so that's the kind of stuff that are really interesting to to find out so the point was echina is able to do that and is doing that and it's a really important feature and during the the debate and and the Twitter thread there is a lot of pros and cons and right now it can also Implement like hybrid trading that is something else but I will not go too much in in the detail but basically at the end the the point was um typically right there generating sequence of transaction and it seems that it's not possible with Forge using the environment testing so I took a look at the the repository to be honest it's not obvious or you are supposed to use that and so on even if there is example it's not as straightforward as a kidney is doing but I think that's some really good start so typically if you want to learn more about invariant testing or like generation of sequence of cores and so on um during phasing I really invite you to take a look at that so that's also why I put the link below but my final overview of that is it's not as straightforward and user friendly that it should be typically what I will expect from uh from that typically is you will get multiple sequence of cores and you will first call setup and then you will basically call um some public function like this one like this one with some random value that will be set so that's what using we will do and then you're gonna call the the test set number in that case with this specific exception in mind so let's say if we have a maximum number and I'm doing some increment I could have a test name a test maximal number my maximum number shouldn't be bigger than 10 for example if I'm doing that right now I will I will get no issue by Forge like a third as I can equal 10 and um I will just do nothing else like that's all I I'm just asserting my number is not equal to 10 okay or bigger than 10. and if I'm running that it will tell me that it's perfectly fine because it will not be a context dependent so in that case it's not happy for whatever reason I don't know um oh service to that number I actually so let me just run that again um so it will tell me that this one is not correct but uh oh actually I don't know why let me tell let me check let's put some variability um oh yeah of course it's because by defaulty will be zero so that's why it's not equal to 10. so let's say I want to accept that my number is never bigger than so I want to accept that it's less than 10 okay and if I'm running that it will be perfectly fine so as you can see it's just taking is basically just doing setup and then calling my test which at the end is not context dependent um so that that's the the bad part of it um what we are looking for when we are doing first thing and with sequence of chords is basically URL setup and then you are cutting some other public function and you are changing the state of the contract and then you are calling this verification this assertion this property testing function again so if I'm doing 10 time test increment and then I'm calling this function my assertion will fail but because my state the state of the contract is basically my number that have been set to 10 after 10 calls to test increment so that's basically what she's missing and um as I mentioned if we take a look at the invaliant example there is some stuff of course uh there is definitely really some interesting stuff and you have this same kind of addition um stuff I think addition contract but I mean you can Implement that so it's definitely something that is possible and you have plenty of stuff to play with uh but again that's not straightforward and it will require a lot of manual stuff for you to do that and it's not just um um like off the shelf like foreign so that's my my point of view I think again as I mentioned like last year during my my toolkat um ethcc um for me and I actually I think I I just have to slide uh yeah right there um my my final um final point no sorry it was not this one oh no it was not this uh this one my bad um I will uh I will give you the the link below but the the link to the slide below sorry uh so basically my point at the end was that for me echina is still the best uh even with those new features that have been implemented and for me Foundry is definitely the most um not interesting but the most um um promising tool that we have regarding fuzzing on on the term and so on and I I'm I will maintain my uh my thoughts on that um so that's uh that's really good let's hope it will be more straightforward you have plenty of links uh on the on the on this readme and uh of course as usual uh I aggregate all the stuff I'm doing regarding the time security fuzzing and reversing of smart contract inside uh this uh these three courses I mean you also have the video on YouTube you just have the content all the script and so on in addition on this so um if you are interesting about this subject and want to learn more than I really invite you to uh to take a look at that and please let me know in the comments uh what you would like to to see next in term of like smartphone track solidity smart contract security and so on so thanks and uh have a nice one
Up Next

GTDA Method for Smart Contract Auditing: Finding Critical Bugs
@0xOwenThurm
15.1K views•2024-02-01

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Ethereum Smart Contract Audit with Mythril: Solidity Security Analysis
@fuzzinglabs
11.4K views•2022-02-01

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto







































