forked from Consensys/permissioning-smart-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PIE-1515: New Admin contract with linked list library (Consensys#35)
- Loading branch information
1 parent
c6948fb
commit 643f4bb
Showing
18 changed files
with
1,157 additions
and
857 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
pragma solidity >=0.4.22 <0.6.0; | ||
|
||
import "./AdminProxy.sol"; | ||
import "./AdminList.sol"; | ||
|
||
|
||
contract Admin is AdminProxy, AdminList { | ||
modifier onlyAdmin() { | ||
require(isAuthorized(msg.sender), "Sender not authorized"); | ||
_; | ||
} | ||
|
||
modifier notSelf(address _address) { | ||
require(msg.sender != _address, "Cannnot invoke method with own account as parameter"); | ||
_; | ||
} | ||
|
||
constructor() public { | ||
add(msg.sender); | ||
} | ||
|
||
function isAuthorized(address _address) public view returns (bool) { | ||
return exists(_address); | ||
} | ||
|
||
function addAdmin(address _address) public onlyAdmin notSelf(_address) returns (bool) { | ||
return add(_address); | ||
} | ||
|
||
function removeAdmin(address _address) public onlyAdmin notSelf(_address) returns (bool) { | ||
return remove(_address); | ||
} | ||
|
||
function getAdmins() public view returns (address[] memory){ | ||
return getAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
pragma solidity >=0.4.22 <0.6.0; | ||
|
||
import "solidity-linked-list/contracts/StructuredLinkedList.sol"; | ||
|
||
|
||
contract AdminList { | ||
using StructuredLinkedList for StructuredLinkedList.List; | ||
|
||
StructuredLinkedList.List private list; | ||
|
||
function size() internal view returns (uint256) { | ||
return list.sizeOf(); | ||
} | ||
|
||
function exists(address _address) internal view returns (bool) { | ||
return list.nodeExists(uint256(_address)); | ||
} | ||
|
||
function add(address _address) internal returns (bool) { | ||
return list.push(uint(_address), true); | ||
} | ||
|
||
function remove(address _address) internal returns (bool) { | ||
uint node = uint(_address); | ||
return list.remove(node) != 0 ? true : false; | ||
} | ||
|
||
function get(uint index) internal view returns (bool _exists, address _address) { | ||
uint listSize = list.sizeOf(); | ||
if (index >= listSize) { | ||
return (false, address(0)); | ||
} | ||
|
||
uint counter = 0; | ||
uint pointer = 0; | ||
bool hasFound = false; | ||
|
||
while(counter <= listSize) { | ||
(bool nodeExists, uint256 prev, uint256 next) = list.getNode(pointer); | ||
if (nodeExists) { | ||
if (counter == index + 1) { | ||
hasFound = true; | ||
break; | ||
} else { | ||
counter++; | ||
pointer = prev; | ||
} | ||
} else { | ||
break; | ||
} | ||
//Getting rid of unused variable warning | ||
next; | ||
} | ||
|
||
if (hasFound) { | ||
return (true, address(pointer)); | ||
} else { | ||
return (false, address(0)); | ||
} | ||
} | ||
|
||
function getAll() internal view returns (address[] memory) { | ||
uint listSize = list.sizeOf(); | ||
if (listSize == 0) { | ||
return new address[](0); | ||
} | ||
|
||
address[] memory allAddresses = new address[](listSize); | ||
|
||
bool hasNext = true; | ||
uint counter = 0; | ||
uint pointer = 0; | ||
|
||
while(hasNext) { | ||
(bool nodeExists, uint256 prev, uint256 next) = list.getNode(pointer); | ||
if (nodeExists) { | ||
if (pointer > 0) { | ||
allAddresses[counter++] = address(pointer); | ||
} | ||
|
||
if (prev != 0) { | ||
pointer = prev; | ||
} else { | ||
hasNext = false; | ||
} | ||
} | ||
//Getting rid of unused variable warning | ||
next; | ||
} | ||
|
||
return allAddresses; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
pragma solidity >=0.4.22 <0.6.0; | ||
|
||
import "./AdminList.sol"; | ||
|
||
|
||
// This class is used as a proxy to allow us to write unit tests. | ||
// All methods in the original class are internal. | ||
contract ExposedAdminList is AdminList { | ||
function _size() public view returns (uint256) { | ||
return size(); | ||
} | ||
|
||
function _exists(address _address) public view returns (bool) { | ||
return exists(_address); | ||
} | ||
|
||
function _add(address _address) public returns (bool) { | ||
return add(_address); | ||
} | ||
|
||
function _remove(address _address) public returns (bool) { | ||
return remove(_address); | ||
} | ||
|
||
function _get(uint _index) public view returns (bool __exists, address __address) { | ||
return get(_index); | ||
} | ||
|
||
function _getAll() public view returns (address[] memory) { | ||
return getAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.