Can we say the Ethereum Virtual Machine (EVM) is like a computer that exists on the Ethereum network or a special computer where all the action happens on the Ethereum network?
Just as you use your computer to run programs, the EVM is where programs on the Ethereum blockchain run. It's in charge of running programs and making sure everything works smoothly.
Follow me as we break down the EVM into bits and pieces.
The EVM is a stack machine with a maximum stack size of 1024. Think of the EVM's "stack" as a virtual notepad that can hold up to 1024 notes, and each note is like a piece of information that can be used while running programs. These notes on the stack are 256-bit in size. It's like having 256 tiny boxes in each note, and each box can hold a piece of data. This means the EVM is a 256-bit word machine.
In the EVM, data can be stored in:
Stack: This is like a temporary sticky note. Imagine a stack like a pile of sticky notes. When the computer is doing something, it keeps small bits of information on these notes temporarily. Once the task is done, these notes are thrown away.
Calldata: This is like a sheet of instructions you get for a task. It contains the details of what a program needs to do. It is read-only, meaning you can see it but can't change it.
Memory: This is like a short-term notepad used for calculations. It's a bit bigger than the stack and can hold more data. But it gets wiped clean after each task.
Storage: This is like a drawer in a big filing cabinet. You keep important stuff here, and it sticks around even after tasks are done. It is used for storing information that needs to last a long time.
Code: This is like the rulebook or recipe for a task. It contains the instructions for the program to follow. It cannot be changed once the program is running.
Logs: These are like a diary where you jot down important events. They help you keep track of what happens during a task. They are useful for checking the history of transactions.
What is stack-too-deep error? Note: This is not the same as the call stack.
You must have come across a stack-too-deep error while writing your Solidity code.
Stack in simple terms
Let's use another analogy for the stack instead of the stick note. This time, I want you to think of the stack as a pile of plates. Each plate holds a piece of information, like a number or a task to do. Now, you can only touch or look at the top 16 plates at once. These are the ones you can easily reach and work with.
If you try to put too many plates on top of each other (like having more than 16 items), it becomes hard to manage. The stack might get wobbly, and you might drop some plates. That's what "stack too deep" means—trying to handle more than the allowed 16 plates at the same time.
To avoid errors, programmers need to keep their tasks simple and make sure they're only dealing with the top 16 plates on the stack at any given moment. Keep it simple and manageable.
You might be wondering and asking, "Why 16?"
The number 16 is chosen because it's a reasonable balance between complexity and efficiency. Dealing with too many things at once can slow down the computer, so there's a practical limit.
Call Stack in Ethereum: Super Simple Terms
Can you picture a call stack as a special list?
A special list where Ethereum keeps track of which contracts (programs) are currently doing something. Picture this list as a series of addresses, each representing a different contract (program) on the Ethereum network. When Ethereum starts working on a new contract, it adds its address to the top of this list. As tasks finish, the addresses are removed from the top. The call stack refers to the current environment where a contract is running within the Ethereum Virtual Machine (EVM).
At the top of the list is the current contract. The contract at the top of the list is the one Ethereum is currently dealing with. It's like saying, "Right now, we're working on this contract." The bottom of the list is where it started. The address at the bottom of the list is where everything began. It's the original contract.
When Ethereum encounters CALL opcodes (commands), it adjusts this list. As operations (CALL opcodes) are executed, contract addresses are added to or removed from this stack. If it calls for a new contract, that address gets added to the top of the list. This is not the same as a regular stack. Remember, the call stack is a specific list for keeping track of contracts. The call stack is specific to keeping track of the contract execution sequence, different from the regular stack used for general data handling within the EVM. The call stack is like a to-do list for Ethereum, showing which contracts have tasks to complete.
Why does the call stack matter?
It helps Ethereum keep track of where it is in the process of running contracts. It also ensures that tasks are completed in the right order.
Going through this write-up, we can agree that the EVM is very interesting. There is still more to know and to love. In the next article, we will see more about memory and storage in the EVM.
Don't forget to follow me to get more interesting and very simple explanations of the EVM, Solidity, and the Web3 world.
Thank you! See you in the next article! Until then, please take care.