Build on BNB Chain - Course 1 | BNB Chain Fundamentals
EVM Model - Part 2
EVM Model - Part 2
Hi! In this video, Iāll continue explaining the Ethereum Virtual Machine (EVM). Weāll explore how the EVM works, what message call transactions are, and how gas and fees function within the system.
š§ How the Ethereum Virtual Machine (EVM) Works
The EVM is the runtime environment that powers smart contracts on the BNB Smart Chain. It ensures all transactions are correctly executed and recorded in a secure and isolated software-based environment.
Letās break down its key components using a simple diagram:
- Machine State: A dynamic representation of the blockchainās current state, including:
- Smart contracts and their storage
- Program counter
- Stack
- Memory
EVM Core Components:
- Program Counter: Points to the next instruction to be executed.
- Gas Available: Specifies the maximum gas limit for a transaction. If exceeded, the transaction fails.
- Stack: A memory structure used to execute smart contract logic.
- Memory: Temporarily stores data during contract execution.
- Storage: Stores persistent data associated with smart contracts.
- Code Space: Stores the compiled bytecode of the smart contract.
š Key Insight: The EVM is designed to execute smart contracts securely in a "sandboxed" environment using bytecode compiled from high-level languages like Solidity.
š Message Call Transactions
A message call (also known as a contract call) is a transaction that allows one smart contract to interact with another.
- Initiated when one contract calls another or itself.
- Parameters are pushed to the stack.
- The receiving contract (callee) accesses these inputs to execute its code.
- Can also modify the stack with outputs or intermediate results.
š Stack Principle: The EVM stack follows a LIFO (Last In, First Out) principleānewest values are removed first.
Why It Matters:
- Enables the creation of complex, interconnected decentralized applications (dApps).
- Ensures smooth execution of smart contracts across a blockchain ecosystem.
ā½ Gas and Fees
In the EVM, every operation (represented as an opcode) consumes gas.
What is Gas?
- A unit of computation cost in the EVM.
- Prevents abuse and ensures efficient resource usage.
- Encourages developers to write optimized code.
When is Gas Used?
- Retrieving/writing data from/to storage
- Sending transactions
- Executing contract logic
Gas Fee Structure:
- Gas Limit: Max gas user is willing to consume.
- Gas Price: Fee paid per gas unit.
- Total Cost = Gas Used Ć Gas Price (paid in BNB)
š Gas in Action:
- If gas runs out mid-execution, the transaction fails, but the gas is still consumed.
- Higher gas fees prioritize your transaction on the network.
š How All Components Work Together
- User initiates a transaction.
- Parameters are placed on the stack.
- EVM reads and processes opcodes using gas.
- Memory and storage are updated with new data.
- The transaction is completed and recorded on the blockchain.
- Gas fees are paid to compensate the network validators.
Final Thoughts
This was a technical deep dive, and itās completely okay if not everything clicks right away. You donāt need to memorize all these details to write smart contractsābut understanding how things work under the hood helps you become a more effective blockchain developer.
I encourage you to explore more, look into each component, and experiment.
Test
Comments
You need to enroll in the course to be able to comment!