|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +contract TransientStorageTest { |
| 5 | + constructor() { |
| 6 | + } |
| 7 | + |
| 8 | + // Test 0: Initial Constructor Test |
| 9 | + function runTests() public returns (bool) { |
| 10 | + _runTests(); |
| 11 | + } |
| 12 | + |
| 13 | + function _runTests() internal { |
| 14 | + testBasicFunctionality(); |
| 15 | + testLifecycleValidation(); |
| 16 | + } |
| 17 | + |
| 18 | + // Test 1: Basic Functionality |
| 19 | + function testBasicFunctionality() public { |
| 20 | + uint256 slot = 1; |
| 21 | + uint256 value = 42; |
| 22 | + |
| 23 | + // Store value using TSTORE |
| 24 | + assembly { |
| 25 | + tstore(slot, value) |
| 26 | + } |
| 27 | + |
| 28 | + // Retrieve value using TLOAD |
| 29 | + uint256 retrievedValue; |
| 30 | + assembly { |
| 31 | + retrievedValue := tload(slot) |
| 32 | + } |
| 33 | + |
| 34 | + require(retrievedValue == value, "TLOAD did not retrieve the correct value"); |
| 35 | + |
| 36 | + // Verify TLOAD from uninitialized location |
| 37 | + uint256 uninitializedSlot = 2; |
| 38 | + uint256 uninitializedValue; |
| 39 | + assembly { |
| 40 | + uninitializedValue := tload(uninitializedSlot) |
| 41 | + } |
| 42 | + |
| 43 | + require(uninitializedValue == 0, "Uninitialized TLOAD did not return zero"); |
| 44 | + } |
| 45 | + |
| 46 | + // Test 2.1: Verify transient storage clears after transaction |
| 47 | + function testLifecycleValidation() public { |
| 48 | + uint256 slot = 3; |
| 49 | + uint256 value = 99; |
| 50 | + |
| 51 | + // Store value using TSTORE |
| 52 | + assembly { |
| 53 | + tstore(slot, value) |
| 54 | + } |
| 55 | + |
| 56 | + // Verify it exists within the same transaction |
| 57 | + uint256 retrievedValue; |
| 58 | + assembly { |
| 59 | + retrievedValue := tload(slot) |
| 60 | + } |
| 61 | + require(retrievedValue == value, "TLOAD did not retrieve stored value within transaction"); |
| 62 | + } |
| 63 | + |
| 64 | + // Test 2.2: Verify transient storage clears in subsequent transactions |
| 65 | + function testLifecycleValidationSubsequentTransaction() public { |
| 66 | + uint256 slot = 3; |
| 67 | + bool cleared = isStorageCleared(slot); |
| 68 | + require(cleared, "Transient storage was not cleared after transaction"); |
| 69 | + } |
| 70 | + |
| 71 | + // Utility Function: Check if transient storage is cleared |
| 72 | + function isStorageCleared(uint256 slot) public view returns (bool) { |
| 73 | + uint256 retrievedValue; |
| 74 | + assembly { |
| 75 | + retrievedValue := tload(slot) |
| 76 | + } |
| 77 | + return retrievedValue == 0; // True if cleared |
| 78 | + } |
| 79 | + |
| 80 | + // Test 3: Verify nested contract independence |
| 81 | + function testNestedContracts(address other) public returns (bool) { |
| 82 | + uint256 slot = 4; |
| 83 | + uint256 value = 88; |
| 84 | + |
| 85 | + TransientStorageTest nested = TransientStorageTest(other); |
| 86 | + |
| 87 | + // Store in this contract's transient storage |
| 88 | + assembly { |
| 89 | + tstore(slot, value) |
| 90 | + } |
| 91 | + |
| 92 | + // Call nested contract to write its own transient storage |
| 93 | + nested.writeTransientData(slot, 123); |
| 94 | + |
| 95 | + // Verify this contract's data is unchanged |
| 96 | + uint256 retrievedValue; |
| 97 | + assembly { |
| 98 | + retrievedValue := tload(slot) |
| 99 | + } |
| 100 | + require(retrievedValue == value, "Nested contract interfered with this contract's storage"); |
| 101 | + |
| 102 | + // Verify nested contract's data independently |
| 103 | + uint256 nestedValue = nested.readTransientData(slot); |
| 104 | + require(nestedValue == 123, "Nested contract data incorrect"); |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + // Test 4: Reentry scenario |
| 109 | + function testReentry(address otherContract) public returns (bool) { |
| 110 | + uint256 slot = 5; |
| 111 | + uint256 value = 123; |
| 112 | + |
| 113 | + // Store a value in transient storage |
| 114 | + assembly { |
| 115 | + tstore(slot, value) |
| 116 | + } |
| 117 | + |
| 118 | + // Call the other contract to trigger a callback to this contract |
| 119 | + TransientStorageTest(otherContract).reentryCallback(); |
| 120 | + |
| 121 | + // After reentry, check that the transient storage still has the correct value |
| 122 | + uint256 retrievedValue; |
| 123 | + assembly { |
| 124 | + retrievedValue := tload(slot) |
| 125 | + } |
| 126 | + |
| 127 | + require(retrievedValue == value, "Reentry altered transient storage"); |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + // Utility Function for Test 4: Reentry callback |
| 132 | + function reentryCallback() public { |
| 133 | + uint256 slot = 6; |
| 134 | + uint256 value = 456; |
| 135 | + |
| 136 | + // Store a different value in a different slot |
| 137 | + assembly { |
| 138 | + tstore(slot, value) |
| 139 | + } |
| 140 | + |
| 141 | + // Verify the value was stored correctly |
| 142 | + uint256 retrievedValue; |
| 143 | + assembly { |
| 144 | + retrievedValue := tload(slot) |
| 145 | + } |
| 146 | + |
| 147 | + require(retrievedValue == value, "Reentry callback failed to store correct value"); |
| 148 | + } |
| 149 | + |
| 150 | + // Utility Function for Test 3: Write to transient storage |
| 151 | + function writeTransientData(uint256 slot, uint256 value) external { |
| 152 | + assembly { |
| 153 | + tstore(slot, value) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // Utility Function for Test 3: Read from transient storage |
| 158 | + function readTransientData(uint256 slot) external view returns (uint256) { |
| 159 | + uint256 value; |
| 160 | + assembly { |
| 161 | + value := tload(slot) |
| 162 | + } |
| 163 | + return value; |
| 164 | + } |
| 165 | +} |
0 commit comments