Smock talk draft final docx
ETHGlobal·Sun, Oct 9, 2022, 12:00 AM
Join Kelvin Fichter for a talk titled, "Smock talk draft final docx," at the DevTools Summit, part of ETHOnline 2021, a month-long virtual hackathon with over 1,000 engineers, designers, and creators building new protocols, learning at summits and workshops, and gaining valuable feedback from ecosystem professionals along the way. ---------------------------------------------------- Follow us: https://twitter.com/ETHGlobal Check us out: https://ethglobal.com Email us at: hello@ethglobal.com
Transcript
[Music] [Applause] [Music] today um up next we have kelvin and calvin's going to be talking about smok and um i'll let kevin come and introduce what smock is but i'm already excited about the talk title and i'm also glad that kelvin used a 2007 plus version of microsoft word instead of the original 2003 so without further ado let's welcome kelvin there we go let's give you turn video on and you can get started kelvin all right all right um cool fantastic so let me share my screen where is this okay sharing perfect let me make sure i can see the comments just in case great alrighty so hello hello uh i'm kelvin and today i'm going to present to you smock smock is a tool that uh well it's the solidity mocking library and it's a tool that we originally built over at optimism um last year to sort of simplify our smart contract development process and today we'd sort of uh we've evolved and we've created a v2 in collaboration with d5 wonderland uh and we would like to present it to you and hopefully see if you can get something out of it these are let me make sure that they're actually showing up where are we there we go got the twitter handle for optimism got the twitter handle for d5 wonderland and you've got my twitter handle so first things first i want to set the scene i want to set the scene for you and for why you need smock in your life this is you you are a smart contract developer you are a very smart smart smart smart contract developer uh you are a big brand gigantic brain smart contract developer and as such you sit on your very expensive uh very powerful you know 2013 macbook pro and you spit out smart contracts into the ether now spitting out smart contracts into the ease there is a very dangerous activity you need to be very careful when you do this but you are a big brand smart contract developer you know that what you need is you need well-tested smart contracts so let's talk about testing how do people usually test their smart contracts well they might do a little bit of this you know they do some javascript and they might do some calls and some assertions they might do a little bit of this and some calls and some assertions and then they sit there and they they put a little check box on their contract and they say oh it's safe it's perfect it's beautiful but but as you can see this is a very this is a single small brand smart contract but you you as the legitimate d5 developer you don't you don't build these smart small smart contracts you build real smart contracts you are a big marine d5 developer and you only build the biggest of brain contracts right so when your smart contract systems start to get bigger and start to get a little more complicated it gets harder to effectively test your contracts so you need to evolve with it and you need to become a big brain defy contract smart contract tester so how does this work how does smok make your life easier well let's first talk about what smock is through an example i'm going to talk about a very specific example of how smok functions and why it might be useful and sort of get you if get you to sort of uh to bring back dark memories from your past about difficulties that you might have had while trying to test smart contracts because i've definitely had these dark memories blocked deep in my brain smart solidity smock is the solidity mocking library the facility library there is no better it allows you to write better tests that is a key feature of smile why do you need it let's talk examples this is you this is your big brain uh testing creature and you're gonna you're gonna write some smart contracts you're gonna start with writing a small smart contract and then you might write a big smart contract and you want to test how these things work you want to test them individually you want to test how they work together you know you throw out words like unit testing and smoke testing and integration testing and whatever all these different testing things but ultimately you want to make sure that your system works or people might lose money which is a big problem so let's throw an example on this let's uh let's specifically let's talk about something reasonably complicated we're going to talk about a merkle air drop do you know what a merkle airdrop is it doesn't make a difference the point is something reasonably complicated that people can kind of comprehend right so let's say we have a merkle airdrop and then we have a recipient of a merkle airdrop right so how would you go about testing the burglar drop contract well you might do some stuff like you might you might you know you're gonna have to create a merkle tree and you're gonna have to hash a bunch of stuff and you're gonna have to deploy the contract and then you're gonna have to publish the merkle root and you're gonna have to you know test if you have a balance and all this right so it's a ton of effort but you need to do that in order to effectively unit test your smart contract but how are you gonna test another contract that interacts with your merkle airdrop contract how are you going to do that because you want this right this is the ideal end result the ideal end result is that your your recipients get their money and they can go off and do stuff and they can spend it on yams or whatever they want to spend it on and you don't want this right you don't want their drop to just fall on the floor because no one can get their money and you just whatever it's a whole headache right how are you going to test this well let's talk about let's talk about this other this other character right this other airdrop recipient think about how you might test this airdrop recipient well the air yeah there's the airdrop recipient the answer to this question is basically for most people if you think about it you'd have to basically perform the entire testing process that you already performed for the first contract right in order to test if this person is going to get a balance you have to go and you have to create this merkle airdrop contract and you got to go and and set it up right you got to do this you got to load the whole contract you got to deploy it you got to do all this setup right this is a huge headache so how do developers actually do this in practice how have they been doing this they've been writing these fixtures right all these fixtures and then you get fixtures inside of your fixtures and then you get fixtures inside of those fixtures and it keeps going until at the very bottom there's a bose einstein condensate of fixtures and it all matter is just fixtures this is bad what you really want is you want smock so how does smog fix your problem smok means you don't need to load anything smart means you don't have to do all of this junk setup that you are on all this this uh fixture code that just completely pollutes the code base and you can just get straight to the air drop straight to the airdrop and how do you do that through the power of mocking we love mocking mocking is a beautiful thing so let's talk about it let's talk about how this tool actually works now you have a scenario you want to test it you want to test a contract that interacts with a much more complex contract but you don't want to set up the entire complex contract because that's a huge headache so how does this actually work how are you going to make this happen well smock gives you two primary tools gives you fakes and it gives you mocks so first we're going to talk about fakes this is what it looks like to create a fake with smock so you can see here hopefully people can see my little uh mouse if you can't you can see here that we are going to import smart from defy wonderland smart d5 wonderland is where we're hosting the uh the npm package where we're hosting this you get it and uh then it's straight really straightforward this is a hard hat plugin so you get this beautiful syntax where i can just say smock dot fake and then the name of the contract and now i have a fully fledged fake version of my contract that contract doesn't have any real code but it has a real address and if you make calls to that contract address you are actually going to get back real results so for instance i can make let's say this fake has a function called bark and i tell bark to return wolf if my smart contract calls that fake contract it says you know fedcontract.bark or whatever you know contract.park it's actually going to return woof when you run the tests in the evm so it hooks into the to the evm in hardhat the hard hat ebm at a low level and so it really simulates that there's a smart contract there and you can make it do whatever you want you can even do these beautiful assertions this nice syntax if you use these smart smock matchers you get this nice syntax and even make assertions that your mod was called with a specific input data so this basically just removes all of the need to do all this setup right i don't need to set up my my merkle airdrop anymore i just create a fake version of the merkle airdrop and i just give myself a balance in it i just say the balance of me returns however much i want what crazy so the docs are at smock.readthedocs.io and inside those docs you're going to find some really cool things as to what you can do with these fakes so pretty straightforward one thing you can just do is have a return default values default values it's all zeros so you know if you if it's the function it's called get address and it returns an address and you just return like this it's going to return the zero address you can also make it return fixed values you can make a return you know a given address a specific address or you can make a return a number depending on what the function expects to return you can also make the function revert this is really useful if you want to test cases where okay this one thing went wrong and you know what i want to make sure that my contract behaves properly but you know think about you you have to do all this weird modification to your smart your other smart contract to to be able to get it to revert and to get into that specific state where it's going to trigger that one specific revert no get rid of all of that it's just going to return the revert message that you expect and you can test the behavior of your contract fantastic you can make it revert with a string you can make a revert with bytes you can make a reverb whatever you want but here's where it gets more interesting you can also make it return more complex structures right so we have support for structs you can make a return structs you can make a return arrays right so you can use a really interesting uh you know data structures beyond just the basic ones you can even make a return dynamic values so you can stick a function inside of the return and this function can return whatever you want and it can even look at this so it could randomly return one value or the other value i wouldn't recommend doing this in practice but uh you could definitely have a more complex piece of logic inside of this return function this return function can even be asynchronous you can go call you know you can make an http request inside of a smart contract which is a little insane but only during testing don't try to do this in production it won't do anything because it does that doesn't make any sense but in testing you do whatever you want fantastic and you can even use the arguments that were provided to the function inside of the function that you are triggering so for instance if i want if i had a function that took a human as an input and i wanted to return whatever the input is times 10 i can do that i can just have it take the input to the function and multiply it by 10 and i'm going to get my my output value and it's just going to return what i expect i can literally i could essentially and i have these beautiful things written in javascript that can do whatever they whatever you could possibly imagine to make your testing life easier you no longer need to set up your contracts to be in this very specific state fantastic so fakes fakes already get you really really far fakes are a beautiful thing but i have to tell you there is something beyond fakes there's something even more beautiful than fakes and that of course is mocks mocks are mocks are fantastic and let me tell you a little bit about mocks inside of smog smock mocks are like fakes but they're backed by a real smart contract so this means that you deploy your smart contract and you essentially have the ability to manipulate your smart contract after it's been deployed which is very it can be very very useful so a basic feature that you have with this this mocking ability is call through right by default when you call your contract it's a normal contract it's a standard solidity smart contract it's going to behave just like the solidity code that you wrote but you can also manipulate it so let's say by default i have an add function and i call count and it's going to return you know 10 whatever right and this is because there's actually solidity code running but then i'm going to say okay okay but for now now i actually want to make count return one and now account returns one easy right beautiful but there's more and now this let's see if this works because i animated this by hand in powerpoint drumroll oh there we go you can set the value of the variable this is this is where the game changes with mox you can completely manipulate what's happening inside of your smart contract you can set the value of any variable to whatever you want look at this my variable name is being set to one two three four i can sit the owner of my contract so i don't have to do all this you know what if there's a bunch of complex logic that i want to test that's based on an internal variable being something well i don't want to do all the complex logic to get the internal variable to be equal to that value right there's a huge amount of effort that goes into this you don't need to do that anymore you just say okay well i'm going to test the case where this internal variable is equal to one two three four and uh i want to see how my contract works when the internal variable is equal to one two three four but there's more you can even set the value of a struct or a mapping you can set the value of whatever you want you can set a value of a nested mapping you can change literally anything inside of your smart contract and make it possible to to test every tiny little interaction without mountains of boilerplate fixture code somebody's going to have to maintain for the rest of time this is how you should be writing your smart contracts and if you're not writing your smart contract tests this way you are you are missing out on a deep and satisfying tell you that this is you this is you after learning about smok this is this is the entire universe after learning about smok and uh this is smok so there you go smock is is all there is the entire world is now just smoke so how do you get started well it's pretty straightforward d5 wonderland slash mark on npm the code is available github.
com g5 wonderland slash smock and the docs right smocked up read the docs io it's quite intuitive the docs are good the code is pretty straightforward it's got a really nice api shout out to the d5 wonderland team for really cleaning up the api and i really recommend that you check it out you try to write a few tests especially for your more complicated smart contracts and get a feeling for just how easy it is to build great tests when you're using smart and of course i'm gonna throw this in there optimism's hiring if you wanna you know if you're bored of your web two job and you wanna work on cool stuff like this um and uh there's the jobs link lever dot jobs.lever dot co slash optimism and that's my talk pretty straightforward pretty short but uh i hope you enjoyed it and very excited to see all the new people who are now going to use smok forever and always thank you thank you so much coming i'm still uh just laughing through all the incredible stuff that you got in this the drum rolls kill me um it's really good um one final plug for everybody uh for the sake of everybody following up um what is their url to learn more about smog the website the github enp and package all these things in one-off slides but uh tell us where people can go to use it immediately yes i would recommend starting with looking at the github it's got instructions on how to get started and it's got links to everything you'll need to know so github.comwonderland smock we'll have everything you need um to get started awesome and uh one last question before we close today is obviously been working on this thing for a year and and there are a lot of people that are using this but what does uh the future of the library look like what are some features or additions or things it's not good at that you want to do just talk to us a bit more about that um so the thing that i'm most excited for is we're planning a rewrite of the core internal stuff um smock was originally it uses a very hacky method to do what it does uh it uses the fact that the ethereum js vm emits things in events and it turns out that emits objects by reference and so you can you can listen to the event and then manipulate the thing that you receive in the event and it will have an impact on the vm which is really terrible so what we are planning to do is a more comprehensive rewrite which essentially involves a fork of the underlying vm instead and the feature that i'm most excited for is the ability to directly test libraries there's a uh if you if you've ever tried to test libraries you know that the standard pattern is you create these these mock libraries that wrap wrap the libraries and expose all the functions and you can actually test them and so in the plan sort of smock v3 it'll be possible to directly test libraries with one of these contracts you don't need to keep writing this boilerplate mock code for your libraries and you'll also be able to manipulate internal functions so you can say okay you know my internal function is going to return whatever i want instead of just these external public functions oh that is incredible it's like god mode for edm uh god mode for ubm cool well thank you so much for that uh incredible presentation we gotta get you back on just so we can see what the next one would look like now you have to top yourself if not at least meet the same expectations but this was great thank you herefore close to today's devtools summit uh thank you for hundreds of you for sticking all the way to the end it's been six incredible hours of just back to back that devtools showcases and talks so i can't wait for the next version of the summit ourselves all right so income [Music] [Applause] [Music]
Automatic transcript — names and jargon may be misspelled.