Skip to content

Commit 69d0fbc

Browse files
authored
chore: gas optimisation for the for loop using the unchecked block (#212)
<!-- Please read and fill out this form before submitting your PR. Please make sure you have reviewed our contributors guide before submitting your first PR. --> ## Overview Closes #202 ## Checklist <!-- Please complete the checklist to ensure that the PR is ready to be reviewed. IMPORTANT: PRs should be left in Draft until the below checklist is completed. --> - [ ] New and updated code has appropriate documentation - [ ] New and updated code has new and/or updated testing - [ ] Required CI checks are passing - [ ] Visual proof for any user facing features like CLI or documentation updates - [ ] Linked issues closed with keywords
1 parent 85e8eeb commit 69d0fbc

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/QuantumGravityBridge.sol

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -198,27 +198,29 @@ contract QuantumGravityBridge is IDAOracle, Initializable, UUPSUpgradeable, Owna
198198
uint256 _powerThreshold
199199
) private pure {
200200
uint256 cumulativePower = 0;
201-
202-
for (uint256 i = 0; i < _currentValidators.length; i++) {
203-
// If the signature is nil, then it's not present so continue.
204-
if (isSigNil(_sigs[i])) {
205-
continue;
206-
}
207-
208-
// Check that the current validator has signed off on the hash.
209-
if (!verifySig(_currentValidators[i].addr, _digest, _sigs[i])) {
210-
revert InvalidSignature();
211-
}
212-
213-
// Sum up cumulative power.
214-
cumulativePower += _currentValidators[i].power;
215-
216-
// Break early to avoid wasting gas.
217-
if (cumulativePower >= _powerThreshold) {
218-
break;
201+
// Note: be cautious when updating the code inside the unchecked block.
202+
// Make sure to verify if all the bypassed security checks are respected.
203+
unchecked {
204+
for (uint256 i = 0; i < _currentValidators.length; i++) {
205+
// If the signature is nil, then it's not present so continue.
206+
if (isSigNil(_sigs[i])) {
207+
continue;
208+
}
209+
210+
// Check that the current validator has signed off on the hash.
211+
if (!verifySig(_currentValidators[i].addr, _digest, _sigs[i])) {
212+
revert InvalidSignature();
213+
}
214+
215+
// Sum up cumulative power.
216+
cumulativePower += _currentValidators[i].power;
217+
218+
// Break early to avoid wasting gas.
219+
if (cumulativePower >= _powerThreshold) {
220+
break;
221+
}
219222
}
220223
}
221-
222224
// Check that there was enough power.
223225
if (cumulativePower < _powerThreshold) {
224226
revert InsufficientVotingPower();

0 commit comments

Comments
 (0)