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

Commit

Permalink
[OP-92] - Updating ingress contracts to simplify registry mapping (#155)
Browse files Browse the repository at this point in the history
Signed-off-by: Usman Saleem <usman@usmans.info>
  • Loading branch information
usmansaleem authored and lucassaldanha committed Sep 27, 2019
1 parent 85790bf commit f973035
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 38 deletions.
2 changes: 1 addition & 1 deletion contracts/AccountIngress.sol
Expand Up @@ -24,7 +24,7 @@ contract AccountIngress is Ingress {
return true;
}

return AccountRulesProxy(registry[indexOf[RULES_CONTRACT]-1]).transactionAllowed(
return AccountRulesProxy(registry[RULES_CONTRACT]).transactionAllowed(
sender, target, value, gasPrice, gasLimit, payload
);
}
Expand Down
51 changes: 18 additions & 33 deletions contracts/Ingress.sol
Expand Up @@ -9,7 +9,8 @@ contract Ingress {
bytes32 public ADMIN_CONTRACT = 0x61646d696e697374726174696f6e000000000000000000000000000000000000; // "administration"

// Registry mapping indexing
address[] internal registry;
mapping(bytes32 => address) internal registry;

bytes32[] internal contractKeys;
mapping (bytes32 => uint256) internal indexOf; //1 based indexing. 0 means non-existent

Expand All @@ -20,23 +21,18 @@ contract Ingress {

function getContractAddress(bytes32 name) public view returns(address) {
require(name > 0, "Contract name must not be empty.");
uint256 i = indexOf[name];
if (i > 0 && i <= registry.length) {
return (registry[i-1]);
} else {
return address(0);
}
return registry[name];
}

function getSize() public view returns (uint256) {
return registry.length;
return contractKeys.length;
}

function isAuthorized(address account) public view returns(bool) {
if(getContractAddress(ADMIN_CONTRACT) == address(0)) {
if (registry[ADMIN_CONTRACT] == address(0)) {
return true;
} else {
return AdminProxy(registry[indexOf[ADMIN_CONTRACT]-1]).isAuthorized(account);
return AdminProxy(registry[ADMIN_CONTRACT]).isAuthorized(account);
}
}

Expand All @@ -45,46 +41,35 @@ contract Ingress {
require(addr != address(0), "Contract address must not be zero.");
require(isAuthorized(msg.sender), "Not authorized to update contract registry.");

address info = address(0);
uint256 i = indexOf[name];
if (i > 0 && i <= registry.length) {
info = registry[i-1];
}
// create info if it doesn't exist in the registry
if (info == address(0)) {
// Update registry indexing
indexOf[name] = registry.push(addr);
contractKeys.push(name);
} else {
// update record in the registry
registry[indexOf[name]-1] = addr;
if (indexOf[name] == 0) {
indexOf[name] = contractKeys.push(name);
}

emit RegistryUpdated(addr,name);
registry[name] = addr;

emit RegistryUpdated(addr, name);

return true;
}

function removeContract(bytes32 _name) public returns(bool) {
require(_name > 0, "Contract name must not be empty.");
require(registry.length > 0, "Must have at least one registered contract to execute delete operation.");
require(contractKeys.length > 0, "Must have at least one registered contract to execute delete operation.");
require(isAuthorized(msg.sender), "Not authorized to update contract registry.");

uint256 index = indexOf[_name];
if (index > 0 && index <= registry.length) { //1-based indexing
if (index > 0 && index <= contractKeys.length) { //1-based indexing
//move last address into index being vacated (unless we are dealing with last index)
if (index != registry.length) {
address lastAccount = registry[registry.length - 1];
bytes32 lastKey = contractKeys[registry.length - 1];
registry[index - 1] = lastAccount;
if (index != contractKeys.length) {
bytes32 lastKey = contractKeys[contractKeys.length - 1];
contractKeys[index - 1] = lastKey;
indexOf[lastKey] = index;
}

//shrink registry and keys arrays
registry.length -= 1;
contractKeys.length -= 1;
//shrink contract keys array
contractKeys.pop();
indexOf[_name] = 0;
registry[_name] = address(0);
emit RegistryUpdated(address(0), _name);
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions contracts/NodeIngress.sol
Expand Up @@ -17,7 +17,7 @@ contract NodeIngress is Ingress {
}

function emitRulesChangeEvent(bool addsRestrictions) public {
require(registry[indexOf[RULES_CONTRACT]-1] == msg.sender, "Only Rules contract can trigger Rules change events");
require(registry[RULES_CONTRACT] == msg.sender, "Only Rules contract can trigger Rules change events");
emit NodePermissionsUpdated(addsRestrictions);
}

Expand All @@ -32,10 +32,11 @@ contract NodeIngress is Ingress {
uint16 destinationEnodePort
) public view returns (bytes32) {
if(getContractAddress(RULES_CONTRACT) == address(0)) {
//reject connection
return 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
}

return NodeRulesProxy(registry[indexOf[RULES_CONTRACT]-1]).connectionAllowed(
return NodeRulesProxy(registry[RULES_CONTRACT]).connectionAllowed(
sourceEnodeHigh,
sourceEnodeLow,
sourceEnodeIp,
Expand Down

0 comments on commit f973035

Please sign in to comment.