# EVM Storage Explained - Upgradeable Contracts

- Channel: [ETHCluj Meetup](https://streameth.org/ethcluj-meetup)
- Date: 2024-10-09
- Duration: 41:31
- Topics: decentralize, ethereum, ethcluj, eth, ethereumcluj, developers, solidity, EVM, EVM storage, smart contracts, gas costs, opcodes
- Watch: https://streameth.org/watch/yt-_9x7b2I_xG0
- YouTube: https://www.youtube.com/watch?v=_9x7b2I_xG0

## Description

In preparation for designing and working with upgrade-able contracts, we need to first grasp how EVM storage works.

Catalin Balut discusses here some of the important aspects around:

- Opcodes, gas costs, static, delegate & simple calls
- EVM Storage context
- A few smart contract examples

## Transcript

so the talk today is part of a series of three talks on contract smart contract upgradability so before we tackle smart uh contract upgradability we need to understand better how ethereum virtual machine storage is organized and how it was designed and Cataline will explain it better why it is important to understand the what is behind the scenes uh in evm when it uh related to storage so this being said please Kine I'll pass the mic to you so you can share a screen [Music] right okay my [Music] screen every to but too technical so I want to offer if you're developers probably I want to offer some useful informations about maybe some um things that you will study more in more separately so now let's start before the okay so the subject is the ethereum virtual machine storage explain the first of all I think this is the key of advanced solity development and as the title says it's all about upgradable contracts and how how this will affect all the protocols and the new smart contracts are built based on ethereum okay now before we start to talk about the um the storage I want to talk about the ethereum V virtual machine which is the heart of the ethereum protocol and I collect some facts that I will go fast through them and I think these are important okay so the etherum vter machine hand smart contract deployment ex and execution uh the only way you can avoid the ethereum virtual machine is for example to send a simple transaction like from an AOA to another AOA uh this will will do will be a transaction that avoids the eum v machine now um as you know it v machine runs on the ethereum blockchain and this make this makes it a global decentralized computer and um the AVM is a gasi touring complete machine as a state machine and why it's gasi because it's solving the halting problem how it's solving the halting problem as you know the a blockchain it's composed by blocks which means um it have a gas limit a block have a gas limit and the fact that blocks have a gas limit solve the halting problem which means the halting problem is when a process is uh executed to or it takes time like infinite time to be executed as you know this is impossible in blockchains because transa transactions are limited by gas limit so the halting problem on avvm is solved basically just because we use gas limits through blocks now the architecture of the AVM it's stack based that means it's storing all in memory values on a stack and it works with word size of 2 56 bits um this was chosen to be because it's the optimal version for hashes and for eliptic curve encryption I don't know why okay now we have to talk a bit about the transactions as I said the only transactions when you don't use AVM it's AOA to AA this is a basic transaction probably you know it have the NOS gas price gas limit but we have to pay attentions to this parameter two and this parameter data which change everything based on on um what holds on them okay so if you have a value transfer transactions for for example I I send some money through the ethereum the gas limit is standard it's 21k gas it can be modified the the two parameter will be the address of the one I want to of the one who I want to send the eer and the data will be empty now this is a standard value but when we when we do first of all uh when we do when we create um a smart contract okay so we have the solidity the solidity um file then we use a solidity compiler and we obtain two files one is bite code which will be deployed on the smart on the blockchain and the a ABI which is the interface to interact with the with the smart contract with the bite code okay now when we do a contract deployment the major difference is that the two field is empty okay so in this way the virtual machine we know we have a contract deplo deployment and the data will be the bite code as I said before this file okay the bite code will be uploaded to the blockchain and now I have a question for you as I said here the data is the bite code but at not asked a good question is see trivia when you compile a solidity contract you get the buy code and the deploy deployed buy code they are almost identical what is the difference someone can tell me if is if is a difference between the buy code and the deploy buy code I think that the contract is not included in the deployed by code what what do you mean by by contract the The Constructor sorry the code from The Constructor the code of the Constructor correct that is the answer because the when when we upload the bite code uh we the evm will take the first part of the bite code which is the Constructor we upload will modify the store data from the smart contract and only then we'll deploy the buy code after eliminating the Constructor okay so there will be no Constructor in the deploy by code on the on the ethereum blockchain good answer thanks now if another type of transaction is function call okay we deploy the smart contract as we s u so the two value is like from eoa to eoa but here is the Smart contract okay and the the main difference is the data it will be no more empty it will have as I wrote here a method select which is the name of the function and the parameters so this is this will be the this is the data transmit uh transmitted for transaction to interact with the smart contract now um if because we discuss about the AVM storage we have to know that a smart contract is uh is this this is not the way a smart contract is stor on the ethereum um blockchain it is it is compiled it is transferred to the buy code okay and then dis dis dissembled in op code now the op code is the part when the things get rough okay so it's very important to know some aspects each op code have a price each so each bite code here it have a price as you can see here the storage is very expensive it cost close to two uh 20K to store a value 5K to update one but as you can see here if we want to uh sum two numbers it's very cheap it's just elri gas but if you want to know the balance it cost 400 gas so is important to know what is um what we should store on the smart contract what we should emit as Vents and um um this will make this is the first step to observe how a smart contract can be optimize for the gas consum consumption okay now if you want to Deep dive more in the op code because is is rough I recommend uh this YouTube channel ethereum Engineering Group these are the guys from the consensus and they have um a YouTube video CH this is the video from solidity to bite code memory and the storage it's close to two hours and it's only about all codes o codes all codes and it's it's a bit hard to follow but if you take time to watch I think this will help help you as a developer good now how is the structure of the AVM storage okay we have the nonvolatile part which is the bite which have the bite code and the storage and the volatile part part probably you know the memory will hold um temporary data temporary data for example when you call a function you can um send um an array that will be Memory I I think it happened to you this will be stored here okay then when when the transaction is over it will be uh deleted from the memory because it's volatile as you know okay now I will have some examples of of how the the AVM um not storage can be influenced by us okay so if we talk about State variables as you can see here we have four integers and all of them have the um value of 12 okay but this this is the way it is stored in the smart contract as you can see there are four values why there are four values because in the stack each each variable it each slot will be um represented by uh bytes or 32 bytes okay so uint 256 is is the exact 32 bytes this means it will hold four it will occupy four slots in the story but if we change because we we don't need you in 256 is a very big number okay we don't need that to storage the number 12 so we reduced the U in 264 this as you can see here in the storage we can store all the four values in the same slot because each value contains all bytes so this is an optimization that will uh reduce your smart contract um storage and as you saw before the storage is very expensive so by doing this type of tricks on state variables for example um you will red reduce the price of the cost of the of the cost of smart contract but be aware oh I will show you phot okay so be aware Uh u in 8 can be more can cost more than U 256 so this is a warning from solidity docs that says if you are using elements more than 32 bytes your Contra gas usage may be higher okay why because the ethereum virtual machine operates in 32 bytes at a time as I said therefore if the element is smaller than that the ethereum virtual machine must use more operation in order to reduce the size of the element so for example if you use just a uint8 not like in this case because in this case we F fulfilled a slot okay we fulfill 32 bytes but in this case the the AVM have to do more operations to um to to use an wi8 so be careful when you try to do optimizations how you do it and uh take care to fulfill 32 32 bytes on a slot if not use use all the bytes for just for one um just for one variable good now some facts as we talked before what about the Constructor okay so the bu code deployed on the network does not contain the Constructor code okay as we saw before if if you want to use immutable and constant I recommend this why because as I wrote this here is a print screen from solidity docs where it says that the compiler does not reserve a storage slot for these variables so if each time you declare a mutable or a constant variable there will be no necessary to store a slot and this this will be U automated made by AVM now why is important to understand the AVM storage first of all for better memory management you will see when we do the upgrade upgradable contract is is very important to um to be careful about how you manage the storage and then understanding the security of smart contracts as you know re enty um harm a lot of smart contracts and more a lot of protocols so this is also a part of the um of of the deep understanding of AVM storage and also you can increase the per performance of the smart contract how because you reduce the the number of op codes are necessary if you understand how many um how many op codes are necessary for of function for example and also the gas optimization as you saw before in a small example okay so storing storing large data is not recommended uh to to be done on chain as you probably know uh if we talk about nfts we know that probably as developers we we don't Store The nft Meta metad data on the smart contract we use ipfs and if you are normal users you have to take care about this if if the users don't use if the developer didn't use ipfs you should take care because your picture of nft can disappear if they use I don't know a central database of a local database or a Ser a local server so take care to ask and check about this information because you um we made just with an index and without any picture of your nft okay now the most important part of this discussion is about the lowlevel calls the call the delegate call and the sty call and I I tried my best to find the best picture to explain this I don't know if mine explanation will be the best but I will try so the most important thing about the call is that the context of the smart contract will be the color context so as you can see here eoa can do only calls is the only way it can communicate with contract now if we if we are for example contract a okay we call the contract a it have the context of AOA but when uh in this case the contract a also adds uh some value the EA doesn't add value okay now when we call the contract B the context of the contract B will be the contract a as you can see here the message sender is a because we use the call and the message value is 0.5 because is the value sented by our contract other very important aspect about call is that each contract use his its storage as you can see here each contract owns storage which is the main part different from delegate call now if we want to see some use cases so as an EA you want to for example to batch mint for multiple smart contracts for example we have the batch Min contract contract a have a function batch Min and the batch Min function call multiple smart contracts or in this case we call the contract B and delete this arrow and consider also the contract a the function batch meeting after calling the contract B will call the contract C and like this we have a batch mint function that will call contract B and contract C but what is very important to observe is that by using call and sending the context of contract A to B and C the badge meaning nfts for example or tokens will be sent to the contract a not to the EA okay so it if you don't test or or if a use if a developer don't test or don't understand this thing it it will block the nfts on the contract a another another use cases for the example for example EA you want to do a reentrancy attack so if you want to create a reentrancy attack probably you know is impossible if you don't do it using a smart contract basically Orient attack is a smart contract that attacks another smart contract and um it change its state but not how um the you the smart contract would expect to do so this is a use case like for white hacking not for stealing money okay now as a smart contract you want to change the state of another smart contract this is basically like as I said before if we do the B if we use the batch Min function U and maybe this will have I don't know some storage about who Meed I will change the the the state of the smart contract B and see by calling B bch Min function another use case for example if you want to call uh for random numbers if you use chain link VF numbers for example you have to call a smart contract like this we have the contract a we have a function to give us a random number then we have to call the smart contract from chaing so is necessary to communicate with another smart contract and this is very useful for Advanced solidity smart contract that have um that communicate with another protocols and so on now if we talk about delegate call the major difference is that the context of the now for example we contract B okay and we use a delegate call the context of contract c will not be the contract B will be the contract a because delegate call doesn't hold the um the color context but it holds the the first one who the first one who made a call okay so for example if we have this we if we have here call here delegate call and here another delegate call then the context of of contract c will be the AOA eoa so the first one who do the call is the one who will be on the on the context of the delegate call now as you can see here on this part the storage of contract B this this is the the main thing and the most important thing when we um discuss about upgradability of the storage so as you can observe here the storage of the contract B is operates on B um it's um linked to the contract C okay so this on the next image it's is the same as like the as this okay so we we we don't have two storages in case of for example for proxy contracts okay we we don't have a store storage we don't use a storage for each contract we use just a general storage which is the proxy storage so as you can see it's not necessary to have two storages it's basically used just one storage so when you use the delegate call we have to call trusted contracts and these contracts doesn't hold a state they own only have uh utility functions that will change the state of the main contract okay we um the so as you can see here the contract C doesn't have anymore storage so that would be like uh like you are using contract C just like a library exactly which code you have imported into contract B exactly now uh I want to show you another example okay so this is the diamond standard proxy and it is one that I really like to use and this is for upgradeable smart contracts Diamond one and Diamond two are proxies faucet a faucet B are basically smart contracts like contract C okay so this part B it's equivalent with diamond one and Diamond 2 and the C contract smart contract it's equivalent with foret a and faucet b as you can observe here the main thing is that faucet A and faet B as I explained before doesn't have any kind of storage why because the storage it's moved to the proxy so when we co we we we are the user okay we um we call a function from d for from Diamond but we have to use the proxy so when we call a function then from proxy we go to the faet and the faucet communicate with data from our diamond from and that's only that's only on upgradeable on upgradeable sorry on delegate call yeah only on delegate right so so so the black black arrows are delegate calls only yeah in this Cas exactly okay so uh if we want to use call that means the faucet should have its own storage if we want to use call that smart contract have to have storage if we use delegate call that smart contact doesn't have uh does not uh need to have a storage so this is just uh a short explanation about what I would like to present to the to the next talk about the upgradability smart contracts the discussion is maybe quite long and I didn't want it to combine with this one um and oh yeah one more we talked about call the delegate call and the static call which uh is used to carry other contracts for example exle you have just a smart contract with views and you query for data you query for I don't know public views for public variables to to get the DAT to get the data in your smart contract this static call will not change the basically static call it's like a delegate call because it doesn't change the the storage of the of the contract that is called okay now I write a biography this was my presentation and here you have all the links I used to document myself um you can find me on Twitter and if you have any questions I'm open to discuss oh nice so guys how how do you feel about the the presentation meaning did you get uh do you want us to go back do you want Cal to go back to some slide and re explain something if it's too um if something is not clear thank you so much caline for this presentation it was pretty clear for me yeah I'm happy to hear that thanks cool that's good so we're all ready to do upgradable contracts right right right cool nice so um so as we said this is uh uh the first yes so so we have um a question can you re revisit the the batchman slide yeah actually actually you can unmute if you want to ask the question or no if not just uh you cannot access the Google okay yeah yeah I'll I'll ask to add the I think now you can see it oh even better don't think that okay so can you revisit the B batment slide okay so this one okay so this is the batchman slide the can you hear me yeah yeah just I think the previous one with I think there was a slide with uh this one uh I tell you was the title was the storing large data not recommended on chain is this one no I think it's the next one I think there's no pictures on it it's just a few lines of text no after or before this one with delegate call no hold on the title of the slide is storing large data not recommended on chain that's the name of the of the slide if you can go up where is where you said about file Quin and 19 yeah yeah okay so which is the question no I just want you to wanted you to go through the slide again oh okay okay um what I wanted to say here is that users I I think all user I talk they they told the nfts are on the blockchain like purely on the blockchain that picture is on the blockchain so you probably know to um store a picture on the on the ethereum blockchain cost like uh $50,000 or something like this because we made the calculation uh I think there was a presentation the presentation we had with the ipfs we calculated at that time at the at that uh price of ether if you want to store one megabyte of jpeg uh you would have uh had to pay about $50,000 I think I think ether was $3,000 at that time or I think even less 1,600 or so so so so that's we even run a test and actually uh yeah so uh but yeah I'll let let caline just just to to one note that I have is that some um some nfts that are I know generative NFS and so on they they do not store necessarily jpeg formats or PNG formats they actually store for example SVG and that does not need as much storage so you can draw something with within an SVG which occupies a lot less space than JPEG but generically I think the statement of this slide is correct so this is the usual practice probably 90% of people just story to my PFS but uh sorry K so I I want just that um so the smart contract for example you meet you meet an monkey I don't know how they are named that nft collection with exclusive monke okay Apes okay so for example exact exactly board Apes so if when you mean board ape you basically buy an index on the blockchain you you don't buy an like it's yeah it's truly an nft but it's just an index a link to the ipfs best case ipfs where the picture is stored it's not stored on the blockchain and I saw a lot of users have this confusion why is not stored on blockchain and that's why it's very expensive to store it on the blockchain so I wrote this note because it happened the developers to not store the data the images on the ipfs as I saw here as I wrote here they stored for example on a local database so your link from the smart contract uh will point to their database and it's it works fine until they close the server or until they delete the database and you will H you will remain just with with an empty link or just with an index on a smart contract not with an nft the right pet is to be stored on a PFS this will confer um more safety to your um to your nft and you have to make sure that the ipfs um is um synced on enough nodes because even ipfs does not guarantee permanent storage if the nodes choose choose to delete that data so yeah exactly so if you buy an nft you probably want to also make sure you have an ipfs server yeah so the truly the truly nft is just that one that is on the ethereum and I think there are no nfts stored on the ethereum blockchain like the the fully nft the full nft sync tempin correct thank you you're welcome cool other questions guys Okay cool so um again as I said this is part one of three-part series after this talk uh we'll have the talk about proxy contracts based on open zapping uh libraries and then we'll have uh then we'll have another talk based on um diamond pattern that is Cataline likes very much so he'll he'll also present the other talk next talk will be presented all this second part will be presented by by George he's on the call here then caline will present the third part which is going to uh address uh diamond pattern for upgradeable contracts and it probably has other use cases not just upgradeable contracts right yeah right yeah okay so we we need two more talks because we want to offer more um more variation about upgradability smart contracts and you you must observe the difference between them and the use cases you um you don't need to use the the same um the same way to upgrade the smart contract in each us case because maybe I'm sure one and others maybe are more um pH feasible for your problem than others yeah so this was an introductory um Talk About Storage which is crucial to understand when you do upgradeability correct K yeah correct yeah cool nice so we'll have the other talks too uh so I think the next talk made by George on um the proxy contracts I think it will be in one month from now uh I want myself to do a talk that is not related to this subject and it's going to be related to open GSN open uh Gas Station Network on ethereum and how to do meta transactions that are also decentralized so I kind of sneak in my talk in between is I hope this does not break the continuity but we'll have the recording available I hope uh the video is recorded well um and for the next for the future talks I uh I think we'll have a zoom account and we'll do presentations from zoom and it would be a lot easier to record the videos and to also share them store them and share them so please expect the video record recording in the I'll post it in a comment um on the event page and that's about it thank you guys and um kataline if you have to add something yeah just thank you for coming I'm I'm glad you're here do you want to give I help you cool do you want to um paste your um Twitter handle so the people would follow you or do you want to add I know comment with all the links that you yeah it's here I also wrote some medium articles yeah so also follow caline's um yeah GPT for example if you want to use AI I you can see here how I how I tasted test it and then um if you want to do internal audits I think this is the most read one also about the diamond star an explanation and what we talked get together today so these are my articles if you're interested also if you want to write me something write me on Twitter I'm I I'm glad to to help you with informations no that's all thanks cool thanks a lot guys and see you the next time bye bye bye bye thank you bye but
