-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathLinkedListSetLib.sol
219 lines (191 loc) · 9.35 KB
/
LinkedListSetLib.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// This file is part of Modular Account.
//
// Copyright 2024 Alchemy Insights, Inc.
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General
// Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with this program. If not, see
// <https://www.gnu.org/licenses/>.
pragma solidity ^0.8.26;
type SetValue is bytes31;
/// @dev The sentinel value is used to indicate the head and tail of the list.
bytes32 constant SENTINEL_VALUE = bytes32(uint256(1));
struct LinkedListSet {
// Byte Layout
// | value | 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA__ |
// | meta | 0x______________________________________________________________BB |
// Bit-layout of the meta byte
// | unused | 0b0000000_ |
// | sentinel | 0b_______A |
// Key excludes the meta bytes, except for the sentinel value, which is 0x1
mapping(bytes32 => bytes32) map;
}
/// @title Linked List Set Library
/// @author Alchemy
/// @notice This library provides a set of functions for managing enumerable sets of bytes31 values. It is a fork
/// of the LinkedListSet library in modular-account-libs, with the following changes:
/// - The flags feature has been removed, so the library no longer supports both the "has next" flag, and the
/// user-defined flags.
/// - The library has been modified to work with bytes31 values instead of bytes30 values.
library LinkedListSetLib {
/// @notice Add a value to a set.
/// @param set The set to add the value to.
/// @param value The value to add.
/// @return True if the value was added, false if the value cannot be added (already exists or is zero).
function tryAdd(LinkedListSet storage set, SetValue value) internal returns (bool) {
mapping(bytes32 => bytes32) storage map = set.map;
bytes32 unwrappedKey = SetValue.unwrap(value);
if (unwrappedKey == bytes32(0) || map[unwrappedKey] != bytes32(0)) return false;
bytes32 prev = map[SENTINEL_VALUE];
if (prev == bytes32(0) || isSentinel(prev)) {
// Set is empty
map[SENTINEL_VALUE] = unwrappedKey;
map[unwrappedKey] = SENTINEL_VALUE;
} else {
// set is not empty
map[SENTINEL_VALUE] = unwrappedKey;
map[unwrappedKey] = prev;
}
return true;
}
/// @notice Remove a value from a set.
/// @dev This is an O(n) operation, where n is the number of elements in the set.
/// @param set The set to remove the value from.
/// @param value The value to remove.
/// @return True if the value was removed, false if the value does not exist.
function tryRemove(LinkedListSet storage set, SetValue value) internal returns (bool) {
mapping(bytes32 => bytes32) storage map = set.map;
bytes32 unwrappedKey = SetValue.unwrap(value);
bytes32 nextValue = map[unwrappedKey];
if (unwrappedKey == bytes32(0) || nextValue == bytes32(0)) return false;
bytes32 prevKey = SENTINEL_VALUE;
bytes32 currentKey;
do {
currentKey = map[prevKey];
if (currentKey == unwrappedKey) {
// Set the previous value's next value to the next value,
// and the flags to the current value's flags.
// and the next value's `hasNext` flag to determine whether or not the next value is (or points to)
// the sentinel value.
map[prevKey] = nextValue;
map[currentKey] = bytes32(0);
return true;
}
prevKey = currentKey;
} while (!isSentinel(currentKey) && currentKey != bytes32(0));
return false;
}
/// @notice Remove a value from a set, given the previous value in the set.
/// @dev This is an O(1) operation but requires additional knowledge.
/// @param set The set to remove the value from.
/// @param value The value to remove.
/// @param prev The previous value in the set.
/// @return True if the value was removed, false if the value does not exist, or if the wrong prev was
/// specified.
function tryRemoveKnown(LinkedListSet storage set, SetValue value, bytes32 prev) internal returns (bool) {
mapping(bytes32 => bytes32) storage map = set.map;
bytes32 unwrappedKey = SetValue.unwrap(value);
if (prev == bytes32(0) || unwrappedKey == bytes32(0)) {
return false;
}
// assert that the previous value's next value is the value to be removed
bytes32 currentValue = map[prev];
if (currentValue != unwrappedKey) {
return false;
}
bytes32 next = map[unwrappedKey];
if (next == bytes32(0)) {
// The set didn't actually contain the value
return false;
}
map[prev] = next;
map[unwrappedKey] = bytes32(0);
return true;
}
/// @notice Remove all values from a set.
/// @dev This is an O(n) operation, where n is the number of elements in the set.
/// @param set The set to remove the values from.
function clear(LinkedListSet storage set) internal {
mapping(bytes32 => bytes32) storage map = set.map;
bytes32 cursor = SENTINEL_VALUE;
do {
bytes32 next = map[cursor];
map[cursor] = bytes32(0);
cursor = next;
} while (!isSentinel(cursor) && cursor != bytes32(0));
}
/// @notice Check if a set contains a value.
/// @dev This method does not clear the upper bits of `value`, that is expected to be done as part of casting
/// to the correct type. If this function is provided the sentinel value by using the upper bits, this function
/// may returns `true`.
/// @param set The set to check.
/// @param value The value to check for.
/// @return True if the set contains the value, false otherwise.
function contains(LinkedListSet storage set, SetValue value) internal view returns (bool) {
mapping(bytes32 => bytes32) storage map = set.map;
return map[SetValue.unwrap(value)] != bytes32(0);
}
/// @notice Check if a set is empty.
/// @param set The set to check.
/// @return True if the set is empty, false otherwise.
function isEmpty(LinkedListSet storage set) internal view returns (bool) {
mapping(bytes32 => bytes32) storage map = set.map;
bytes32 val = map[SENTINEL_VALUE];
return val == bytes32(0) || isSentinel(val); // either the sentinel is unset, or points to itself
}
/// @notice Get all elements in a set.
/// @dev This is an O(n) operation, where n is the number of elements in the set.
/// @param set The set to get the elements of.
/// @return ret An array of all elements in the set.
function getAll(LinkedListSet storage set) internal view returns (SetValue[] memory ret) {
mapping(bytes32 => bytes32) storage map = set.map;
uint256 size;
bytes32 cursor = map[SENTINEL_VALUE];
// Dynamically allocate the returned array as we iterate through the set, since we don't know the size
// beforehand.
// This is accomplished by first writing to memory after the free memory pointer,
// then updating the free memory pointer to cover the newly-allocated data.
// To the compiler, writes to memory after the free memory pointer are considered "memory safe".
// See https://docs.soliditylang.org/en/v0.8.22/assembly.html#memory-safety
// Stack variable lifting done when compiling with via-ir will only ever place variables into memory
// locations below the current free memory pointer, so it is safe to compile this library with via-ir.
// See https://docs.soliditylang.org/en/v0.8.22/yul.html#memoryguard
assembly ("memory-safe") {
// It is critical that no other memory allocations occur between:
// - loading the value of the free memory pointer into `ret`
// - updating the free memory pointer to point to the newly-allocated data, which is done after all
// the values have been written.
ret := mload(0x40)
}
while (!isSentinel(cursor) && cursor != bytes32(0)) {
unchecked {
++size;
}
// Place the item into the return array manually. Since the size was just incremented, it will point to
// the next location to write to.
assembly ("memory-safe") {
mstore(add(ret, mul(size, 0x20)), cursor)
}
cursor = map[cursor];
}
assembly ("memory-safe") {
// Update the free memory pointer with the now-known length of the array.
mstore(0x40, add(ret, mul(add(size, 1), 0x20)))
// Set the length of the array.
mstore(ret, size)
}
}
function isSentinel(bytes32 value) internal pure returns (bool ret) {
assembly ("memory-safe") {
ret := and(value, 1)
}
}
}