Polygon Validator Participation Analyzer

The script checks the involvement of specific validator addresses on the Polygon network, identifying the blocks in which these addresses did not sign and keeping track of how many times each address acted as the proposer. The results are saved in a file named "results.txt."

import requests

def extract_validator_addresses(block, address):
    # URL for a specific block endpoint
    url = f"https://heimdall-api.polygon.technology/blocks/{block}"

    
    response = requests.get(url)

   
    if response.status_code == 200:
        
        data = response.json()

        
        if 'last_commit' in data['block']:
            # Extracting the proposer address
            proposer_address = data['block']['header'].get('proposer_address')

            
            precommits = data['block']['last_commit'].get('precommits', [])

            
            validator_addresses = [commit.get('validator_address') for commit in precommits if commit]

            # Checking if the address is in the list of validators
            if address in validator_addresses or address == proposer_address:
                return address, True, proposer_address
            else:
                return address, False, proposer_address
        else:
            print(f"No commit data in Block {block}.")
            return address, False, None
    else:
        
        print(f"Failed request for Block {block}. Response code: {response.status_code}")
        return address, False, None

# Define the desired block range
initial_block = 16378652
final_block = 16379652

with open("addresses.txt", "r") as file:
    addresses_file = [line.strip() for line in file]


addresses_not_signing = []
addresses_proposer_count = {}

for block in range(initial_block, final_block + 1):
    for address in addresses_file:
        _, signed, proposer_address = extract_validator_addresses(block, address)
        if not signed:
            addresses_not_signing.append({"address": address, "block": block, "proposer_address": proposer_address})
            # Counting the number of times the address was the proposer
            if proposer_address in addresses_proposer_count:
                addresses_proposer_count[proposer_address] += 1
            else:
                addresses_proposer_count[proposer_address] = 1


addresses_not_signing = sorted(addresses_not_signing, key=lambda x: x['block'])


with open("results.txt", "w") as result_file:
    result_file.write("Addresses not signing blocks, sorted by block:\n")
    for item in addresses_not_signing:
        result_file.write(f"Address: {item['address']}, Block: {item['block']}, Proposer Address: {item['proposer_address'] if item['proposer_address'] is not None else 'None'}\n")

    result_file.write("\nTotal times each address proposed:\n")
    for address, count in addresses_proposer_count.items():
        result_file.write(f"Address: {address}, Total Proposals: {count}\n")

print("Results saved in results.txt")

addresses.txt

Python and Requests Installation:

Make sure you have Python installed on your system. Open the terminal or command prompt. Execute the following command to install the requests library:

pip install requests

Script Execution:

Save the Python script (Your_script.py) in the directory of your choice. Place a file named "addresses.txt" in the same directory as the script, containing the addresses of the validators.

Run the Script:

In the terminal or command prompt, navigate to the directory where the script is located. Execute the script using the following command:

python3 your_script.py

Results:

The script will process the blocks and generate a file named "results.txt" in the same directory as the script. Ensure that the "addresses.txt" file is present in the same directory as the script to ensure proper script functionality.

Last updated