Skip to content

Commit bb16e23

Browse files
authored
Feature/dynamic set (#100)
* Add DynamicSet library * Remove old StringSet * Add doc comments to the DynamicSet library * Small fixes
1 parent e554591 commit bb16e23

File tree

16 files changed

+909
-401
lines changed

16 files changed

+909
-401
lines changed

contracts/access/RBAC.sol

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {IRBAC} from "../interfaces/access/IRBAC.sol";
77

88
import {TypeCaster} from "../libs/utils/TypeCaster.sol";
99
import {SetHelper} from "../libs/arrays/SetHelper.sol";
10-
import {StringSet} from "../libs/data-structures/StringSet.sol";
10+
import {DynamicSet} from "../libs/data-structures/DynamicSet.sol";
1111

1212
/**
1313
* @notice The Role Based Access Control (RBAC) module
@@ -31,8 +31,8 @@ import {StringSet} from "../libs/data-structures/StringSet.sol";
3131
* Where ROLE is assignable to users
3232
*/
3333
abstract contract RBAC is IRBAC, Initializable {
34-
using StringSet for StringSet.Set;
35-
using SetHelper for StringSet.Set;
34+
using DynamicSet for DynamicSet.StringSet;
35+
using SetHelper for DynamicSet.StringSet;
3636
using TypeCaster for string;
3737

3838
string public constant MASTER_ROLE = "MASTER";
@@ -47,10 +47,11 @@ abstract contract RBAC is IRBAC, Initializable {
4747

4848
string public constant RBAC_RESOURCE = "RBAC_RESOURCE";
4949

50-
mapping(string => mapping(bool => mapping(string => StringSet.Set))) private _rolePermissions;
51-
mapping(string => mapping(bool => StringSet.Set)) private _roleResources;
50+
mapping(string => mapping(bool => mapping(string => DynamicSet.StringSet)))
51+
private _rolePermissions;
52+
mapping(string => mapping(bool => DynamicSet.StringSet)) private _roleResources;
5253

53-
mapping(address => StringSet.Set) private _userRoles;
54+
mapping(address => DynamicSet.StringSet) private _userRoles;
5455

5556
modifier onlyPermission(string memory resource_, string memory permission_) {
5657
require(
@@ -165,15 +166,15 @@ abstract contract RBAC is IRBAC, Initializable {
165166
ResourceWithPermissions[] memory disallowed_
166167
)
167168
{
168-
StringSet.Set storage _allowedResources = _roleResources[role_][true];
169-
StringSet.Set storage _disallowedResources = _roleResources[role_][false];
169+
DynamicSet.StringSet storage _allowedResources = _roleResources[role_][true];
170+
DynamicSet.StringSet storage _disallowedResources = _roleResources[role_][false];
170171

171-
mapping(string => StringSet.Set) storage _allowedPermissions = _rolePermissions[role_][
172-
true
173-
];
174-
mapping(string => StringSet.Set) storage _disallowedPermissions = _rolePermissions[role_][
175-
false
176-
];
172+
mapping(string => DynamicSet.StringSet) storage _allowedPermissions = _rolePermissions[
173+
role_
174+
][true];
175+
mapping(string => DynamicSet.StringSet) storage _disallowedPermissions = _rolePermissions[
176+
role_
177+
][false];
177178

178179
allowed_ = new ResourceWithPermissions[](_allowedResources.length());
179180
disallowed_ = new ResourceWithPermissions[](_disallowedResources.length());
@@ -253,8 +254,10 @@ abstract contract RBAC is IRBAC, Initializable {
253254
string[] memory permissionsToAdd_,
254255
bool allowed_
255256
) internal {
256-
StringSet.Set storage _resources = _roleResources[role_][allowed_];
257-
StringSet.Set storage _permissions = _rolePermissions[role_][allowed_][resourceToAdd_];
257+
DynamicSet.StringSet storage _resources = _roleResources[role_][allowed_];
258+
DynamicSet.StringSet storage _permissions = _rolePermissions[role_][allowed_][
259+
resourceToAdd_
260+
];
258261

259262
_permissions.add(permissionsToAdd_);
260263
_resources.add(resourceToAdd_);
@@ -275,8 +278,10 @@ abstract contract RBAC is IRBAC, Initializable {
275278
string[] memory permissionsToRemove_,
276279
bool allowed_
277280
) internal {
278-
StringSet.Set storage _resources = _roleResources[role_][allowed_];
279-
StringSet.Set storage _permissions = _rolePermissions[role_][allowed_][resourceToRemove_];
281+
DynamicSet.StringSet storage _resources = _roleResources[role_][allowed_];
282+
DynamicSet.StringSet storage _permissions = _rolePermissions[role_][allowed_][
283+
resourceToRemove_
284+
];
280285

281286
_permissions.remove(permissionsToRemove_);
282287

@@ -299,10 +304,10 @@ abstract contract RBAC is IRBAC, Initializable {
299304
string memory resource_,
300305
string memory permission_
301306
) internal view returns (bool) {
302-
mapping(string => StringSet.Set) storage _resources = _rolePermissions[role_][true];
307+
mapping(string => DynamicSet.StringSet) storage _resources = _rolePermissions[role_][true];
303308

304-
StringSet.Set storage _allAllowed = _resources[ALL_RESOURCE];
305-
StringSet.Set storage _allowed = _resources[resource_];
309+
DynamicSet.StringSet storage _allAllowed = _resources[ALL_RESOURCE];
310+
DynamicSet.StringSet storage _allowed = _resources[resource_];
306311

307312
return (_allAllowed.contains(ALL_PERMISSION) ||
308313
_allAllowed.contains(permission_) ||
@@ -322,10 +327,12 @@ abstract contract RBAC is IRBAC, Initializable {
322327
string memory resource_,
323328
string memory permission_
324329
) internal view returns (bool) {
325-
mapping(string => StringSet.Set) storage _resources = _rolePermissions[role_][false];
330+
mapping(string => DynamicSet.StringSet) storage _resources = _rolePermissions[role_][
331+
false
332+
];
326333

327-
StringSet.Set storage _allDisallowed = _resources[ALL_RESOURCE];
328-
StringSet.Set storage _disallowed = _resources[resource_];
334+
DynamicSet.StringSet storage _allDisallowed = _resources[ALL_RESOURCE];
335+
DynamicSet.StringSet storage _disallowed = _resources[resource_];
329336

330337
return (_allDisallowed.contains(ALL_PERMISSION) ||
331338
_allDisallowed.contains(permission_) ||

contracts/access/extensions/RBACGroupable.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pragma solidity ^0.8.4;
33

44
import {IRBACGroupable} from "../../interfaces/access/extensions/IRBACGroupable.sol";
55

6-
import {StringSet} from "../../libs/data-structures/StringSet.sol";
6+
import {DynamicSet} from "../../libs/data-structures/DynamicSet.sol";
77
import {SetHelper} from "../../libs/arrays/SetHelper.sol";
88

99
import {RBAC} from "../RBAC.sol";
@@ -23,13 +23,13 @@ import {RBAC} from "../RBAC.sol";
2323
* Where ROLE and GROUP are assignable to users
2424
*/
2525
abstract contract RBACGroupable is IRBACGroupable, RBAC {
26-
using StringSet for StringSet.Set;
27-
using SetHelper for StringSet.Set;
26+
using DynamicSet for DynamicSet.StringSet;
27+
using SetHelper for DynamicSet.StringSet;
2828

2929
uint256 private _defaultGroupEnabled;
3030

31-
mapping(address => StringSet.Set) private _userGroups;
32-
mapping(string => StringSet.Set) private _groupRoles;
31+
mapping(address => DynamicSet.StringSet) private _userGroups;
32+
mapping(string => DynamicSet.StringSet) private _groupRoles;
3333

3434
/**
3535
* @notice The initialization function
@@ -115,7 +115,7 @@ abstract contract RBACGroupable is IRBACGroupable, RBAC {
115115
* @return groups_ the list of user groups
116116
*/
117117
function getUserGroups(address who_) public view override returns (string[] memory groups_) {
118-
StringSet.Set storage userGroups = _userGroups[who_];
118+
DynamicSet.StringSet storage userGroups = _userGroups[who_];
119119

120120
uint256 userGroupsLength_ = userGroups.length();
121121

contracts/interfaces/access/IRBAC.sol

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.4;
33

4-
import {StringSet} from "../../libs/data-structures/StringSet.sol";
5-
64
/**
75
* @notice The RBAC module
86
*/

contracts/libs/arrays/Paginator.sol

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ pragma solidity ^0.8.4;
33

44
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
55

6-
import {StringSet} from "../data-structures/StringSet.sol";
6+
import {DynamicSet} from "../data-structures/DynamicSet.sol";
77

88
/**
99
* @notice Library for pagination.
1010
*
11-
* Supports the following data types `uin256[]`, `address[]`, `bytes32[]`, `UintSet`,
12-
* `AddressSet`, `BytesSet`, `StringSet`.
11+
* Supports the following data types `uint256[]`, `address[]`, `bytes32[]`, `UintSet`,
12+
* `AddressSet`, `Bytes32Set`, `BytesSet`, `StringSet`.
1313
*/
1414
library Paginator {
1515
using EnumerableSet for *;
16-
using StringSet for StringSet.Set;
16+
using DynamicSet for *;
1717

1818
/**
1919
* @notice Returns part of a uint256 array
@@ -129,11 +129,28 @@ library Paginator {
129129
}
130130
}
131131

132+
/**
133+
* @notice Returns part of a bytes set
134+
*/
135+
function part(
136+
DynamicSet.BytesSet storage set,
137+
uint256 offset_,
138+
uint256 limit_
139+
) internal view returns (bytes[] memory list_) {
140+
uint256 to_ = getTo(set.length(), offset_, limit_);
141+
142+
list_ = new bytes[](to_ - offset_);
143+
144+
for (uint256 i = offset_; i < to_; i++) {
145+
list_[i - offset_] = set.at(i);
146+
}
147+
}
148+
132149
/**
133150
* @notice Returns part of a string set
134151
*/
135152
function part(
136-
StringSet.Set storage set,
153+
DynamicSet.StringSet storage set,
137154
uint256 offset_,
138155
uint256 limit_
139156
) internal view returns (string[] memory list_) {

contracts/libs/arrays/SetHelper.sol

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pragma solidity ^0.8.4;
33

44
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
55

6-
import {StringSet} from "../data-structures/StringSet.sol";
6+
import {DynamicSet} from "../data-structures/DynamicSet.sol";
77

88
/**
99
* @notice A simple library to work with Openzeppelin sets
@@ -12,7 +12,7 @@ library SetHelper {
1212
using EnumerableSet for EnumerableSet.UintSet;
1313
using EnumerableSet for EnumerableSet.AddressSet;
1414
using EnumerableSet for EnumerableSet.Bytes32Set;
15-
using StringSet for StringSet.Set;
15+
using DynamicSet for *;
1616

1717
/**
1818
* @notice The function to insert an array of elements into the address set
@@ -43,10 +43,19 @@ library SetHelper {
4343
}
4444
}
4545

46+
/**
47+
* @notice The function to insert an array of elements into the bytes set
48+
*/
49+
function add(DynamicSet.BytesSet storage set, bytes[] memory array_) internal {
50+
for (uint256 i = 0; i < array_.length; i++) {
51+
set.add(array_[i]);
52+
}
53+
}
54+
4655
/**
4756
* @notice The function to insert an array of elements into the string set
4857
*/
49-
function add(StringSet.Set storage set, string[] memory array_) internal {
58+
function add(DynamicSet.StringSet storage set, string[] memory array_) internal {
5059
for (uint256 i = 0; i < array_.length; i++) {
5160
set.add(array_[i]);
5261
}
@@ -81,10 +90,19 @@ library SetHelper {
8190
}
8291
}
8392

93+
/**
94+
* @notice The function for the strict insertion of an array of elements into the bytes set
95+
*/
96+
function strictAdd(DynamicSet.BytesSet storage set, bytes[] memory array_) internal {
97+
for (uint256 i = 0; i < array_.length; i++) {
98+
require(set.add(array_[i]), "SetHelper: element already exists");
99+
}
100+
}
101+
84102
/**
85103
* @notice The function for the strict insertion of an array of elements into the string set
86104
*/
87-
function strictAdd(StringSet.Set storage set, string[] memory array_) internal {
105+
function strictAdd(DynamicSet.StringSet storage set, string[] memory array_) internal {
88106
for (uint256 i = 0; i < array_.length; i++) {
89107
require(set.add(array_[i]), "SetHelper: element already exists");
90108
}
@@ -119,10 +137,19 @@ library SetHelper {
119137
}
120138
}
121139

140+
/**
141+
* @notice The function to remove an array of elements from the bytes set
142+
*/
143+
function remove(DynamicSet.BytesSet storage set, bytes[] memory array_) internal {
144+
for (uint256 i = 0; i < array_.length; i++) {
145+
set.remove(array_[i]);
146+
}
147+
}
148+
122149
/**
123150
* @notice The function to remove an array of elements from the string set
124151
*/
125-
function remove(StringSet.Set storage set, string[] memory array_) internal {
152+
function remove(DynamicSet.StringSet storage set, string[] memory array_) internal {
126153
for (uint256 i = 0; i < array_.length; i++) {
127154
set.remove(array_[i]);
128155
}
@@ -157,10 +184,19 @@ library SetHelper {
157184
}
158185
}
159186

187+
/**
188+
* @notice The function for the strict removal of an array of elements from the bytes set
189+
*/
190+
function strictRemove(DynamicSet.BytesSet storage set, bytes[] memory array_) internal {
191+
for (uint256 i = 0; i < array_.length; i++) {
192+
require(set.remove(array_[i]), "SetHelper: no such element");
193+
}
194+
}
195+
160196
/**
161197
* @notice The function for the strict removal of an array of elements from the string set
162198
*/
163-
function strictRemove(StringSet.Set storage set, string[] memory array_) internal {
199+
function strictRemove(DynamicSet.StringSet storage set, string[] memory array_) internal {
164200
for (uint256 i = 0; i < array_.length; i++) {
165201
require(set.remove(array_[i]), "SetHelper: no such element");
166202
}

0 commit comments

Comments
 (0)