-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataStoreModuleLib.sol
More file actions
284 lines (249 loc) · 7.47 KB
/
DataStoreModuleLib.sol
File metadata and controls
284 lines (249 loc) · 7.47 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.20;
// external - interfaces
// external - libraries
// external - contracts
// internal - globals
// internal - interfaces
import {DataStoreModuleStorage} from "../structs/storage.sol";
// internal - structs
// internal - libraries
/**
* @title DSML: DataStore Module Library
*
* @notice A Storage Management Library created for the contracts and modules that inherits DataStoreModule (DSM).
* Enables Dynamic Structs with unlimited key space.
* Provides an Isolated Storage Layout with IDs and KEYs.
* Focusing on upgradable contracts with various data types to create a
* * sustainable development environment.
* In summary, extra gas cost that would be saved with Storage packing are
* * ignored to create dynamic structs.
*
* @dev Distinct id and key pairs SHOULD return different storage slots. No collisions!
* @dev IDs are the representation of an entity with any given key as properties.
* @dev review: Reserved TYPEs are defined within globals/id_type.sol
* @dev review: For a safer development process, NEVER use the DataStoreModuleStorage with strings. Refer to globals/reserved_key_space.sol
*
* @dev While it is a good practice for keeping a record;
* * TYPE for ID is not mandatory, an ID might not have an explicit type.
* * e.g., When a relational data is added with getKey, like allowance, it has a unique ID but no TYPE.
* * Thus there are no checks for types or keys.
*
* @dev readUint(id, arrayName) returns the lenght of array.
*
* @dev Contracts relying on this library must use DataStoreModuleLib.DataStoreModuleStorage
* @dev This is an internal library, requires NO deployment.
*
* @author Ice Bear & Crash Bandicoot
*/
library DataStoreModuleLib {
/**
* @custom:section ** HELPERS **
*
* @custom:visibility -> pure-internal
*/
/**
* @notice generalized method of generating an ID
*
* @dev Some TYPEs may require permissionless creation, allowing anyone to claim any ID;
* meaning malicious actors can claim names to mislead people. To prevent this
* TYPEs will be considered during ID generation.
*/
function generateId(bytes memory _name, uint256 _type) internal pure returns (uint256 id) {
id = uint256(keccak256(abi.encode(_name, _type)));
}
/**
* @notice hash of given ID and a KEY defines the key for the DataStoreModuleStorage
* @return key bytes32, hash.
**/
function getKey(uint256 id, bytes32 param) internal pure returns (bytes32 key) {
key = keccak256(abi.encode(id, param));
}
/**
* @custom:section ** DATA GETTERS **
*
* @custom:visibility -> view-internal
*/
function readUint(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key
) internal view returns (uint256 data) {
data = self.uintData[getKey(_id, _key)];
}
function readBytes(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key
) internal view returns (bytes memory data) {
data = self.bytesData[getKey(_id, _key)];
}
function readAddress(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key
) internal view returns (address data) {
data = self.addressData[getKey(_id, _key)];
}
/**
* @custom:section ** ARRAY GETTERS **
*
* @custom:visibility -> view-internal
*/
function readUintArray(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
uint256 _index
) internal view returns (uint256 data) {
data = self.uintData[getKey(_index, getKey(_id, _key))];
}
function readBytesArray(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
uint256 _index
) internal view returns (bytes memory data) {
data = self.bytesData[getKey(_index, getKey(_id, _key))];
}
function readAddressArray(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
uint256 _index
) internal view returns (address data) {
data = self.addressData[getKey(_index, getKey(_id, _key))];
}
/**
* @custom:section ** STATE MODIFYING FUNCTIONS **
*
* @custom:visibility -> internal
*/
/**
* @custom:subsection ** DATA SETTERS **
*/
function writeUint(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
uint256 _data
) internal {
self.uintData[getKey(_id, _key)] = _data;
}
function addUint(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
uint256 _addend
) internal {
self.uintData[getKey(_id, _key)] += _addend;
}
function subUint(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
uint256 _minuend
) internal {
self.uintData[getKey(_id, _key)] -= _minuend;
}
function writeBytes(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
bytes memory _data
) internal {
self.bytesData[getKey(_id, _key)] = _data;
}
function writeAddress(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
address _data
) internal {
self.addressData[getKey(_id, _key)] = _data;
}
/**
* @custom:subsection ** ARRAY SETTERS **
*/
function appendUintArray(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
uint256 _data
) internal {
bytes32 arrayKey = getKey(_id, _key);
self.uintData[getKey(self.uintData[arrayKey]++, arrayKey)] = _data;
}
function appendBytesArray(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
bytes memory _data
) internal {
bytes32 arrayKey = getKey(_id, _key);
self.bytesData[getKey(self.uintData[arrayKey]++, arrayKey)] = _data;
}
function appendAddressArray(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
address _data
) internal {
bytes32 arrayKey = getKey(_id, _key);
self.addressData[getKey(self.uintData[arrayKey]++, arrayKey)] = _data;
}
/**
* @custom:subsection ** BATCH ARRAY SETTERS **
*/
function appendUintArrayBatch(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
uint256[] memory _data
) internal {
bytes32 arrayKey = getKey(_id, _key);
uint256 arrayLen = self.uintData[arrayKey];
uint256 _dataLen = _data.length;
for (uint256 i; i < _dataLen; ) {
self.uintData[getKey(arrayLen++, arrayKey)] = _data[i];
unchecked {
i += 1;
}
}
self.uintData[arrayKey] = arrayLen;
}
function appendBytesArrayBatch(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
bytes[] memory _data
) internal {
bytes32 arrayKey = getKey(_id, _key);
uint256 arrayLen = self.uintData[arrayKey];
uint256 _dataLen = _data.length;
for (uint256 i; i < _dataLen; ) {
self.bytesData[getKey(arrayLen++, arrayKey)] = _data[i];
unchecked {
i += 1;
}
}
self.uintData[arrayKey] = arrayLen;
}
function appendAddressArrayBatch(
DataStoreModuleStorage storage self,
uint256 _id,
bytes32 _key,
address[] memory _data
) internal {
bytes32 arrayKey = getKey(_id, _key);
uint256 arrayLen = self.uintData[arrayKey];
uint256 _dataLen = _data.length;
for (uint256 i; i < _dataLen; ) {
self.addressData[getKey(arrayLen++, arrayKey)] = _data[i];
unchecked {
i += 1;
}
}
self.uintData[arrayKey] = arrayLen;
}
}