Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
118 commits
Select commit Hold shift + click to select a range
262be5f
move data structures to data directory
ItsNickBarry Jul 25, 2022
7062487
moves bn conversion functions to solidstate lib
0xCourtney Sep 5, 2022
f06fc7d
binary heap logic
0xCourtney Sep 5, 2022
ad31789
binary heap mocks and tests
0xCourtney Sep 5, 2022
f7c22b3
incremental merkle tree library
NouDaimon Sep 14, 2022
ed2c883
removes /binaryheap folder, moves contents to /data
0xCourtney Sep 15, 2022
b3511a8
Merge branch 'master' into data-structures
ItsNickBarry Sep 15, 2022
976c14e
add alternative IncrementalMerkleTree implementation
ItsNickBarry Sep 16, 2022
91b445e
Merge branch 'data-structures' into IMT
ItsNickBarry Sep 16, 2022
fbd9731
bytes32 tests
0xCourtney Sep 18, 2022
e4cc31d
delete original and rename new IMT contract
NouDaimon Sep 20, 2022
3389849
fix imports
NouDaimon Sep 20, 2022
ec3c875
use explicity naming for variables
0xCourtney Sep 20, 2022
7227bff
adds title and description
0xCourtney Sep 20, 2022
b00c7fc
Update contracts/data/BinaryHeap.sol
Sep 20, 2022
cc2048a
--i, save some gas
0xCourtney Sep 22, 2022
42f17ae
adds unchecked blocks
0xCourtney Sep 22, 2022
6ee774a
typo
0xCourtney Sep 22, 2022
ca1de73
adds toArray to BinaryHeap
0xCourtney Sep 22, 2022
e1f9a35
explicit naming
0xCourtney Sep 22, 2022
814ea35
cleans up tests
0xCourtney Sep 22, 2022
501ae26
remove console import
ItsNickBarry Sep 25, 2022
a1968ea
overwrite original IMT implementation
ItsNickBarry Sep 25, 2022
bea6a67
clean up files
NouDaimon Sep 25, 2022
3648691
fix test name
NouDaimon Sep 25, 2022
3c9983f
Merge branch 'IMT' of github.com:solidstate-network/solidstate-solidi…
ItsNickBarry Sep 25, 2022
b6633f0
revert yarn.lock update
ItsNickBarry Sep 25, 2022
0908c1e
prevent revert in root function
ItsNickBarry Sep 25, 2022
43861a4
test size and height functions
ItsNickBarry Sep 25, 2022
141f53b
test root function
ItsNickBarry Sep 25, 2022
a8adbf4
add push and set tests, update root tests
ItsNickBarry Sep 25, 2022
8d1c373
improve test variable names
ItsNickBarry Sep 25, 2022
b2c9faa
optimize _set function
ItsNickBarry Sep 26, 2022
540883b
fix typo in _set
NouDaimon Sep 26, 2022
2679172
test suggestions for library alignment
NouDaimon Sep 26, 2022
0d02343
parent >= child
0xCourtney Sep 27, 2022
76247ee
checks if root is removed
0xCourtney Sep 27, 2022
d107a57
fixes tests
0xCourtney Sep 27, 2022
05ee407
checks node is removed from heap
0xCourtney Sep 27, 2022
2e4f785
more efficient hashing
NouDaimon Sep 27, 2022
75d0353
inline efficient hash function
ItsNickBarry Sep 27, 2022
f479ba9
use assembly for array reads
ItsNickBarry Sep 27, 2022
9448ac2
Merge branch 'master' into data-structures
ItsNickBarry Sep 29, 2022
bd82f79
fix EnumerableSet imports
ItsNickBarry Sep 29, 2022
c18a044
Merge branch 'data-structures' into IMT
ItsNickBarry Sep 29, 2022
f1f895e
remove ternary flow control from size function
ItsNickBarry Sep 29, 2022
415acf2
remove ternary flow control from root function
ItsNickBarry Sep 29, 2022
47f2d74
add NatSpec comments to IncrementalMerkleTree
ItsNickBarry Sep 29, 2022
01c7a87
add missing param comment
ItsNickBarry Sep 29, 2022
2f4fde3
adds unchecked blocks, comment and rename
0xCourtney Sep 29, 2022
df978f2
optimize swap
0xCourtney Sep 29, 2022
b9e9dc1
optimize maxHeapify
0xCourtney Sep 29, 2022
5182760
more gas optimization
0xCourtney Sep 29, 2022
fcf9e6c
Merge branch 'binary-heap' into EnumerableSetTests
0xCourtney Oct 4, 2022
099d0bc
converts set to array
0xCourtney Oct 4, 2022
1ae2588
EnumerableSet tests
0xCourtney Oct 4, 2022
7a09e0e
Merge branch 'master' into data-structures
ItsNickBarry Oct 5, 2022
3e6626b
Merge branch 'data-structures' into binary-heap
ItsNickBarry Oct 5, 2022
0b39421
Merge branch 'data-structures' into IMT
ItsNickBarry Oct 5, 2022
f538417
Merge branch 'binary-heap' into EnumerableSetTests
ItsNickBarry Oct 6, 2022
fbbb124
fix heap => set typo
NouDaimon Oct 6, 2022
89f5253
uintSet mock contract
NouDaimon Oct 6, 2022
45b093b
uintSet tests
NouDaimon Oct 6, 2022
0ba41af
Merge branch 'master' into data-structures
ItsNickBarry Oct 7, 2022
4b26b58
Merge branch 'data-structures' into IMT
ItsNickBarry Oct 7, 2022
8f89c04
accept pre-hashed Merkle tree inputs
ItsNickBarry Oct 7, 2022
d6dd9f1
use ethers helper for random bytes generation
ItsNickBarry Oct 7, 2022
2dfe636
replace random value generation with direct random hash generation
ItsNickBarry Oct 7, 2022
dd1eb3d
add random hash helper
ItsNickBarry Oct 7, 2022
ed7b158
typo
0xCourtney Oct 7, 2022
2407980
custom error
0xCourtney Oct 7, 2022
c5657dc
adds address mock and tests
0xCourtney Oct 7, 2022
ffb9354
splits test
0xCourtney Oct 7, 2022
17e6266
splits test case
0xCourtney Oct 7, 2022
d15d154
typo
0xCourtney Oct 7, 2022
afb3311
fix _at(AddressToAddressMap storage map, uint256 index)
NouDaimon Oct 20, 2022
4057b14
EnumerableMapAddressToAddressMapMock contract, EnumerableMapAddressTo…
NouDaimon Oct 20, 2022
c322b5a
remove POC test
ItsNickBarry Oct 23, 2022
65f824d
prevent variable shadowing and unassigned variable warnings
ItsNickBarry Oct 23, 2022
3bb20aa
add IncrementalMerkleTree at function and revert tests
ItsNickBarry Oct 23, 2022
1daeaa4
add IncrementalMerkleTree#pop function
ItsNickBarry Oct 23, 2022
cba6980
remove unnecessary pop, test against state corruption
ItsNickBarry Oct 23, 2022
69dff63
Update contracts/data/BinaryHeap.sol
ItsNickBarry Oct 23, 2022
ea4d1b0
remove assembly block #at(AddressToAddressMap,uint256)
NouDaimon Oct 23, 2022
01e18ae
EnumerableMapUintToAddress mock contract
NouDaimon Oct 23, 2022
cd33efd
EnumerableMapUintToAddress tests
NouDaimon Oct 23, 2022
fe3d5c2
add missing #set(address,address) test
NouDaimon Oct 23, 2022
635b48a
Merge pull request #136 from solidstate-network/IMT
ItsNickBarry Oct 24, 2022
f83e398
cleaning code
0xCourtney Oct 28, 2022
615d0f9
use callStatic for view/pure calls, shorten tests
NouDaimon Oct 31, 2022
f674279
expand set tests
NouDaimon Oct 31, 2022
d975595
toArray function
NouDaimon Oct 31, 2022
cc51e31
toArray function tests
NouDaimon Oct 31, 2022
8d8b457
keys & values functions
NouDaimon Oct 31, 2022
4e1edc1
keys & values functions tests
NouDaimon Oct 31, 2022
65785f5
Merge branch 'master' into data-structures
ItsNickBarry Nov 1, 2022
c93133c
Merge branch 'data-structures' into binary-heap
ItsNickBarry Nov 1, 2022
15d1420
optimize arithmetic in _maxHeapify function
ItsNickBarry Nov 1, 2022
dc2e9ad
clean up _heapify function
ItsNickBarry Nov 1, 2022
fa3a94c
decrement inline
ItsNickBarry Nov 1, 2022
32b7b79
remove explicit returns
ItsNickBarry Nov 1, 2022
b3c5c15
remove whitespace
ItsNickBarry Nov 1, 2022
c692368
fallback to default panic
0xCourtney Nov 2, 2022
ace57a8
specify BinaryHeap panic code in tests
ItsNickBarry Nov 2, 2022
dc70e6f
define test constants programatically
ItsNickBarry Nov 2, 2022
39fcfba
test length of toArray output
ItsNickBarry Nov 2, 2022
42b5d7e
clean up return value tests
ItsNickBarry Nov 2, 2022
6f416a9
use loops for indexOf tests
ItsNickBarry Nov 2, 2022
1b10f3e
use random values for some add and remove tests
ItsNickBarry Nov 2, 2022
342e94f
fix test suite names
ItsNickBarry Nov 2, 2022
e4075ee
use loops for at tests
ItsNickBarry Nov 2, 2022
d07f316
remove unused deployer references
ItsNickBarry Nov 2, 2022
60c0f53
use predefined constants sparingly
ItsNickBarry Nov 2, 2022
46fb72e
initialize constants randomly
ItsNickBarry Nov 2, 2022
b8657a9
Revert "initialize constants randomly"
ItsNickBarry Nov 2, 2022
2a986a9
Merge pull request #143 from solidstate-network/binary-heap
ItsNickBarry Nov 2, 2022
f46df77
Merge pull request #144 from solidstate-network/EnumerableSetTests
ItsNickBarry Nov 2, 2022
f0355fb
Merge pull request #151 from solidstate-network/EnumerableMapTests
ItsNickBarry Nov 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/access/access_control/AccessControlInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

