-
Notifications
You must be signed in to change notification settings - Fork 0
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: blocknumber binsearch #68
Conversation
d649524
to
23ec54e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice PR!
packages/blocknumber/package.json
Outdated
@@ -25,6 +25,7 @@ | |||
"dependencies": { | |||
"@ebo-agent/shared": "workspace:*", | |||
"axios": "1.7.7", | |||
"bignumber.js": "^9.1.2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOOO IT'S BACK 🪓
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just one doubt, else lgtm
private async getNextBlockWithDifferentTimestamp( | ||
block: BlockWithNumber, | ||
): Promise<BlockWithNumber | null> { | ||
let nextBlock: BlockWithNumber = block; | ||
|
||
try { | ||
while (nextBlock.timestamp === block.timestamp) { | ||
nextBlock = await this.client.getBlock({ blockNumber: nextBlock.number + 1n }); | ||
} | ||
|
||
return nextBlock; | ||
} catch (err) { | ||
if (err instanceof BlockNotFoundError) { | ||
// This covers the case where the search surpasses the latest block | ||
// and no more blocks are found by block number. | ||
return null; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Search the block with the lowest height that has the same timestamp as `block`. | ||
* | ||
* @param block the block to use in the search | ||
* @returns a block with the same timestamp as `block` and with the lowest height. | ||
*/ | ||
private async searchFirstBlockWithEqualTimestamp( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe call this getFirstBlockWithEqualTimestamp
or the other function searchNextBlockWithDifferentTimestamp
, so you keep a naming convention for search/get functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice suggestion, I think I'll go with search
; feels like a more appropriate naming for O(n)
lookups.
get
feels like more appropriate for O(1)
functions, wdty?
let candidateBlockNumberBN = new BigNumber(lastBlock.number.toString()) | ||
.dividedBy(timestampDeltaBN.dividedBy(estimatedBlockTimeBN)) | ||
.integerValue(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this okay? original code is: let candidateBlockNumber = lastBlock.number - timestampDelta / estimatedBlockTime;
and you're doing two divisions (idk if im missing smth on the math)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh boy what a nice catch 🎯
🤖 Linear
Closes GRT-226
Description