# Peer Delivering

Gets information about peers in the Bor network using the admin.peers command in the Geth console. Reads logs from a specified file defined by log\_file\_path. Filters the logs to find lines containing the string "Peer delivering stale transactions." Extracts the IDs of peers from the filtered logs. Prints the extracted enodes and corresponding IDs. For each peer ID found in the logs, removes the corresponding peer using admin.removePeer in the Geth console.

```bash
import subprocess
import re

def get_enodes_from_peers_info(peers_info):
    
    enode_pattern = re.compile(r'enode: "([^"]+)",\s+id: "([^"]+)"')
    enodes = enode_pattern.findall(peers_info)
    return dict(enodes)

def get_peer_id_from_log(log):
    
    peer_id_pattern = re.compile(r'peer=([a-fA-F0-9]+)')
    match = peer_id_pattern.search(log)
    return match.group(1) if match else None

def print_peer_ids_from_logs(logs):
   
    unique_peer_ids = set()
    
    print("Peer IDs in logs:")
    for log in logs:
        peer_id = get_peer_id_from_log(log)
        if peer_id is not None and peer_id not in unique_peer_ids:
            unique_peer_ids.add(peer_id)
            print(f"  {peer_id}")
    return unique_peer_ids

command = "bor attach /var/lib/bor/bor.ipc --exec 'admin.peers'"

peers_info = subprocess.check_output(command, shell=True, text=True)

log_file_path = "/your-path/log"

with open(log_file_path, 'r') as log_file:
    
    filtered_logs = [line for line in log_file if "Peer delivering stale transactions" in line]

peer_ids_from_logs = print_peer_ids_from_logs(filtered_logs)

enodes_dict = get_enodes_from_peers_info(peers_info)
print("\nExtracted Enodes:")
for enode, peer_id in enodes_dict.items():
    print(f"  {enode}: {peer_id}")

for peer_id_from_log in peer_ids_from_logs:
    if peer_id_from_log in enodes_dict.values():
        enode_to_remove = [k for k, v in enodes_dict.items() if v == peer_id_from_log][0]
        print(f"\nRemoving enode: {enode_to_remove}")
        
        remove_command = f'bor attach /var/lib/bor/bor.ipc --exec \'admin.removePeer("{enode_to_remove}")\''
        subprocess.run(remove_command, shell=True, text=True)
        
        print(f"Enode removed: {enode_to_remove}")
    else:
        print(f"\nPeer ID {peer_id_from_log} not found in peers.")
```

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:

```bash
pip install requests
```

Script Execution:

Save the Python script (`Your_script.py`) in the directory of your choice.\
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:

```bash
python3 your_script.py
```

Results:

The script is useful for identifying specific peers based on specific logs and then removing those peers from the Bor network. Make sure to adjust the log file path (log\_file\_path) to the correct location before running the script.\
**Note:** You can use cron to automate the periodic execution of the script


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.stakepool.dev.br/polygon/scripts/peer-delivering.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
