-
Notifications
You must be signed in to change notification settings - Fork 4
/
TransientUintUintMapLib.sol
62 lines (54 loc) · 1.89 KB
/
TransientUintUintMapLib.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-FileCopyrightText: 2024 Lido <info@lido.fi>
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.24;
type TransientUintUintMap is uint256;
using TransientUintUintMapLib for TransientUintUintMap global;
library TransientUintUintMapLib {
function create() internal returns (TransientUintUintMap self) {
// keccak256(abi.encode(uint256(keccak256("TransientUintUintMap")) - 1)) & ~bytes32(uint256(0xff))
uint256 anchor = 0x6e38e7eaa4307e6ee6c66720337876ca65012869fbef035f57219354c1728400;
// `anchor` slot in the transient storage tracks the "address" of the last created object.
// The next address is being computed as keccak256(`anchor` . `prev`).
assembly ("memory-safe") {
let prev := tload(anchor)
mstore(0x00, anchor)
mstore(0x20, prev)
self := keccak256(0x00, 0x40)
tstore(anchor, self)
}
}
function add(
TransientUintUintMap self,
uint256 key,
uint256 value
) internal {
uint256 slot = _slot(self, key);
assembly ("memory-safe") {
let v := tload(slot)
// NOTE: Here's no overflow check.
v := add(v, value)
tstore(slot, v)
}
}
function get(
TransientUintUintMap self,
uint256 key
) internal view returns (uint256 v) {
uint256 slot = _slot(self, key);
assembly ("memory-safe") {
v := tload(slot)
}
}
function _slot(
TransientUintUintMap self,
uint256 key
) internal pure returns (uint256 slot) {
// Compute an address in the transient storage in the same manner it works for storage mappings.
// `slot` = keccak256(`self` . `key`)
assembly ("memory-safe") {
mstore(0x00, self)
mstore(0x20, key)
slot := keccak256(0x00, 0x40)
}
}
}