Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AuthWeighted): improving gas usage #140

Merged
merged 11 commits into from
Mar 30, 2023
40 changes: 26 additions & 14 deletions contracts/auth/AxelarAuthWeighted.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ contract AxelarAuthWeighted is Ownable, IAxelarAuthWeighted {
mapping(uint256 => bytes32) public hashForEpoch;
mapping(bytes32 => uint256) public epochForHash;

uint8 internal constant OLD_KEY_RETENTION = 16;
// solhint-disable-next-line var-name-mixedcase
uint256 internal constant OLD_KEY_RETENTION = 16;

constructor(bytes[] memory recentOperators) {
for (uint256 i; i < recentOperators.length; ++i) {
uint256 length = recentOperators.length;

for (uint256 i; i < length; ++i) {
_transferOperatorship(recentOperators[i]);
}
}
Expand Down Expand Up @@ -67,15 +70,15 @@ contract AxelarAuthWeighted is Ownable, IAxelarAuthWeighted {

if (weightsLength != operatorsLength) revert InvalidWeights();

uint256 totalWeight = 0;
for (uint256 i = 0; i < weightsLength; ++i) {
totalWeight += newWeights[i];
uint256 totalWeight;
for (uint256 i; i < weightsLength; ++i) {
totalWeight = totalWeight + newWeights[i];
}
if (newThreshold == 0 || totalWeight < newThreshold) revert InvalidThreshold();

bytes32 newOperatorsHash = keccak256(params);

if (epochForHash[newOperatorsHash] > 0) revert DuplicateOperators();
if (epochForHash[newOperatorsHash] != 0) revert DuplicateOperators();

uint256 epoch = currentEpoch + 1;
currentEpoch = epoch;
Expand All @@ -93,18 +96,19 @@ contract AxelarAuthWeighted is Ownable, IAxelarAuthWeighted {
bytes[] memory signatures
) internal pure {
uint256 operatorsLength = operators.length;
uint256 operatorIndex = 0;
uint256 weight = 0;
uint256 signaturesLength = signatures.length;
uint256 operatorIndex;
uint256 weight;
// looking for signers within operators
// assuming that both operators and signatures are sorted
for (uint256 i = 0; i < signatures.length; ++i) {
for (uint256 i; i < signaturesLength; ++i) {
address signer = ECDSA.recover(messageHash, signatures[i]);
// looping through remaining operators to find a match
for (; operatorIndex < operatorsLength && signer != operators[operatorIndex]; ++operatorIndex) {}
// checking if we are out of operators
if (operatorIndex == operatorsLength) revert MalformedSigners();
// return if weight sum above threshold
weight += weights[operatorIndex];
// accumulating signatures weight
weight = weight + weights[operatorIndex];
// weight needs to reach or surpass threshold
if (weight >= threshold) return;
// increasing operators index if match was found
Expand All @@ -115,12 +119,20 @@ contract AxelarAuthWeighted is Ownable, IAxelarAuthWeighted {
}

function _isSortedAscAndContainsNoDuplicate(address[] memory accounts) internal pure returns (bool) {
for (uint256 i; i < accounts.length - 1; ++i) {
if (accounts[i] >= accounts[i + 1]) {
address prevAccount;
address account = accounts[0];

if (account == address(0)) return false;

for (uint256 i = 1; i < accounts.length; ++i) {
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
prevAccount = account;
account = accounts[i];

if (prevAccount >= account) {
return false;
}
}

return accounts[0] != address(0);
return true;
}
}