<aside> 📘 Relates:
Solidity layout and access of storage state variables simply explained
Solidity delegatecall usage and pitfalls
Understanding Smart Contract Vulnerabilities
</aside>
This guide will explain how data stored on smart contracts. Contract storage layout refers to the rules governing how contracts’ storage variables are laid out in long-term memory.
Contract storage layout refers to the rules governing how contracts’ storage variables are laid out in long-term memory. Almost all smart contracts have state variables that need to be stored long-term.
In addition to the public functions and variables we define to make up our contract’s external interface, the layout of state variables is also considered to be part of the external interface as well.
Smart contract developer’s do not have direct control over this aspect of their contract’s external interface, it is controlled by the compiler. However, if the compiler version changes and the rules of contract storage layout were to change, a developer would need to be aware of this.
Smart contracts are computer programs that run on blockchains. Programs consist of functions and data (also known as variables or parameters) that functions operate on. The data that functions use need to be stored somewhere in the computer’s memory. In this case the computer is the EVM.
There are 3 different types of memory in Solidity that developers can use to instruct the EVM where to store their variables: memory, calldata, and storage.
There are also rules about how long the variable’s memory location will be valid as well as rules about how the variable can be used. For example, can the variable be read? Can the variable be written to?
Developers use the memory keyword for variables and parameters that are used within a function. These types of variables only exist during the lifetime of a function being executed. When a function finishes running, the variables and parameters stored in the memory area go away.
For those with programming backgrounds memory is the type of memory that most people are familiar with.