Created
June 30, 2021 22:46
-
-
Save bogatyy/d4cafa354e0c2e04809ce527f616a476 to your computer and use it in GitHub Desktop.
Extract top peers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
GETH_LOGFILE = 'geth-log.txt' | |
def extract_parameter(source_str, before_str, after_str): | |
start_pos = source_str.find(before_str) + len(before_str) | |
return source_str[start_pos:source_str.find(after_str, start_pos)] | |
def extract_top_peers_from_logs(): | |
logs = open(GETH_LOGFILE, 'r').readlines() | |
# count imports | |
block_imports = [line for line in logs if 'Importing propagated block' in line] | |
peer_id_to_block_count = defaultdict(int) | |
for block_import_line in block_imports: | |
peer_id = extract_parameter(block_import_line, 'peer=', ' ') | |
peer_id_to_block_count[peer_id] += 1 | |
# convert peer IDs to enodes | |
peer_id_to_enode = defaultdict(str) | |
peer_logs = [line for line in logs if 'Peer:' in line] | |
for peer_log_line in peer_logs: | |
peer_id = extract_parameter(peer_log_line, 'id=', ' ') | |
enode = extract_parameter(peer_log_line, 'enode=', ' ') | |
# potentially problematic if there are overlaps, but for simplicity just take the latest chronologically | |
peer_id_to_enode[peer_id] = enode | |
# merge top enodes together | |
peer_id_and_block_counts = list(peer_id_to_block_count.items()) | |
peer_id_and_block_counts.sort(key=lambda x: x[1], reverse=True) | |
enodes = [] | |
for peer_id, block_count in peer_id_and_block_counts: | |
if peer_id_to_enode[peer_id]: | |
enodes.append(peer_id_to_enode[peer_id]) | |
return enodes[:50] | |
if __name__ == '__main__': | |
top_peers_from_logs = extract_top_peers_from_logs() | |
nodes_data = ( | |
'[\n' + | |
',\n'.join( | |
' "' + enode + '"' | |
for enode in top_peers_from_logs | |
) | |
+ '\n]' | |
) | |
with open('trusted_nodes.json', 'w') as nodes_file: | |
nodes_file.write(nodes_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello,
i have a problem to get a result in trusted.json.
only this is in the trusted.json after runing with "python3 extract_top_peers"
[
]
my geth-log.txt give me these lines:
...
INFO [01-20|10:02:03.848] Peers:
INFO [01-20|10:02:03.848] Peer: id=139b1d5faf2339bbfe1280a105c42c639f5e6e826e20268d0428fe0ab32d10ce enode=enode://68814e6a27fad242efdbcb5cf9c74609d0db391feafe7135d6aa88f01490377b521582370d3d49e74a729ef2bb5dcdd641036257f767a5ec6efbc5b4ebeb6830@84.17.42.218:30303 importedBlocksFrom=0 totalBroadcastsTo=4 broadcastsToWithoutBlock=4
,,,
for an example.
so where is the bug oder error?
i use ubuntu 18 with python or python 3.6