-
Notifications
You must be signed in to change notification settings - Fork 2
/
system_event_detector.py
416 lines (363 loc) · 16 KB
/
system_event_detector.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import json
import asyncio
import multiprocessing
import resource
import signal
import time
from signal import SIGINT
from signal import SIGQUIT
from signal import SIGTERM
import httpx
from eth_utils.address import to_checksum_address
from web3 import Web3
import sys
import os
from snapshotter.processor_distributor import ProcessorDistributor
from snapshotter.settings.config import settings
from snapshotter.utils.callback_helpers import send_telegram_notification_sync
from snapshotter.utils.default_logger import logger
from snapshotter.utils.exceptions import GenericExitOnSignal
from snapshotter.utils.file_utils import read_json_file
from snapshotter.utils.models.data_models import DailyTaskCompletedEvent
from snapshotter.utils.models.data_models import DayStartedEvent
from snapshotter.utils.models.data_models import EpochReleasedEvent
from snapshotter.utils.models.data_models import SnapshotterIssue
from snapshotter.utils.models.data_models import SnapshotterReportState
from snapshotter.utils.models.message_models import TelegramEpochProcessingReportMessage
from snapshotter.utils.rpc import get_event_sig_and_abi
from snapshotter.utils.rpc import RpcHelper
from urllib.parse import urljoin
from snapshotter.utils.models.data_models import SnapshotterPing
class EventDetectorProcess(multiprocessing.Process):
def __init__(self, name, **kwargs):
"""
Initializes the SystemEventDetector class.
Args:
name (str): The name of the process.
**kwargs: Additional keyword arguments to be passed to the multiprocessing.Process class.
Attributes:
_shutdown_initiated (bool): A flag indicating whether shutdown has been initiated.
_logger (logging.Logger): The logger instance.
_last_processed_block (None): The last processed block.
rpc_helper (RpcHelper): The RpcHelper instance.
contract_abi (dict): The contract ABI.
contract_address (str): The contract address.
contract (web3.eth.Contract): The contract instance.
event_sig (dict): The event signature.
event_abi (dict): The event ABI.
"""
multiprocessing.Process.__init__(self, name=name, **kwargs)
self._shutdown_initiated = False
self._logger = logger.bind(
module=name,
)
self._last_processed_block = None
self.rpc_helper = RpcHelper(rpc_settings=settings.anchor_chain_rpc)
self._source_rpc_helper = RpcHelper(rpc_settings=settings.rpc)
self.contract_abi = read_json_file(
settings.protocol_state.abi,
self._logger,
)
self._reporting_httpx_client = httpx.Client(
base_url=settings.reporting.service_url,
limits=httpx.Limits(
max_keepalive_connections=2,
max_connections=2,
keepalive_expiry=300,
),
)
self._telegram_httpx_client = httpx.Client(
base_url=settings.reporting.telegram_url,
limits=httpx.Limits(
max_keepalive_connections=2,
max_connections=2,
keepalive_expiry=300,
),
)
self.contract_address = settings.protocol_state.address
self.contract = self.rpc_helper.get_current_node()['web3_client'].eth.contract(
address=Web3.to_checksum_address(
self.contract_address,
),
abi=self.contract_abi,
)
self._last_reporting_service_ping = 0
self._last_reporting_message_sent = 0
# event EpochReleased(uint256 indexed epochId, uint256 begin, uint256 end, uint256 timestamp);
# event DayStartedEvent(uint256 dayId, uint256 timestamp);
# event DailyTaskCompletedEvent(address snapshotterAddress, uint256 slotId, uint256 dayId, uint256 timestamp);
EVENTS_ABI = {
'EpochReleased': self.contract.events.EpochReleased._get_event_abi(),
'DayStartedEvent': self.contract.events.DayStartedEvent._get_event_abi(),
'DailyTaskCompletedEvent': self.contract.events.DailyTaskCompletedEvent._get_event_abi(),
}
EVENT_SIGS = {
'EpochReleased': 'EpochReleased(address,uint256,uint256,uint256,uint256)',
'DayStartedEvent': 'DayStartedEvent(address,uint256,uint256)',
'DailyTaskCompletedEvent': 'DailyTaskCompletedEvent(address,address,uint256,uint256,uint256)',
}
self.event_sig, self.event_abi = get_event_sig_and_abi(
EVENT_SIGS,
EVENTS_ABI,
)
self.processor_distributor = ProcessorDistributor()
self._initialized = False
async def init(self):
self._logger.info('Initializing SystemEventDetector. Awaiting local collector initialization and bootstrapping for 60 seconds...')
await asyncio.sleep(15)
self._last_processed_block = await self._load_last_processed_block()
await self.processor_distributor.init()
if self._last_processed_block is None:
self._logger.info('Initializing SystemEventDetector. Awaiting local collector initialization and bootstrapping for 60 seconds...')
await asyncio.sleep(15)
await self._init_check_and_report()
await asyncio.sleep(15)
async def _init_check_and_report(self):
try:
self._logger.info('Checking and reporting snapshotter status')
current_block_number = await self._source_rpc_helper.get_current_block_number()
event = EpochReleasedEvent(
begin=current_block_number - 9,
end=current_block_number,
epochId=0,
timestamp=int(time.time()),
)
self._logger.info(
'Processing simulation event: {}', event,
)
await self.processor_distributor.process_event(
"EpochReleased", event,
)
except Exception as e:
self._logger.error(
'❌ Simulation event processing failed! Error: {}', e,
)
self._logger.info("Please check your config and if issue persists please reach out to the team!")
self._send_telegram_epoch_processing_notification(
error=e,
)
sys.exit(1)
async def get_events(self, from_block: int, to_block: int):
"""
Retrieves events from the blockchain for the given block range and returns them as a list of tuples.
Each tuple contains the event name and an object representing the event data.
Args:
from_block (int): The starting block number.
to_block (int): The ending block number.
Returns:
List[Tuple[str, Any]]: A list of tuples, where each tuple contains the event name
and an object representing the event data.
"""
if not self._initialized:
await self.init()
self._initialized = True
events_log = await self.rpc_helper.get_events_logs(
**{
'contract_address': self.contract_address,
'to_block': to_block,
'from_block': from_block,
'topics': [self.event_sig],
'event_abi': self.event_abi,
},
)
events = []
latest_epoch_id = - 1
for log in events_log:
if log.event == 'EpochReleased':
self._logger.info(f"EpochReleased: {log.args.dataMarketAddress}!")
if log.args.dataMarketAddress == settings.data_market:
event = EpochReleasedEvent(
begin=log.args.begin,
end=log.args.end,
epochId=log.args.epochId,
timestamp=log.args.timestamp,
)
latest_epoch_id = max(latest_epoch_id, log.args.epochId)
events.append((log.event, event))
elif log.event == 'DayStartedEvent':
event = DayStartedEvent(
dayId=log.args.dayId,
timestamp=log.args.timestamp,
)
events.append((log.event, event))
elif log.event == 'DailyTaskCompletedEvent':
if log.args.snapshotterAddress == to_checksum_address(settings.instance_id) and\
log.args.slotId == settings.slot_id:
event = DailyTaskCompletedEvent(
dayId=log.args.dayId,
timestamp=log.args.timestamp,
)
events.append((log.event, event))
self._logger.info('Events: {}', events)
return events
def _generic_exit_handler(self, signum, sigframe):
"""
Handles the generic exit signal and initiates shutdown.
Args:
signum (int): The signal number.
sigframe (object): The signal frame.
Raises:
GenericExitOnSignal: If the shutdown is initiated.
"""
if (
signum in [SIGINT, SIGTERM, SIGQUIT] and
not self._shutdown_initiated
):
self._shutdown_initiated = True
raise GenericExitOnSignal
async def _save_last_processed_block(self):
with open("last_processed_block.log", 'w') as f:
f.write(str(self._last_processed_block))
async def _load_last_processed_block(self):
# check if last_processed_block.log exists
if os.path.exists("last_processed_block.log"):
with open("last_processed_block.log", 'r') as f:
return int(f.read())
return None
async def _detect_events(self):
"""
Continuously detects events by fetching the current block and comparing it to the last processed block.
If the last processed block is too far behind the current block, it processes the current block.
"""
while True:
try:
if settings.reporting.service_url and int(time.time()) - self._last_reporting_service_ping >= 30:
self._last_reporting_service_ping = int(time.time())
try:
self._reporting_httpx_client.post(
url=urljoin(settings.reporting.service_url, '/ping'),
json=SnapshotterPing(instanceID=settings.instance_id, slotId=settings.slot_id).dict(),
)
except Exception as e:
if settings.logs.trace_enabled:
self._logger.opt(exception=True).error('Error while pinging reporting service: {}', e)
else:
self._logger.error(
'Error while pinging reporting service: {}', e,
)
else:
self._logger.info('Reporting service pinged successfully')
current_block = await self.rpc_helper.get_current_block_number()
self._logger.info('Current block: {}', current_block)
except Exception as e:
self._logger.opt(exception=True).error(
(
'Unable to fetch current block, ERROR: {}, '
'sleeping for {} seconds.'
),
e,
settings.rpc.polling_interval,
)
if int(time.time()) - self._last_reporting_message_sent >= 600:
self._last_reporting_message_sent = int(time.time())
self._send_telegram_epoch_processing_notification(
error=e,
)
await asyncio.sleep(settings.rpc.polling_interval)
continue
if not self._last_processed_block:
self._last_processed_block = current_block - 1
# save it to last_processed_block file
await self._save_last_processed_block()
if self._last_processed_block >= current_block:
self._logger.info(
'Last processed block is up to date, sleeping for {} seconds...',
settings.rpc.polling_interval,
)
await asyncio.sleep(settings.rpc.polling_interval)
continue
if current_block - self._last_processed_block >= 10:
self._logger.warning(
'Last processed block is too far behind current block, '
'processing current block',
)
self._last_processed_block = current_block - 10
await self._save_last_processed_block()
# Get events from current block to last_processed_block
try:
events = await self.get_events(self._last_processed_block + 1, current_block)
except Exception as e:
self._logger.opt(exception=True).error(
(
'Unable to fetch events from block {} to block {}, '
'ERROR: {}, sleeping for {} seconds.'
),
self._last_processed_block + 1,
current_block,
e,
settings.rpc.polling_interval,
)
if int(time.time()) - self._last_reporting_message_sent >= 600:
self._last_reporting_message_sent = int(time.time())
self._send_telegram_epoch_processing_notification(
error=e,
)
await asyncio.sleep(settings.rpc.polling_interval)
continue
for event_type, event in events:
self._logger.info(
'Processing event: {}', event,
)
asyncio.ensure_future(
self.processor_distributor.process_event(
event_type, event,
),
)
self._last_processed_block = current_block
await self._save_last_processed_block()
self._logger.info(
'DONE: Processed blocks till {}',
current_block,
)
self._logger.info(
'Sleeping for {} seconds...',
settings.rpc.polling_interval,
)
await asyncio.sleep(settings.rpc.polling_interval)
def _send_telegram_epoch_processing_notification(
self,
error: Exception,
):
"""
Sends a Telegram notification with the given message.
Args:
error (Exception): The error to report.
"""
telegram_message = TelegramEpochProcessingReportMessage(
chatId=settings.reporting.telegram_chat_id,
slotId=settings.slot_id,
issue=SnapshotterIssue(
instanceID=settings.instance_id,
issueType=SnapshotterReportState.UNHEALTHY_EPOCH_PROCESSING.value,
projectID='',
epochId='',
timeOfReporting=str(time.time()),
extra=json.dumps({'issueDetails': f'Error : {error}'}),
),
)
send_telegram_notification_sync(
client=self._telegram_httpx_client,
message=telegram_message,
)
def run(self):
"""
A class for detecting system events.
Methods:
--------
run()
Starts the event detection process.
"""
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(
resource.RLIMIT_NOFILE,
(settings.rlimit.file_descriptors, hard),
)
for signame in [signal.SIGINT, signal.SIGTERM, signal.SIGQUIT]:
signal.signal(signame, self._generic_exit_handler)
self.ev_loop = asyncio.get_event_loop()
self.ev_loop.run_until_complete(
self._detect_events(),
)
if __name__ == '__main__':
event_detector = EventDetectorProcess('EventDetector')
event_detector.run()