-
Notifications
You must be signed in to change notification settings - Fork 4
fix: last_checked_block retrieval #138
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: last_checked_block retrieval #138
Conversation
observer/lib/observer.js
Outdated
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.
fix lgtm, could you please also add a test for this? You can mock ieContract
so we only test the sql logic
observer/bin/dry-run.js
Outdated
@@ -18,4 +18,8 @@ await pgPool.query('DELETE FROM daily_reward_transfers') | |||
|
|||
await observeTransferEvents(pgPool, ieContract, provider) | |||
|
|||
// Do it a second time, without clearing the table. | |||
// This should find 0 events, unless rewards are currently being released. | |||
await observeTransferEvents(pgPool, ieContract, provider) |
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.
I think it's safer to add an automated test instead
) | ||
let queryFromBlock = rows[0].last_checked_block | ||
let queryFromBlock = rows[0].last_checked_block + 1 |
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.
I think there is bug lurking here.
In the initial run against an empty database, last_checked_block
will be undefined
and queryFromBlock
will be set to NaN
.
❯ node
Welcome to Node.js v20.14.0.
Type ".help" for more information.
> undefined + 1
NaN
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.
You're right!
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.
We check the truthy value of the queryFromBlock directly after this, so it ends up being reset to the -1900 block
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.
Ah, I see. I verified that this works as you described.
> queryFromBlock = undefined + 1
NaN
> currentBlockNumber=6543
6543
> !queryFromBlock || queryFromBlock < currentBlockNumber - 1900
true
>
It's great that the bug is not there.
It would be nice to rewrite this part to make it easier for casual readers (or novice programmers) to correctly understand how exactly the code works and how it handles different edge cases.
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.
Ok! Do you prefer just a comment explaining it, or a logic change?
const getDayAsISOString = d => d.toISOString().split('T')[0] | ||
const today = () => getDayAsISOString(new Date()) |
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.
Was there any particular reason for moving these two helper functions into a describe
block? Can we move them back to the top where they were before?
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.
You're right here as well
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.
I reviewed too quickly
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.
Oops, merge conflicts confusion! Shall I open a PR for this?
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.
yes please
Related to #134 and #102