ETH SD x Alchemy University: Ethereum Developer Bootcamp - Week 7 Lecture
Ethereum San Diego Talks · 1:22:50
Mentor Toby gives the final lecture of the Ethereum San Diego and Alchemy University Ethereum Developer Bootcamp, covering proxy contracts, libraries, contract upgradability and governance. The talk starts with how the EVM works, including virtual machines, memory layout and word size, to show why upgrades can go wrong. It contrasts immutable contracts with upgradeable ones, explains how governance can vote on changes, and ends with the final project overview and a live token shop example on Arbitrum.
Topics
From Ethereum San Diego Talks; community: Ethereum San Diego.
Transcript
a sound out there can you hear me all right or does it need to be louder good right yeah man so many different types of doubt he doesn't want a copy you want to like iterate yeah I think like but also all right there's just constructively five people yo yo yo yo definitely not feeling fresh feeling very run out and tired it's because Chad keeps making me do other stuff if anyone wants to do a lecture on zero
knowledge I'm ready to go what's up perfect you're done you've mastered it all right I'm gonna start yeah Chad good to go all right uh yeah okay so uh this is for our last this is our last lecture for the 44 people who are watching and you all it's really for you um but I just did before I start I wanted to say that I speak for myself but I'm going to also speak for the other mentors that uh the work that you've done uh is very
impressive we're very proud of you and I think y'all have have done a really great job especially the constraint of trying to do all this in seven weeks this the the course material from Alchemy learn is designed to be open-ended so we're trying to force this constraint on you to do it in seven weeks I think you all have done really good uh really good work and I'm impressed and I just want to offer that to y'all every everyone in here has done a really great job so we're going to get into governance for this week and I just thought we don't get a chance to talk about a lot of kind of culture and behavior in this space we're really dealing with um
I'm trying to shove a bunch of information at you but we gloss over a lot of things that may or may not be relevant or important to actually working in the space but does everybody know what a hardware wallet is yeah yeah yeah okay do we know how to use them do we use them hopefully we do it's going to come into play here with governance a bit um but and my fancy really expensive clicker doesn't work for some reason to decide not to it does this though so I might be doing this but it but it doesn't so I'll be over here today I'm pushing these but um so this is week seven governance uh the whole module for this week is basically building a
governance contract however congratulations to you Top Gun graduates impressive best of the best right here you should be proud Tom is proud all right so here's the topics we're going to go over proxy contracts libraries contract upgradability and governance to get there we're gonna do this we'll get to governance I'm going to start with going backwards we're going to go to multi-contract calls proxy contracts contract storage memory evm VMS memory management machine resources so I'm going to start up machine resources I'm going to get you to governance why am I doing that well you can tell me if it's worthwhile or not
afterward but I thought you guys should know a little bit about how the machine is built and what you're working with it's going to come into uh it's going to come into importance when you are dealing with contract upgradability because that a lot of people got burned with contract upgradability whether or not you're using a diamonds design or however you are thinking about this there are two thought or there are two camps in smart contract development one is you don't upgrade contracts what you deploy is supposed to be immutable so you don't change that ever and that's the con that's the guarantee that you're putting out to the community is this code can never change well after a lot of hacks people said
maybe that's not such a great idea maybe we should be able to change this a little bit or as companies started to build up they thought well this doesn't follow normal software development we usually can upgrade we can usually deploy patches fixes or even add more features so this idea of contract upgradability started to come into the industry and so you still have these two camps one is contract smart contract is kind of like firmware it's like once you deploy it it is baked into the machine and you can never change it and then there's also now we're getting into even governance where you could say well we could actually vote on the changes that we want to make or features that we want to add so I'm going to
start with machine resources and I'll try and get you to governance hopefully that will make sense if not I'm going to take you on a Magic Carpet Ride of computer science yeah all right so let's talk about storage so what machine are we using what is that is an initialism what is it that's a good point too right so so what's the machine that we're actually interacting with it's DVM which stands for ethereum virtual machine great bonus point mark that all right what is a what is a virtual machine yeah like we can just we'll just talk it out virtually simulated in another machine
or maybe perhaps another machine that doesn't exist Franco I'm just going to say it interprets the code and translated trans cool take that anybody else it's a virtual machine anybody else I like all those answers not what I like huh Docker container application kind of what man said dot application program yes uh so I'm not going to read I have a bunch of these I'm not going to read all of them I'll just like kind of like read a couple sentences and then move on um I believe our sides are available but you can always look up what a virtual machine is virtual machine is a computer
resource that uses software instead of a physical computer to run programs and deploy apps one or more virtual guest machines run on a physical host machine right okay so we kind of have this idea that we have a software computer that's running on Hardware right so you have a program that's running on metal right so it kind of looks like this uh it's going to be really tough because we're going to do this it's you know this thing always sounds better than it is so you've got one machine this is virtual machine this is a virtual machine two and then the bottom right so this is the metal right and then we have these two you know kind of like software programs running well what we're going to do is we're going to take one of
these and we're gonna make this the ethereum virtual machine so as Sam kind of said you could run lots of these uh you can have VMS running on your machine but also uh the ethereum virtual machine could be running as well but instead of it being its own program that you're running amongst many we we don't really do that so much anymore but the machine that in which you're deploying code to is going to be for this thing not for this thing and not for that thing it's its own encapsulated machine with its own and it's got its own operating system it's got its own storage layout and all that stuff so we're going to get into that a little bit talk about memory layout everybody should know what this is and if you don't you're going to learn about it right now what is this
Adrian knows what is it you could say it's a call stack I wouldn't call it a call stack there are stack frames in there yeah it's this is your memory layout on a machine all right tell me about it don't make me do all the hard work it's you young Brilliant Minds tell me what is it like what's the bottom yeah yeah it's no wrong answers here there's just the right answers uh okay so the way this works is this is a layout of the memory on your machine not the storage
that's not the hard disk it's it's the ram right so typically what happens is you have the zero address down here you don't get to write to this space you don't get any control over the space and then you have the highest address up here that's the end of your space you'll throw in program that's the that's like what you'd say the byte code or any compiled code anything that runs any program that runs is going to get loaded in here and that's what you don't get access to that that's just when you say open up uh Brave or something right the machine go the machine code gets loaded into here you don't have a choice about that you don't get any control of that but the operating system loads it in there any initialized variables any other data then you get your HEAP and your stack okay so these are like
Global variables things like that get loaded into here your HEAP is a certain um area of memory and then the stack is the other area of memory and typically most machines although I was told this is not the case but so that's fun but most machines will have the Heap on the bottom which grows up and the stack on the top which goes down why is that well they will never overlap your machine will stop before they overload okay you don't have to move the stack up well they're all you could have it I mean what if I just said you get I cut this in half I'll give you the Heap here and
the stack here and you can operate within the Heap in the stack okay so you know your maximum size you don't have overloads well you're already the the operating system will give you your max you won't be able to get more than a couple megabytes in here Thing by doing it this way but because you can just basically have a sign fit as like the negative not so much it's not about addressability uh this is the best optimization layout that you can have for not overlapping if you put them at opposite ends and they grow towards each other you don't have to like guess how much you need
although this you can't these aren't um infinite resources the operating system where we usually limit these but typically these are where your functions run you load them up on the stack and here's where you want like a little bit more persistent memory and the growth together because that is the best optimized way to get the most space out of both of those there's it's very difficult to optimize when they're both growing up however there are heaps that do go in the opposite direction but this is your memory layout this is where all the computer operating system like loads in resources for memory right okay hopefully that will become clear later when we talk about evm so let's talk about memory
addressability and word size so you have to address your memory right you need to know how to get to it or access to it and then the word size is kind of when you drill down to your machine you kind of end up with the word size and the word size is basically the width of the data that you're dealing with like how much data resolution can you can you um store basically so in Computing a word is a natural unit of data used by a particular processor design a word is a fixed size datum handled as a unit by the instruction set or the hardware of the processor
so this is the smallest piece of reference that you have in your machine to say how much information can you deal with [Laughter] is basically like when you talk about machines a processor will operate on instructions those instructions are a certain size there's I mean there's a certain amount of bits in there that you can deal with it could be 16 bits it could be 32 bits this machine right here is 64 bits it means that's what the computer CPU the instruction size can deal with it means it can address memory up to 64 bits it could if you're doing operations all those have to fit within
64 bits if I'm using numbers the integer values are going to be represented by 64 bits could be a 16-bit machine right could be a 32-bit machine it's basically like how much information are you able to represent I can represent 64 bits of information all right you know if I go back here this Adrian said that there was a stack frame stack frame would be up here that's where all your function calls would go to address those spaces the width of this is basically your word size like how big how how wide is that how many bits can you shove in there so like on my machine this whole frame
you could I mean this is a bad analogy but this whole frame you'd say this was like 64 bits you could fit in there right so 64 ones and zeros right okay you don't need to really intimately know this but I want it in your brains okay so here's I'm going to give you an example here but so word size it limits the number of addressable storage locations a machine can read and write to Sam you wanted memory well here we go okay well it's okay you can you there's too much information in here to like really know it's just you know of it right the whole point as you know of it okay so here's an example where you have addressable memory on the far side in red and then your store your memory
locations are in blue so how many like what's the word size here so it's you can just fit 16 zeros and one in here right and then the addressable memory has a whole bunch of slots that you can point to the locations in right so you've got 16 bits that's your word size that's like the width of the amount of information that you can deal with David is gone okay well keep drink some coffee yeah I'll get there
hard drive is you could you could have like like this separate when you talk about memory of a system vans talk about memory of a system you're talking about how much basically how much RAM you have how much the CPU has access to random memory storage gets paged in and out so you could have like infinite storage but the machine is operating within your memory layout so this is all if you can think about this it's all Ram we're talking about you have CPU registers that will Define how your instruction set and how what like how much information you could store in those registers is basically the what gets mapped to your memory there's only
like 8 or 16 registers in the CPU you don't need to know any of this I'm just giving you some background but those are defined by you know however however large those registers are are basically going to Define what your memory layout is going to be if you have a 32-bit CPU registers you're probably going to have 32-bit machine um okay so the word size so this is the width of the information that we're going to be able to reference like this the datum the smallest data objects that we can represent are going to be 16 bits right or the largest I should say the largest data that we can represent are 16 bits right and then we're going to say well we have 256 slots so this is addressable over here we have 256 of those that we can reference uh storage
locations right so this equates to 512 bytes of memory that means I have six 256 slots that I can those are addressable memory locations that I can I can represent this actual storage locations with so that's 256 times 16 bits it's 4096 and if you divide that by 8 bits per byte you've got 512 bytes so this particular machine right here it requires 512 bytes to have this memory layout right so I'm going to kind of like walk us through what the memory implications are for that
so we have this kind of like little thing here um just so you know for this it takes eight bits because you have 256 slots here it takes eight bits of information to reference all of the memory locations but you have but you're limited to 256 slots okay so uh let's do instead of having 256 slots I just want to do a little bit of a uh it's kind of like a a little example here of what happens if you go one to one so instead of having 256 slots that represent all of the available memory what if you just had one slot of addressable memory point to one memory location it's not storage
this is Ram right so you would have two two the six I don't want to get too far into the computer science but does everybody know why 2 to the 16 is your total possibilities there no okay does somebody want to I can say but does somebody want to say because it's binary right what does that mean binary zero one so yeah two different types of data which is zero one right and then you have 16 which is your your word size so you have the option of having zero one so there's two options and you have 16 of those options so to get like the total possibilities a lot what you'll see is you'll say okay so you have
binary which is two and then you have 16 Slots of or 16 places of those so 2 to the 16 will give you about 65 000 possibilities so if you have zero zero zero zero one zero zero zero zero zero those are two possibilities right so you can get total possibilities of sixty five thousand five hundred thirty six locations right so if I have a 16-bit machine I could have this many of those memory locations that I can point to and if I do one to one that means I have 65 000 memory addressable slots that I point to 65 000 locations right so if we have 16 bits per slot we get to to do this one to one we'd need about 131
kilobytes of ram to represent that okay going forward yeah the same wave of colors yes yeah uh so you can address like so if 8-bit color like how many colors can you represent with eight bits you know or you get this what I don't know the colors but yeah this what is it the c-y-mg it's 256 for eight bits yeah we exactly exactly I said there was 8-bit representation here to do 256 slots 8-bit color 256 different colors this is just binary calculations for
possibilities yeah I mean yeah these are physics numbers right um so if we did 32-bit we had a machine that could understand 32 bits of information and we did one to one that would mean that we'd need about 17 gigabytes of RAM to do one to one right that's like 17 gigabytes of RAM that's not crazy right um uh we see like my phone I think has I don't know well that's hard storage uh like Ram today I've seen like 64 gigabytes right that's not uncommon so let's go one more up so this is 64 bits so 64 bits of slots that point to 64 bits of different memory addresses I
would need this many gigabytes to represent that right so so let's let's do the math on this machine right here so Toby's MacBook 13 inch 2017 don't make fun of me 2.3 gigahertz I5 has so this machine has 16 gigabytes of RAM right well I need if I do this one to one all the storage slots that I have point to all the memory locations I need about 147 million terabytes of ram to do that yeah it kept going up I was like where you guys aren't going to know so we've got
exabyte and then petabyte yeah so I was like I'll just keep it in terabytes because that sounds really big so how do I get 16 gigabytes of a that I have of ram here to represent what I would need to do one to one that would require 147 million terabytes like how do I do that that's kind of crazy I don't have that like nobody has a machine that has that many terabytes of ram not this is not storage is Ram right how do we get 16 gigabytes to represent all those terabytes yeah just keep ramping it up keep ramping it up the other little Kink here is that you
want to allow every single program running on your machine to have access to all of that space that's what your operating system does so if I'm running 10 applications they all get access to all of the memory on my machine so our super shut what was it shadowy supercoder right here they're running all these applications every one of these applications has access to the entirety of the memory on the machine how do you do that swapping what right that's paging out but that's not what that's what happens mechanically when you need something that's not there but what how do we manage that
could do threading not really but yeah yeah that's like a consequence of how you manage multiple things but the answer what we're looking for is virtual memory here right we're going into the virtual world so virtual memory I'm not going to read this whole thing but I'll say virtual memory is a storage allocation scheme in which a full range of memory addresses can be made private to each individual running program and addressed as though they were part of the main memory of the physical computer let's jump down to the bottom it is a technique that is implemented using both hardware and software it Maps memory addresses used by a program called virtual addresses into physical addresses in computer memory so this is how we get 16 gigabytes on my machine
that can represent like tons of possibilities of of possible memory locations right this is Ram this isn't a disk so our discus here that could be as big as we want but we're limited to the amount of memory and it's kind of ironic we think of memory as like how much a computer can store but when we talk about a machine the memory is actually how much of ram you basically have because that's what the computer operates with the registers are really really fast on the on the CPU they get accessed you get access to the CPU to this your memory layout and then it gets dumped to disk if you aren't using it or it gets stale or something like that so the CPU here is basically using this page table to
say give me you know I I stored something in Slot 2 here give you know what what did that point to and it points to your physical memory here go give me that information if the stuff if the memory here gets stale or you need more space in your page table you'll page that out to disk and then if you're looking for something that wasn't there you said well give me you know give me what is a zero one two three four give me what I had at four it's not there page it in from disk all right so this is how your computer works okay that was as far deep as we're going to go into computer science you just need to know that there's a
scheme that operates that kind of takes finite resources right we have a limited number of memory in our machine that's denoted by the width of our word size of that machine right and we have some kind of schemes for managing how we do that with limited resources we don't have all the resources enabled for our machine to say we'll go to memory address you know 32 billion and give me what's stored there right it says well I'm going to make this little space and then we're just going to keep virtually addressing the entire memory space that we have that we our operating system thinks it has right this is also going to come into we touch this a little bit I think we talked about it in the one of the workshops
which is how do you represent 2 to the 56 bits of storage in a mapping right it doesn't all get represented it's just what do I need to have access to and then it's kind of like a virtual representation of I'll get it whenever I need it but I don't need it I don't need to have it represented all the time let's go over Sam and then exactly exactly it's like in our mind I have access like the CPU gives the impression to a program like hey you have all this space
whatever you want and the program says okay well I only need to do this little thing and then the operating system will manage that but it says well if you want to use the whole thing you can but I also have you know 20 other programs that I also say that to and so the operating system a lot of time just lies and you know gives you kind of a pretty interpretation of that you have access to all these resources but you don't we can do stuff like you know you page in and out or you you know you only like run one program for a couple milliseconds and another one actually less than that uh germano to have you're you're creating Computing debt like like uh yeah I guess you could do that credit card right yeah like you can use this for now
but you don't necessarily have to pay for it right now right um okay so what does this have to do with anything well let's go back to the evm okay so what type of machine are we programming for it's the ethereum virtual machine so what are those specs yeah you don't know nobody does this does does anybody know what has anybody read this yeah okay well we know what it exists because the Bitcoin white paper was white I don't know why they I only know that it was not this is I think it was a second yellow paper as a preface for the more features that
they were playing I think the yellow paper is forever changing but the white paper was the initialization of like I think didn't vitalik initially write the white paper I don't know and then I think this is the technical spec the yellow paper is the yeah yeah if you ever hear Gavin would speak I would recommend it it's a very smart guy who built ethereum interesting uh okay so I'm just going to read the top here this is uh from the ethereum white paper just I would take a just know that it exists if you ever need to dig down in there but um you know I think I've probably like five times referenced this so it's not something you're going to do every day but so 9.1 Basics the AVM is a simple stack based architecture the word size
if you know what word size is the word size well you don't have to know what it is but you know that we referenced it here 256 bits why this was chosen to facilitate the Keck 256 hash scheme and elliptic curve computations right so it's natively going to accept what um the hash spits out all right and the memory model is a simple word addressable byte array this stack has a maximum of 1024 stack frames right that's going to be important okay so our word size is 256 bits our memory addressability is word addressable and we have this stack versus storage contract thing yeah 256 really big okay
so here's our memory layout we have storage this is like contract storage starts at zero goes to 2 to the two five six minus one it's 256 bits wide hopefully I laid this out so it was familiar with the one before right so you start here and you go up uh or you increase in um in minor increasing the the number increase the numbers not size I mean what do you say like just increasing the number right so the memory is a word addressable byte array I just made it horizontal because you can think of it as vertical it's just it's kind of the idea or the analogy that they give
of it thinking about like a computer science array which would be just an indexed array you can read 256 bits wide question you know yes no no okay uh and you can write in either eight bits or 256 bits and it's Square it scales quadratically so it gets kind of expensive to write to if you do a writing a lot and then you have your stack so this is your call stack and this is 256 wide So the instructions that it can take are 256 wide which means like put put on the stack this really large number that's 256 bits it can take that but it's only 1024 tall if you want to say so what does that mean well it means
this is where you revert when you revert all right you revert right there if you put up a lot of op codes and a lot of those you don't you know you don't have access to actually because there's the machine is doing things for you if you fill it up you will revert and you'll see this I've run into this a lot when you're if you're doing a really complicated function and you're putting a lot of variables in that function a lot of local variables at a certain point the compiler will tell you you need to use less variables because your call stack is full there need to be multiple stack frames in a single function
so you're going to think of this every each one of these in op code the stack frame but the op codes get shoved in there so store read jump are all all put in here and each one of those OP codes has gas associated with it right this is how we calculate this stuff all right so our word size is 256 bits here is the implementation of gath the golang implementation of the ethereum virtual machine uh that's the op M store M store 8 s load and s store so what this takes is a pointer which is a un 64-bit pointer to the program counter
now the program counter is basically the amount of resources that you can use to operate all of all of the address addressable space right so this isn't truncate the 256 bit uh word size that you get to deal with what it says is within operating a program on the ethereum virtual machine I can have like instructions that are 64 bits a 64-bit number which is a really really big number you're not going to even use this much resources so not only do you have a really large um word size but you also have a lot of space to account for what is happening in the program and because you're re
you're so resource um constrained with gas you will never use like that's just a huge amount of of program counter resource and the program counter is basically just going to keep track of like how where you are in your instructions like how many instructions do you need where are you jumping to all those types of things you don't need to know exactly what it is you just need to know that you've got tons of resources on this machine you'll never ever use fully okay so let's go back to storage so the contract storage starts at zero and goes up right so why do we care about that well the proxy that's going to come into play when we deal with proxies because proxy contracts are gonna
deal with the contract storage in which you give to it so what is a proxy contract what is a proxy do we know what a proxy is yeah stand in anybody else yeah yeah amount of data for a bigger thing like a representation type deal s middleman intermediary okay the Webster's Dictionary says a proxy is an authority or power to act for another
so if I just Ram in a contract into that I say The Authority or power of a contract to act for another okay so that's the idea of a proxy contract you have an intermediary representative that will have power to act on the behalf of another contract kind of works like this what is it shadowy super coder it's over here that's an eoa much more much more rich all right so that's your eoa what's our eoa what does that stand for look at this peanut gallery all right nice okay uh so our externally owned account will interact with a proxy and
that proxy will point to an implementation contract right so maybe the logic is here but we deal we interact with this contract right and then I think Adrian uh no maybe no not well Adrian's referenced something later um so what I can do is I can still interact with the same contract but I can use a different implementation so the idea here is that you this address doesn't change I can always just provide that to the community and say hey everybody usdc or tether or crypto kitties here's the address right and from that I can have different implementations if I wanted to because this
contract will act on behalf of what is actually executed here if proxy is a smart contract and it's how do you get I guess if you just make it a bear yeah does everybody do I assume that everybody with the the folded Corner knows that that's a smart contract I assume that but does that is that clear yeah I should change that yeah if it just had just four I know but what I'm concerned about is if everybody understands this thing because I put these in everything and if you don't know it um so we'll get to that so that's that deals with upgradability right how do I upgrade this
if I'm dealing with this thing is that what you're asking is it should we look we'll look later okay okay uh I know for I'm yeah the answer is yes I know for I know for a fact like usdc is because I told you last week like I was looking at that and there's a lot of like fallback just forwarding calls to other things um okay so let's talk about libraries uh because that's in this week as well actually this week is all governance you don't even do any libraries but we hit them all the time has anybody ever heard of safe math this library is synonymous like everybody uses it why do we use it you
don't need this anymore actually which is kind of a nice little treat you guys are lucky now uh this is just basically a library for Math and it back in the day before the compiler handled overflows for you this would account for if you overflowed uh your adding or subtracting it would throw an error so that is used all over the place now people kind of make their own math libraries but you have signed math what signed math this was I'd say unsigned signed would mean that you could have negative numbers in there like a signed integer yeah yeah I know that's bad naming um but there's two styles of these libraries I think Adrian did reference this last
time with what can be in a library but before we get there you can have a library that gets pulled into your contract right you take all that code it's like almost inherited it's not inherited because it's not a separate contract but it gets pulled in as if it was inherited you could have that in your code or it could be an external call it's it's deployed somewhere and then you interact with that contract right so strings if you guys are doing nfts I know these are you use a string Library a lot if I import strings and I'm using strings for un256 tell me how that makes sense then it will pull this uh if I import it it will pull this contract code into this contract itself if I like verified
it you would see that it's one of many contracts if it's a flat file and you would internally make calls to this library right but I can also have the library deployed and if it's deployed I can make what's called a delegate call to it a delegate call will grab whatever is in that deployed contract and run it on top of my contract right so I run your code on my state right sold changes because it's also using your proxy another proxy like what does that affect
yeah so you're saying if this uses this and this uses this and this changes well what do you think would happen not fall apart it would just use this this logic over here if this changed it would use well so this happens does anybody know what uh Chad can you help me out here the the multi-sig wallet for what is it yeah the parity they were using a math library and somebody destroyed that library and then they they locked their
or was it no it was a multi-sig I think it was a multi-sig it was a it was a deployed multi-sig that they were using and somebody just destroyed it they ran self-destruct on it just because they were playing around and it Bri it like locked a lot of funds for them so yeah so what I would say the answer to that is I would personally if I'm using a deployed Library which people you this is a way to save gas right because you don't need to import that code so it makes your file smaller deployment gas I if you're actually running the calculation it you'd have to look at how much it costs to do the delegate call yeah it is all what every every part of
this is a trade-off so is that just like the reason of like using an external call versus just importing versus like Humanity yeah no there's more reasons than that but you that does fall under here I would say if I was using an external Library I would make sure it's immutable like it is not itself a proxy I would say what is in that code do I like you know does it work and then you would kind of audit that and if you and if it works and it works for what you need then you could use that but I will say like if you needed access to a lot of libraries you're not going to be able to deploy all those so if I needed access to like a calculus library or a in an integer math if I needed access to Strings if I needed access to a bunch of
stuff I can't fit all that in my code because there's like a 24 kilobyte limit on the deployment I mean that changes with some some l2s but typically you have some kind of limit on how much code you can deploy so there's a factor in there but typically you know I wouldn't necessarily use another proxy I would say like what code is running because that code runs basically in here on your state right but the delegate call is kind of this kind of like neat little thing it makes it possible to implement the Library featuring solidity and it's reusable Library code that can be applied to a contract storage so this idea of delegate call opens up upgradability and governance for us and is everybody clear on what that is
because that's kind of an important one I want you all to get that so you have a call right dot call we can use that to transfer funds and things like that I think you've used that this is a DOT delegate call it's right here does everybody know what this does it uses this code and it changes this state okay is everybody clear on that kind of a weird concept yeah is it is it falling into another contract are doing it right to their call that contract is running and is running using the state within that limit where it's a delegate call is saying take that function and apply it to my contract exactly
it's almost like inheriting at runtime in a way yeah yeah yeah yeah yeah it does it it operates I don't know if you could think of it that way I don't know if that's what happens but it does pull like this code onto the stack it just uses your state so like you said if this was a call the call would do stuff over here and then return whatever you wanted to return right it would change things over here and say true you know did that happen or not like this would happen with a token this is what happens when you transfer balance like a transfer token value right you say change like some kind of State on this contract and then tell me something back true or like
did it did it go through or not is my terms out there but would this technically be composition over inheritance in a way typically composability means you can interact with maybe I don't quite know if it fills in maybe we'll say maybe all right so now that we have a delegate call Devin do you understand what the delegate call you can say we can talk about later if you a little bit so I'll go through one more example um with upgradability and if it doesn't let me know and then if it really
doesn't we can talk about that after so with a delegate call we can do upgrading so what is that look like so we have the power or the authority of a contract to act for another contract right so this one gets the request this one does the request right so if I have some State on the proxy contract so I have X here right and I and I have this function over here you know you see that the state is mirrored right and then I write the the logic here so I have this function f of x and it says that X is equal to X plus one basically right all right so if I call f of x on this
contract it writes one into the state of that contract also writes one here but we don't really care this is the contract we care about this is where our state is by state does everybody know what I mean by state it's the storage it's like the physical yeah yeah everything it's your total like uh snapshot of all of the memory within your contract all right so I forwarded a delegate call I said hey Run f of x f of x runs and then it changes my state here is everybody clear on that so Toby real quick all your state has to be defined in the proxy contract prior correct we can get to that the answer is yes otherwise this is an amdar's question which is how do you upgrade
this contract if you're pointing at these right uh okay so what if I change uh the implementation from here to here and I say we have state we have x y and then we have these two functions here right uh so we have f of x is x equals X plus one and F of Y says that y equals x times two right so I call F of Y what happens over here yeah that State updates right so we had what is this F of Y it says so before I had um this is one right so it says all right whatever's here so this is X
whatever's here multiply that by two and save it here and that's what happens over here it says well what's here is one multiply that by two that's two store it there right so we get one two right what about the other one what about that that's good point okay so this is one here right we don't care about well and then I don't care about it here either because it's zero right there X is zero so that's the execution so if the
execution reverts it's going to revert but that happens well you're it happens on your stack well the state is changing here only the state is changing there so you're saying like if I do X plus one I don't care what told like where it told me to do X plus one I'm just X plus one goes on the stack right Plus you get an OP code for addition right I don't care where that logic came from but what I do care about is where does it get written to and where does it get read from but is it actually mutating State no yes yeah it does but we don't care because I never read that it's valid on the proxy but invalid on
it does that cause an issue with your proxy contract it could this is why you get two initializers typically it could you if you do get off I see what you're saying like if there's some kind of state that would be would fail here but wouldn't necessarily failed here because this one carried over it would fail yeah yeah so there is this concept of initializes which will get to uh I won't get to it in this but you do get that in the modules of this idea of initializers although if you're in the week seven Discord I show you why initializers don't work either so fun question yeah I'm gonna say so you're basically leveraging leveraging is a loaded word we're giving
ourselves the ability to change that we're giving ourselves the ability to change this stuff in here so I could say f of x here could be plus equals and that might be confusing I'm sorry I should have been more explicit but X could be plus equals two in here I couldn't do that here I just added a different completely different function and it's a bad example because I also added State here which we'll get to in a minute because that's the main purpose of the no main purpose of the proxy is this address never changes and I can change these hopefully right right it's what it's what well there's different reasons for proxies but you could have a proxy that
is upgradable and this is how you would do the upgrading right you would point to different things and then you would and those things could change but your proxy is and you'll see there's a different there's a couple different versions of proxy contracts in the modules you could have just a pass-through proxy which has a fallback function and just forwards everything through it or you can but then you get but that is harder what happens with that is if you're trying to maintain State you would wipe out your state every time so then you get this idea of upgradable proxy contracts where your state is stored here execution runs somewhere else but you always maintain your state and that address never changes right you're always pointing your users to the same address you don't lose State over
upgrades that's the kind of idea with upgradable proxies yeah main reason we use other content points like a proxy conscious to point to another because functions themselves can't change what the state can well ask yourself that question why why don't I just keep changing this yeah but you can but it will make a different contract address and then you'll say like oh users last week we were at zero one two three four now we're at zero four five six you gotta change and then the next week after that you say oh we're changing again and then you lose your state right and then you lose your state so that means that you can have like an owner that can be passing like I'll check the address okay that makes a lot of sense okay all
right um I'm just gonna get to why this is dangerous okay so so we got there Chad let me know if I didn't answer that question I kind of forgot it um but I'm getting I'm getting to this idea of proxy upgrade ability okay so the code is used from the implementation contract the state is changed on the proxy contract proxy contract address stays the same implementation contract can be changed with different code right not addressing um Sam's Point we don't address that here but it could be the case that you're state in your proxy could be different if you re-implemented a different contract your state could be different if you don't handle that um but for this one let's say that our
proxy contract is one two three four right so the user always sends transactions to one two three four and in our proxy contract this points to 3333 right we have one one one two two two three three three all right okay so storage layout must persist across upgrades right we don't care what this storage necessarily is but the layout needs to be the same why those so if you were to have a different layout the function is to be essentially acting on it's not an expected data structures right it's expecting the
layout to be well it doesn't really care but if the layout changes it will it will change how the data is read and stored right and we'll see that here yeah on the implementation X also has to be us ing right so if you go back to this way the bit the the word size you get a you get this length that you can shove things in right you get like this box so if I have a an 8-bit representation I either get that in one 256 bit storage slot or I've if there's a couple of those eight eight
bit integers I can put them I can stack them in there so if one of those changes it will affect how your layout is changed right so you have the data type which is important right and as we'll see you also have the actual variables themselves because you have such a large word size anyway oh I see and so you know if you're only using one 8-bit integer in your state for your home program you're still getting allocated 256 bit Gene yeah so the compiler strangely enough optimizes 256-bit integers
so you will save on gas if you use those however if you're dealing with structured data you can be very crafty about how you lay that out so that you can actually fit your storage footprint you can make it a lot smaller by putting let's say like three U and eights together instead of saying well yeah I have a una then I have an address then another una and then I'll have a mapping so you can put those together in a way in which the actual footprint becomes very concise savings to optimizing for storage footprint because it sounds to me like gas is more related to CPU Norwich resources so every op code has a
gas cost so if it's having to do more I think this is a I think this is more of how much it costs you to write and read because those are the most expensive process actually the contract deployment is the most expensive opcode then you have a re no then you have a right is the next most expensive then you ever read those are the most expensive things to do on the evm so this would be how to make your layout more concise where were the savings come in I guess maybe it would be writing I think it's deployment gas in that particular one but I don't know about computation as far as like you had
like you had said you wanted to see like pointer arithmetic I don't know if it's more optimized if it doesn't have to go to a whole if it doesn't have to jump to an entirely different like memory address if it can point to the same one and then go in I don't know okay let's see what happens with don't do this okay what were we not doing here okay so uh uh oh I see uh did we okay okay so let's sorry there's a subtle change here right uh I have X here and Y here right uh X Y what happens if I switch them my employ uh the implementation switched same data type you in whatever un8 but
the first one is y and the second one is X so what happens when I call uh don't do that what happens when I call F of Y on this it'll be looking for that place of memory where X was previously well let's just say so if I call it okay so what's my state X is one y is two I call F of Y what does y do it says take whatever is in X which is here multiply that by two well what's here that's two two so that's so I say 2 times 2 is 4 save it to y Y is here so it writes it there so it puts four there all right did I do this right man I was
like really yeah this is so confusing right so I called f so I called f of Y on this contract and it wrote four on my X I didn't expect that to happen so what happens when I call f of x f of x says take whatever's in X which is here right what's here is now it's four right that's my state there add one to it which is 5 and store it in this spot right which is my y so now I get 4 3.
question well let's see you tell me here we go here's my state proxy contract says your implementation is an address that's my first storage slot next storage slot is X next door slot is y i point to three three three implementation contract first is y next is X all right no problem here's our back to our storage layout we start here this is where you when you have contract storage you start here and you go up right okay so I call so I have this storage so slot zero is that's my implementation address
slot one is one that's X and this would be y and then the implementation would say I have y here and X here okay so uh I run F of Y what what happens F of Y what does it do it takes whatever is an X which is here that's storage slot it's one technically but yeah let's let's take zero one let's take this okay and then add one to that so that's two and then you write that to storage slot zero okay so I wrote two here that gets this is what storage slot zero
is when I call this proxy it points to zero two okay everybody should be like oh my god what happened you lost your Implement your last year implementation card you just bricked your contract yeah we could go over that many many times percent and implementation that appears to be correct um and kind of get out from underneath Auditors in a way if they're not paying attention to the ordering of their decorative decorations this is malicious this is by
accident this stuff happens all the time and there are in what safeguards do we have for tooling to kind of catch these things if it's accidental or catch these things and malicious right so that's what the module is about it'll show you you know what the idea of initializers are what some tools are you can use to protect against this what the current design pattern is there's an EIP for this it's kind of fancy dap idea great all right let's go through this one more time because it is very tricky um first of all what did I do here what did I mess up first I switched these which is awkward right
I switched those which was bad I was a carryover from the previous one but what is the really really bad thing how many of these do I have two how many of them are over here I inadvertently kept what I thought was important which was I need to represent X and I need to represent y but I also really really want to implement or keep track of what my uh proxy contracts points to that's an address right what happens is I left that out here so think of this as you know you have this block of memory it sits in here and you have this block of memory then sits
in here this one operates on this as well but it it changes it over here and we only ever read our state from this contract so what this does is it say when I call F of Y I'm looking at this right this is here I'm looking at it and I'm calling the code over here but the way that I've told this code that it has access to variables is that you have a variable in slot zero variable in slot one and then that's it over here I have I have a address in slot one a variable in slot two or sorry 0 1 and then two here I have zero and one
first of all I have a mismatch in my layout second of all I done a really bad thing where I said the most important thing is what the contract points to however I left that off here so what this does is say Call F of Y so F of Y says all right I'm going to store something in y and here that's the zero slot right take whatever is in X and multiply it by two well X is my slot one so I'll look at that and I'll say all right because I'm looking at this data here it says I have one there right so this contract doesn't look at this contract this contract pulls this in it doesn't Pull It in literally but it
uses this for On The Stack runs the numbers off of what the storage layout is here right so it says great it uses yeah that is that is what runs on the stack you know it doesn't like pull it in there and then doing it you know the the basically you just told the virtual machine go to this address pull the execution code and use the state that is in this this address here right so what it does though is it says okay whatever is in X which is my slot this multiply that by two so one times two is two and then store that in whatever's in slot zero it gets stored here right what happens is I just
overwrote what this is pointing to with just a very important two yeah with two because it doesn't care it's using this layout but you've confused it to say I I pointed the I messed some stuff up here what happens is when you go when you tell your users to interact with this contract again it points to sorry points to whatever two is right whatever that is we don't even know what that is we don't have control over it we don't have anything and if it does nothing we just bricked it like you can't use that contract anymore because what happens is it says go ahead call Y of you know X of T of f of x it goes okay well let me go to two two doesn't even exist or
certainly doesn't have f of x on it of which do I do I build yeah yeah but that's the whole point of the product so everything yeah so the the answer to the question Chad had and I think number had this earlier is all the state has to be laid out initially laid out like you have to know everything that the state can do and if you need to change the state you have to redeploy this and you have to tell everybody all right well you we're looking at a new contract here but if there's changes you can make to
uh or like what sort of your ruggability is limited so I put in I put into the Discord this uh auditor Riptide who got 400 eth for finding a bug with arbitrum's sequencer and they used an initializer which you know how we write con The Constructor in all of our smart contracts and that runs once on deployment well what doesn't run when you switch and deploy a new implementation contract your Constructor in the proxy so the way you get around that is you write
initialization functions that will run every time there's like a new upgrade and that should kind of like manage State and also do things like well set things that need to be said on this like if this point somewhere else make sure that that's pointing at the right spot well Arbitron had a little mistake in their initializer it didn't run so it had one of these things where it was like well whoever wants to be able to upgrade this can upgrade this and it was like address zero and anybody can like shove in their own address so you could basically like make this this um I think it was it wasn't their Bridge it was their sequencer but it controlled like what all the money was doing and so you could basically tell that where to point and it was a huge bug
right it was like uh he has it's funny if you watch uh uh follow this guy because he he has like he shames um companies when they like don't pay for Audits and stuff it's like they go on a Wall of Shame so it's kind of funny but that one I thought was particularly interesting because Arboretum has a very very good team and they still messed it up you know everybody messes it we all mess it up that's kind of the idea of contract upgrade ability for governance um we put all of that together and we kind of we kind of let the community vote on implementations when we switch them things like that um so we kind of say let's vote on when to make change to an upgradable contract
and so this stuff really has the power when it's all on chain if you want to like vote to all wear purple shirts one day that's less interesting than when you say we're going to release funds from the uh uh the treasury right so you can do that on chain you can't really make people wear purple shirts maybe you can but I don't know how you do it so this is kind of what the layout is right so we have uh everybody has some voting tokens and somebody makes a new proposal to Target a new implementation and they send that to the governance contract so our top shadowy Super coder says Hey I want to make a new proposal and so that's proposition one and then um
everybody gets to vote on that so there's a new proposition that came up and everybody can vote and it makes Quorum which means you know passing vote however you want to calculate that after the voting period you can send a transaction and you will switch the implementation right and that's all on chain and that's all done by voting like there's no intermediaries there well there's everybody basically comes together it's a decentralized way to switch to make that switch and there are different ways to do this I mean you don't necessarily have to do typically you have to wait on the voting period in order in in order to do something but you could also have it automatically engage if the Quorum is hit and that's how your governance contract will work when you do it um but um
let's see now yeah okay I'll I'll kind of run through one gotcha that I found with the hard hat task for governance but before that I just wanted to show you like how this actually works because one of these companies that I followed for a long time actually does this a lot I mean there's lots of companies that do this um but hop is one that is um they're they're a bridge uh basically they uh you can use hop to send funds to l2s and they have a pretty Lively uh governance uh community so this is just from their page about how to do their governance so so this is the process so they have the hop The Forum that people can use they use Snapshot
which is a very common tool to see you can either do um uh temperature checks or that's how you can allocate voting distribution by a certain snapshot a certain time period to see who has what of the coins and then they can they use uh their governance portal they have one and then they also have tally XYZ down there which is the Dow uh platform for voting so it kind of looks like this uh you you can do a request for comment ask a general you know you start this way um you know should hop governance add liquidity right so these are all on-chain things right what can we do on chain and typically um if the community supports it you can
you can actually you know make a proposal and then vote on it and if it passes you can enact it right uh so they do a temperature check uh either through their Forum also with a snapshot and you know you asking for comments and they go through a whole process let's see after the end of five days uh whichever option has the majority of votes wins and can be included in a governance proposal for stage three a 300 000 hop yes vote Quorum is required for a temperature check to pass right so they do this this is what they do all the time say again oh this is you're saying this
has been up to date yeah awesome cool um so they have uh here's the actual governance portal where you actually make the vote and the the idea here that I want to stress is one this is happening in the community like this is a project that does this daily you'll see they do it the exact same way in which the government governance contract that you'll work on does it and um it's all on chain which is kind of exciting but also again you can't like not you can't make everybody in hops Community wear purple shirts but you can maybe allocate where their treasury is going right here's how they do the here's how you make a formal vote I'm just you know this isn't you don't need to know all
this I just want you to know that they've all laid all this out and they're doing it and this is exactly what the contract that you're going to write all right and here's the glossary right you have erc20 tokens uh you have the delegation of those tokens right you can make proposals there's a quorum which needs to be satisfied in order for the votes the proposal to pass you have the voting which actually is is happening uh you're sending you're making transactions on the governance contract there's a voting period And there's a time lock right I thought this was interesting a proposal is code that is executed by the governance contract through time lock it can replace the governance contract transfer tokens from Community treasury or perform an almost infinite range of
other on-chain actions so it's on chain right so I just want I just want to point this out because this this stuck me and I don't want it to it was kind of a pain in the ass to get around this but um here's the the contract that you'll basically build for the entire modules will you'll build the governance contract and solidity but for the task that you have to do for this week you just have to engage this uh governance contract so you'll make one my Governor what does this mean brilliant we've done something right governor governor settings Governor blah blah blah right and so what is this
based based on track of what you're in here anymore Constructor keyword so the Constructor for governor has some kind of input that's probably the name because that looks like a string this one the settings has one two three settings Governor votes has the token in which you're going to be using here you're it's a it's an erc20 token that I think you like basically add to the supply of it so that's a address this is the percentage that a vote you need in order to pass and then there's a time lock here right so be careful with the block this is the number of
blocks you need in order to activate the vote and this is the amount of time that vote voting period is active so don't do like me and put five here because that's five blocks long in which the Voting is active and so you'll never be able to pass well you will but then you have to write a really complicated script in order to make the voting pass in five blocks because what happens is they say to reference the open Zeppelin governance wizard which are kind of fun didn't we show Wizards once yeah I think we did yeah there's kind of really kind of really nice um so you have the voting delay that that means you engage the vote right then you
have the voting period this is one week all right 50 400 what all right how do we get that number 12 divided by no 12 times or you could do so 60 seconds divided by 12 gives you what I've already done this I'm not that smart five blocks per minute how many are in a day well there's you know 60 minutes in an hour times 24 hours in a day times five what is that or times one hour well I mean no you I mean I do this stuff I always calculate these anyway
there's 50 400 blocks in a week at 12 second intervals right so don't make this really small because you have to do these uh you have to make the proposal then you have to wait a block then you have to vote and then you have to engage and then you have to like execute the vote right well at 12 seconds blocks go by in like a minute so if you don't do all that in a minute your proposal won't pass you can change the time yeah and then if you see it if you do a generic hard hat there's a a lock is a lock contract is the first one and
you can see how they put in time they use time with that and Foundry will do it as well where you can change the time right and then it'll show you it'll say like they'll do a uh a a unit test in hard hat and they'll show you like okay well when you do this you should change these values because we were doing this for unit testing right all right so then you got the uh the proposal threshold is here that's just like how many what was The Proposal threshold is how many votes you need in order to make a new proposal you're going to make it zero because you just want you're just testing it out and then the Quorum percent is four you can leave those as default this is just the one here you want this to be one because you don't
want to wait around and then because you're going to deploy this to goreally so it's going to be on a test Nest you don't really have control over that and then you just make sure that's something that's one you you have to wait for it to be over in order to execute the proposal so you don't want to make it a week because in order to do the task you'd have to wait a week you don't want to do that but you also don't want it to be to blow by really fast all right so we've talked about all these things and the final project again just to reiterate is a full stack dapplication right with a smart contract and a front end um some of the stuff that you're responsible for is the project name GitHub repo it has to be live so it's
going to be deployed and Cloud will be heading up a workshop on how to deploy uh front ends a whole dap pretty easy because that's one of the requirements if you don't know how to do that or just wants to want to see how he does it I would encourage that description short lung how did you make it work presentations right logo cover image screenshots and the demo video so and we're accepting uh hackathon projects from Denver as well so if you do something there you can turn that in uh to us as your final project and then if you want I can show you what one of these looks like this is what I built after my boot camp and uh with that this uh this series is coming to a close yeah
I'll show you this so this is my token shop which is big for some reason I know what the heck I think it's my monitor like it's like my monitors oh what happened there it's been a while um so I did this presentation at Edge actually uh which one do I want a couple weeks ago and I somebody had looked up like my um how does this work who knows somebody had looked up my
my website and went to this and it was like hey I tried to do your thing and it didn't work and I was like of course it works and so back and forth with this guy and uh it ended up it didn't work because it was a really nice little it's not a little kind of logical kerfuffle that happens with this um I'm not going to tell you what it is but if you want to talk about we certainly can but basically I wrote when I initially did this I did this with oracles so you can basically buy Toby tokens with ethereum now I wrote it because now the stable tokens are so synonymous I just it's a swap contract so it just swaps out whatever you want I've checked it it does work but you
would basically like approve a near C20 token I've already done that um and then you would trade that and the whole thing here was that uh so basically I'm going to trade one what one usdc four one Toby token right and I'm able to spend like a lot of them that's not how many I have that's just what the approval amount was and um you know there's a little bit of uh contract um introspection but this doesn't do a lot it just basically is one component of a social uh project that I made where you buy these online and then you come to me in person I'll pay you back in cash and you can burn the Toby tokens right and so if I do this I'm basically going to spend 13 cents on arbitrum to
buy Adobe token for one usdc all right and then that happens and then I think I should it's pretty fast I might have already missed it if I look at my assets I've got I had five now I have six so and then I paid one usdc for that so uh if you wanted to actually look at that I'm not lying but it does work and this is what I built um it was I think the I think the um the uh so this is on arbitrum mainnet um we can talk about l2s if you want um but I thought the Oracle was a little bit more interesting where it actually would do the dollar conversion of the price of ethereum but I used an oracle service that changed their API and then
bricked the contract so that's also something to think about is if you're you if you're using a uh infrastructure provider that changes their API or changes how their technology works and it's a smart contract that you can't change they could be the ones that make your contract not work anymore so that's a good lesson there it was not chain link but now you can do on-chain price calls and things like that so it's very simple but before you actually had to scrape a price out of a URL and then send it back so it's kind of fancy but also doesn't work anymore because they change the way that they do that all right and I think cloudy want to come up here to do that or do you want to go in the workshop do you want to take a break what are we
doing I'm done um yeah I mean shoot oh man don't just let's give it up for Toby that was amazing um I guess uh do you guys prefer doing it in the workshop or up here for clouds Workshop you just we can we can do governance here right we can vote on what we think oh purple shirts purple shirts so who wants to do it up here workshop and we'll take a break regardless but Workshop all right we'll do a workshop last thing I want to say is thank you guys so much for this uh this has been a trip um we did seven weeks the trial by fire had no idea what was going to happen but I think it was really successful and we're excited to continue this boot camp and do another
cohort and another one you guys are the Genesis block of this uh this boot camp so congratulations um and shout out to all the mentors every single one from Toby to James to Cloud to Adrian right um so let's give everyone a round of applause great job everyone thank you so much for coming and to you we are very proud of you and see you guys of course pizza's coming too soon
Related talks
- Blockchain Basics Full Course
- Introduction to Smart Contracts
- Структура Ethereum Virtual Machine ▫️ Dive into Solidity ▫️ Эльшан Джафаров
- Как работает EVM: практическая часть ▫️ Dive into Solidity ▫️ Эльшан Джафаров
- Как компилировать Solidity в EVM байт-код 🔘 Эльшан Джафаров
- Writing Smart Contracts without Solidity
- Off-Chain Execution Environment for Testing & Profiling of Smart Contracts
- Unlimited Size Contracts Using Solidity
- Ethereum Protocol Fellowship Cohort #4 Town Hall
