DEV Community

ASHDEEP SINGH
ASHDEEP SINGH

Posted on

Intro to EVM

This week we decided to deep dive into EVM (Ethereum virtual machine) . But before that let us know why to deep dive into EVM.

1. Understanding Smart Contracts:

The EVM is where smart contracts (self-executing programs on the blockchain) are deployed and executed. If you want to write, deploy, or interact with smart contracts, understanding how the EVM processes and executes these contracts is crucial.

2. Building Decentralized Applications (dApps):

Ethereum is one of the most popular platforms for building decentralized applications. To develop a dApp, you need to know how your code interacts with the EVM, as it determines how your contracts will execute, use gas, and handle data.

3. Gas Fees and Efficiency:

The EVM charges gas fees for every operation (computation or storage). Learning about the EVM helps you optimize your smart contract code to be more efficient, minimizing gas usage and making your application cheaper to run.

4. Security and Vulnerabilities:

Many smart contract hacks and exploits happen due to developers not fully understanding how the EVM works. Learning the EVM helps you write secure code by avoiding common pitfalls like reentrancy attacks, integer overflows, or issues with how state is stored and managed on-chain.

5. Cross-Chain Compatibility:

The EVM has become a de facto standard for many other blockchain networks like Binance Smart Chain, Polygon, Avalanche, and Fantom. Understanding the EVM can open doors to working with various EVM-compatible blockchains beyond Ethereum.

6. Career Opportunities:

Blockchain development is a growing field, and Ethereum is the second-largest blockchain by market capitalization. Mastering EVM-based development increases your marketability as a blockchain developer and opens up numerous career opportunities in DeFi, NFTs, gaming, and other blockchain-based industries.

7. Token Standards (e.g., ERC-20, ERC-721):

Many Ethereum token standards, such as ERC-20 (for fungible tokens) and ERC-721 (for NFTs), rely on the EVM. Understanding how these tokens work under the hood requires knowledge of the EVM’s mechanics.

8. Blockchain Scaling Solutions:

Understanding how the EVM works helps you comprehend the challenges Ethereum faces (e.g., scalability, gas costs) and how Layer 2 solutions like rollups (Optimistic or ZK) aim to solve them by extending the EVM's capabilities off-chain.

9. Upgrades and Future Development:

Ethereum is constantly evolving, with upgrades like Ethereum 2.0 (the transition to Proof of Stake) and other scaling solutions. Understanding the EVM makes it easier to follow and adapt to these changes, ensuring your knowledge stays relevant as the platform develops.

10. Interoperability with Web3 Ecosystem:

Ethereum’s EVM is integral to the broader Web3 ecosystem. Understanding how the EVM interacts with decentralized finance (DeFi) protocols, NFTs, oracles, and other blockchain services helps you navigate the decentralized internet and create applications that can integrate across different blockchain services.

Now let's proceed further.

In bitcoin blockchain , a block size = 1MB , while in Ethereum Block size = 30 million Gas. If all the Ethereum transaction costs 21,000 gas, what will be the total number of transactions in an Ethereum block? Total number of transaction in an Ethereum Block = 30,000,000/Tx gas = 30,000,000/21000 = 1428. That means ideally , it's around 1428 when we have 30 million Gas.
Now you might think why only 30 mn Gas. Reason is to prevent Heavy Transaction from running. So does it have some drawbacks too ?
Well yes.
1) Any transaction above 30 million gas will fail.

2) The average block time for Ethereum is 12.06 seconds (at the time of recording the video), so a maximum of 1,428 transactions can ideally be processed.

In the Ethereum Virtual Machine (EVM), the fundamental data unit is a 32-byte word (256 bits), which is used to store various types of data, including hexadecimal numbers. storage is organized into slots of fixed size, each slot being a 32-byte (256-bit) chunk. Understanding the slot arrangement is crucial for managing how smart contracts store and access data in the blockchain.

Each bytecode is divided in 3 parts
1) Contract Deployment
2) Runtime Bytecode
3) Metadata

Now let us see slots in more details.

Look at this code below :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;


contract Demo {
    uint8 public a = 0x1e; //slot 0
    uint256 public b = 0xffe123; //slot 1
    bool public c = true; //slot 2
    string public d = "hello"; //slot 3


    // Function to read the contents of a storage slot
    function getSlotValue(uint slot) public view returns (bytes32) {
        bytes32 value;
        assembly {
            value := sload(slot)
        }
        return value;
    }
}
Enter fullscreen mode Exit fullscreen mode

Using this code we can see the slots and what they are storing. This way we can know the slots in which we are having our variables. Note how it happens is we have all variables in one slot till it's memory runs out. We store them in either full or none. If some space is left that is wasted.

So that's all for today folks.
Stay tuned for more.
Next time we'll dive even deeper into EVM and explore some more aspects of it.

Top comments (0)