/* | |
• Zeppelin Audit | |
• Draft for a proof of concept code on how to make the revoke function of the BeamBalanceStore | |
• linear on the size of the policy array. | |
• DO NOT USE IN PRODUCTION | |
*/ | |
function linearRevoke(string memory _policyIdentifier) public returns (bool _revoked) { | |
bytes32 _hash = keccak256(abi.encodePacked(_policyIdentifier)); | |
uint256 i = 0; | |
uint256 timesFound = 0; | |
/* | |
Note that the loop will end the travelsal one element before the last one. | |
This is to avoid accessing an element out of bound (see the assigment below) | |
*/ | |
while (i+timesFound < policies.length-1) { | |
if (policies[i] == _hash) { | |
// solium-enable zeppelin/no-arithmetic-operations | |
timesFound++; | |
} | |
else { | |
i++; | |
} | |
// solium-enable zeppelin/no-arithmetic-operations | |
/* | |
Here is were the removal of the elements and shifting of array elements occurs. | |
Note that if timesFound==0 this statement does nothing but when timesFound>0 the element found | |
will be overwritten with a subsequent element in the array. | |
*/ | |
policies[i] = policies[i+timesFound]; | |
} | |
// Check whether the last element is also an ocurrence | |
if (policies[i] == _hash) { | |
// solium-enable zeppelin/no-arithmetic-operations | |
timesFound++; | |
} | |
// Reduces the size of the array according to the number of elements found | |
policies.length-=timesFound; | |
if (timesFound>0) { | |
// If a policy if found, call reauthorize to rebuild the authorization mapping | |
reAuthorize(); | |
// Inform that the policy was found | |
_revoked = true; | |
emit Revoked(msg.sender, _policyIdentifier); | |
} | |
else { | |
// If not inform that the policy was not found | |
_revoked = false; | |
emit NotRevoked(msg.sender, _policyIdentifier); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment