-
Notifications
You must be signed in to change notification settings - Fork 2
/
processor_distributor.py
458 lines (407 loc) · 18.1 KB
/
processor_distributor.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import asyncio
import json
import importlib
from collections import defaultdict
from typing import Union
from eth_utils.address import to_checksum_address
from httpx import AsyncClient
from httpx import AsyncHTTPTransport
from httpx import Limits
from httpx import Timeout
from web3 import Web3
import time
from snapshotter.settings.config import projects_config
from snapshotter.settings.config import settings
from snapshotter.settings.config import preloaders
from snapshotter.utils.data_utils import get_snapshot_submision_window
from snapshotter.utils.data_utils import get_source_chain_epoch_size
from snapshotter.utils.data_utils import get_source_chain_id
from snapshotter.utils.default_logger import logger
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 PreloaderResult
from snapshotter.utils.models.data_models import SnapshotFinalizedEvent
from snapshotter.utils.models.data_models import SnapshotterIssue
from snapshotter.utils.models.data_models import SnapshotterReportState
from snapshotter.utils.models.data_models import SnapshottersUpdatedEvent
from snapshotter.utils.models.message_models import EpochBase
from snapshotter.utils.models.message_models import SnapshotProcessMessage
from snapshotter.utils.models.message_models import TelegramSnapshotterReportMessage
from snapshotter.utils.rpc import RpcHelper
from snapshotter.utils.snapshot_worker import SnapshotAsyncWorker
from snapshotter.utils.callback_helpers import send_failure_notifications_async
from snapshotter.utils.callback_helpers import send_telegram_notification_async
class ProcessorDistributor:
_anchor_rpc_helper: RpcHelper
_reporting_httpx_client: AsyncClient
_telegram_httpx_client: AsyncClient
def __init__(self):
"""
Initialize the ProcessorDistributor object.
Args:
name (str): The name of the ProcessorDistributor.
**kwargs: Additional keyword arguments.
Attributes:
_rpc_helper: The RPC helper object.
_source_chain_id: The source chain ID.
_projects_list: The list of projects.
_initialized (bool): Flag indicating if the ProcessorDistributor has been initialized.
_upcoming_project_changes (defaultdict): Dictionary of upcoming project changes.
_project_type_config_mapping (dict): Dictionary mapping project types to their configurations.
"""
self._rpc_helper = None
self._source_chain_id = None
self._projects_list = None
self._initialized = False
self._upcoming_project_changes = defaultdict(list)
self._project_type_config_mapping = dict()
self._preloader_compute_mapping = dict()
self._all_preload_tasks = set()
for project_config in projects_config:
self._project_type_config_mapping[project_config.project_type] = project_config
for preload_task in project_config.preload_tasks:
self._all_preload_tasks.add(preload_task)
self._snapshotter_enabled = True
self._snapshotter_active = True
self.snapshot_worker = SnapshotAsyncWorker()
async def _init_rpc_helper(self):
"""
Initializes the RpcHelper instance if it is not already initialized.
"""
if not self._rpc_helper:
self._rpc_helper = RpcHelper()
self._anchor_rpc_helper = RpcHelper(rpc_settings=settings.anchor_chain_rpc)
async def _init_httpx_client(self):
"""
Initializes the HTTPX clients with the specified settings.
"""
transport_settings = dict(
limits=Limits(
max_connections=100,
max_keepalive_connections=50,
keepalive_expiry=None,
),
)
self._reporting_httpx_client = AsyncClient(
base_url=settings.reporting.service_url,
timeout=Timeout(timeout=5.0),
follow_redirects=False,
transport=AsyncHTTPTransport(**transport_settings),
)
self._telegram_httpx_client = AsyncClient(
base_url=settings.reporting.telegram_url,
timeout=Timeout(timeout=5.0),
follow_redirects=False,
transport=AsyncHTTPTransport(**transport_settings),
)
async def _init_preloader_compute_mapping(self):
"""
Initializes the preloader compute mapping by importing the preloader module and class and
adding it to the mapping dictionary.
"""
if self._preloader_compute_mapping:
return
for preloader in preloaders:
if preloader.task_type in self._all_preload_tasks:
preloader_module = importlib.import_module(preloader.module)
self._logger.debug('Imported preloader module: {}', preloader_module)
preloader_class = getattr(preloader_module, preloader.class_name)
self._preloader_compute_mapping[preloader.task_type] = preloader_class
self._logger.debug(
'Imported preloader class {} against preloader module {} for task type {}',
preloader_class,
preloader_module,
preloader.task_type,
)
async def init(self):
"""
Initializes the worker by initializing the RPC helper, loading project metadata.
"""
if not self._initialized:
self._logger = logger.bind(
module='ProcessDistributor',
)
self._anchor_rpc_helper = RpcHelper(
rpc_settings=settings.anchor_chain_rpc,
)
protocol_abi = read_json_file(settings.protocol_state.abi, self._logger)
self._protocol_state_contract = self._anchor_rpc_helper.get_current_node()['web3_client'].eth.contract(
address=to_checksum_address(
settings.protocol_state.address,
),
abi=protocol_abi,
)
try:
source_block_time = self._protocol_state_contract.functions.SOURCE_CHAIN_BLOCK_TIME(Web3.to_checksum_address(settings.data_market)).call()
except Exception as e:
self._logger.error(
'Exception in querying protocol state for source chain block time: {}',
e,
)
else:
self._source_chain_block_time = source_block_time / 10 ** 4
self._logger.debug('Set source chain block time to {}', self._source_chain_block_time)
try:
epoch_size = self._protocol_state_contract.functions.EPOCH_SIZE(Web3.to_checksum_address(settings.data_market)).call()
except Exception as e:
self._logger.error(
'Exception in querying protocol state for epoch size: {}',
e,
)
else:
self._epoch_size = epoch_size
try:
snapshotter_address = self._protocol_state_contract.functions.slotSnapshotterMapping(
settings.slot_id,
).call()
if snapshotter_address != to_checksum_address(settings.instance_id):
self._logger.error('Signer Account is not the one configured in slot, exiting!')
exit(0)
except Exception as e:
self._logger.error(
'Exception in querying protocol state for snapshotter status: {}',
e,
)
exit(0)
try:
self._current_day = self._protocol_state_contract.functions.dayCounter(Web3.to_checksum_address(settings.data_market)).call()
task_completion_status = self._protocol_state_contract.functions.checkSlotTaskStatusForDay(
Web3.to_checksum_address(settings.data_market),
settings.slot_id,
self._current_day,
).call()
if task_completion_status:
self._snapshotter_active = False
else:
self._snapshotter_active = True
except Exception as e:
self._logger.error(
'Exception in querying protocol state for user task status for day {}',
e,
)
self._snapshotter_active = False
self._logger.info('Snapshotter active: {}', self._snapshotter_active)
await self._init_httpx_client()
await self._init_rpc_helper()
await self._load_projects_metadata()
await self._init_preloader_compute_mapping()
await self.snapshot_worker.init_worker()
self._initialized = True
async def _load_projects_metadata(self):
"""
Loads the metadata for the projects, including the source chain ID, the list of projects, and the submission window
for snapshots. It also updates the project type configuration mapping with the relevant projects.
"""
if not self._projects_list:
with open(settings.protocol_state.abi, 'r') as f:
abi_dict = json.load(f)
protocol_state_contract = self._anchor_rpc_helper.get_current_node()['web3_client'].eth.contract(
address=Web3.to_checksum_address(
settings.protocol_state.address,
),
abi=abi_dict,
)
self._source_chain_epoch_size = await get_source_chain_epoch_size(
rpc_helper=self._anchor_rpc_helper,
state_contract_obj=protocol_state_contract,
data_market=Web3.to_checksum_address(settings.data_market),
)
self._source_chain_id = await get_source_chain_id(
rpc_helper=self._anchor_rpc_helper,
state_contract_obj=protocol_state_contract,
data_market=Web3.to_checksum_address(settings.data_market),
)
submission_window = await get_snapshot_submision_window(
rpc_helper=self._anchor_rpc_helper,
state_contract_obj=protocol_state_contract,
data_market=Web3.to_checksum_address(settings.data_market),
)
self._submission_window = submission_window
async def _epoch_release_processor(self, message: EpochReleasedEvent):
"""
This method is called when an epoch is released. It starts the snapshotting process for the epoch.
Args:
message (EpochBase): The message containing the epoch information.
"""
epoch = EpochBase(
begin=message.begin,
end=message.end,
epochId=message.epochId,
day=self._current_day,
)
preloader_tasks = {}
preloader_results_dict = {}
failed_preloaders = set()
# Determine which preloaders are needed across all projects
project_required_preloaders = set()
for project_config in self._project_type_config_mapping.values():
project_required_preloaders.update(project_config.preload_tasks)
for preloader_task in project_required_preloaders:
preloader_class = self._preloader_compute_mapping[preloader_task]
preloader_obj = preloader_class()
preloader_compute_kwargs = dict(
epoch=epoch,
rpc_helper=self._rpc_helper,
)
self._logger.debug(
'Starting preloader obj {} for epoch {}',
preloader_task,
epoch.epochId,
)
preloader_tasks[preloader_task] = asyncio.create_task(
preloader_obj.compute(**preloader_compute_kwargs)
)
await asyncio.gather(
*preloader_tasks.values(),
return_exceptions=True,
)
for preloader_task, task in preloader_tasks.items():
try:
result = task.result()
if isinstance(result, PreloaderResult):
preloader_results_dict[preloader_task] = result.result
else:
raise ValueError(
f"Unexpected result from preloader {preloader_task}: {result}"
)
except Exception as e:
self._logger.error(
'Exception in preloader {} for epoch {}: {}',
preloader_task,
epoch.epochId,
e,
)
failed_preloaders.add(preloader_task)
# Distribute results to each project based on its requirements
for project_type, project_config in self._project_type_config_mapping.items():
# Check if all required preloaders for this project succeeded
project_required_preloaders = set(project_config.preload_tasks)
if not failed_preloaders.intersection(project_required_preloaders):
project_preloader_results = {
task: preloader_results_dict[task]
for task in project_required_preloaders
if task in preloader_results_dict
}
asyncio.ensure_future(
self._distribute_callbacks_snapshotting(
project_type, epoch, project_preloader_results,
)
)
else:
self._logger.warning(
'Skipping project type {} for epoch {} due to failed preloader tasks: {}',
project_type,
epoch.epochId,
failed_preloaders.intersection(project_required_preloaders)
)
if failed_preloaders:
self._logger.warning(
'Some preloader tasks failed for epoch {}: {}',
epoch.epochId,
failed_preloaders
)
await send_telegram_notification_async(
client=self._telegram_httpx_client,
message=TelegramSnapshotterReportMessage(
chatId=settings.reporting.telegram_chat_id,
slotId=settings.slot_id,
issue=SnapshotterIssue(
instanceID=settings.instance_id,
issueType=SnapshotterReportState.MISSED_SNAPSHOT.value,
epochId=str(epoch.epochId),
timeOfReporting=str(time.time()),
projectID=project_type,
extra=json.dumps({'issueDetails': f'Failed preloaders: {failed_preloaders}'}),
),
status=self.snapshot_worker.status,
),
)
async def _distribute_callbacks_snapshotting(self, project_type: str, epoch: EpochBase, preloader_results: dict):
"""
Distributes callbacks for snapshotting to the appropriate snapshotters based on the project type and epoch.
Args:
project_type (str): The type of project.
epoch (EpochBase): The epoch to snapshot.
Returns:
None
"""
process_unit = SnapshotProcessMessage(
begin=epoch.begin,
end=epoch.end,
epochId=epoch.epochId,
day=epoch.day,
)
asyncio.ensure_future(
self.snapshot_worker.process_task(process_unit, project_type, preloader_results),
)
async def process_event(
self, type_: str, event: Union[
EpochReleasedEvent,
SnapshotFinalizedEvent,
SnapshottersUpdatedEvent,
DayStartedEvent,
DailyTaskCompletedEvent,
],
):
"""
Process an event based on its type.
Args:
type_ (str): The type of the event.
event (Union[EpochReleasedEvent, SnapshotFinalizedEvent, SnapshottersUpdatedEvent,
DayStartedEvent, DailyTaskCompletedEvent]): The event object.
Returns:
None
"""
if type_ == 'EpochReleased':
return await self._epoch_release_processor(event)
elif type_ == 'DayStartedEvent':
self._logger.info('Day started event received, setting active status to True')
self._snapshotter_active = True
self._current_day += 1
elif type_ == 'DailyTaskCompletedEvent':
self._logger.info('Daily task completed event received, setting active status to False')
self._snapshotter_active = False
else:
self._logger.error(
(
'Unknown message type {} received'
),
type_,
)
async def _send_failure_notifications(
self,
error: Exception,
epoch_id: str,
project_id: str,
):
notification_message = SnapshotterIssue(
instanceID=settings.instance_id,
issueType=SnapshotterReportState.MISSED_SNAPSHOT.value,
projectID=project_id,
epochId=str(epoch_id),
timeOfReporting=str(time.time()),
extra=json.dumps({'issueDetails': f'Error : {error}'}),
)
telegram_message = TelegramSnapshotterReportMessage(
chatId=settings.reporting.telegram_chat_id,
slotId=settings.slot_id,
issue=notification_message,
status=self.snapshot_worker.status,
)
tasks = [
asyncio.create_task(
send_failure_notifications_async(
client=self._reporting_httpx_client,
message=notification_message,
),
),
asyncio.create_task(
send_telegram_notification_async(
client=self._telegram_httpx_client,
message=telegram_message,
),
),
]
await asyncio.gather(*tasks)