Using EIP-1153 transient storage from Solidity: inline assembly tstore/tload since 0.8.24, `transient` data location only from 0.8.28
Question: Dencun shipped EIP-1153, so how do you actually use transient storage from Solidity?
State of the compiler:
- Solidity 0.8.24 added Cancun support, including the TSTORE/TLOAD opcodes, but no high-level syntax. You use them from inline assembly only.
- Solidity 0.8.28 introduced a transient data location for value types declared at contract level (e.g. uint256 transient locked;). Reference types (mappings, arrays, structs) were still not supported when it landed, so check the release notes for the version you compile with before assuming a mapping works.
You must also select the right EVM version, or the opcodes are rejected or produce a runtime invalid-opcode revert on chains that are not post-Cancun: evm_version = 'cancun' in foundry.toml, or the equivalent solc/Hardhat setting.
Canonical assembly guard, from the Solidity 0.8.24 announcement:
modifier nonreentrant { assembly { if tload(0) { revert(0, 0) } tstore(0, 1) } _; assembly { tstore(0, 0) } // clear, so the pattern stays composable }
Two things to get right. Clear the flag on exit: transient values live for the whole transaction, not the call, so leaving it set blocks legitimate later calls in the same transaction. And pick slot numbers deliberately — in assembly, slots are unnamespaced within your contract, so two mixins both using slot 0 collide silently, and delegatecalled code shares your namespace.
Source: adapted from a Stack Exchange answer by Mila A (question by Seongyun Ko), https://ethereum.stackexchange.com/questions/159252/with-dencun-we-have-now-eip-1153-but-how-can-i-use-the-new-feature-in-solidity — CC BY-SA 4.0