pragma solidity ^0.8.0;

import { EnumerableSet } from '../../data/EnumerableSet.sol';
import { AddressUtils } from '../../utils/AddressUtils.sol';
import { EnumerableSet } from '../../utils/EnumerableSet.sol';
import { UintUtils } from '../../utils/UintUtils.sol';
import { IAccessControlInternal } from './IAccessControlInternal.sol';
import { AccessControlStorage } from './AccessControlStorage.sol';
Expand Down
2 changes: 1 addition & 1 deletion contracts/access/access_control/AccessControlStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.0;

import { EnumerableSet } from '../../utils/EnumerableSet.sol';
import { EnumerableSet } from '../../data/EnumerableSet.sol';

library AccessControlStorage {
struct RoleData {
Expand Down
335 changes: 335 additions & 0 deletions contracts/data/BinaryHeap.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,335 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.8;

/**
* @title Binary Heap implementation
* @dev The data structure is configured as a max-heap
*/
library BinaryHeap {
struct Heap {
bytes32[] _values;
// 1-indexed to allow 0 to signify nonexistence
mapping(bytes32 => uint256) _indexes;
}

struct Bytes32Heap {
Heap _inner;
}

struct AddressHeap {
Heap _inner;
}

struct UintHeap {
Heap _inner;
}

function at(Bytes32Heap storage heap, uint256 index)
internal
view
returns (bytes32)
{
return _at(heap._inner, index);
}

function at(AddressHeap storage heap, uint256 index)
internal
view
returns (address)
{
return address(uint160(uint256(_at(heap._inner, index))));
}

function at(UintHeap storage heap, uint256 index)
internal
view
returns (uint256)
{
return uint256(_at(heap._inner, index));
}

function contains(Bytes32Heap storage heap, bytes32 value)
internal
view
returns (bool)
{
return _contains(heap._inner, value);
}

function contains(AddressHeap storage heap, address value)
internal
view
returns (bool)
{
return _contains(heap._inner, bytes32(uint256(uint160(value))));
}

function contains(UintHeap storage heap, uint256 value)
internal
view
returns (bool)
{
return _contains(heap._inner, bytes32(value));
}

function indexOf(Bytes32Heap storage heap, bytes32 value)
internal
view
returns (uint256)
{
return _indexOf(heap._inner, value);
}

function indexOf(AddressHeap storage heap, address value)
internal
view
returns (uint256)
{
return _indexOf(heap._inner, bytes32(uint256(uint160(value))));
}

function indexOf(UintHeap storage heap, uint256 value)
internal
view
returns (uint256)
{
return _indexOf(heap._inner, bytes32(value));
}

function length(Bytes32Heap storage heap) internal view returns (uint256) {
return _length(heap._inner);
}

function length(AddressHeap storage heap) internal view returns (uint256) {
return _length(heap._inner);
}

function length(UintHeap storage heap) internal view returns (uint256) {
return _length(heap._inner);
}

function root(Bytes32Heap storage heap) internal view returns (bytes32) {
return _root(heap._inner);
}

function root(AddressHeap storage heap) internal view returns (address) {
return address(uint160(uint256(_root(heap._inner))));
}

function root(UintHeap storage heap) internal view returns (uint256) {
return uint256(_root(heap._inner));
}

function add(Bytes32Heap storage heap, bytes32 value)
internal
returns (bool)
{
return _add(heap._inner, value);
}

function add(AddressHeap storage heap, address value)
internal
returns (bool)
{
return _add(heap._inner, bytes32(uint256(uint160(value))));
}

function add(UintHeap storage heap, uint256 value) internal returns (bool) {
return _add(heap._inner, bytes32(value));
}

function remove(Bytes32Heap storage heap, bytes32 value)
internal
returns (bool)
{
return _remove(heap._inner, value);
}

function remove(AddressHeap storage heap, address value)
internal
returns (bool)
{
return _remove(heap._inner, bytes32(uint256(uint160(value))));
}

function remove(UintHeap storage heap, uint256 value)
internal
returns (bool)
{
return _remove(heap._inner, bytes32(value));
}

function toArray(Bytes32Heap storage heap)
internal
view
returns (bytes32[] memory)
{
uint256 len = _length(heap._inner);
bytes32[] memory arr = new bytes32[](len);

unchecked {
for (uint256 index = 0; index < len; index++) {
arr[index] = at(heap, index);
}
}

return arr;
}

function toArray(AddressHeap storage heap)
internal
view
returns (address[] memory)
{
uint256 len = _length(heap._inner);
address[] memory arr = new address[](len);

unchecked {
for (uint256 index = 0; index < len; index++) {
arr[index] = at(heap, index);
}
}

return arr;
}

function toArray(UintHeap storage heap)
internal
view
returns (uint256[] memory)
{
uint256 len = _length(heap._inner);
uint256[] memory arr = new uint256[](len);

unchecked {
for (uint256 index = 0; index < len; index++) {
arr[index] = at(heap, index);
}
}

return arr;
}

function _at(Heap storage heap, uint256 index)
private
view
returns (bytes32)
{
return heap._values[index];
}

function _contains(Heap storage heap, bytes32 value)
private
view
returns (bool)
{
return heap._indexes[value] != 0;
}

function _indexOf(Heap storage heap, bytes32 value)
private
view
returns (uint256)
{
unchecked {
return heap._indexes[value] - 1;
}
}

function _length(Heap storage heap) private view returns (uint256) {
return heap._values.length;
}

function _root(Heap storage heap) private view returns (bytes32) {
return _at(heap, 0);
}

function _add(Heap storage heap, bytes32 value)
private
returns (bool update)
{
if (!_contains(heap, value)) {
heap._values.push(value);
heap._indexes[value] = _length(heap);
_heapify(heap);

update = true;
}
}

function _remove(Heap storage heap, bytes32 value)
private
returns (bool update)
{
if (_contains(heap, value)) {
uint256 index = _indexOf(heap, value);

unchecked {
// move node with last element in the tree, then remove it
_swap(heap, index, _length(heap) - 1);
}

heap._values.pop();
delete heap._indexes[value];

_heapify(heap);

update = true;
}
}

function _heapify(Heap storage heap) private {
uint256 len = _length(heap);
unchecked {
uint256 index = len / 2;
while (index > 0) {
_maxHeapify(heap, len, --index);
}
}
}

function _maxHeapify(
Heap storage heap,
uint256 len,
uint256 index
) private {
uint256 largest = index;
bytes32[] storage values = heap._values;

unchecked {
uint256 left = (index << 1) | 1;
uint256 right = left + 1;

if (left < len && values[largest] < values[left]) {
largest = left;
}

if (right < len && values[largest] < values[right]) {
largest = right;
}
}

if (largest != index) {
// swap until the largest node is the root node
_swap(heap, index, largest);
_maxHeapify(heap, len, largest);
}
}

function _swap(
Heap storage heap,
uint256 a,
uint256 b
) private {
bytes32[] storage values = heap._values;

bytes32 aValue = values[a];
bytes32 bValue = values[b];

(values[a], values[b]) = (bValue, aValue);

mapping(bytes32 => uint256) storage indexes = heap._indexes;
(indexes[aValue], indexes[bValue]) = (indexes[bValue], indexes[aValue]);
}
}
43 changes: 43 additions & 0 deletions contracts/data/BinaryHeapAddressMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.8;

import { BinaryHeap } from './BinaryHeap.sol';

contract BinaryHeapAddressMock {
using BinaryHeap for BinaryHeap.AddressHeap;

BinaryHeap.AddressHeap internal addressHeap;

function at(uint256 index) external view returns (address) {
return addressHeap.at(index);
}

function contains(address value) external view returns (bool) {
return addressHeap.contains(value);
}

function indexOf(address value) external view returns (uint256) {
return addressHeap.indexOf(value);
}

function length() external view returns (uint256) {
return addressHeap.length();
}

function root() external view returns (address) {
return addressHeap.root();
}

function add(address value) external returns (bool) {
return addressHeap.add(value);
}

function remove(address value) external returns (bool) {
return addressHeap.remove(value);
}

function toArray() external view returns (address[] memory) {
return addressHeap.toArray();
}
}
Loading