The One Question bounty closes Thursday 10 December.

Join in
We Them Media logo We Them Media Support us

Invariants Testing for Smart Contracts in the World of Medusa - Joshua Adesanya

The picture loads from YouTube. The player loads only when you press Play. Turn on captions in the player if you need them.

Joshua Adesanya explains the difference between unit, integration and invariant testing for smart contracts, and how fuzzing feeds random values into functions. Using a savings contract, he lists rules that must always hold, such as a maximum deposit and yearly interest, then runs the tests in Medusa with several users calling functions at random. The run shows the contract does not check its deposit limit.

Not by We Them Media Channel: Web3Bridge. Published Sunday 13 October 2024.

Titles are the owner’s. It plays from their channel.

Transcript

This transcript may contain errors. This video belongs to Web3Bridge. Letters mark each change of speaker.

These are the speakers’ own words. We have not checked their claims.

0:08 A: Hello. Okay, let's get straight into it. We don't have much time. So smart contracts testing, very interesting. We just have us Foundry. I know a lot of us here, we use Foundry, right? Yes. Great. So what we'll be looking at is something called invariant. We're gonna have a very short code walkthrough. And then we're going to jump to the rest of it. So testing, smart contract testing. I know we have a lot of Web2 devs here. You know what testing is. Unit testing. What does it do? Unit testing. So it's a kind of test whereby you are testing a chunk of a codebase. So let's say you have a very large codebase and you want to know what is happening in a specific function. That's a unit test. Integration. It has to do with, oh, I want to know how this 2, 3, 4 functions in a particular context, how they work together. But the emphasis on this, my presentation today, is actually on invariant testing. And if you're in the smart contract world, I know you, you have heard this once or more than once. So what are invariants? Invariants are basically properties. So when you, uh, develop a smart contract, there are conditions in which must stay true. And these conditions are what we refer to as invariant. There are things that no matter what happens in your contract, they must not change. It must remain the same. Do we get that? Because we're gonna need- we're gonna need it, um, later on. It must not change. So those are what, uh, that, that is what I am referring to as invariant. And like I said, a lot of us, we are familiar with Foundry. Foundry, it's a framework that, uh, we are actually used to actually build our, our smart contracts. You can do a lot of things there. You can code, you can fuzz, and a whole lot of things. But today we're looking at Medusa. Medusa is, uh, basically a fuzzing framework. It does just one thing, and what it does is fuzzing. But you can also fuzz on Foundry. But in addition to that, you can code, you can run your test and do all sorts of things, uh, in Foundry. And Medusa also does one thing. Medusa works across, across board. Medusa works across board. So you can use Medusa with Foundry, you can use Medusa with ADAT and all one. Can you guys hear me very well? Yes. Okay, great. I'm trying to rush through so we can meet up with the time. So when it comes to invariants in smart contracts, we have 2 types of invariant. We have the function level and we have the system level. Like I said, we're going to check this in the codebase. So the function level invariant are just like unit tests. They have to do with a particular function. But the difference between a function level invariant and a unit test is that you have the opportunity to actually play around with a large value. So for example, if I want to do a unit test for a function, I have to input my my parameters. So x is 10, b is 20. But when I'm fuzzing, I don't have to specify what x is. I don't have to specify what y is. The fuzzer is actually going to pass in a whole range of values. So it keeps trying 0, try with 1, try with 2, try with 3, depending on the type that you're using with it. So which means that I can easily know that, oh, if I'm using a unit 256 as my parameter, it's actually going to start from 0, to the max. Does that make sense? Okay. System level, on the other hand, has to do with the whole system. So when you are building, uh, a protocol, when you're building a smart contract, there are stuff on a level in which must remain the same. And in the code works we are going to do, we're going to see some of them. Uh, for example, if you actually, um, if you have a mass deposit, which you're going to see in, in the code works, if you have a max deposit of, let's say, 1 million in the protocol you are developing, and then you say this is the max deposit that I have to- that this protocol can take. And then when we start depositing, start depositing funds, you can actually have a system-level invariant that says that, that, uh, the deposits must not actually exceed the max deposit. That's, that's a system level. It means that a lot of things need to happen. People need to deposit. You're not checking at the, at the function level, but actually going a step further to check it at the, at the, um, system level. We're going to understand that better. So let's jump into the code straight up. Do I need to increase the font, or you guys can see it very well? Is it clear now? Yes. Okay, so this is a very simple smart contract called Savings. What, what can you do in this smart contract? You can, you can deposit, you can withdraw, and you can get interest per annum. I think that's clear enough. Hello, is that clear enough? You can deposit, you can withdraw, and then you can get interest per annum. And then we have a couple of interesting variables. We have a max deposit like I mentioned before. We have a max deposit amount, we have a minimum deposit, and we have a couple of mappings. We have total deposited and blah blah blah. So what are the invariants in this, uh, small contract? So this is, this is going to give light into what we have been talking about. So the first invariant is user deposits must increase by amount added after deposit. Does that make sense? User deposits must increase by amount added after. So when a user actually deposits, uh, a particular amount into the, into the protocol, when we check the balance of the user inside the savings contract, it must actually increase by that amount. Does that make sense? Number 2, user balance must decrease by amount withdrawn. So when a user decides to, let's say, I want to withdraw a particular amount from my savings when we check it, it must, it must actually see that the particular amount we withdrew is actually equal to when we actually check the balance, right? Does that make sense? The third one is that deposit must not exceed contract max deposit. The example I made before. So if we check the- go back to the contract, we'll see that we have, um, a constant max deposit, and then we have hardcoded it right there. So we're saying that in this, um, One of our invariants is that no matter how much, once we get to that amount, we don't want users to deposit again. So that's, that's, this is a system invariant. We also have another one that says users must get interest, uh, after, after every year. So at the end of the year, every user must be able to get interest per annum. And then the last one, uh, says that user must be able to withdraw at any time. So this is where we go into Medusa. Like I said, you can force with, uh, Foundry. It's quite straightforward. You can do that. But like I also mentioned, Medusa, it's a fuzzing framework. It does not- that's the only thing it does. So it gives you that very interesting, uh, uh, feel. And then you know that all what I'm doing here is fuzzing. You can, you can go to, um, on, uh, on Google and search for Medusa. Uh, you can add Medusa to it so you can find it easy and you can download it and start using on your laptop. So what are the things that we need to do to start using Medusa? So the first thing that we need is to identify the invariant. It's very, very important. So Medusa does not have any strength without your invariant. And like I mentioned before, if you actually want to force, you need to have an LD understanding of the codebase you're trying to force. It is not magic. So, Okay. Okay. So, fuzzing. So, the shorthand is fuzz. So, fuzz, I'm actually still going to show us. So, what we mean by fuzzing is that we actually- we have- let's say we have a function, right? And then that function has a parameter x and y that we're passing into it. On a normal day, if we are writing a unit test, We're actually going to pass in 10 and maybe 20 and then we'll run it through. But when you are fuzzing, I don't need to pass anything to it. I just leave it at X and Y. So what the fuzzing engine or the fuzzing framework does is that it actually generates that X and Y value on its own. So it doesn't just do that, it actually does that for a wide range. So for example, it can do that for, let's say, X if we're using in Solidity, if you're using something like maybe unit 256. It can do that from 0 to the max of int256. So your unit test is actually specific for a particular scenario, but when you are fuzzing, you're actually fuzzing through the spectrum of all possible values. Does that make sense? So invariant testing takes it to another level because when you are fuzzing, like I said, there's a function level fuzzing, which is what I just explained now. But system level is that, okay, A lot of things happen in the system. We are actually depositing, we are withdrawing, we are getting interest. What are the, uh, properties of the system that must always remain true? So we can also go as far as actually checking those levels and making sure that it's always true. And like I mentioned, uh, we have 2 invariants here that are actually very, very important, which is the system level, um, invariant. We have the ones that say that deposits must not exceed contract max deposit. You can see you can't really test this with just a function because a function means one person is actually the one just- you're just doing one action. It's something that you may need about maybe 1,000 transactions plus before you can actually get to a point of testing this. So it's not really physically possible for you to do, like, to test this. So that is where Medusa comes in. So as I was explaining We have a couple of things to do. I'm assuming that we have- we're going to get Medusa and we're going to install it. So I don't want to go through all those. So identify the invariants. It's- that's the most important thing. That's the most important thing. You have to identify the invariant. It's very, very important. It's when you have your invariant that you can now start using, um, the, uh, fuzzing framework that you have. So the next thing you need to do is the- one of the first functions that you run when you actually want to start is medusa init. We're all familiar with init. So what it- what Medusa init does is that it creates this JSON. So this is basically a, uh, where you actually put in your properties, that things that you're like your settings for the old faucet suite. And there are a couple of interesting things here. I'll just touch in some of them. Uh, one of the, one of the very important thing here is the target contract. So this is me just saying this is the contract that I want you to actually use, uh, for the fuzzing. I'm still going to show us this, so just note that target contract, very important. And that thing that's very also very important is the corpus directory. This is where you go in to check your coverage. So Metasploit also gives you, gives you, uh, a kind of, um, way to check that because it is possible that you are fuzzing but you are fuzzing wrongly. Just the same way that you can test, you can do right unit test and you are testing rubbish. So you can actually force and you have a feeling that, oh, I'm forcing something, I'm doing something, I'm a forcer, but you're not doing the right thing. So that is where the coverage comes in. So you can be able to see, you can see what is actually going on there. So another interesting, uh, thing that Medusa does is that it gives you the, uh, the ability to actually include more than one users. So when you are doing, when you are forcing in, let's say, Foundry or some other, uh, frameworks, You can only, by default, you can only force with one user at a time. So you need to actually write some, a lot of boilerplate to actually force. But Medusa actually comes, I can even add more. Currently by default it comes with 3. You can see sender address, you can add like as much as you want. So by default when I run this, my force on Medusa, it's actually 3 users actually doing all those deposits, withdrawals, and all 3 users by default. but I can add more and so on and so forth. We can check the rest. Let's go straight into our first test. So this is the folder invariant, and I'll just go straight to savings. There are a lot of things I will not mention, but I want to just go to the core of it. So the first, like, like we're seeing here, it's just like a smart contract. Like, it's just like a normal contract that I'm writing, but this is actually my uh, fuzzing, uh, contract. So the fourth- the first thing I did was I imported the savings that we saw, the tokens I'm using inside the savings. Uh, uh, Medusa has a cheat, um, a cheat sheet that you can use for things like pranking that we are familiar with in Foundry. And I added something called total balances for my test. So the first one, user deposit must increase by amount added after deposit, which I mentioned, right? That's my first invariant. And this is a test for it. What did I do? Uh, the first thing I did was mint some tokens to the user, ms.sender, prank the user, approve the amount for savings. I'm tracking the total balances. And the first thing I did was get my pre-balance. So the reason I'm getting my pre-balance is so that I can have a point of comparison. So I get my pre-balance, save the money, get my balance after deposit. And then I do my assertion, which is after balance minus pre-balance equals to amount. Does that make any sense? I'm just trying to check that I have deposited this money now and I get my, my, uh, my balance. So when I subtract my balance after and my balance before, I should be able to get the amount actually deposited, right? So, and then I'm testing with the assertion, and if it goes true, that means- if it remains true, that means the test passes. So that is the first invariant. The second invariant is the withdrawal. So you're going to notice something here that I'm not doing anything. I'm not trying to like do the deposit or anything. I just go straight into withdraw. You're going to feel like, what's going on? So which money are you actually trying to withdraw when you have not deposited? So Medusa does something interesting that it calls all those functions, like all these functions that we have in this file, it calls them randomly. So it calls deposit, calls withdraw, calls- so what is happening here is that when I call this test deposit here and I'm passing msg.sender, remember I mentioned that we have multiple users. So it calls all those users randomly, calls deposit, so the user already has some amount, calls another one, has some amount, then calls another random user and calls withdraw. Does that make any sense? So it's doing such a way that it's not really- it's not Straightforward. It's magic. It's not straightforward. So in that way, we're actually mimicking real life, uh, scenario. Is that fine? Is that clear? Okay, so let's go to the next one. So withdraw is basically just withdraw some amount, and then like we did for the first one, we get our pre-balance, we get our after balance, and then we check the assertion to make sure that we actually, uh, withdrawing that, that particular amount. Okay, so this 3rd level, uh, which is the system level, definitely must always- must exceed contract max deposit. This one is very, very straightforward. All I need to do is know the total deposited in the contract and make sure that always is always less than or equal to the max deposit. Does that make any sense? Yes. So in the- if you go back to our savings, we'll discover that there is a, there is a, um variable, the total deposited, which is actually tracking the total amount of tokens that all the users in the contract are depositing, right? And in my, in my test here, by invariant test, I'm trying to say that, okay, in this contract I want to make sure that the total deposited is always less than or equal to the max deposit, in which I have said that this is the max number that I want people to deposit. Does that make any sense? So this is a system level invariant. System level invariant. Okay. Okay. So in the test withdrawal, we do the pre- we're doing the pre-balance. So I know you guys- I'll talk about this total balances later on. So then I'm doing the withdrawal, pranking the email sender, then doing the withdrawal, then getting the after balance. That's all. The deposit is coming from. So like I said, Medusa does this stuff that it calls all those functions randomly. It calls deposit, calls withdraw, calls everything that is in this contract. So that means that user probably already has a deposit when it Because that email sender you are seeing actually is like 3 users randomly being called. So it already has- if it doesn't have, it's going to fail on the backend. It's going to tell me it's going to fail until, until the user actually has an amount before you can call the, the withdrawal. So it's actually working very fine. So you don't need to actually- you would have done that if you are doing a unit test. What you're saying would have made sense, but now we're actually forcing We are not restricted. So what my design does, it just calls them randomly, and at the point each of those users will actually have some deposits, and then they can withdraw. Does that make sense?

