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 chain reorg in event reader #50

Closed
hieuh25 opened this issue Oct 27, 2022 · 4 comments · Fixed by #62
Closed

Handle chain reorg in event reader #50

hieuh25 opened this issue Oct 27, 2022 · 4 comments · Fixed by #62
Assignees
Labels
bug Something isn't working enhancement New feature or request priority: P2 Default priority. An issue might not yet be fixed in the next release. size: M An ordinary task that can be completed in a few days or in a week

Comments

@hieuh25
Copy link
Contributor

hieuh25 commented Oct 27, 2022

Current the event reader can't handle chain reorg situation, so it's likely to fail here:

if extract_timestamps is not None:
timestamps = extract_timestamps(web3, start_block, end_block)
for log in logs:
block_hash = log["blockHash"]
block_number = int(log["blockNumber"], 16)
# Retrofit our information to the dict
event_signature = log["topics"][0]
log["context"] = context
log["event"] = filter.topics[event_signature]
try:
log["timestamp"] = timestamps[block_hash] if extract_timestamps else None
except KeyError as e:
raise RuntimeError(f"Timestamp missing for block number {block_number:,}, hash {block_hash}, our timestamp table has {len(timestamps)} blocks") from e

@hieuh25 hieuh25 added bug Something isn't working enhancement New feature or request priority: P2 Default priority. An issue might not yet be fixed in the next release. size: M An ordinary task that can be completed in a few days or in a week labels Oct 27, 2022
@swarna1101
Copy link

swarna1101 commented Dec 24, 2022

if extract_timestamps is not None:
    timestamps = extract_timestamps(web3, start_block, end_block)

for log in logs:
    block_hash = log["blockHash"]
    block_number = int(log["blockNumber"], 16)
    # Retrofit our information to the dict
    event_signature = log["topics"][0]
    log["context"] = context
    log["event"] = filter.topics[event_signature]
    if extract_timestamps:
        try:
            log["timestamp"] = timestamps[block_hash]
        except KeyError as e:
            # If the block hash is not found in the timestamps table, it could be due to a chain reorg.
            # In this case, we can try to retrieve the timestamp using the block number instead.
            try:
                block = web3.eth.getBlock(block_number)
                log["timestamp"] = block["timestamp"]
            except Exception as e:
                raise RuntimeError(
                    f"Timestamp missing for block number {block_number:,}, hash {block_hash}, our timestamp table has {len(timestamps)} blocks"
                ) from e
    else:
        log["timestamp"] = None

This code first checks if extract_timestamps is not None, and if it is not, it retrieves the timestamps for the blocks within the specified range using the extract_timestamps function. Then, for each log in the logs list, it retrofits the information to the dict and tries to retrieve the timestamp for the block using the block hash. If a KeyError is raised, it means that the block hash is not found in the timestamps table, which could be due to a chain reorg. In this case, it tries to retrieve the timestamp using the block number instead using the web3.eth.getBlock method. If this also fails, it raises a RuntimeError with a message indicating that the timestamp is missing.

@swarna1101
Copy link

swarna1101 commented Dec 28, 2022

#59 can you check this once @miohtama @hieuh25

@miohtama
Copy link
Contributor

miohtama commented Dec 30, 2022

@swarna1101 Thank you for your feedback. I am sorry your comment is not relevant for this issue. Please do not post comments, unless you have more context on the issue.

@hieuh25 hieuh25 linked a pull request Feb 26, 2023 that will close this issue
@hieuh25
Copy link
Contributor Author

hieuh25 commented Feb 26, 2023

Fixed in #62

@hieuh25 hieuh25 closed this as completed Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request priority: P2 Default priority. An issue might not yet be fixed in the next release. size: M An ordinary task that can be completed in a few days or in a week
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants