Introduction
Transient Storage creates a place in a block with temporary data for Ethereum transactions. The benefits are significant gas savings for users, for rollups, and in making certain aspects of smart contracts more secure. We often take for granted the permanence of data on the blockchain, but there is value in being able to store temporary data too. Today’s write-up explains how. Enjoy.
Sections
How is Smart Contract Data Stored on Ethereum?
What is the Difference between Contract and Memory Storage?
What are Transient Storage and its benefits?
Cheaper Contract Deployment
Efficient Rollup Storage
Protection from Re-Entrancy Attacks
In Conclusion
*Thank you to Andy & Ziad for proofreading.
Contract Storage on Ethereum
Whenever a smart contract is deployed to Ethereum, it receives an amount of storage that it can use to hold data. This data storage contains everything a contract may need; whitelisted addresses, who the contract owner is, what the balances of token holders are, and so on. Every piece of data that a contract may use is stored here, in this place named contract storage, recorded directly onto the blockchain.
When one of these pieces of data needs to be updated, like changing the owner of a contract for example, a transaction is submitted with the old owner’s address as well as the new owner’s. This rewrites the data held in contract storage, and a record of the old contract owner is maintained in the transaction history. The next time the contract checks who the owner is, it’ll only see the address belonging to its new owner.
The problem is that this operation of writing or rewriting data to the blockchain is the most expensive operation that can be performed (it involves updating Ethereum’s state trie, which you can read about here). Despite the cost, this operation happens frequently out of necessity. Transactions people perform cause changes to a contract’s storage, like who the current holders of a token are, for example. As a result, part of the cost people pay when buying an NFT or a token goes towards updating the contract’s storage.
Memory Storage
If a contract’s storage is the hard drive, you can think of memory storage as the RAM. Memory storage is where a contract stores the data it actually uses when performing a transaction (retrieving the old contract owner’s name and updating it with the new one, for example). Once a transaction is finalized, any relevant new values in memory storage are posted or updated in contract storage, i.e. the expensive transaction. Everything that shouldn’t be posted to contract storage is deleted.
Memory storage is not on-chain, it’s a temporary data source that exists entirely on the node’s computer while a node executes a transaction as part of a block. One might wonder if memory storage is sufficient to allow “transient storage,” a place for temporary data to exist for the duration of a transaction. This suspicion is somewhat correct, and there are some implementations of transient storage on Ethereum today that do things this way. There are however limitations to using memory storage as a temporary data source that necessitate transient storage as a new feature.
EIP-1153: Transient Storage
EIP-1153 creates a new space in blocks to hold data temporarily while a transaction is executed. This approach to transient storage delivers several benefits that are explained below, including cheaper deployment of some contracts, cheaper costs for rollups reporting their state to Ethereum, and protection from re-entrancy attacks.
Cheaper Contract Deployment
Many contracts today allow users to create completely new contracts, as in Uniswap when creating a new liquidity pool to swap two assets, or when setting up a new NFT contract with Manifold. When users use Uniswap or Manifold for this purpose, the contract they interact with “initializes” the new contract with some basic parameters. To do this it’ll have some generic data like the structure of the liquidity pool, or code compliant to the standard of ERC-721. Alongside the generic data there’ll also be some individual data that describes the specific assets in a pool or the fee it charges for trades, or the name of an NFT smart contract.
Both these generic and individual types of data* are passed to a contract when it is initialized today, often resulting in individual data being held in contract storage, inflating costs. EIP-1153 enables the generic data (init code) to always stay the same by instead passing the individual data (constructor arguments) into transient storage. This method saves gas costs on deployments of these types of contracts since individual data is not posted on-chain. It also permits the development of new features that would have otherwise been prohibitively expensive.
*The technical terms for these are constructor arguments for individual data, and init code for the generic data.
Rollup Storage
Rollups aggregate transactions and submit them together to Ethereum in batches. When rollups submit to Ethereum, they submit the proof (the result of all the transactions), as well as the data executed itself. The data is published so that the proof can be verified as valid.
Transient storage can allow rollups to generate their proofs more efficiently. This reduces gas costs for all rollup submissions, and by extension, all transactions on rollups.
Read more about rollups here.
Re-Entrancy Attacks
A re-entrancy attack happens when a transaction is able to withdraw assets from a contract multiple times before stopping, often draining a contract of its funds. This happens when a vulnerable contract sends assets out first, then updates its balance afterwards. A re-entrancy attack gets in the middle of those two actions, preventing the vulnerable contract from updating its balance and allowing the attacker to drain it of its funds.
Re-entrancy attacks can be prevented with properly planned code, but transient storage makes them even easier to mitigate. By storing information about a withdrawal in transient storage, withdrawals will happen only for pre-determined amounts. No more re-entrancy attacks like the one that resulted in millions of ETH being drained from “The DAO” in 2016.
In Conclusion
Transient storage can reduce gas costs across a variety of complex smart contracts. This enables using dApps more cost-effectively while also enabling novel applications on Ethereum. Someone has already developed a way to give someone a quick, instant loan while keeping custody of the funds.
While transient storage benefits users, the bigger benefit is to the protocols that use it. Uniswap and Optimism have pushed aggressively for transient storage to be included in the Shanghai upgrade instead of staking withdrawals. The controversy around that motivated me to start this newsletter to keep people informed on developments in Ethereum. My hope is this information enables people to understand where Ethereum is going, and to add your voice on where you think that should be.
Thank You & Additional Reading!
Thanks a lot for reading! Here are some more resources if you'd like to dive deeper.
EIP-1153 by Alexey Akhunov and Moody Salem
Please like this post and sign up for more simple write-ups on blockchain concepts.
If you’d like me to cover any topics or have any questions, reach out in the comments!
Share a Summary
Thanks again, please consider sharing this write-up below!
Stay kind. Stay curious.
https://twitter.com/RamiWrites/status/1646897788779925504?s=20