18:06 B: Okay.

18:07 A: So I think the mass deposit is very straightforward. And then we also have the get interest, which we're actually warping the time. So those that are familiar with Foundry, they know what warp- so we're basically just increasing the time. And you can- you discover that in all these things that we are doing We are not specifying amount manually. You can see, like I mentioned before, the amount is just there. Hello? The amount is just there. We're not doing any, uh, 10 ether, 1 ether. So every time that it calls deposit, they actually just put in any amount randomly. So let me run. I think my time is running. So we have get interest. It does the same thing with time. And like I mentioned, you need 1 year to be able to like get any- get a particular interest. But you may do that because we can actually warp the time and we can get time randomly. We just run random time and we can run through it. And then the last one, we have that test have enough to pay deposit, which means that at every point in time in this protocol, we- the, the contract must be able to pay them. And how are we checking that? We're checking that by making sure that the total balance inside the savings is always greater than or equal to total balances, which was this. You guys notice I've been tracking these total balances, and I did that in- okay, no, I'll run that now. Okay, okay. So I did that in the test deposit, and I did that in the withdraw. So to track when the user is actually depositing and when the user is actually withdrawing, and that gave me the opportunity to be able to actually test this. So let me just run it. Let me run the test so we can just see one or two So the command is Medusa Force, and when I run the command- Ah, what's happening? I mean, the direction. Just take your time. I got you. The pressure.

