The One Question bounty closes Thursday 10 December.

Join in
We Them Media logo We Them Media Support us

Web3bridge Students (cohort 11) Share What They’ve Learned: EVM Explained

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

Favour explains the Ethereum Virtual Machine: how Solidity contracts compile to bytecode, why running them costs gas, and how opcodes set the cost. They also describe the five levels of the Ethereum stack and the memory areas the machine uses, including the stack and persistent state.

Not by We Them Media Channel: Web3Bridge. Published Tuesday 15 October 2024.

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

Transcript

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

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

0:04 A: Hi, my name is Favour. I'm here to discuss the EVM. So let's get started. EVM is short for Ethereum Virtual Machine. Now, this Ethereum Virtual Machine is a core component of the Ethereum blockchain. Now, what does this mean? One of the key functionalities of Ethereum is the ability to execute smart contracts. The EVM is solely responsible for that. So what happens is a developer writes a smart contract in a high-level language like Solidity, and this high-level language smart contract gets compiled into EVM bytecode. Now it gets compiled into EVM bytecode because this is the only language that the EVM actually understands. Once it's compiled into EVM bytecode, it gets deployed on the blockchain and broadcasted on the network. Everybody has access. Every node participating in the network has access. Now, these nodes run the bytecode in an attempt to execute the smart contract. One thing I should point out here is the network's ability to run the smart contracts requires computational effort, and that brings in the concept of gas. Running your contract on the network is not free, so you need to pay a certain fee each Each step in your instruction from your bytecode has a certain charge to it that the EVM would cost you to run it. Now, this is just to prevent bad actors from hogging the resources of the Ethereum network and also to make sure that the resources of the Ethereum network are effectively distributed.

1:34 B: So programs on the EVM are called smart contracts, and they are written in high-level languages like Solidity, Vyper, and so forth. So for the EVM to be able to run the smart contract, it has to compile it, and then it is compiled into bytecodes. These bytecodes are made up of opcodes, which also stands for operation codes. So opcodes are basically- they basically represent singular instructions and operations like addition, subtraction, push, and all. So opcodes are the assembly language, and EVM has its own version of opcodes which has standard operations like add, subtract, etc. And then it also has blockchain-specific operations like balance, address, and block hash. So each particular opcode, it has its own associated gas fee, and they play a big role in determining the total gas fee for your smart contract. And they also have different categories such as we have the logical operations, that's the AND, NOT. We have the arithmetic opcodes, ADD, SUB, DIV. The smart contract creation and interaction opcodes, that's the CREATE, DELEGATE. So an understanding of opcodes is really important in optimizing the performance of your smart contracts and Optimizing your gas prices.

3:06 C: So the EVM is the first level of the Ethereum stack. We have 5 levels. The Ethereum stack is all the technologies that enable developers to build applications, right, on top of the Ethereum blockchain. All right, so there are 5 levels. There's Ethereum Virtual Machine, smart contracts, node software, client APIs, and then decentralized applications. So essentially, you really- a developer has to understand the ins and outs of the Ethereum virtual machine in order to produce good code. So the major difference that separated Ethereum from previously built blockchains like Bitcoin is that Ethereum was conceptualized as a global distributed computer. What that means is that anybody that runs a node can run programs, can execute programs on Ethereum, and it's the EVM that enables this to happen, right? So the EVM is also a Turing-complete machine. What this means is that it can run any program. And like my colleague said, the gas is the mechanism that ensures that bad actors can't run malicious or accidental code. like infinite loops on the EVM. So once the amount of gas that you pay to run a transaction is exhausted, that transaction is stopped. So the EVM is also distributed and it's virtual. What that means also is that for every transaction that is sent to a smart contract, for the smart contract to be executed, an instance of the EVM is created like a sandbox The node, right? And the EVM has different memory locations, so to speak, where it stores different types of variables or data. There's the program code ROM where- and this part is immutable because smart contracts are generally immutable. They can't be changed once they're deployed. So the code of the smart contract is stored in the program code ROM. ROM, that's read-only memory, kind of like a hard drive on EVM, but not really because it's virtual. Then there's also the volatile memory, which contains the stack and the memory location. Here is where the opcodes are run. The opcodes are run on the 1024-depth stack as 256-bit words. So a stack is a data structure where the last data object is executed first. While the memory holds other pieces of information like updates, logs, like update logs. And then there's also the persistent memory which stores the world state or which stores the state of the Ethereum blockchain. So every transaction, every successful transaction changes the state of Ethereum through a transition function. So in essence, once a transaction is executed successfully, It changes the state and then passes that on to the account storage, which also holds other information about the account, like the sender of the account, the value that is passed by the account initiator.

6:27 B: To continue getting more of these weekly updates, I encourage you to like, share, and subscribe to all our social media platforms. Thank you very much.

More from this hub

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