Rpc Update

This application automates the management of the RPC endpoint URL to ensure it is always connected to a synchronized Bor node. If the node encounters failures or is not fully synchronized, the application automatically performs an update to a new URL

import requests
import toml
import subprocess

def update_bor_rpc_url(new_url):
    config_file_path = "/path/to/your/config.toml"  # Replace with the actual path to your TOML config file

    try:
        # Load the existing TOML config
        with open(config_file_path, "r") as config_file:
            config = toml.load(config_file)

        # Update the bor_rpc_url parameter
        config["bor_rpc_url"] = new_url

        # Write the updated config back to the file
        with open(config_file_path, "w") as config_file:
            toml.dump(config, config_file)
        
        print("bor_rpc_url updated to:", new_url)

    except Exception as e:
        print("Error updating bor_rpc_url:", e)

def check_bor_sync_status():
    bor_rpc_url = "http://localhost:8545"  # Replace with your Bor RPC URL

    try:
        response = requests.post(bor_rpc_url, json={"jsonrpc": "2.0", "method": "eth_syncing", "params": [], "id": 1})
        if response.status_code == 200:
            data = response.json()
            if "result" in data and data["result"] is False:
                print("Bor is fully synchronized")
            else:
                new_bor_rpc_url = "https://your-bor-rpc"
                update_bor_rpc_url(new_bor_rpc_url)
                print("Bor is not fully synchronized. Updated bor_rpc_url.")
        else:
            print("Error in Bor RPC request - Status code:", response.status_code)

    except requests.ConnectionError as ce:
        print("Connection error while checking Bor sync status:", ce)
        print("Attempting to update bor_rpc_url...")
        new_bor_rpc_url = "https://mainnet.stakepool.dev.br/rpc"
        update_bor_rpc_url(new_bor_rpc_url)
    except Exception as e:
        print("Error while checking Bor sync status:", e)

if __name__ == "__main__":
    check_bor_sync_status()

This script was developed to provide dynamic management of the RPC URL for a Bor node. It performs periodic checks of the synchronization status of the Bor node, and if it is not fully synchronized, dynamically updates the RPC URL with a new value. During this process, it loads the existing configuration from the TOML file, updates the bor_rpc_url parameter with a new URL, and performs the necessary checks to carry out the update automatically. To set up an alternative URL, replace "https://your-bor-rpc" with your desired RPC URL.

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 periodically check the synchronization status of the Bor node. If the node is not fully synchronized, the script will dynamically update the RPC URL with the alternative URL you configured.

Managing with systemd unit.

Service Installation:

[Unit]
Description=Reload heimdall Service on Config Change
After=network.target

[Service]
ExecStart=/path/to/reload-heimdall-service.sh

[Install]
WantedBy=default.target

After making the changes, reload systemd to apply the modifications:

sudo systemctl daemon-reload

Start the Service:

sudo systemctl start <name-service>

Enable:

sudo systemctl enable <name-service>

This unit file configures the Heimdall service to be restarted whenever there is a change in the configuration. Make sure to replace /path/to/reload-heimdall-service.sh with the actual path to your Heimdall service reload script.

Monitoring Changes:

#!/bin/bash

CONFIG_FILE="/path/to/your/heimdall-config.toml"
SERVICE_NAME="heimdall"  # Change this to match the actual service name

while inotifywait -e close_write "$CONFIG_FILE"; do
    systemctl restart "$SERVICE_NAME"
done

This script continuously monitors the TOML configuration file. If the file is modified, it automatically restarts the Heimdall service. Replace /path/to/your/heimdall-config.toml with the actual path to your Heimdall configuration file.

  • Save the script with a meaningful name, for example, heimdall-config.sh.

  • Make the script executable:

chmod +x heimdall-config.sh

Run the script:

./heimdall-config.sh
  • The script will enter into a loop and use inotifywait to watch for close_write events on the specified configuration file.

  • When a change occurs in the configuration file, the script will restart the Heimdall service using systemctl restart.

Last updated