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

Handle exception when getting block timestamp #792

Merged

Conversation

trizin
Copy link
Contributor

@trizin trizin commented Jan 17, 2024

Fixes #791

Changes proposed in this PR:

  • Handle exception when getting block timestamp

Comment on lines 119 to 123
try:
block_timestamp = web3.eth.get_block(int(block_i)).timestamp
except Exception as e:
print(f"An exception occurred while getting block {block_i}, {e}")
block_timestamp = 0
Copy link
Member

Choose a reason for hiding this comment

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

So if the exception happens, then block_timestamp = 0

And it returns block_timestamp - self.target_timestamp = 0 - self.target_timestamp = -self.target_timestamp.

This seems wrong.

Copy link
Member

Choose a reason for hiding this comment

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

One more thing: we should have a unit test for this exception too.

In general, when fixing a bug the best flow is to do the following:

  1. Write a unit test that fails because it's working the bug
  2. Write code to make the test pass
  3. See the test pass, done

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on my understanding, the Sapphire RPC error occurs when the block number is too far in the past, causing the active flow to fail. The proposed solution to address this issue involves setting the timestamp to 0 if this error is encountered, under the assumption that the block number is very much in the past.

Failed flow:
https://github.com/oceanprotocol/df-py/actions/runs/7555123620/job/20569794417?pr=790

I've tried running this solution locally for the previous week and the algorithm was able to find the correct timestamps for week start and week end.

Before proceeding with testing, I would appreciate your feedback on whether you think there might be a more optimal approach or if this solution is acceptable.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe if there's an error, return a value of 0, rather than - self.target_timestamp ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, committed, will proceed with testing. Thanks!

Copy link

codeclimate bot commented Jan 17, 2024

Code Climate has analyzed commit db30b3a and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 40.0% (90% is the threshold).

This pull request will bring the total coverage in the repository to 92.9% (0.0% change).

View more on Code Climate.

@trizin trizin requested a review from trentmc January 17, 2024 15:49
@trizin trizin merged commit f8ed89e into main Jan 17, 2024
7 checks passed
@trizin trizin deleted the issue791-handle-block-not-found-errors-in-timestamp_to_block branch January 17, 2024 18:22
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.

Handle block not found errors in timestamp_to_block
2 participants