Skip to content

Commit

Permalink
Add LibTransient
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Jan 27, 2024
1 parent d25eb6f commit 0e65965
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
LibTransientTest:testTransientSetAndGet(bytes32,uint256) (runs: 256, μ: 700, ~: 700)
LibTransientTest:test__codesize() (gas: 1779)
ReentrancyGuardTest:testRecursiveDirectUnguardedCall() (gas: 32203)
ReentrancyGuardTest:testRecursiveIndirectUnguardedCall() (gas: 45600)
ReentrancyGuardTest:testRevertGuardLocked() (gas: 31984)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The Solidity smart contracts are located in the `src` directory.

```ml
utils
├─ LibTransient — "Transient storage get/set helper"
└─ ReentrancyGuard — "Reentrancy guard mixin"
```

Expand Down
22 changes: 22 additions & 0 deletions src/utils/LibTransient.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

/// @notice Transient storage get/set helper.
/// @author Soledge (https://github.com/vectorized/soledge/blob/main/src/utils/LibTransient.sol)
library LibTransient {
/// @dev Returns the value at `tSlot` in transient storage.
function get(bytes32 tSlot) internal view returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
result := tload(tSlot)
}
}

/// @dev Sets the value at `tSlot` in transient storage to `value`.
function set(bytes32 tSlot, bytes32 value) internal {
/// @solidity memory-safe-assembly
assembly {
tstore(tSlot, value)
}
}
}
15 changes: 15 additions & 0 deletions test/LibTransient.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./utils/SoladyTest.sol";
import {LibTransient} from "../src/utils/LibTransient.sol";

contract LibTransientTest is SoladyTest {
function testTransientSetAndGet(bytes32 s, uint256 i) public {
unchecked {
assertEq(LibTransient.get(s), bytes32(0));
LibTransient.set(s, bytes32(i));
assertEq(LibTransient.get(s), bytes32(i));
}
}
}

0 comments on commit 0e65965

Please sign in to comment.