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: blocknumber binsearch #68

Merged
merged 9 commits into from
Oct 24, 2024
Merged

fix: blocknumber binsearch #68

merged 9 commits into from
Oct 24, 2024

Conversation

0xyaco
Copy link
Collaborator

@0xyaco 0xyaco commented Oct 23, 2024

🤖 Linear

Closes GRT-226

Description

  • Handle the cases where multiple blocks have the same timestamp (in arbitrum multiple blocks can have the same timestamp if working with the precision in seconds)
  • Fixed precision errors when the estimated block time is between [0, 1)

Copy link

linear bot commented Oct 23, 2024

@0xyaco 0xyaco force-pushed the fix/blocknumber-binsearch branch from d649524 to 23ec54e Compare October 23, 2024 10:21
@0xyaco 0xyaco changed the title Fix/blocknumber binsearch fix: blocknumber binsearch Oct 23, 2024
jahabeebs
jahabeebs previously approved these changes Oct 23, 2024
Copy link
Collaborator

@jahabeebs jahabeebs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice PR!

@@ -25,6 +25,7 @@
"dependencies": {
"@ebo-agent/shared": "workspace:*",
"axios": "1.7.7",
"bignumber.js": "^9.1.2",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥕

Copy link
Collaborator Author

@0xyaco 0xyaco Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOOO IT'S BACK 🪓

jahabeebs
jahabeebs previously approved these changes Oct 23, 2024
Copy link
Collaborator

@0xnigir1 0xnigir1 left a 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

Comment on lines 266 to 294
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(
Copy link
Collaborator

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

Copy link
Collaborator Author

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?

Comment on lines 136 to 138
let candidateBlockNumberBN = new BigNumber(lastBlock.number.toString())
.dividedBy(timestampDeltaBN.dividedBy(estimatedBlockTimeBN))
.integerValue();
Copy link
Collaborator

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)

Copy link
Collaborator Author

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 🎯

@0xyaco 0xyaco requested review from 0xnigir1 and jahabeebs October 24, 2024 08:50
@0xyaco 0xyaco merged commit 81b9d8b into dev Oct 24, 2024
5 checks passed
@0xyaco 0xyaco deleted the fix/blocknumber-binsearch branch October 24, 2024 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants