chainsaw massacre
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
import argparse
|
||||
import requests
|
||||
import time
|
||||
|
||||
# Parse command-line arguments
|
||||
parser = argparse.ArgumentParser(description='Compare block hashes from two Ethereum RPC endpoints.')
|
||||
parser.add_argument('rpc_url_1', type=str, help='The first RPC URL')
|
||||
parser.add_argument('rpc_url_2', type=str, help='The second RPC URL')
|
||||
args = parser.parse_args()
|
||||
|
||||
# Define the RPC endpoints from the command-line arguments
|
||||
rpc_url_1 = args.rpc_url_1
|
||||
rpc_url_2 = args.rpc_url_2
|
||||
|
||||
# Define the JSON-RPC payload
|
||||
def get_block_by_number_payload(block_number):
|
||||
return {
|
||||
"jsonrpc": "2.0",
|
||||
"method": "eth_getBlockByNumber",
|
||||
"params": [hex(block_number), False],
|
||||
"id": 1
|
||||
}
|
||||
|
||||
# Function to get the latest block number
|
||||
def get_latest_block_number(rpc_url):
|
||||
response = requests.post(rpc_url, json={
|
||||
"jsonrpc": "2.0",
|
||||
"method": "eth_blockNumber",
|
||||
"params": [],
|
||||
"id": 1
|
||||
})
|
||||
result = response.json()
|
||||
return int(result['result'], 16)
|
||||
|
||||
# Function to get the block hash by block number
|
||||
def get_block_hash(rpc_url, block_number):
|
||||
response = requests.post(rpc_url, json=get_block_by_number_payload(block_number))
|
||||
result = response.json()
|
||||
return result['result']['hash']
|
||||
|
||||
# Get the latest block number from the first RPC endpoint
|
||||
latest_block_number = get_latest_block_number(rpc_url_1)
|
||||
|
||||
# Binary search-like approach to find the first matching block hash
|
||||
low = 0
|
||||
high = latest_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:
|
||||
first_matching_block = mid
|
||||
high = mid + 1 # Continue searching in the upper half
|
||||
else:
|
||||
low = mid - 1 # Continue searching in the lower 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:
|
||||
print("No matching block hash found.")
|
||||
@@ -1,63 +0,0 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
def get_block_by_number(endpoint, block_number):
|
||||
hex_block_number = hex(block_number)
|
||||
response = requests.post(
|
||||
endpoint,
|
||||
headers={"Content-Type": "application/json"},
|
||||
data=json.dumps({"jsonrpc": "2.0", "method": "eth_getBlockByNumber", "params": [hex_block_number, False], "id": 1})
|
||||
)
|
||||
response_data = response.json()
|
||||
return response_data['result']
|
||||
|
||||
def find_highest_matching_block(endpoint1, endpoint2, start_block):
|
||||
low = 0
|
||||
high = start_block
|
||||
highest_matching_block = None
|
||||
highest_matching_hash = None
|
||||
|
||||
while low <= high:
|
||||
mid = (low + high) // 2
|
||||
|
||||
block1 = get_block_by_number(endpoint1, mid)
|
||||
block2 = get_block_by_number(endpoint2, mid)
|
||||
|
||||
if block1 is None or block2 is None:
|
||||
print(f"Block {mid} not found in one of the endpoints")
|
||||
high = mid - 1
|
||||
continue
|
||||
|
||||
if block1['hash'] == block2['hash']:
|
||||
highest_matching_block = mid
|
||||
highest_matching_hash = block1['hash']
|
||||
low = mid + 1
|
||||
else:
|
||||
high = mid - 1
|
||||
|
||||
if highest_matching_block is not None:
|
||||
# Linear search upwards from the highest known matching block to find the highest matching block
|
||||
while True:
|
||||
next_block1 = get_block_by_number(endpoint1, highest_matching_block + 1)
|
||||
next_block2 = get_block_by_number(endpoint2, highest_matching_block + 1)
|
||||
if next_block1 is None or next_block2 is None or next_block1['hash'] != next_block2['hash']:
|
||||
break
|
||||
highest_matching_block += 1
|
||||
highest_matching_hash = next_block1['hash']
|
||||
|
||||
print(f"Matching block found at height {highest_matching_block}")
|
||||
print(f"Matching block hash: {highest_matching_hash}")
|
||||
print("I did it!")
|
||||
|
||||
if next_block1 is not None and next_block2 is not None:
|
||||
print(f"Following block number {highest_matching_block + 1} does not match")
|
||||
print(f"Endpoint1 hash: {next_block1['hash']}")
|
||||
print(f"Endpoint2 hash: {next_block2['hash']}")
|
||||
else:
|
||||
print("No matching blocks found")
|
||||
|
||||
if __name__ == "__main__":
|
||||
endpoint1 = "https://rpc-de-15.stakesquid.eu/base"
|
||||
endpoint2 = "https://mainnet.base.org"
|
||||
start_block = 7945476
|
||||
find_highest_matching_block(endpoint1, endpoint2, start_block)
|
||||
Reference in New Issue
Block a user