-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_layers
executable file
·182 lines (154 loc) · 5.57 KB
/
export_layers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/python3
import bech32
import json
import argparse
import subprocess
import re
import os
import sys
import base64
import binascii
from datetime import datetime, timedelta, timezone
try:
subprocess.run(["grpcurl", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except subprocess.CalledProcessError:
print("grpcurl is required but not installed.")
print("You can install grpcurl on Linux with following command:")
print("curl -sSL \"https://github.com/fullstorydev/grpcurl/releases/download/v1.8.6/grpcurl_1.8.6_linux_x86_64.tar.gz\" | tar -xz -C /usr/local/bin")
parser = argparse.ArgumentParser(description="""
This script searches for files which could be potentially spacemesh node configurations.
It then extracts the GRPC socket from these files and queries the nodes for their
eligible layers. The layers are written to a JSON file which can be directly imported
in the Spacemesh App Tracker for mobile phones.
""")
parser.add_argument(
"--search-root",
help="Root directory for node config search. Default is current directory.",
default=".",
action="store",
)
parser.add_argument(
"--search-depth",
help="Define max. recursive depth for node config search. Default is 5.",
default=5,
type=int,
action="store",
)
parser.add_argument(
"--search-string",
help="""Provide a string which identifies node config files (e.g. node-config.json).
Default is .json which will result in searching for all json files.""",
default=".json",
action="store",
)
args = parser.parse_args()
def base64_to_hex(base64_string):
decoded_bytes = base64.b64decode(base64_string)
hex_result = binascii.hexlify(decoded_bytes).decode('utf-8')
return hex_result
def get_current_epoch():
genesis_date = datetime(2023, 7, 14, 8, 0, tzinfo=timezone.utc) # genesis
current_date = datetime.now(timezone.utc)
time_since_genesis = current_date - genesis_date
current_layer = int(time_since_genesis.total_seconds() / 60 / 5)
return int(current_layer / 4032)
def is_json_file(file_path):
try:
with open(file_path, 'r') as file:
json.load(file)
return True
except (json.JSONDecodeError, FileNotFoundError):
return False
def find_files(search_string, search_root, max_depth):
result = []
for root, dirs, files in os.walk(search_root, followlinks=True):
current_depth = root[len(search_root):].count(os.sep)
if current_depth >= max_depth:
continue
for file in files:
if file.endswith(search_string):
full_path = os.path.abspath(os.path.join(root, file))
if is_json_file(full_path):
result.append(full_path)
return result
def get_events_stream(grpc_socket):
cmd = ["timeout", "0.5s", "grpcurl", "-plaintext", "-d", "{}", grpc_socket, "spacemesh.v1.AdminService.EventsStream"]
try:
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, text=True)
except subprocess.CalledProcessError as e:
output = e.output
return output
def split_stream_blocks(stream):
blocks = []
count = 0
start = 0
for i, char in enumerate(stream):
if char == '{':
count += 1
elif char == '}':
count -= 1
if count == 0:
blocks.append(stream[start:i+1])
start = i+1
return blocks
def get_eligible_layers(grpc_socket, epoch):
stream = get_events_stream(grpc_socket)
blocks = split_stream_blocks(stream)
node_id = None
for block in blocks:
if "smesher" in block and "initStart" in block:
j = json.loads(block)
node_id = j["initStart"]["smesher"]
node_id = base64_to_hex(node_id)
break
if not node_id:
return None
layers = []
for block in blocks:
if not "eligibilities" in block:
continue
j = json.loads(block)
if j["eligibilities"]["epoch"] != epoch:
continue
for e in j["eligibilities"]["eligibilities"]:
layers.append([e["layer"], e["count"]])
return [node_id, sorted(layers)]
def get_node_grpc_socket(node_config):
try:
with open(node_config, 'r') as f:
j = json.load(f)
return j["api"]["grpc-private-listener"]
except (json.JSONDecodeError, FileNotFoundError, KeyError, TypeError):
return None
print(f"Searching for node configuration files (files matching {args.search_string})")
file_matches=find_files(args.search_string, args.search_root, args.search_depth)
print("")
layers_export = {}
node_counter = 0
layer_counter = 0
for fn in file_matches:
socket = get_node_grpc_socket(fn)
if not socket:
continue
el = get_eligible_layers(socket, get_current_epoch())
if el:
node_counter += 1
node_id, el = el
print("Node ID:", node_id)
print("Config file:", fn)
if node_id in layers_export:
print("Node ID was already queried. Skipping ...\n")
continue
print("Eligible layers:", el, "\n")
layers = []
for l in el:
layer_counter += l[1]
layers.append({"layer": l[0], "count": l[1]})
layers_export[node_id] = layers
if layers_export:
print(f"Retrieved {layer_counter} layers eligible for rewards from {node_counter} nodes.")
print("Writing results to layers.json ...")
with open("layers.json", 'w') as json_file:
json.dump(layers_export, json_file, indent=4)
else:
print("Could not find any nodes.")