20:22 B: Yes, pressure. The pressure is real.

20:34 A: Okay, something is not right. Okay, okay, I think I know why. I deleted something that is erroring out. Okay, that's fine. Without this, we have not done a demo yet, so wow. What's really happening? I'm sure we can. All right. Okay. So, it's running. Okay. It's running. Sorry. I'm always the devil at the class. So, we can see it caught the first one and we can actually trace the path. You can see in test max deposit. It means that at a point in time, the invariant for test max deposit actually fails. Why is that? And we can also look through the sequence that is being called. We can see it called test deposit and it calls test mass deposit. Are you guys still with me? So if we actually go through the function, we are going to notice that the, uh, get interest is actually going to be a problem because, uh, we don't have- we're not actually, uh, putting any, uh, extra funds into the protocol. So by the time the, uh, by the time the, the part it starts calling, it calls the get interest, we're actually going to take part of the funds that are supposed to be for the normal user. And but the actual issue that I actually caught here is the issue of, um, max deposit, which means at the point you actually deposited more than they should. Because when we look at our savings dots. So we'll discover that in the deposit, we're actually not checking to make sure that, uh, the users have actually gotten to the max deposit. So this is one. The other one is the one I just explained now. So I guess I have to stop here. I hope you guys enjoy my talk. Thank you so much.

23:02 B: Thank you very much.

More from this hub

Something wrong, or a video you want off this page? Write to wethem.xyz@gmail.com and it is fixed or taken down within 48 hours.