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

Rely on strict increasing order in checkpoints lookups #4544

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions contracts/utils/structs/Checkpoints.sol
Expand Up @@ -168,10 +168,14 @@ library Checkpoints {
) private view returns (uint256) {
while (low < high) {
uint256 mid = Math.average(low, high);
if (_unsafeAccess(self, mid)._key > key) {
uint32 currentKey = _unsafeAccess(self, mid)._key;
if (currentKey > key) {
high = mid;
} else {
low = mid + 1;
if (currentKey == key) {
return low;
}
}
}
return high;
Expand All @@ -192,10 +196,14 @@ library Checkpoints {
) private view returns (uint256) {
while (low < high) {
uint256 mid = Math.average(low, high);
if (_unsafeAccess(self, mid)._key < key) {
uint32 currentKey = _unsafeAccess(self, mid)._key;
if (currentKey < key) {
low = mid + 1;
} else {
high = mid;
if (currentKey == key) {
return high;
}
}
}
return high;
Expand Down Expand Up @@ -558,10 +566,14 @@ library Checkpoints {
) private view returns (uint256) {
while (low < high) {
uint256 mid = Math.average(low, high);
if (_unsafeAccess(self, mid)._key > key) {
uint96 currentKey = _unsafeAccess(self, mid)._key;
if (currentKey > key) {
high = mid;
} else {
low = mid + 1;
if (currentKey == key) {
return low;
}
}
}
return high;
Expand All @@ -582,10 +594,14 @@ library Checkpoints {
) private view returns (uint256) {
while (low < high) {
uint256 mid = Math.average(low, high);
if (_unsafeAccess(self, mid)._key < key) {
uint96 currentKey = _unsafeAccess(self, mid)._key;
if (currentKey < key) {
low = mid + 1;
} else {
high = mid;
if (currentKey == key) {
return high;
}
}
}
return high;
Expand Down
12 changes: 10 additions & 2 deletions scripts/generate/templates/Checkpoints.js
Expand Up @@ -189,10 +189,14 @@ function _upperBinaryLookup(
) private view returns (uint256) {
while (low < high) {
uint256 mid = Math.average(low, high);
if (_unsafeAccess(self, mid).${opts.keyFieldName} > key) {
${opts.keyTypeName} currentKey = _unsafeAccess(self, mid).${opts.keyFieldName};
if (currentKey > key) {
high = mid;
} else {
low = mid + 1;
if (currentKey == key) {
return low;
}
}
}
return high;
Expand All @@ -213,10 +217,14 @@ function _lowerBinaryLookup(
) private view returns (uint256) {
while (low < high) {
uint256 mid = Math.average(low, high);
if (_unsafeAccess(self, mid).${opts.keyFieldName} < key) {
${opts.keyTypeName} currentKey = _unsafeAccess(self, mid).${opts.keyFieldName};
if (currentKey < key) {
low = mid + 1;
} else {
high = mid;
if (currentKey == key) {
return high;
}
}
}
return high;
Expand Down