The Ethereum Virtual Machine (EVM) is a stack-based virtual machine that serves as the runtime environment for executing smart contracts on the Ethereum blockchain. It operates as a transaction-driven state machine, where each transaction triggers the EVM to execute bytecode instructions, update the Ethereum state, and broadcast changes to all network nodes. The EVM uses multiple machine spaces including stack (temporary LIFO data structure), memory (volatile RAM-like storage), storage (persistent key-value store), call data (read-only function arguments), and logs (write-only event storage). Smart contracts written in high-level languages like Solidity are compiled into bytecode, which the EVM executes using opcodes (operation codes) that manipulate the stack, memory, and storage. The EVM is considered quasi-Turing complete because it cannot run indefinitely or use infinite memory due to gas limits, which also serve as a security mechanism to prevent spam and denial-of-service attacks.
EVM Explained: A Deep Dive into the Ethereum Virtual Machine's Architecture
Added:hey hey everyone I am uttam Singh and this is a video and basically a crashed course on evm that is ethereum virtual machine right so in this uh course I will tell you everything about evm ethereum virtual machine what it is how it works and everything interests me after you watch this video you will not have to read any other article you or you will don't have to watch any other video of how evm works I will also go deep inside how you know bytecoders get compiled and how is evm running and how you know your your code gets into the evm and how it runs it executes what happens right I will discuss each and everything about the evm so let's not waste more of our time and let's get started and see about what is the agent of this course but before getting started if you are an expert do leave your feedback in the comment section if you you know you can watch the video if you're an expert if I left anything or if there is something that I can can improve so do leave your feedback in the comment section and do subscribe to the channel right now because this is just a one crash course I'm going to upload other many crash course as well and my goal is to educate people about blockchain so please please subscribe to the channel right now like this video comment it and also follow me on Twitter and Linkedin and if you have any doubt please reach out to me so let's get started with our video so let's talk about the agenda for this crash course on evm and I've tried he I know I will I will cover every topic right so first of all we will talk about the history of epr right it's very important for us to understand what was the history of evm who designed it what was the topic behind it what was the you know motive behind it right it's very important for us to know so we will discuss history of evm and then we will also discuss virtual machines okay so what are virtual machines ethereum is a virtual machine so it's very important for us to understand what is a virtual machine how it works and other things as well after that we will be discussing you know what is evm what actually is ethereum virtual machine and then we will also you know discuss ethereum state right because the job of evm is actually to update the ethereum state right that's the simple Java BPM evm right and we will be discussing how ethereum State Works how it gets update every time the evm runs and what happens behind the code right and then we will also go into the evm architecture what is the architecture of evm how it works what components it have and all other stuff as well and then we will be talking about machine space of evm right so this is a new topic the evm can access and store information in you know multiple places multiple places like like that is around five to six places so we are going to discuss that as well and then we will also discuss evm op codes like what are operation codes what are byte codes ABI and solidity like I'm not going to go deep inside each and every topic of code bytecode AP and solidity but it's very important for us if I if we are you know learning about evm then it's very important for us to learn how opcode Works how bytecode Works what is ABI and what is solidity just an overview is very important right so again you know machine machine cannot understand solidity code right so that's why we need all these things op code by code ABI right because machine can't directly understand solidity because it's a high level language right so we will talk about this as well and we will also going to you know take a few examples right we will take a real life example we will go through remix we will write a solidity code and we will compile it and we will you know compare the byte code and you know convert our bytecode into op codes right which is a good example and you will really understand how your you know code works at a lower level and how it's get turned into op code which is uh which is executed on the evm right uh after that we are going to discuss EDM working so why I'm not you know discussing evm working in above in the above slides when before before all these things because you know I want to set up a stage for you guys to understand what is happening behind the scenes and what are these small small things machine space is op code and once you know your brain is clear about what is happening we can you know you know discuss about how evm is working when you know when we have connected the dot so at the end you know when we connect all the dots we will see evm working and you know I will also take an example there as well and after that the last topic will be curing completeness and the guess like what is during completeness evm is a turing complete machine is it or is it not is it quasi-tuning complete machine and we will also discuss gas as well what is gas and why it is required what is the need for it right so this is going to be a small crash course and evm hope so I have you know covered everything if I left anything just write down in the comment section if you are still watching this video I guess you are interested pretty much in the course so please like the video and hit the Subscribe Button as well so let's get started with our first topic that is the history of evm right so you know everyone knows who vitalik is right vitalik vitalik buterin is the founder of ethereum he founded ethereum at the age of 19 around 2013 if not if I'm not wrong then again you know at that time only the evm was also designed as well right because evm is the heart of ethereum right ethereum is nothing without of evm right which as a human body we don't have anything without our heart right we are nothing without our heart right similarly ethereum is nothing without evidence the heart of the theorem so in similar year in 2013 only the evm was designed and uh you know with vitalik and with given words so most of you know who vitalik is the founder of ethereum but uh you know people in the outside the industry people don't know who given what is so Kevin Wood is actually also one of the founder of ethereum right and here he was the one who designed the evm machine in his tenure right he's also the inventor of solidity language yeah he was the one who created the solidity language and now he is the creator of polka dot and Cosmos ecosystem right so I just wanted to discuss who Gavin wood is because most of us you know people know vitalik but don't know Gavin wood so he was one also one of the founder of ethereum and he was the one who designed evm along with vitalik and also created the solidity language right and why you must be thinking why I have this BitTorrent logo in the slide so you know according to vitalik what he what he says is that uh BitTorrent was actually one of the first depth that was created you know a Bitcoin BitTorrent was created by Braham Cohen and it is a file sharing protocol that was invented is around 2001 and despite several efforts to destroy it like BitTorrent you must be have been using it to download movies and other stuff as well and despite several Awards efforts from government and other organization to destroy it BitTorrent remains very much alive why why because to truly halt audit to truly remove the BitTorrent from the internet every computer in every home across the world would have to shut down because you know it's it's uh it's it's something that is decentralized also and it's in every computer right so it's to if you want to truly end or truly destroy the BitTorrent then it have to be removed from each and every computer from the around the world right which is a very very hard task right so similarly it's it's like a virtual machine as well you can say you know it's like more like ethereum and uh if that's why vitalik said that BitTorrent was actually one of the most one of the first dab that was created it's like uh virtual machines unbounded by any physical constraints right so yeah this is all about I guess the history of UVM just a small history I wanted to discuss and yeah so let's move on to our next topic as I discussed about virtual machines right so what is virtual machine you know why it is important why we have created this ethereum virtual machine right so virtual machine says you can see in the diagram as well virtual machine is nothing but it's a program that simulates the operation of a computer by isolating its real components and operating system right so basically you know it's it's a independent machine that is working working on top of your computer so for example you have a computer you have a operating system with OS like Windows or any other OS right you have a Mac or any other OS and what what will be happening that virtual machine will run as an independent separate machine on top of your computer with different OS right so you can do that as well this is like one a short example of virtual machines right so this one physical computer can actually run several virtual computers right so your computer can actually run more and more and more virtual machine maybe two or three depends upon your uh you know capabilities of your computer what are its programs and memory and all that stuff so your computer can actually run separate virtual computers to be precisely you know it we can call it as separate virtual machines right so this is like a simple definition of virtual machine set so now virtual machines can actually differ in their capabilities right every virtual machine will have their own different tasks and you know they will they will have own capabilities so for example virtual machines such as virtualbox right virtualbox is a type of app that can you know help you create a virtual machine on top of your hardware and allow you to run any operating system on top of it right so you can download a virtual box on your computer and then you can create a virtual machine basically that will create a different OS in your computer which you can run and maybe if you have Windows you can download the virtual box and then you can create a Linux OS on your computer as a virtual machine right you can do that right and uh other than that there are several other virtual machines as well like Java virtual machines which helps to that that is basic basically a Java runtime environment right it can you know run the Java code and stuff will not go into the specifics of each and every technology because you know each of them have its own set of approaches that is worthy of different video because every virtual machine is different and they have like different motive of that virtual machine however these virtual machines have some common features and some criterias for their evolution regardless of what task they are designed to handle so you know every virtual machines have some common features which we can discuss in our next slide so you know as I as I said that virtual machines run in isolation from the other computer components and using you know using only the computation resources like this is different actually from writing and executing native programs that directly work from the computer components right so for example basically you have a Windows you have a OS suppose Windows you have a Windows OS and you are running any dap and running any of the applications so that application will actually run on your own computer right and that will use your you know Windows computational resources that will run on it but it's not the case for virtual environment in word for virtual machine like in virtual machine it will have its own you know it's its own computational resources and if you're running an application on top on the virtual machine it will you know use the computational resources of that virtual machine and not of your computer rights so this is what uh you know evm it runs like and you can see it in the diagram as well one more interesting thing to know is basically if you are a developer then you know what you will do is you will write your program in a source code and you will you know compile it into machine code and that machine code is it actually executable by the processor of with a specific architecture so for example you you are a developer you build a code that is actually you know running that that is actually a high level language so what will happen is that your high level level code will be compiled and into a machine code and that you know machine code is will be you know not platform independent it will be like platform dependent that that machine code you know it it can only be understand by a particular machine or by particular components right and and in the case of virtual machine it's different in in case of virtual machine source code you can see it in the slide as well the source code is actually compiled to the byte course right and this and this byte code is then compiled to the machine code right so so you know byte code is actually acting as a platform independent thing right because it can actually be executed on any platform using the virtual machine so for example you are writing a source code and then this source code that's in a high level language in our case in EM it will be in solubility right basically right now it's in Java so you compile this code and this will generate a byte code actually this byte code can be you know compiled or maybe converted to machine code that is understood by each and every OS right so for example there is a Windows OS there is a Linux OS right there is a different OS and not every you know there is not every machine can understood as a single machine code every machine has its own architecture every machine has its own specification so you have to you know provide them different machine codes dependent uh you know depends on their what machine codes they'll understand right so it's a very big problem that's why we have this byte code this byte code is actually acting as a middle man right so in the virtual machine this is acting as a middleman you just compiled your code in byte code and this byte code gets converted into the machine code of whatever machine you are using and it will be easier understood right so that's why we need this byte code because it's it's very important right and also and so thanks to byte code because due to byte code only we can create this an isolated Universal environment you can say for executing any of the program and without even worrying about its compatibility with the process architecture or whatever the other computer components is you don't have to worry about whether the OS in Windows whether the OS is Linux or whether the OS is Mac you just have to write your code and it will you know if you're using a virtual machine you it will create a isolated basic simple environment Universal environment and where you can convert the code in byte code and then you know this byte could get converted into machine code so you don't have to worry about the architecture of a different OS you don't have to worry about it so this makes this actually makes the life of developers where life of developers very very easy right so this is all about virtual machines and I hope you understand what are virtual machines now and uh you can see it in the diagrams as well and we will understand what byte code is especially in the context of solidity and evm so yeah this is all about e-virtual machines and if you are still watching I guess please go and write down in the comment section if you are still liking this video and give it a thumbs up as well right now and let's now let's actually move on to our next topic that is evm the ethereum virtual machine what it is it is a type of virtual machine but what actually is evm okay so yeah ethereum virtual machine so what is the theme virtual machine in simple very simple terms ethereum virtual machine is nothing but a runtime environment for smart contracts in ethereum right so similarly you know we discussed Java virtual machines before that Java virtual machine actually allows any Java program to run on any machine right this was what the Java virtual machines what we discussed in over their slides so similarly what similarly with our jvm we also have evm and what this evm does is basically this evm allows you to run evm byte code on any of the machine right so this is what the simple job is of evm is right and this evm is you know isolated from other parts of the system this means that operation on the evm should not affect your data or your programs in any of the way no matter how many times you call a particular function or you know any any function you are calling it multiple times it should not affect it right so basically you know evm is isolated from other parts of the system no matter what as we discussed like you know virtual machine has nothing to do with the it is basically an isolated independent machine right so it's basically an isolated environment right so this has not this will not affect the data or any of the program that is running on the virtual machines no matter you know how many times you call it how many things you do with it right so so you can also say that evm is basically a computer or a machine that helps us to store the data on the blockchain on the blockchain right so yeah this is all about ethereum virtual machine and I guess you know this is like a small definition let's have let's discuss more you know deep inside it and let's let's understand that you know you know broader level at a high level what uh how does it run as a developer as well right the so so basically what happens is that developer writes a code right any developer basically if you're a developer you write a code in solidity for a smart contract you are running that and then evm will actually compile that human readable solidity code or the wiper code as well Viper is also one of the language right so you you can the the compiler will compile the human readable solidity you know it is will be written by developer right and into basically machine code or basically op code so by byte code and op code there is a difference in that we will discuss it in our uh you know future slides as well but yeah you can right now say that it compiles it into byte code and that byte code is basically you know basically collection of op codes that and that basically that is a collection of op codes and that op code will actually you know go on to the evm that will change State the state of the theory we will discuss what you know ethereum stated but you know on a high level you can understand this in a very simple language that you can you compile the smart contract that's come smile contract into byte code and then that byte code actually goes into the evm your code is not going under the UVM you know the early your byte code is going on to the evm and that is only you know that it gets executed and that's converted or basically it's already a collection of op code and then that op codes tells the virtual machines what to do like what will happen it will tell the evm what to do you know what not to do right it will tell it right so you can say in other words the resulting byte words of these program does not have to vary depending on the specific machines right so basically as we discussed like you the virtual machines don't have to worry about the underlying CPU architecture right so you know that there are different nodes of the ethereum virtual machines right so basically with the goal and first of all the goal of this presentation is not to explain how depths work we are going to understand how ethereum evm virtual machine works right so this is this is just a broad level overview right so as I said that you know you compiled those smart contracts right and this smile contracts you know goes into the evm it gets converted into byte code and that stuff and then you know the op code gets executed on evm it which tells what to do and what not to do and then this changes the state of the ethereum nodes and there's you know all the ethereum nodes will have to you know agree with the ethereum state and then that's how the state gets you know upgraded every time you write a code or every time something is executed on the evm right it's so simple this is like a so you know notes is like just like distributed Ledger that everyone has to agree once there is a change in the ethereum state right and uh you can say basically you know evm is also people say it is a turing complete machine but it is not actually during complete there is a little bit of thing in it we will discuss it in a future slides so it can actually run which means that it can run any program as long as there is enough gas to process it so you know writing on the blockchain as I guess is not free I hope you are aware of it you need to provide some gas to you know deploy anything on the blockchain right so as long as there is enough gas the process will run right and again the goal here is not to understand how depths were what is the architecture of it I am just focusing on it this ethereum virtual machine and how you know it it this works but this is all like all the overview of where evm fits in our you know dap ecosystem or in our ethereum ecosystem like what is the job of evm in this depth working right we often as a solidity developer as well we often ignore what is happening under the hood of the solidity and how the code gets work so this video is all about explaining once you create your smart contract what will happen What will it happen on the evm and once it is get compiled and how op code run and what will happen if there are you know call to another contract and how the ethereum nodes get updated due to you know the ethereum virtual machine as well and again let me tell you that view also will be having a question regarding where is evm and where does it fits right so we will talk about get get or any other client service that are in the you know that are in right now in the ethereum nodes which help you to run the ethereum node get get is basically a go implementation of the ethereum protocol and there are several other implementation of uh you know Client Services of ethereum like arigaon or there is also python implementation I guess Netherland also had one right so we will discuss all those implementation and actually when you download those things like basically have value download those client services like for example you download gets that you want to hey I want to you know run ethereum node and with the get it comes evm along with it comes along with the evm right so we will discuss where it fits the evm as well when what happens when you downloads it and uh what will happen but yeah this is all about what is evm and where it actually fits right so let's move on to our next topic which is very important that is about the two types of the accounts that exist in the EVMS or basically in the evm compatible machines or basically in the ethereum right so so there are two types of ethereum accounts as you can see in the presentation as well one is the externally owned account and the other one is the contract account so what is the difference between what is the difference between these two accounts right so let's discuss that so you know each of these accounts as you can see it it contains few of the things one is the address that is the address of every account that already you know that's already there and the other thing is they have nonce that is number only used once and then they have balance then they have storage hash and then they have they have code hash as well right the code as well and uh there is the difference between these two accounts What is the difference is that the externally owned account right they in with in that they have addressed they have announced they have balance but the storage and the code hash is empty which means that there is you know there is no code there is no storage in these types of accounts these types of accounts are you know basically they are controlled by an external actor right so if you go to metamask you create an account then you are creating an externally owned account and that account you know that will be uh what to say that will be handled or that will be you know take can care by you with your private keys that will be controlled by your private keys right so that is the externally owned account that you create on you know using metamask and any other wallet and it does not contains any storage hash or any code hash while if we talk about contract accounts so again the contract account we have this address we also have the nouns we also have the balance but there are two extra things that is the storage and codash which are not empty in the contract account so basically you know you write your smart contracts right so this is that is basically a contract account when when it's get deployed right so this code is like your code that is you know code hash is stored in the account in the contract account and then so also it has storage right so if you have your code it will also have some of the storage which you know which which downloads the data or basically stores the data in that particular contract so it also have a storage as well and I like like externally owned account in the contract account the action is actually controlled by the code right you can say that contract is controlled by the evm code so whatever you tell the contract account once you deploy or you know what once you deploy your code then that contract will be controlled by what the code says right so you know everyone says the code is the law here code is the law in the theorem or in the blockchain that's why we say it because the contract accounts are controlled by the evm code right they are not controlled by any external private key yes the owner will be you know holding the private key of from where he deployed right but the contract account is actually you know controlled by the evm code and this contract account contains the evm code while in externally owned account uh you know there is a private key that is controlling that account and uh by the way externally owned account cannot contain the code right there is no code in this externally owned account like in the while in the contract account and it stores the byte code and the root hash of the Merkel of the state tree we will we will talk about the try as well you know Merkel try root as well in the future slides and these are basically the accounts that they just not hold or the your ether or your token balance but they are also capable of storing your smart contract code within themselves and that are not controlled by private key as I said right and but they're controlled by the logic of the code so you write the code you write logic in the form of code and then that logic controls the contract account right and uh any transaction between externally owned account can only be limited to either token transfer or either nft transfer any kind of transfer right right so this is the limitations of external accounts and this is only is not in the case and every time you have you're doing any of the externally owned account right the owner will have to sign the transaction on uh you know to initiate any of the activity in their account so basically if you are if you're doing any of the transaction from your metamask as well your address as well you will have to sign the transaction every time you do it right so so since it is controlled by the externally actor that is the private key of yours right so this is what about uh two types of accounts that exist in the ethereum ecosystem and this mostly exists in all other evm compatible chains so this is going to be same right so so let's move on to our next slide and our next topic which is uh ethereum state which is also one of the very very most important topic and it's very important to understand what ethereum state is if we want to understand the working of the evm and ethereum right so let's get to move on to our next slide so what is ethereum state so first of all let's talk about what is a state so state is basically you can say it as what is the particular State like what is your state at this particular point of time whether you are a student whether you are a working professional what it is right so this is this state right and now what is State machine State machine in simple terms you can say it as basically a device that specializes in storing the state as well as defining the rules of a state of for changing a particular system for example if something happens then this state machine will have some kind of rules that how will the state change and how there will be a new state right so to clear a confusion ethereum is a transaction driven State machines or you can say EVMS also even ethereum is a transaction driven State machine that that which means that whenever there is a transaction there is a change in the state and this is what a state machine does so basically you know evm you can also call it evm as a transaction driven State machine and it changes the state of the ethereum every time there is a transaction so in in nutshell every successful transition irrespective of whether it is a very big transaction whether you are transferring 1000 ethers whether you are transferring 0.001 ether whatever the whatever the amount is or it is big or very small it will lead to a change in the ethereum state even in the slightest change in any of the accounts we talked about two accounts right externally owned account and contract accounts so even a little bit of change in the ethereum or any transaction in externally owned account or contract account it will lead to a change and the you know it will lead to a new state as well right this means that every single transaction that is mined whether if you are transferring your nft whether you are sending a transaction or whether that is a governance proposal anything that is executed it will lead to a change in the overall state so you have a state and this transaction happens and there will be a new state right so this is what actual job of ethereum is like the the evm is the job of the evm is actually to update the ethereum state by Computing a valid transaction right as a result of smart contract execution or you know external accounts or externally on the account if there is a transaction you know the job of events to update this theorem State you know it is defined by the theorem protocol right so as you can see in in the example as well right so there is World State like for example consider you are doing any transaction right so before that this is the state of ethereum right so let's let's take a real life example right so basically for example you are using a tap right you are you're trying to use a depth so basically this world state that is T that is the world State before your transaction right this is the store like this there is a address of that smart contract there is a code and there is a storage as well right now you go to that app yes you says then basically you create you want to create your own account or basically you want to do anything with that smart contract you want to execute it now what will happen is that what you will do is basically you will send a transaction of a message call along with any of the input data that you want to transfer that hey my name is this is this please store this you know information into the into the smart contract and the storage of it in the in the blockchain basically right and you you send your transaction with the input data and then what will happen is that the evm will actually execute it how will this change State like that when you do a transaction right you gives an input data then how would the state of the you know the ethereum will change how will it know that you know there is a reputation so every time you send a transaction it goes to the evm and then evm runs and then it updates the state and there is basically a new state right so you know that there is a storage before this you send in a transaction you go it goes to the evm and then evm updates the state of the you know ethereum and uh there will be a new state after your transaction is successfully mined there will be a new state in this new state will be broadcasted to every other notes of the blockchain they will also have to update it right so evm code is again you know executed on the ethereum virtual machine you know whatever The Happening will be happening in the ethereum virtual machine your code you know it will call another smart contract what smart contract are you interacting with it you will call it in the evm and then all the changes will happen in ethereum and then it will you know change the world State we will talk about deep in the working of evm how is that happening how is actually the State of the State of the you know ethereum is changing how it calls other contract as well right so you are doing a simple call basically if you are going to on a uni swap you don't know what is happening you are just calling the smart contract but how will evm execute it we will talk about in our future lectures but this is all about ethereum State and how you know ethereum state is very important and uh basically you can you can you can uh you know understand the world state of ethereum basically it represents the current state of the entire blockchain which comprises of all the historical States that actually forms the current state hash you can see that as well that right now if you say the world state so World state is nothing but it represents the current state of the theorem but that particular moment of time the world State represents the current state of the entire blockchain right so this is what world stated in a simple matter in a simple manner I hope you understand what ethereum stated and what is World State and yeah if you understand it uh do write in the comment section below as well and now I guess let's move on to our next topic that is evm architecture what is the architecture of evm what you know what what goes inside the evm what are the components it contains of what is the use of that particular component we will go through each and every component of the evm what what is this what is the program counter what is storage hash what is is stack and everything will go into each of them right so let's move on to our next topic that is the ethereum architecture okay so let's talk about evm architecture like what is the architecture of the ethereum virtual machine how does it look like so you can see it in the screen as well like the architecture of ethereum virtual machine it contains of this program counter gas available stack memory virtual machine and there is account storage as well then first of all I would like to tell you that evm is a simple stack based architecture machine so so let's actually talk about what is stack based structure machine before we talk about the architecture so there so there are two types of virtual machine one is the register based virtual machines which uses a set of registers to store variables and for calculating any of the results which are mostly present in the Intel architecture if I'm not wrong you can you can Google it I'm not sure about it but yeah these are like register paid virtual machines and then we have a stack based machine machine so stack is basically uh stack based motion machines uses stack for the purposes for purposes of storing variables or calculating the results right so stack is and database architecture I hope you aware of it is lifo that is last in first route whatever goes last comes first out right so it uses a stack for the purpose of storing the variables and calculating the results and there are other well-known stack based machines like jvm jvm uses stack Basin machine only and in the stack based machine the data is always available like this is one of the advantages of Stack based machine that the data is always available at the top of the state machine right so you can see in the diagram as well like you insert and you you know you know push the numbers like 27 and then what what you want you want the result of 20 plus seven right so always all the time the result the result will always be available at the top of the stack by default rights and you can see the SP pointer the SP is the stack pointer which refers always to the head of this state right so basically this is what stack is and this is how the stack based machine work and evm is a stack based machine right so that that's how that's how that's we know about it the TVM right now is a simple stack based architecture virtual machine now let's talk about different components of the ethereum you know ethereum architecture the ethereum virtual machines right so it has program counter now what is program counter so evm initializes a program counter that is actually pointing to the op code that is to be executed right we have discussed what up coded we will discuss in our future slides as well and uh you know opcode is basically just a code or basically that that is that tells what what to do to the evm right it tells basically it's just like a small code as well or you can say it's like Command right that tells us what to be executed right so ethereum initializes a program counter that is pointing to the opcode so program counter in quotes which instruction to be stored in the code and which should be the next to be executed by the evm next and you know every time it executes the there is a increment as the contract run so as the contract run there is a increment in the program counter so basically the job of program counter is to you know encode the op code you know and uh it tells which op code should be executed next to the evm and it increments every time the contract runs right so it tells the evm which op code to be run next right then we have gas available gas available is nothing but how many how much amount of gas you provide for that particular Transaction what is the capability of that gas right after that we have a stack so we have talked about stacked light last and first out so this is basically also a temporary memory you can set it's a Titi 2 byte element uh used uh used to store smart contract instructions Imports and outputs whatever you provide right and there is also one stack created per call context it is very important that every call context there is only one stack that is created and it is destroyed as the call context ends right so this is like a temporary you can say that every time you create a smart contract there will be a stack not basically there every time there is an instance of a virtual machine there will be a stack per call context and it is destroyed once the called context ends okay then we have evm memory so Eva memory is also not persistent right it is and it is destroyed at the end of the call context as the call context ends the instance of the idiom ends the memory also ends right then we have Storage storage is actually the persistent memory of the smart contraction each contract has actually has its own storage right so we will discuss in our future slides as well how the evm grabs the storage and the the state address of that particular contract the code of that address and then how it changes the storage of that particular contract as well so storage is the persistent memory of the smart contract and again each contract has its own storage which is persistent and this is that is like a world State as you can say on the time we've this we already discussed what world state is that is the current uh you know state of the ethereum right and then we have the code the virtual room that is the evm code so that code is the region where the instructions are stored so this is the reason where the instructions are stored the instruction data is stored in the code that it is also persistent this is a part of contract account State field and this this is actually uh this is actually loaded with the byte code of the smart contracts that is to be executed as I said that on the blockchain also in the evm that nothing is present as your solidity code everything is a byte code and that byte code is understand by the virtual machine that is by the evm right so this is also like uh it is loaded with the bytecon byte code of the smart contracts that is to be executed okay so yeah this is all about the evm architecture like what are this each and every components so we've discussed each and every component of this ethereum architecture we will discuss the machine space of evm again in the future slides what is the spaces where ethereum can or basically evm can store and read the value from and uh yeah so this is the small architecture of ethereum and we will talk about more when we will be discussing the working of the IBM but I guess hope you're clear of what each and every component of the ethereum or basically evm does right so now let's move on to the ethereum virtual machines where is where it is you know actually stored I want to give you a broader uh way of how where and where is the even actually stored and at the point of ethereum right so we have this ethevm code it and then we have the theme virtual machine then we have the runtime processes like this ethereum node basically the client services like get parity in other Minds where we've talked arigon or any other software that is used for running the ethereum nodes The Client Services then we have the hardware of that physical professors or wherever your node is running on whatever machine it is running so evm functions basically as you can say as a virtual CPU or as a virtual machine that is running inside of the ethereum node or the get program or any other any other client service so ethereum virtual machine is basically a virtual machine or a virtual CPU that is running inside of the get you can go and actually look at the what is get and what is the code of it you can definitely look it is basically a runtime process machines and everything happens it updates the state of how the node actually you know contact each other how the state actually changes and gets updated every time and then or every machine that is running that client service that is the get they have their own physical processors they have their own virtual basically OS as well right so that is the hardware of that particular machine that we are talking about so solidity code again you know it's compiled to byte code and the distribution to and then actually it is distributed to every computer node running the get in the network right so your solidity byte code compiles into the byte code solidity codes and then it is distributed to every computer node running the get network so right so this is how it works we will talk about it more in the working so just I want to give you a whole broader overview of where the evm fits in it right so let's now discuss the machine space of evm so before going into the working of the evm we will have to understand like we will have let's run through the vocabulary of the places where the evm can actually access and store information okay so as we discussed that evm is a stack based machine right so there are no registers out there right there are no registers and the Intel computers actually have registers and evm is like CPU right it's like a CPU okay you can stay that so let's let's actually talk about the machine spaces that is places where it can access and store the information the evm so first of all let's talk about stack so we have already discussed stack stack is like uh last in first out data structure so stack stores value temporarily during the run time to be executed for any operation used by any operations any operation you know takes word from the stack of pushed word onto this stack or basically both you know you know it is Texas push and pop right so basically you know any operation takes any of the word from this stack right so you can say that stack serves as the intermediate to read and write and manipulate data from this storage memory call data analog so we will talk about all these things but yeah you can understand right now at as logs it is basically an intermediate to read write or manipulate data from other spaces right so yeah you will understand it when we discuss more about memory account story locks and call data but yeah right now you know understand it basically as an intermediate that for writing and manipulating the data right now let's talk about the above before moving on this is the size of the stack memory 256 bits and the depth is one 124 elements 1024 elements now let's talk about memory so memory is also a temperature storage that is during that is executed during the run time of the transition think memory as a ram so it's just like a ram it is also a volatile memory and uh yeah it's a byte addressing linear memory you can also say that as well and now let's talk about the called data we will talk about what is the difference between memory storage and call data as well but yeah in the future in the future but let's let's discuss called it as of now the call data is basically the data field of transaction it is also just a read-only memory it is a quite special data location of the evm that actually refers to the location that stores the input parameter or the arguments of the function that is supposed to be called so basically if you are a solidity developer I hope you have used call data in your code right and quite similar to memory called it is also non-persistent like it's a well what's volatile it's a it's a it's a non-persistent data holder that is cleared after the execution of the transaction so as the transaction is done it is clear like Hindu called it as cleared right so it is also a temporary memory right and uh you know it stores the input parameters of the arguments of the function that is supposed to be called so when you called a function like you you use the call data string the name right so you you do that right so this is here what called Data is if you're not solidity developer you haven't coded yet so it's okay but yeah if you have used solidity I hope you are aware of what called Data is then we have logs so what is locks locks is uh write only input area to the emit log so basically uh it's a right only you can write to the logs if you're a Solitude up again you know what are locks because once you create event you create you know you specify events in your solidity code and then you emit the your event once your transaction is done or once your you know your function is executed you want to admit that the so that it is you know showed in the locks as well then you can you know these those locks and do it you know take it to the front end you can read the locks and all that stuff so I hope you are aware of our debt as well and we have the storage lastly so storage is a persistent memory and you can see the size as well 256 to 2 to 56 it's a key value store we will we will talk about actually we will see in the working of the ethereum virtual machine we will see it is actually a key value store and how it actually works it is so consider as of not it's a persistent memory and it's just key value stored so um so as it is persistent so it is persistent between any transaction you know it is important what is persistent means then then when we say it is persistent it is persistent between any of the transactions right so and storage is represented in the form of Merkel Patricia try right the ethereums Python and The Go client implementation as we were talking about they uses level DB to store those strikes right and if I'm not wrong they are changing it to cockroachdb right but the other users as of now label DB to store these tries and the Miracle batteries are tree is not just an architecture basically it's architecture but it's also not just represents only the data structure right but it is also you know it is also a database schema right one thing to keep in mind is that blockchain itself Only Stores the blocks including the hash of this to trade in the header right state route of the header while the clients like go ethereum and Aragon they store the full price content in the database right so it's a very important difference right the blockchain itself Only Stores the block while the clients store the full price contained in a database okay so coming back to the topic the evm stores data in both storage and the memory storage is used to store data permanently while memory is used to store data during any function calls right it is it is volatile right and you can also pass in function arguments as call data right if your solid little web developer you are already aware of it but again you know you can also pass in function argument is called data which act is allocating uh to memory except the data is non-modifiable the data is non-modifiable and you can you know you pass on data as an arguments when you are calling a function as well so you know I hope you know now you also understand stack as well like stack is actually acting as a intermediate to read and write and manipulate data right from memory storage call data right and locks so stack is basically like intermediate I hope you get that right now so these are several resources as a space that evm uses to access and store any of the information I hope you are clear of all those resources we have discussed each of them and stack memory account storage locks call data I hope I've covered everything if I've missed anything like write down in the comment section but I hope I have covered everything now let's actually talk about before again you know we are we're connecting the dots right now right please bear it with me we will get to the evm working of the evm but before that we have to understand few things and we that's why I'm you know telling all of these things and uh next we will also discuss what is solidity like just one single single slide small small side water solidity what is byte code what is ABI what is opcode and once it is clear we will go through one of the example we will go through remix we will write our code we will uh you know compile it we will see the byte code what is that byte code what it tells like we will convert that byte code into our op code as well and we will understand what this byte call will do to the evm and after that once we are clear we have broader understanding and even as a solidity developer is it's important for you to learn what is happening under the behind to optimize your code right so it's very important to understand it so we will go through that as well and then we will be talking about the working of evm right so next topic is solidity what solidity is all it is an object oriented language that is used for writing a smart contract you can also use wiper as well for writing smart contracts on evm machines on even compatible chains the all these languages like object running languages similarities uh you know such just like C plus plus or Java and solidity can be compiled down to byte cores with salts compiler that is present in the evm itself and then this byte code is uh you know gets onto the evm and basically on the ethereum and yeah it runs right so this is a simple definition of what solidity is now let's move on to byte code so as we suggested that ethereum smart contract is a byte code that is actually deployed on the ethereum blockchain you know it's not your code doesn't goes into the ethereum blockchain it's your bytecode that goes into it the event byte code is written in hexadecimal format so if you see that 0xine which means that it is saying that your number is in hexadecimal format now let's move forward and also one more thing is when we compile the solid your solidity smart contract it will translate your code into byte code that byte code is only understandable why the evm bytecode is not designed to be executed by the processor directly but actually there's another another program right so you can actually the you know it can be directly executed on the virtual machine itself or the bytecode can be either directly executed on the virtual machine such as an interpreter or it may be further compiled on into my my machine code for the better performance you can see in the example as well it gets compiled into ABI and byte code we will talk about what ABI is but yeah you can see that here here's the byte code on the right hand side and this is the assembly view of it because you can actually uh you know break down your op codes into a breakdown will byte code into a bunch of op codes okay yeah so let's talk about actually what op code is before that let's talk about API so ABI is a application interface so when you are solidity code gets compiled now it it you know it gives two things one is the Json API and one is the byte code so what is the Abi Abi is application binary interface as it says like it usually it's it's an interface to interact with the smart contract right so it's energy is an object basically Json is Javascript object notation format object it describes the contract functions the variables modifiers and the contract parameters that are you know in the Json format all these things are available so when you compile down your right your solidity code you will able to see your ABI file there as well right whatever you're using remix or vs code whatever hard hat so API allows anyone writing a smart contracts to be able to communicate between the web application that are written in high level languages like JavaScript typescript and actually the byte code of the evm that understood right so it basically is acting as the interface to connect your dap and you know it how it can contract basically connect with the byte code how it can communicate rights it's acting between the internet acting as an interface right so yeah this is all about API now let's talk about opcode so opcode is a short form of operation code as its name suggests the opcode is a type of code that tells the machine what do all ethereum byte code can be broken down into the series of operands and optimums as we discussed so opcode is a different whole topic which can have its own crash course because there are several types of categories and of course as well stack management automatic operations environmental op codes memory management operation codes exam Etc there are a lot of them and it can be a different cache course if you want me to you know write a crash course just let me know here you can see that you know every op code has its name and there is a minimum guessage for example let's actually see the byte code example right so suppose this is a byte code right and uh this is bytecon we will you know break down our byte code into small small op code so here you can see you know the 0x01 stands for add right so you go to the ethereum website you see this list and there will be you know list of ethereum virtual machines what ethereum you know byte code and what the op code is for that particular you know op code right so you can see that for example 0 1 is for add it means that this by this op code means adding right in there is a minimum gas for it that is three right so similarly what here 0x01 is doing is basically an OP code for adding two numbers in the evm byte code so what it does it will what it does it will remove two elements from the stack as we said the stack is last and first notes it will remove two elements from the stack and then it will push the result as I said that on this track your result is always on the top right so this is the case and then we will then and when we and it is very important to understand the evm byte code that is that is for minimizing as consumption right so this is all about opcode so here you can see the other of course as well as like push one right so here watch it consists what it is saying is that push one byte code of data into this stack right it says that so the you know you can convert your byte code into this these different different op codes right so it is very important to understand you know the the how the opcode runs and what is the icon for minimizing the cash consumption in turn uh reducing the cost for your end users right it's very important to know that the goal is not I mean it is the goal to optimize your guess your contract so it uh when you deploy it it reduces the number of gas and the amount that is required to deploy it but also it's the main focus is for your end users when it interacts with your smart contract the guest fees which he or she is has to pay would be less right so that's that's the that's the main goal because you don't want your end users to pay a lot of guest fees because you know you want more and more users to use interact with your solidity smart contract and if the gas piece is very high it is the contract is not optimized eventually the users will have to pay a lot of gas reason they don't want to do it right so it's very important to write the optimize source code it's basically solidity code so that it reduces the cost for the end users okay so this is the relation between the byte code and the op code one more thing is that the Assembly Language also it is important so it is also portable to write the whole smart contract using opcots yes that's why the solidity Assembly Language comes in it might be a lot harder to understand it but it cannot but it could be useful I mean it can you it can do stuff which solidity can't do and it can also save you a lot of gas so you can see that the U language it is basically written in this format in an assembly block is marked with uh you know curly braces and this language is also called Yule you can visit the solidity documentation if you want to learn about it here is the format of writing it this you just write assembly and then there are curly braces you write the smart contract there as well okay awesome I guess that's it for all these small small topics now actually let's go to one of the main example right we will actually you know go on to the remix and we will you know write our own code we will deploy it out we will check the byte code how it how the turns into the different stuff and uh now let's also you know we will understand it at a lower level so let's go on to the coding as well okay so here I am on the remix ethereum IDE and I wrote down a small and very simple smart contract that is my contract name my contract where I have declared unsigned variable which is adding to number 10 plus 2 and it is multiplying it by two right so goal here is to just understand under the under the hood how your solidity smart contracts work with the evm and what is happening under the hood so just goal is to keep it simple so here I have already come you know written the code now let's actually compile our smart contract so it will will generate the byte code and the API yes so it is already deleted now let's copy that byte code and let's paste it here and let's see what is happening where is the byte code okay so you will see all that stutter stuff but your byte code is this one object one this is your actual byte code okay this long one and uh one more thing I have already copied this byte code into this file so we have already had our byte code here right so this is all our byte code right and I will explain it later on but here you will see one more thing you know we will run through it and we will broke it down into the opcode but the compiler has already done it for us and it's a very good thing that they have you know broke down over smart byte code into the op code we can also compare it later on as well with our you know what we what we have broken down into okay so let's move on and let's just uh you know look at what our byte code is doing right so this is our byte code right so as I sold that byte code is a collection of op codes right so let's see what is happening so whereas byte Codes start with 6 0 8 0. so this is like our first four numbers of it so what does 60 stands for let let's go to the ethereum website and there you will see the list of op codes you can visit there so we are looking for what we are looking for six zero right so we are looking for six zero uh so what does six zeros does so here you can see that 6 0 stands for push one right so and it it costs around three guests and what push one is doing is that push one byte value onto this stack this is what the work of push one is here right so what it's doing is push 0x80 so put 0 x 8 0 into the stack it is in hexadecimal format right so first four number of our byte code is doing this right so this like push this this and now again we have 60 so again we know that 60 for push one and again the next two numbers are four zero so what it will again do is push four eight zero into this stack now our our next number is 52 so let's go there and you will see where is 52 right so 52 is uh here and it stands for M store which is memory to store something in the memory itself so write a word to the memory right so this is what it stands for again you will be able to see this is 52 and 52 allocates 60 of the memory space right so this is what this is how we are you know uh breaking down our byte code into our op code so here again you know let's go over let's go to our code file what we have is copied right so this is what our you know compiler is already compiled and break down the op codes for us so again you know we have already discussed it the 60 stands for push one so just push 860 bytes and then push one for push 4x0 and then we have them store that is right to the memory right so this is what it is doing if you want you what you can do is you can go through all over this byte code and try to you know understand what it is going and what what it is doing if we go through each and every op code and compile to you know understand what every cop code is doing it will take like a two hour long video if you want I can create that just comment in the right section but right now I just want to explain to explain explain it to you how it is done so till now what we have done is we have just uh you know uh compiled or broke down the first few digits but this is a magic number you see six zero eight zero six zero four zero this is actually a magic number so whenever you deploy any of your smart contract whether it's uni swap smart contract whether is any of your smart contract it will start with this magic number why because this magic number 6080604052 it you know it says how your smart contract is being initialized right and it can change a little bit uh due to the version of the solidity compiler I've seen it when I was compiling and running through it I've seen that when you use a different compiler version it it can change sometimes the the push thus the op code will be same push one and push one but the you know the memory the 0x801 this this can change right so yeah this is the magic number and it remains same for a smart contract again there is an exception if you change the solidity uh you know compiler version in future maybe there is something there is a change in it uh yeah so to further you know complicate the matter right now you know 0x 80 and 0x 40 to be you know clear it cannot be interpreted as it is not 40 or 80. this is a hexadecimal format and for 40 actually stands for 64 that is 16 into 4 and 80 actually equates to 128 in the hexadecimal format side so 0x60 is not 80 it is a decimal format it is uh right it is it it computes to 128 in the decimal format okay so in short what is is happening under the hood is that push one zero x 80 and push one zero X for T and M2 what it is doing is that is it is allocating 128 bytes of memory okay and moving the pointer to the beginning of the 64th byte this is what it is doing in a simple matters and now we have 64 bytes for the sketch space and 32 bytes for our temporary memory storage right so see what is happening is that here if I'm not wrong you know it is the initialization of your code it basically is also an initialization of your evm instance as well right so it is generating 32 bytes of temporary memory storage and 62 fights for your scratch space if I'm not wrong this uh you know this is the initialization of your evm and this will be remains same for all of your uh you know all of your smart contracts right so let's discuss more about it and again you know there are three places to store the data we've already discussed this in the architecture of the VM but let's again discuss it with our op code one is the first is the push one that is push one push one pushes one by to the stack right so there is an again the architecture of our evm right so what is done what it does is basically it is pushing 60 one byte to this stack so we have this three places stack memory write and call data call data is not available here but yeah you know it's it's not there but we have discussed it if you remember okay so there are three three places where you can store the places where you store the data one is pushed so push one to the stack right this is what it is doing the push 160 next is the SS store right as a store what is doing is store it permanently onto the evm storage that is the blockchain so if you if you use as store what is doing it is pushing your data or basically whatever you are doing into the email store that is permanent that is here account storage here right and uh again if you see push 32 it is again what it is doing is uh it is pushing your memory to the stack right and the last one is memory or called you also use call data as well but yeah memory is also one of the thing so memory what it is doing it is store it in the temporary memory or Ram of the IBM so as we discussed you know memory is the temporary one that is non-persistent is the temporary memory or it is also known as the ram of evm so without what this opcode is doing is that store your uh you know data in this m store or our memory right so these there are many op codes as well like M load sload jump stop return will not go through each and every op code but these are just like three places uh you know our stack our memory and our storage and memory or call data and our storage these are just three places where you can store your uh you know store the data right so this is what of this opcode is doing and again if you go to the code and if you can check if you want to you know go through each and every bytecode and convert it into the opcode you can free feel free to do so and here is here is you here is an auto generated one you can check it once you have done your own work right and also let's okay let me tell you one more thing is that let's let's not deploy the smart contract as I was saying right the your smart contract is not deployed on the blockchain your Eau is a smart contract is converted into bytecode and that then that byte code is actually existing on the blockchain right so let's actually uh deploy our smart contract and see what is happening right so once you deploy your smart contract right it is being deployed successfully you will be able to see it now let's actually debug it right and I not let debug it but yeah let's see the details of it right so we go here uh you can see here let me just zoom it zoom in it for you yeah so you can see the status through mind transaction hash from what is the address that we are using right now let's if you go uh in the input section if you see clearly you will see this as input right and what is this this is our byte code right you can you will be able to see that it starts with 0x80 and start it ends with one zero three three and that's where that's how that's how our byte code ends right so the input is always in the byte code format right evm doesn't understand solidity code it has to be converted into byte code which is understood by the evm so this is why the input here is the E is in evm format right so this is all about our low level understanding of our code I hope you got what we are doing and how the solidity smart contracts actually works under the hood and I hope now if you watch this you will be also able to learn more about optimization of gas and other stuff as well so yeah let's move on to our next topic that is actually understanding the evm working now we have you know connected all our dots right we have learned a lot of things now let's now let's connect all those dots and learn about the evm working okay so we are finally on our slide of evm working on part of evm working how actually ethereum virtual machine and works under the hoods we have already connected and you know learned about all that some amazing stuff now let's talk about how actually even working with a real life example so here is a diagram of evm working and uh you know this here here is actually you know how it actually works so first of all in this diagram uh you will see a transaction happening so you can see a transaction here so this transaction contains some values that is two data the value gas price and all that sum is all that stuff so the two is actually is to the contract where it is you know requesting the transaction like for again this to address will be similar to this contract address we will talk about this in a minute but I will be able to see this contract this two addresses same as this contract address because you have re because this transaction is requesting a transaction or some kind of uh you know and it's calling this contract right this is calling this contract and then you can also see that this transaction has some data which will go to the evm so in this contract and this contract so what happens is that whenever I transaction is coming to in the evm the Eva will pull up the contract that it is calling right so whenever a transaction comes like for example here there is a transaction to this contract then what evm will do is evm will pull up the contract that has been called right so evm has right now pulled this contract because this contract has been called right and the contract has these things like address the NOS the balance of it it also contains it code the code of it which is immutable we have discussed like the code is is in the byte code form right and there is also like op code one in that as well op code it basically breakdowns into op code right so that byte code is present in the in the contract which is immutable which can't be changed and then we have a Storage storage which is immutable so if you request on to you know submitting any of your transaction or any of your balance into that contract you can you know do it on the storage which is immutable it is in the form of key and value pairs okay uh yeah and every evm has this program counter that is set to zero and evm will use this program counter to step through this code so this contract has this code which is in the bytecode format so the evm will use this program counter to step through this code and understand what does this contract code does right so we have this transaction again we have this transaction which is sending some kind of data and what evm will do is vvm will grab the smart contract whatever wherever this transaction is calling right and this will use it this program counter to understand what this code is doing and one more thing to understand is that for each and every transaction that has been called onto the evm the code of contract is always loaded and the program counter is always you know the pro the contract code is always uh you know loaded and then the program counter is set to zero and uh the contract storage is also loaded this storage is also loaded right and the memory is all set to zero in here the stack and memory and everything it is it is set to zero right it creates a new evm instance every time there is a transaction being called right so this this is what happens when a transaction is happening onto the evm now what now again what will happen is that evm will take this data so this transaction has some data maybe you are doing a transaction you are calling a function on this contract right so what this does is that this data will be given to the evm EVN will take that data and deal with the code right for example what if you want to call a function on this contract it will take the data it will you know understand what it wants what does the data wants it will then interact with the contract like what contract understand you know understand this contract with program counter and then it will uh you know execute it for example if you are calling a function right it will execute it and use stack and memory to keep you know records of the things and once it's done it changes the storage on the contract as well right so once your data is understandable by the evm it uses this stack and memory and that all of that stuff to actually uh you know do some kind of changes and once the changes in done is done like using this gas available memory and everything is the changes is done uh there is there is a sketch if there is a sketch that there should be enough gas available so once the gas is enough available and the evm has uh you know calculated the changes like whatever it wants once evm has done that it will update the storage of this contract right so this will update the storage of this contract for example let's let's take an example as well too clearly as well so for example this is an s and you want to submit your well your money or basically your ether to this contract what you will do is basically this transaction is calling to this contract and there is a data which contains message dot value right and uh it contains the value what you want to submit to this contract so this data goes to the evm the evm understands that yeah this you know takes the message dot value and it understands that this is this is what they want this is what this particular transaction wants and what it will do is it will update the storage of that contract that hey this particular contract this particular address the transaction who has sent the transaction they have uh requested or they have post they have provided this much of data this much of function it's calling your this function to you know add the The Ether to this particular address right so what it will do is it uh you know it will call that particular function and it will update the storage of that particular contract so in this way your data was successfully you know gone to the evm and evm has understood it and you know it has calculated done the changes and finally it has done the state changes final changes into the contract itself the contract itself is it's in form of storage in the mutable it's mutable its form of key and value pair right so it has finally updated the contract the escrow contract right so every one one thing to note is that every transaction is happening in line in evm so you know once what when I mean every transaction is happening in line this it says that once one transaction is run then only other transaction is executed no transaction is executed parallely right so it's like one two three like one by one one by one right and then once this state is updated right so the evm has uh changed the state of this contract so there is a new state right we have disc we have already discussed the state as well the theorem State as well so there is a new state and then this new state is uh broadcasted to the block so for example how blockchain works I hope you understand that uh once that block has already you know finds the non's value Which is less than the particular Target and proof of work it has been changed now in proof of stake right so once so for example there is a block uh Miner right block or basically block validator and uh evm the proof of stake evm uh so this uh the block the block validator the block owner Miner has this changes it has done this changes in this get for example he is running get or any other client service he has the they have already calculated this ethereum state this has nuclear trade so now this block the particular validator block or that particular minor block you know the the state of that block is changes changed and what it will do is it will have to validate all other blocks that are in the blockchain have to also agree to the same changes right there are so in the blockchain proof of work I'm not sure about proof of stick yet you know I will have to do my own research but what happens in proof of stake chain is that every other block has some kind of rules and regulations that like some kind of checklist which they check is that if the block has uh you know block has this particular checklist then only it will accept those changes of it right uh right so for example the block hash is this much if that matches the hash of the previous block so there are like a checklist which every block has to check before accepting the block right and uh yeah so every block checks that and then every block accepts the new state of that blockchain okay so this changes happening happens in every 14 seconds in ethereum like for example in every 14 seconds there is a new state and then after 14 seconds there is again a new state there is again a new state so this this is what happens and every block computes those changes and we don't uh right every block has to compute all those changes I'm not sure about the proof of stake thing which I have talked about the validator and all this stuff but I'm pretty sure about the proof of work uh working right maybe I'm wrong on the proof of stake side and uh right so what happens is that every block will have to change all that state all the state changes every block will have to compute it right so so Suppose there are 30 nodes in the ethereum right so what what will happen is that every 30 node will have to update those changes it will not happen that only 20 of those notes will update the the update the changes right every block or every node will have to you know update those State changes as well so we don't have the sharding yet so we will not talk about much about that I can create a separate video about that but uh yeah so Suppose there are 30 blocks or 30 nodes in the ethereum so every block will have to compute those State changes uh we don't have sharding yet in which you know every Shard will have some blocks and they will have to change The Shard the stages of that particular Shard and we don't have that right so let's not talk about it I can create a next video about it and there is other chains that have already implemented chardium so maybe we can you know you if you want you can look into it I I have not done my research as of now but uh yeah so this is how actually the evm Works under the hood and what it will happen again is one there will be one case as well like for example so one thing also to remember or to take care of that this transaction has some data right so this to this evm will update this contract but what if there is a call to another contract from this contract right so for that we have a special case for it that is the ethereum virtual machines when it calls the another smart contract right so so one thing is again to notice that evm has access to the world State and not historical data right evm has only uh access to the particular World State no historical data no previous transaction axis not any access to external apis or anything so that's why that's why we have oracles right so you know ethereum virtual machine evm can't have access to the external apis right so that's why we have this Oracle contracts right so so each smart contract can actually call another smart contract so here you know this evm you know request or this evm understands this transaction this data the request and then it it pushes onto the contract but what if this contract is required to call another contract right so every smart contract can call another smart contract and each call creates a new instance of evm right so every time a smart contracts calls another contracts there is a new evm instance created so let's take an example of an oracle like for example chain link smart contract so chilling will smart contract will have the changes in its storage of the current price of the ethereum right so if the smart contracts on itself cannot uh you know get access to external API so what if you want to know the price of the ethereum right now what's the price of ethereum so chain link smart contract will have the price current price of ethereum in its storage so that the other contracts can check this contract for a given state right or for a particular snapshot so there is new ambiguity so for that particular instance of this state the price of the ethereum right it will be price of ethereum or Bitcoin or any other uh you know any other token will be the same for that particular amount of particular instance of time for every other evm right that's why you know using this is very important because there will be no ambiguity in future right so since the evm smart contract the chilling smart contract the chain link smart contract will have the current price in its storage and that only will be accessible to the smart contracts that is calling that chain link Oracle smart contract right so this is like a simple example of how it will work now let's talk about uh again you know there are there is an evm instance right for example it's again has this program counter gas available stack in memory all that stuff right and every time we have already talked let every time there is a creation of evm the program counter is set to zero stack and memory that memory is set to zero and all this stuff right so the evm instance is sent to zero uh and uh in nfpcive example as we have seen right that Suppose there is a call like for example there is a call from here that you know in this contract do this this changes so this contract now has you know calculated the changes but this contract has now is in the in the basically in the Middle where will where he the contract will have to call another contract to get the price again the FM take the chilling example maybe this contract wants the price of ethereum right so this is this contract will call the chilling contract right so this contract will pass on some arguments we'll pass on some input data and there will be a new instance of evm right and there will again there will be program counter set to zero and gas available this stack and memory is there right so what will happen is that this input data right so there is a transaction here here here is a transaction in this example similarly this input data will act as a transaction for this evm instance right and uh once there is that input data some kind of byte code right it will calculate it or maybe it will call another smart contract like there may be another the chilling Oracle the chain link smart contract will call another smart contract right so there there will be another evm instance that will be created yes this this this is the case and then uh suppose for example it it ends here then it will it will calculate the result here it will use the memory you know all these em instances actually use memory to calculate all these changes and once this uh the uh the calculation is done it will return some kind of data to the smart contract that has you know called this smart contract so it will return some kind of data and the state of the new evm is being calculated and this will be a new state right so and also when it passes the new instance is passing the the input right it is calling another smart contract then it will also pass the current state of that evml stands because for example because right now this evm instance one has already done some changes right it has already calculated some changes so it has some change in its state right so it will also pass on this state to evm instance evm instance 2 right and this state will be here and again you know if it is calling another evm another smart contract then the evm instance 3 will have the current state of evm instance 1 and evm instance 2 right and then at the end when the calculation is done and there is finally a return value that has gone to the evm instance one all this uh you know changes will be done and there will be a new world state right and only and only if the gas doesn't runs out so if the gas runs out then there is there will be the state of that evm has that was calculated will be reverted but the gas that was consumed will be there and the nonce of that transaction that was called will be increase right the gates so the gas will be taken as a basically as a punishment for some for wasting the time of evm uh for you know doing all this work so if the gas has run out then the state of that evm will be reverted back right and the return data again the rate and data is written data will uh is a way of smart contract that can return a value after a call right so this written data is basically a way a smart contract can return a value after a call so this this this particular line is basically other data that is a way that how a smart contract can render a value after a call right so yeah this is the second example of how when evm calls another smart contract that will happen but you have to keep in mind the gas should not run out if that is the case everything will be reverted and if that if it is not everything will be reverted and the evm will just evaporate okay so this is just a simple working of evm and I guess this is overall how the ethereum virtual machine works we have taken both of the examples like uh you know example of evm when it calls like single one and how if it calls another smart contract so yeah this is the evm working I hope you get how actually the evm works and uh yeah if you are liking this video please if you're still watching it please please please like this video comment and please share it online Twitter please uh you know do tag me as well it's very important for me uh right and please share it with your friends and family as well understand and let them understand how actually evm works under the hood right now we have to talk about just one topic that is left or maybe two topic one is the ethereum gas and the evm turing machine or whether it is a quasi turing machine or a turing complete machine so yeah let's move on to the last two topics that is the ethereum gas and the turing machine yeah let's move on okay so let's so let's talk about gas ethereum gas so what is gas so gas is nothing but a unit of measurement that measures the amount of computational effort required to perform a certain operation on the ethereum it is also written on the screen as well like it is a fear it is required to conduct a transition on the ethereum blockchain right and uh it is also calculated in GUI as well which is also one of the unit and since ethereum transaction you know it requires certain amount of computational resources to complete you know there should be a commission you know that's why there is a gas fee that actually acts as a commission to successfully run those transaction in the ethereum blockchain the gas fee actually helps to maintain the security of ethereum network as well right so by demanding a fee for each calculation performed on the network we do not allow attackers to send spam on to the network right so to avoid any random infinite Loops or any other computational losses in the code each transaction should actually set up some kind of limit on the number of computational steps for the code execution right because if there is some kind of a person who basically you know writes a loop that is never ending then there will be a trouble with the block unit will goggle up right so we want we don't want that right so there is a gas fee once that gas fee or that particular gas amount is lost or it is not enough to execute the transaction the transaction will be reverted back so in this way you know it helps us to maintain the security of ethereum as well this gaps is gas is really very important for that as well and each op code has a fixed cost to be paid upon the execution measured by the case so when we were looking into the code example of the working of evm we were you know converting bytecode and evium there was actually you know mention of the gas for that particular op code right so there is a particular gas for op all that op code if you haven't seen that video go and see that video as well uh so some op codes can be triggered for gas reference let's talk about ethereum gasket for the sum of code has can trigger gas refunds which reduces the cost of the transaction so guess refunds are applied at the end of any transition so if a transaction has but if a transaction is insufficient gas to reach the end of its run its gas refund cannot be triggered right so this is the case and the transaction fails as well with the introduction of London for hard folk two aspects of gas refund has been changed first is that the Implement to how much cash can be refunded is lowered by the half of the total transaction cost to one fifth or third DOTA transaction cost and second is that the self-destruct of code the self-destruct op code which destroys and completely deletes the smart contract and right that op code cannot trigger gas reference right so only uh that that transfer cannot that op code self-destruct cannot trigger the gas reference as well so these are the changes after the you know London hard folk and now let's actually talk about ethereum during completeness so whether ethereum or evm is a turing complaint machine or not right so what is a turing complete machine a turing complete machine is a system in in which a program can be written that will find an answer to any computational problem although there is no guaranteed regarding the runtime in the memory right and if you machine is during complete it it then it could compute or it can used to solve any computational problem right so this is all about Turing machines so ethereum is actually people you know say that ethereum is a turing Complete because it can execute any program data Turing complete machine can run while reading and writing the smart contract and memory so ethereum is technically not during complete it is quasi Turing complete and why I say that because there are some things that it doesn't fulfill as ethereum during machine right so what what is that thing is also one more fact if you are watching this during complete you know there was a person who created this turing machine in in the in the in between a war there is a movie called imitation game a few hundred is a good uh way to actually understand the history of turing machine so just go and watch it if you want so now let's talk about why ethereum is a evm or ethereum is a quasi Turing complete so the properties of turing machine are as follows like a turing machine can run forever it can use infinite memory and turing machine has Random Access Memories can simulate any other any other turing machine but this these properties ethereum doesn't have because ethereum in ethereum we cannot have programs that can run forever we cannot allow programs to use infinite memory you know this is the case right and if we recall again if you check out the Turing complete machine you know it basically in that it can uh any program can run forever and use infinite memory so when evm is executed a transaction it carefully accounts for every transaction right ethereum smart contract transaction resume smart contract can only execute a transaction so long it has enough gas right and each instruction has a cost that is a guess so for example adding two numbers cost three gas calculating catch act 256 hash cost Teddy gas right creating new contract account costs 32 000 you know gas so there is a particular guess for every other transaction or every instruction right and if a smart contract is executed and it's run into Infinite Loop for example it would eventually run out of gas and transaction would be reverted right so that's not possible in ethereum right and the evm terminates the execution if the amount of gas exceeds the gas available for that transaction so if the gas is not enough for that particular transaction the the evm terminates that execution right so that's why evm is considered to be a quasi-turing complete machine and not uh during complete machine right so that's all about the crash course and finally we have completed all the topics of ethereum or the evm ethereum virtual machine right so thanks a lot for watching thank you and there are some links in the description which I have you know provided some blocks to evm which you can uh you know which you can go through so please check out those links as well and I have other videos I've created you can visit those videos as well if you like and if you found this video this crash course helpful then please please please please please please like to this video And subscribe to the channel I will be creating more and more crash course for your understanding my goal is here to simplify the blockchain stuff and actually clear the basics for all those blockchain developers that are out there maybe you know some of the developers they don't understand the core stuff so I really want to help them understand the underhood of the working of blockchains evm all that other stuff so please subscribe to the channel uh and what other crash course you want me to you know create please write down in the comment section and do follow me on Twitter at the rate sync a underscore uttam you can you know reach out to me on LinkedIn if you have any doubts regarding this crash course as well just tweet it or DM me and whatever it is I will love to help you but please share this to your uh you know in your family group in your WhatsApp group college group or wherever you are please share it on Twitter and Linkedin and do tag me so thanks a lot for watching I hope you found this video uh helpful yeah thank you so much for watching till that take care and bye bye
Up Next

Understanding the Ethereum Virtual Machine: A Technical Guide
@0xOwenThurm
10.1K views•2023-06-15

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

Operational Security Essentials: A Guide for Hacktivists (OPSEC)
@hitbsecconf
157.4K views•2012-11-26

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






































