EVM Storage

Favour AjayeFavour Ajaye
7 min read

I promised to write about storage... And here it is.

Hi, just in case you have not read my other articles, I am your go-to girl anytime you need a simplification of solidity and blockchain. You would be happy if you dived into my homepage to see what I'm talking about. If you are following my articles, I want to say a big thank you and get ready for more amazing reads.

The last time, we talked about the EVM memory. It was a great write-up for me, and I hope it was for you too. We will be seeing what storage is today and getting a better understanding of it.

What is storage?

storage boxes

In our story, we see Ethereum as a large shared digital field where people can play games and build things. Now, each player in this field has a locker where they can keep their tools, game equipment, notes, and special items.

In Ethereum, these lockers are like storage spaces. They are used to store important information, like account balances, computer programs (smart contracts), and other data.

The catch is that whenever you want to put something new in your locker or take something out, you need to pay a small fee. This fee helps keep the field organized, runs smoothly, and prevents it from getting too disorganized.

You're working on a digital desk. Memory is like your temporary workspace, where you put things while you're actively working on them. It's like having sticky notes or a whiteboard where you jot down quick notes. On the other hand, storage is more like your file cabinet. It's where you keep things for the long term. When you're done with your work on the desk, you put the important stuff into the file cabinet so you can find it later.

So, in the EVM:

  • Memory is for short-term, quick-access stuff while you're actively processing things.

  • Storage is for long-term, persistent data that needs to be kept even after your program (smart contract) stops running.

Picture your smart contract as a big storage room. In this room, you have a massive shelf, and each slot on the shelf can hold a number. But here's the interesting part: each number can be really, really big—it's like having a giant box for each slot.

Now, the reason each box is so huge (256 bits or 32 bytes) is like having big boxes so you can do some operations like math operations.

So, your storage room is big. It is very big—it can store 2^256 boxes! That's a large number. But here's the catch: when you first get the room, all the boxes are empty, and they are filled with zeros by default. It's like having a room full of empty boxes waiting for you to put something in them.

The cool thing is that you can read what's inside any box whenever you want, and you can also put things into the boxes. But remember, until you put something in, they're all zeros.

And the best part? You don't have to pay extra for having this massive room; it's just part of the deal when you create a smart contract. So, you have this enormous space to store and retrieve information, and it doesn't cost you anything until you start using it.

Storage is different from memory because we don't just write to an address. We have this idea of a key-value pair. It can be thought of as a key-value store where the keys and values are both 32-byte chunks of data. Now, let's talk about key-value pairs. In the regular writing scenario (memory), you just write things randomly on different pages without any specific order. In storage, instead of randomly, you decide to be organized. Each piece of item gets a special tag (key), like a label on a box. So, if you want to find something later, you just look for the right labeled box (key) to get the exact item (value) you need.

The maximum key size is 256 bits. This means we can store a lot of things if we want to.
One problem with storage is that with different items in storage, some are going to be fixed and some are going to be dynamically sized.

Let's break down the challenge with storage involving fixed-size and dynamically sized items.

Fixed Size:

  • See fixed-size items in storage, like boxes of a specific size. No matter what you put inside, the box remains the same size.

  • In Ethereum, this could be something like a number or a small piece of data that always takes up a known amount of space. For example, if you're storing a simple integer representing an account balance, you know it will always be a certain number of bits.

Dynamically Sized:

  • You have magical bags instead of boxes for dynamically sized items. These bags can expand or contract based on what you put inside.

  • In Ethereum, this could be something like a string of text or a list of items where the size isn't fixed. For example, if you're storing a description or a list of transactions, the amount of space it takes up can change based on the content.

What is the challenge?

  • The challenge comes when you have a mix of both fixed and dynamically sized items in your storage. It's like trying to organize your boxes and bags on the shelves efficiently.

  • Fixed-size items are easy to manage; you always know where they fit. But with dynamically sized items, you need to be flexible because their size can change, and you might need to find extra space on the shelves.

  • Fixed-size items are straightforward to manage because their size doesn't change. However, with dynamically sized items, the size can vary, making it more complex to organize and retrieve information efficiently.

In Ethereum, managing storage efficiently is crucial because you're paying for the space you use. If you have a lot of dynamically sized items, it's like having bags of varying sizes on your Ethereum shelves, and you need to make sure you're using the space wisely. Storing or updating dynamically sized data can be more expensive because it may require more computation and storage space. Dynamically sized data operations often consume more gas compared to fixed-size operations.

Now, let's look at some advantages and disadvantages of the 256-bit word length in the EVM

Advantages of 256-bit word length in the EVM

Simplicity: Having a single, fixed-size word for everything simplifies the design and execution of the EVM. This reduces complexity and potential security vulnerabilities.
Large Address Space: With 256 bits, the EVM can accommodate a vast address space, allowing for a large number of unique addresses for smart contracts and user accounts.
High Security: The 256-bit word length provides a high level of cryptographic security, making it extremely difficult for malicious actors to compromise the integrity of transactions and data stored on the blockchain.
Flexibility: The size of a 256-bit word allows for complex mathematical operations and data manipulation within smart contracts, enabling developers to create decentralized applications (dApps) with a lot of functionalities.

Disadvantages of 256-bit word length in the EVM

Increased Gas Costs: Operations involving 256-bit numbers often require more computational resources and, consequently, result in higher gas costs for transactions. This can make certain operations on the Ethereum blockchain more expensive.
Potential Resource Consumption: Storing and processing large 256-bit values can consume significant resources, including memory and processing power, which may pose scalability challenges for the Ethereum network, especially during periods of high activity.
Complexity for Developers: Working with 256-bit numbers and operations can be more complex for developers, particularly those new to blockchain development. Understanding how to efficiently use and manipulate these large numbers requires additional knowledge and expertise.

I love simplifying things. So let's look at these two images of the pros and cons... simplified

I think we should stop here for now. This is a lot of information, I guess. Our great storage... Well, wasn't it fun? Let me know what you think in the comment section. Don't forget to check out my other articles. You would be amazed at what you could learn from them.
Please follow me on Twitter and LinkedIn. Don't forget to follow me here too.
See you next time.

8
Subscribe to my newsletter

Read articles from Favour Ajaye directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Favour Ajaye
Favour Ajaye

smart contract developer