Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Feature No host validation (#251)
Browse files Browse the repository at this point in the history
* Fix NodeStorage Access Control

* Added No Host Validation

* Renamed IP to Hosts

* typo to kick off build

* Fix Lint issues

* Host Validation only with enodeId & Rename function

* Flipped the name of variables to clarify the scenario

* Fix parameter name

Co-authored-by: Alberto Hernandez <[email protected]>
Co-authored-by: Sally MacFarlane <[email protected]>
Co-authored-by: Antony Denyer <[email protected]>
  • Loading branch information
4 people committed Oct 24, 2021
1 parent 15cf31b commit 5f1c076
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
4 changes: 4 additions & 0 deletions contracts/NodeRules.sol
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,8 @@ contract NodeRules is NodeRulesProxy, NodeRulesList {
function triggerRulesChangeEvent(bool addsRestrictions) public {
nodeIngressContract.emitRulesChangeEvent(addsRestrictions);
}

function activateValidationEnodeIdOnly(bool _onlyUseEnodeId) external onlyAdmin onlyOnEditMode returns (bool) {
return setValidateEnodeIdOnly(_onlyUseEnodeId);
}
}
4 changes: 4 additions & 0 deletions contracts/NodeRulesList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ contract NodeRulesList {
function getByIndex(uint index) external view returns (string memory enodeId, string memory host, uint16 port) {
return nodeStorage.getByIndex(index);
}

function setValidateEnodeIdOnly(bool _onlyUseEnodeId) internal returns (bool) {
return nodeStorage.setValidateEnodeIdOnly(_onlyUseEnodeId);
}
}
46 changes: 37 additions & 9 deletions contracts/NodeStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ contract NodeStorage {
enode[] public allowlist;
mapping (uint256 => uint256) private indexOf; //1-based indexing. 0 means non-existent

bool private onlyUseEnodeId;

constructor (NodeIngress _ingressContract) public {
ingressContract = _ingressContract;
onlyUseEnodeId = false;
}

modifier onlyLatestVersion() {
Expand Down Expand Up @@ -57,21 +60,21 @@ contract NodeStorage {
return allowlist.length;
}

function exists(string memory _enodeId, string memory _ip, uint16 _port) public view returns (bool) {
return indexOf[calculateKey(_enodeId, _ip, _port)] != 0;
function exists(string memory _enodeId, string memory _host, uint16 _port) public view returns (bool) {
return indexOf[calculateKey(_enodeId, _host, _port)] != 0;
}

function add(string memory _enodeId, string memory _ip, uint16 _port) public onlyLatestVersion returns (bool) {
uint256 key = calculateKey(_enodeId, _ip, _port);
function add(string memory _enodeId, string memory _host, uint16 _port) public onlyLatestVersion returns (bool) {
uint256 key = calculateKey(_enodeId, _host, _port);
if (indexOf[key] == 0) {
indexOf[key] = allowlist.push(enode(_enodeId, _ip, _port));
indexOf[key] = allowlist.push(enode(_enodeId, _host, _port));
return true;
}
return false;
}

function remove(string memory _enodeId, string memory _ip, uint16 _port) public onlyLatestVersion returns (bool) {
uint256 key = calculateKey(_enodeId, _ip, _port);
function remove(string memory _enodeId, string memory _host, uint16 _port) public onlyLatestVersion returns (bool) {
uint256 key = calculateKey(_enodeId, _host, _port);
uint256 index = indexOf[key];

if (index > 0 && index <= allowlist.length) { //1 based indexing
Expand All @@ -98,7 +101,32 @@ contract NodeStorage {
}
}

function calculateKey(string memory _enodeId, string memory _ip, uint16 _port) public pure returns(uint256) {
return uint256(keccak256(abi.encodePacked(_enodeId, _ip, _port)));
function setValidateEnodeIdOnly (bool _onlyUseEnodeId) public onlyLatestVersion returns (bool) {
if (onlyUseEnodeId == _onlyUseEnodeId) {
return true;
}

// First we reset old map entries
enode memory entry;
for (uint256 index = 0; index < allowlist.length; index++) {
entry = allowlist[index];
indexOf[calculateKey(entry.enodeId, entry.ip, entry.port)] = 0;
}

onlyUseEnodeId = _onlyUseEnodeId;

for (uint256 index = 0; index < allowlist.length; index++) {
entry = allowlist[index];
indexOf[calculateKey(entry.enodeId, entry.ip, entry.port)] = index + 1;
}

return true;
}

function calculateKey(string memory _enodeId, string memory _host, uint16 _port) public view returns(uint256) {
if (!onlyUseEnodeId) {
return uint256(keccak256(abi.encodePacked(_enodeId, _host, _port)));
}
return uint256(keccak256(abi.encodePacked(_enodeId)));
}
}

0 comments on commit 5f1c076

Please sign in to comment.