-
Notifications
You must be signed in to change notification settings - Fork 12k
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
Expose binary lookup functions in Checkpoint #5608
Comments
Hello @byeongsu-hong One of the reason these function are not exposed is because inproper arguments would cause unsafe/unexpected behavior, and checking the arguments are correct would require additional sloads that we want to avoid. Can you tell us more about your usecase. What do you mean by "process requests in ranges". Can you give use an example. Maybe what we should ahve is a function that exposes implement that high level feature. |
Hi @Amxx, I understand your intent. For our case, We implemented simple withdrawal queue like this. struct Queue {
uint48 withdrawalPeriod;
uint208 offset;
Checkpoints.Trace208 requests;
}
function requestWithdraw() // push new Checkpoint with accumulated amount
function claimWithdraw() // calculates claimable amount using binary search + set new offset to ignore claimed requests We need to know the |
Can you share the implementation of |
Sure, Here it is. We temporarily made an extension library that contains function claimWithdraw(address receiver) external nonReentrant returns (uint256) {
StorageV1 storage $ = _getStorageV1();
Checkpoints.Trace208 storage requests = $.queue[receiver].requests;
uint256 offset = $.queue[receiver].offset;
uint256 reqLen = requests.length();
require(reqLen > offset, NothingToClaim());
uint256 found = requests.upperBinaryLookup(clock() - $.withdrawalPeriod, offset, reqLen);
require(found > offset + 1, NothingToClaim());
uint256 claimed = requests.valueAt((found - 1).toUint32()) - requests.valueAt(offset.toUint32());
$.queue[receiver].offset = found - 1;
receiver.safeTransferETH(claimed);
emit WithdrawRequestClaimed(receiver, claimed, offset, found - 1);
return claimed;
} |
🧐 Motivation
While implementing WithdrawalQueue using Checkpoint, I discovered that the current Checkpoint implementation only allows retrieving values based on specific keys. Since I'm implementing a system that can process requests in ranges by adjusting the offset, it would be beneficial to have a way to retrieve Checkpoint indices or positions.
📝 Details
Target functions:
_upperBinaryLookup
,_lowerBinaryLookup
The text was updated successfully, but these errors were encountered: