forked from Consensys/permissioning-smart-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeRulesList.sol
59 lines (46 loc) · 2.01 KB
/
NodeRulesList.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
pragma solidity 0.5.9;
contract NodeRulesList {
// struct size = 82 bytes
struct enode {
bytes32 enodeHigh;
bytes32 enodeLow;
bytes16 ip;
uint16 port;
}
enode[] public whitelist;
mapping (uint256 => uint256) private indexOf; //1-based indexing. 0 means non-existent
function calculateKey(bytes32 _enodeHigh, bytes32 _enodeLow, bytes16 _ip, uint16 _port) internal pure returns(uint256) {
return uint256(keccak256(abi.encodePacked(_enodeHigh, _enodeLow, _ip, _port)));
}
function size() internal view returns (uint256) {
return whitelist.length;
}
function exists(bytes32 _enodeHigh, bytes32 _enodeLow, bytes16 _ip, uint16 _port) internal view returns (bool) {
return indexOf[calculateKey(_enodeHigh, _enodeLow, _ip, _port)] != 0;
}
function add(bytes32 _enodeHigh, bytes32 _enodeLow, bytes16 _ip, uint16 _port) internal returns (bool) {
uint256 key = calculateKey(_enodeHigh, _enodeLow, _ip, _port);
if (indexOf[key] == 0) {
indexOf[key] = whitelist.push(enode(_enodeHigh, _enodeLow, _ip, _port));
return true;
}
return false;
}
function remove(bytes32 _enodeHigh, bytes32 _enodeLow, bytes16 _ip, uint16 _port) internal returns (bool) {
uint256 key = calculateKey(_enodeHigh, _enodeLow, _ip, _port);
uint256 index = indexOf[key];
if (index > 0 && index <= whitelist.length) { //1 based indexing
//move last item into index being vacated (unless we are dealing with last index)
if (index != whitelist.length) {
enode memory lastEnode = whitelist[whitelist.length - 1];
whitelist[index - 1] = lastEnode;
indexOf[calculateKey(lastEnode.enodeHigh, lastEnode.enodeLow, lastEnode.ip, lastEnode.port)] = index;
}
//shrink whitelist array
whitelist.length -= 1;
indexOf[key] = 0;
return true;
}
return false;
}
}