You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39Lines changed: 39 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2028,6 +2028,45 @@ function sumArray(uint256[] calldata arr) external pure returns (uint256) {
2028
2028
2029
2029
# Transient Storage
2030
2030
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.
0 commit comments