quicksearch for compare blocks

This commit is contained in:
Sebastian
2024-07-12 11:34:46 +02:00
parent f71e45b8e5
commit fa020e88a5

View File

@@ -1,5 +1,6 @@
import argparse import argparse
import requests import requests
import time
# Parse command-line arguments # Parse command-line arguments
parser = argparse.ArgumentParser(description='Compare block hashes from two Ethereum RPC endpoints.') parser = argparse.ArgumentParser(description='Compare block hashes from two Ethereum RPC endpoints.')
@@ -40,17 +41,27 @@ def get_block_hash(rpc_url, block_number):
# Get the latest block number from the first RPC endpoint # Get the latest block number from the first RPC endpoint
latest_block_number = get_latest_block_number(rpc_url_1) latest_block_number = get_latest_block_number(rpc_url_1)
# Iterate from the latest block down to the earliest # Binary search-like approach to find the first matching block hash
for block_number in range(latest_block_number, -1, -1): low = 0
hash_1 = get_block_hash(rpc_url_1, block_number) high = latest_block_number
hash_2 = get_block_hash(rpc_url_2, block_number) first_matching_block = None
while low <= high:
mid = (low + high) // 2
hash_1 = get_block_hash(rpc_url_1, mid)
hash_2 = get_block_hash(rpc_url_2, mid)
if hash_1 == hash_2: if hash_1 == hash_2:
print(f"The first matching block is {block_number}") first_matching_block = mid
print(f"Block hash: {hash_1}") high = mid - 1 # Continue searching in the lower half
break
else: else:
print(f"Failed to match block {block_number} - {hash_1} - {hash_2}") low = mid + 1 # Continue searching in the upper half
# Sleep for one second before the next comparison
time.sleep(1)
if first_matching_block is not None:
print(f"The first matching block is {first_matching_block}")
print(f"Block hash: {get_block_hash(rpc_url_1, first_matching_block)}")
else: else:
print("No matching block hash found.") print("No matching block hash found.")