-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
157 lines (121 loc) · 4.37 KB
/
main.py
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
import logging
import os
import signal
import time
import requests
# Logger
logger = logging.getLogger()
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('[%(asctime)s][%(levelname)s]: %(message)s', '%H:%M:%S'))
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
# Env Var
val_key = os.getenv("val_key","ethmvaloper1kdtjxywfvwq94jsst2uyshwkel6dwdv5vlf4l2")
routing_key = os.getenv('routing_key')
if routing_key is None:
logger.error('The routing key is missing!')
raise (SystemExit(1))
# Set up globals
ALLOWED_OFFSET = int(10)
LAST_ALERT = None
CURRENT_NETWORK_BLOCK = None
LAST_UPDATE = time.time()
RUNNING = True
MAX_TIMEOUT = 120
# Api
url = 'https://rpc-evm-sidechain.xrpl.org'
def get_height(url):
try:
queryData = '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}'
res = requests.post(url, data=queryData, headers={"Content-type": "application/json"}).json()
logger.debug(f'Getting height from {url}')
return int(res['result']['number'],16)
except Exception as e:
print(e)
return None
def get_status():
global CURRENT_NETWORK_BLOCK
global LAST_UPDATE
CURRENT_NETWORK_BLOCK = get_height(url)
if CURRENT_NETWORK_BLOCK is None:
# RPC down
if time.time() - LAST_UPDATE > MAX_TIMEOUT:
logger.info('Sending alert: No valid response')
send_alert(text=f'No valid response after {MAX_TIMEOUT} seconds')
return False
LAST_UPDATE = time.time()
our_node_heigth = get_height("http://localhost:8545")
if our_node_heigth is None:
logger.info('Sending alert: No valid response from our node')
send_alert(text=f'No valid response from our node')
return False
if CURRENT_NETWORK_BLOCK + ALLOWED_OFFSET < our_node_heigth:
# The height is lower than our last stored height (endpoints are not in sync)
logger.debug('The height is lower than our last stored height (endpoints are not in sync)')
return False
LAST_UPDATE = time.time()
if our_node_heigth < CURRENT_NETWORK_BLOCK - ALLOWED_OFFSET:
logger.info(f'Sending alert: Node is not up to date')
send_alert(blocks_missed=str(CURRENT_NETWORK_BLOCK - our_node_heigth))
return False
return True
# Pager duty
def generate_body(blocks_missed='?', text='Missing blocks!'):
return {
'payload': {
'summary': text,
'severity': 'critical',
'source': 'EVM Validator',
'component': 'validator',
'custom_details': {
'blocks missed': str(blocks_missed),
}
},
'routing_key':
str(routing_key),
'event_action':
'trigger',
'client':
'Validator Monitoring Service',
'client_url':
'https://evmos.org',
'links': [{
'href': 'https://validators.evm-sidechain.xrpl.org/xrp/validators/ethmvaloper1kdtjxywfvwq94jsst2uyshwkel6dwdv5vlf4l2',
'text': 'Explorer link!'
}],
'images': [{
'src': 'https://images.pexels.com/photos/1805164/pexels-photo-1805164.jpeg',
'href': 'https://google.com',
'alt': 'There is no need for this'
}]
}
def send_alert(blocks_missed='?', text='Missing blocks!'):
global LAST_ALERT
if LAST_ALERT is not None:
if time.time() - LAST_ALERT < 5 * 60:
# Only send 1 alert every 5min
return False
x = requests.post('https://events.eu.pagerduty.com/v2/enqueue',
json=generate_body(blocks_missed=blocks_missed, text=text))
while x.status_code != 202:
logger.error(f'Waiting 1 min to resend the alert, status code: {x.status_code}')
time.sleep(60 * 1)
x = requests.post('https://events.eu.pagerduty.com/v2/enqueue',
json=generate_body(blocks_missed=blocks_missed, text=text))
logger.info('Alert sent!')
LAST_ALERT = time.time()
return True
# Handel control + c
def kill_handler(signum, frame):
global RUNNING
_ = signum
_ = frame
logger.info('Closing the program...')
RUNNING = False
if __name__ == '__main__':
signal.signal(signal.SIGINT, kill_handler)
while RUNNING:
get_status()
# Wait at least 2 seconds for the next block
time.sleep(2)
raise (SystemExit(0))