Skip to content

Commit

Permalink
Add function to clear transient storage array
Browse files Browse the repository at this point in the history
  • Loading branch information
fedgiac committed Feb 4, 2025
1 parent 8e9a70f commit badb2c3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/mixin/TransientStorageArray.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,13 @@ abstract contract TransientStorageArray {
mstore(dataMemoryLocation, byteLength)
}
}

/// @notice Clear all the content of the transient storage array, leaving
/// in its stead an empty array
/// @dev The actual transient storage array content isn't actually reset to
/// zero bytes, however this data cannot be accessed and can be overwritten
/// at a later point
function clearTransientStorageArray() internal {
length = 0;
}
}
14 changes: 13 additions & 1 deletion test/mixin/TransientStorageArray.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,22 @@ contract TransientStorageArrayTest is Test {
checkRoundTrip(data);
}

function testFuzz_canOverrideArray(bytes memory firstArray, bytes memory secondArray) private {
function testFuzz_canOverrideArray(bytes memory firstArray, bytes memory secondArray) external {
assertEq(executor.storeTwiceAndRead(firstArray, secondArray), secondArray);
}

function testFuzz_clearingDoesNotAffectFutureStore(bytes memory data) external {
assertEq(executor.storeClearAndRead(data), hex"");
}

function testFuzz_clearedArrayHasZeroLength(bytes memory data) external {
assertEq(executor.storeClearReturnLength(data), 0);
}

function testFuzz_clearingDoesNotAffectFutureStore(bytes memory firstArray, bytes memory secondArray) external {
assertEq(executor.storeClearStoreAndRead(firstArray, secondArray), secondArray);
}

function testFuzz_settingTransientVariablesDoesNotChangeArray(bytes memory data) external {
BusyStoreAndRead busyExecutor = new BusyStoreAndRead(new BusyTransientStorage());
assertEq(busyExecutor.storePopulateAndRead(data), data);
Expand Down
23 changes: 23 additions & 0 deletions test/mixin/TransientStorageArray/ExposedTransientStorageArray.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ contract ExposedTransientStorageArray is TransientStorageArray {
function length() external view returns (uint256) {
return transientStorageArrayLength();
}

function clear() external {
clearTransientStorageArray();
}
}

uint256 constant BYTES_IN_WORD = 32;
Expand Down Expand Up @@ -50,6 +54,25 @@ contract StoreAndRead {
return tsa.read();
}

function storeClearReturnLength(bytes memory data) external returns (uint256) {
tsa.store(data);
tsa.clear();
return tsa.length();
}

function storeClearAndRead(bytes memory data) external returns (bytes memory) {
tsa.store(data);
tsa.clear();
return tsa.read();
}

function storeClearStoreAndRead(bytes memory data1, bytes memory data2) external returns (bytes memory) {
tsa.store(data1);
tsa.clear();
tsa.store(data2);
return tsa.read();
}

function checkLength(Vm vm, bytes memory data) external {
vm.assertEq(tsa.length(), 0);
tsa.store(data);
Expand Down

0 comments on commit badb2c3

Please sign in to comment.