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

Retry block time requests if they fail #40

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def get_block_time_rpc(rpc_url, height, allow_retry=False):
print(e)
# Attempt to retry the request if the error message indicates that the block height is too low
# Pulls the block height from the error message and adds 20 to it
if allow_retry and response and response.status_code == 500 and response.content:
if allow_retry and response is not None and response.status_code == 500 and response.content:
try:
error_message = json.loads(response.content)
if "lowest height is " in error_message.get("error", {}).get("data", ""):
Expand Down Expand Up @@ -673,10 +673,22 @@ def fetch_data_for_network(network, network_type, repo_path):
rest_server_used = current_endpoint
break

# Calculate average block time
current_block_time = get_block_time_rpc(rpc_server_used, latest_block_height)
past_block_time = get_block_time_rpc(rpc_server_used, latest_block_height - 10000, allow_retry=True)
current_block_time = None
past_block_time = None
avg_block_time_seconds = None
for rpc_endpoint in healthy_rpc_endpoints:
# Get average block time
current_endpoint = rpc_endpoint["address"]
current_block_time = get_block_time_rpc(current_endpoint, latest_block_height)
past_block_time = get_block_time_rpc(current_endpoint, latest_block_height - 10000, allow_retry=True)

if current_block_time and past_block_time:
break
else:
print(
f"Failed to query current and past block time for rpc endpoint {current_endpoint}, trying next rpc endpoint"
)
continue

if current_block_time and past_block_time:
current_block_datetime = parse_isoformat_string(current_block_time)
Expand Down
Loading