2022-04-24 13:03:00 +02:00
|
|
|
|
# standard imports
|
|
|
|
|
import logging
|
|
|
|
|
import importlib
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
|
from chainlib.eth.block import block_latest
|
|
|
|
|
from hexathon import (
|
|
|
|
|
to_int as hex_to_int,
|
|
|
|
|
strip_0x,
|
|
|
|
|
)
|
2022-04-24 17:03:21 +02:00
|
|
|
|
from cic_base import CICSettings
|
|
|
|
|
from cic_sync_filter.callback import CallbackFilter
|
2022-04-24 13:03:00 +02:00
|
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
|
from cic_tracker.chain import EthChainInterface
|
|
|
|
|
from cic_tracker.callback import (
|
|
|
|
|
state_change_callback,
|
|
|
|
|
filter_change_callback,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2022-04-24 17:03:21 +02:00
|
|
|
|
class CICTrackerSettings(CICSettings):
|
2022-04-24 13:03:00 +02:00
|
|
|
|
|
|
|
|
|
def process_sync_interface(self, config):
|
2022-04-24 21:28:55 +02:00
|
|
|
|
self.o['SYNCER_INTERFACE'] = EthChainInterface()
|
2022-04-24 13:03:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_sync_range(self, config):
|
|
|
|
|
o = block_latest()
|
|
|
|
|
r = self.o['RPC'].do(o)
|
|
|
|
|
block_offset = int(strip_0x(r), 16) + 1
|
|
|
|
|
logg.info('network block height at startup is {}'.format(block_offset))
|
|
|
|
|
|
|
|
|
|
keep_alive = False
|
|
|
|
|
session_block_offset = 0
|
|
|
|
|
block_limit = 0
|
2022-04-24 21:28:55 +02:00
|
|
|
|
session_block_offset = int(config.get('SYNCER_OFFSET'))
|
2022-04-24 13:03:00 +02:00
|
|
|
|
|
|
|
|
|
until = int(config.get('_UNTIL'))
|
|
|
|
|
if until > 0:
|
|
|
|
|
if until <= session_block_offset:
|
|
|
|
|
raise ValueError('sync termination block number must be later than offset ({} >= {})'.format(session_block_offset, until))
|
|
|
|
|
block_limit = until
|
|
|
|
|
else:
|
|
|
|
|
keep_alive=True
|
|
|
|
|
block_limit = -1
|
|
|
|
|
|
|
|
|
|
if session_block_offset == -1:
|
|
|
|
|
session_block_offset = block_offset
|
|
|
|
|
elif not config.true('_KEEP_ALIVE'):
|
|
|
|
|
if block_limit == 0:
|
|
|
|
|
lock_limit = block_offset
|
|
|
|
|
|
2022-04-24 21:28:55 +02:00
|
|
|
|
self.o['SYNCER_OFFSET'] = session_block_offset
|
|
|
|
|
self.o['SYNCER_LIMIT'] = block_limit
|
2022-04-24 13:03:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_sync_store(self, config):
|
|
|
|
|
syncer_store_module = None
|
|
|
|
|
syncer_store_class = None
|
2022-04-24 21:28:55 +02:00
|
|
|
|
if config.get('SYNCER_BACKEND') == 'fs':
|
2022-04-24 13:03:00 +02:00
|
|
|
|
syncer_store_module = importlib.import_module('chainsyncer.store.fs')
|
|
|
|
|
syncer_store_class = getattr(syncer_store_module, 'SyncFsStore')
|
2022-04-24 21:28:55 +02:00
|
|
|
|
elif config.get('SYNCER_BACKEND') == 'rocksdb':
|
2022-04-24 13:03:00 +02:00
|
|
|
|
syncer_store_module = importlib.import_module('chainsyncer.store.rocksdb')
|
|
|
|
|
syncer_store_class = getattr(syncer_store_module, 'SyncRocksDbStore')
|
|
|
|
|
else:
|
2022-04-24 21:28:55 +02:00
|
|
|
|
syncer_store_module = importlib.import_module(config.get('SYNCER_BACKEND'))
|
2022-04-24 13:03:00 +02:00
|
|
|
|
syncer_store_class = getattr(syncer_store_module, 'SyncStore')
|
|
|
|
|
|
2022-04-24 21:28:55 +02:00
|
|
|
|
logg.info('using engine {} module {}.{}'.format(config.get('SYNCER_BACKEND'), syncer_store_module.__file__, syncer_store_class.__name__))
|
2022-04-24 13:03:00 +02:00
|
|
|
|
|
2022-04-24 21:28:55 +02:00
|
|
|
|
state_dir = os.path.join(config.get('SYNCER_DIR'), config.get('SYNCER_BACKEND'))
|
|
|
|
|
sync_store = syncer_store_class(state_dir, session_id=config.get('SYNCER_SESSION_ID'), state_event_callback=state_change_callback, filter_state_event_callback=filter_change_callback)
|
2022-04-24 13:03:00 +02:00
|
|
|
|
logg.info('sync session is {}'.format(sync_store.session_id))
|
|
|
|
|
|
2022-04-24 21:28:55 +02:00
|
|
|
|
self.o['SYNCER_STORE'] = sync_store
|
2022-04-24 13:03:00 +02:00
|
|
|
|
|
|
|
|
|
|
2022-04-25 08:02:29 +02:00
|
|
|
|
def __create_callback_filter(self, config, path):
|
|
|
|
|
callbacks = []
|
|
|
|
|
for cb in config.get('TASKS_TRANSFER_CALLBACKS', '').split(','):
|
|
|
|
|
task_split = cb.split(':')
|
|
|
|
|
task_name = task_split[0]
|
|
|
|
|
task_queue = config.get('CELERY_QUEUE')
|
|
|
|
|
if len(task_split) > 1:
|
|
|
|
|
task_queue = task_split[1]
|
|
|
|
|
|
|
|
|
|
r = self.__create_filter(config, path, 'CallbackFilter')
|
|
|
|
|
r.set_method(task_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __create_filter(self, config, path, cls):
|
|
|
|
|
m = importlib.import_module(path)
|
|
|
|
|
o = getattr(m, cls)
|
|
|
|
|
r = o(self.o['CHAIN_SPEC'], self.o['CIC_REGISTRY'], config.get('CELERY_QUEUE'))
|
|
|
|
|
self.o['SYNCER_STORE'].register(r)
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
2022-04-24 13:03:00 +02:00
|
|
|
|
def process_sync_filters(self, config):
|
2022-04-24 21:28:55 +02:00
|
|
|
|
for v in config.get('SYNCER_FILTER').split(','):
|
2022-04-24 13:03:00 +02:00
|
|
|
|
logg.debug('processing filter {}'.format(v))
|
|
|
|
|
(path, cls) = v.rsplit('.', maxsplit=1)
|
2022-04-25 08:02:29 +02:00
|
|
|
|
if cls == 'CallbackFilter':
|
|
|
|
|
self.__create_callback_filter(config, path)
|
|
|
|
|
else:
|
|
|
|
|
self.__create_filter(config, path, cls)
|
2022-04-24 13:03:00 +02:00
|
|
|
|
|
|
|
|
|
|
2022-04-24 17:03:21 +02:00
|
|
|
|
def process(self, config):
|
|
|
|
|
super(CICTrackerSettings, self).process(config)
|
2022-04-24 13:03:00 +02:00
|
|
|
|
self.process_sync_range(config)
|
|
|
|
|
self.process_sync_interface(config)
|
|
|
|
|
self.process_sync_store(config)
|
|
|
|
|
self.process_sync_filters(config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
ks = list(self.o.keys())
|
|
|
|
|
ks.sort()
|
|
|
|
|
s = ''
|
|
|
|
|
for k in ks:
|
|
|
|
|
s += '{}: {}\n'.format(k, self.o.get(k))
|
|
|
|
|
return s
|