Skip to content

Commit 2920337

Browse files
committed
Add Transient Storage
1 parent acb4b2c commit 2920337

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,45 @@ function sumArray(uint256[] calldata arr) external pure returns (uint256) {
20282028

20292029
# Transient Storage
20302030

2031+
- Transient storage is a new feature introduced to Ethereum through EIP-1153.
2032+
- It provides a special type of storage for Ethereum smart contracts that:
2033+
2034+
- Exists only during a single transaction.
2035+
- Automatically resets at the end of the transaction.
2036+
- Does not incur persistent storage costs (unlike regular storage, which is expensive because it remains on-chain indefinitely).
2037+
2038+
- Transient storage is implemented through two new opcodes:
2039+
2040+
- `TSTORE`: Temporarily stores a value in transient storage.
2041+
- `TLOAD`: Retrieves a value from transient storage.
2042+
2043+
```solidity
2044+
contract ReentrancyGuard {
2045+
bytes32 constant SLOT = 0;
2046+
2047+
modifier nonreentrant() {
2048+
assembly {
2049+
if tload(SLOT) { revert(0, 0) }
2050+
tstore(SLOT, 1)
2051+
}
2052+
_;
2053+
assembly {
2054+
tstore(SLOT, 0)
2055+
}
2056+
}
2057+
}
2058+
```
2059+
2060+
One practical example of transient storage is using it for a reentrancy guard. Normally, you’d store a boolean flag in contract storage to indicate that a function is currently being executed. With transient storage, you can use the `TSTORE` and `TLOAD` opcodes instead, which let you store and read this flag during the transaction without writing to storage. This not only makes the guard cheaper to implement, but also avoids cluttering your contract’s persistent state.
2061+
2062+
**Differences from Existing Data Locations**
2063+
| Data Location | Persistence | Cost | Typical Usage |
2064+
| --------------------- | ---------------------------- | ------------------ | ------------------------------------------------------------ |
2065+
| **storage** | Persists across transactions | High | State variables, permanent contract data |
2066+
| **memory** | Temporary (function scope) | Medium | Local variables, function operations |
2067+
| **calldata** | Read-only, external inputs | Low | External function parameters |
2068+
| **Transient Storage** | Only within one transaction | Lower than storage | Ephemeral data used across calls within the same transaction |
2069+
20312070
# Send Ether
20322071

20332072
# Function Selector

0 commit comments

Comments
 (0)