Understanding the EVM Storage

Understanding the EVM Storage

Introduction:

Understanding the complexities of the Ethereum Virtual Machine (EVM) is crucial for developing smart contracts for the Ethereum network. The EVM's storage architecture, which provides a variety of storage types that may be used to store data in a smart contract, is one of its most important features. The various types of storage that the EVM offers, such as storage, memory, stack, and calldata, will be examined in more detail in this article. We'll also offer some code examples to show you how to use these various storage types in Solidity.

EVM Storage:

The Ethereum Virtual Machine (EVM) is a virtual machine that executes smart contracts on the Ethereum blockchain. When a smart contract is deployed to the blockchain, it is executed on the EVM. The EVM has several types of storage available for smart contracts to use, including storage, memory, stack, and the calldata.

  1. Storage:

    Storage is a persistent data store in the EVM that is used to store state variables. State variables are variables that are saved to the blockchain and can be accessed and modified by any user of the contract.

    A single 256-bit value is kept in each of the storage's 256-bit slots, which are separated from one another.

    To declare a state variable in Solidity, we use the "storage" keyword:

     contract MyCStorage {
       uint256 myVariable; // State variable stored in storage
     }
    

    When a state variable in a contract is changed, the change is recorded in the blockchain's storage. Because of the higher cost of gas, storage is a more expensive alternative than other types of storage.

  2. Memory:

    Memory is a temporary data store in the EVM that is used to store data during contract execution. Anything that does not need to be saved to the blockchain, including function parameters and local variables, is kept in memory. The allocation of memory occurs at runtime and is not persistent between transactions.

    To declare a variable in Solidity that is stored in memory, we use the "memory" keyword:

     function myMemory(uint256 arg1, uint256 arg2) public {
       uint256 localVariable; // Local variable stored in memory
     }
    

    Memory is a much cheaper option than storage in terms of gas cost, but it is not persistent across transactions.

  3. Stack:

    The stack is a temporary data store in the EVM that is used to store data during contract execution. Data is temporarily stored in the stack for usage when contracts are being executed. Data that needs to be kept only temporarily and doesn't need to be persistent is kept in the stack. According to the principle of last-in, first-out (LIFO), the last item added onto the stack is the first thing that can be removed from it.

    Because the EVM is a stack-based machine, the stack serves as its primary working area. Using the opcodes associated with each data location, all data loaded from storage, memory, or calldata is loaded onto the stack ( SLOAD for storage, MLOAD for memory and CALLDATALOAD for calldata).

Calldata:

Calldata is a temporary data store in the EVM that is used to store data passed to a contract in a transaction. Calldata is read-only and cannot be modified by the contract. Calldata is used to pass arguments to a contract and to return values from a contract. To declare a function in Solidity that takes arguments passed in calldata, we use the "calldata" keyword.
calldata is a useful feature in Solidity because it allows developers to reduce gas costs by avoiding unnecessary copying of data. When a function is called externally, the arguments are passed in the calldata section of memory, which is much cheaper than copying the data to the storage or memory section of the contract.

Conclusion:

It's also critical to keep in mind that various storage options come with a variety of trade-offs and restrictions. Memory is less expensive but not persistent, whereas storage is persistent and can be expensive. Calldata is read-only and may be used to pass data into and out of a contract, while the stack is extremely cheap but only transitory.

While using storage in Solidity, it's crucial to take security concerns into account. When storing sensitive information like private keys or user data, caution must be taken because the storage is recorded to the blockchain and is therefore accessible to any user of the contract. Also, in order to prevent running out of gas or exceeding the block gas limit, it's critical to manage storage utilization effectively.

In conclusion, implementing effective and safe smart contracts for the Ethereum blockchain requires a grasp of the EVM storage architecture. Using the examples in this article, you may start utilizing the various storage options offered by Solidity to reduce gas costs and improve contract performance.