chaind-eth/chaind/eth/runnable/syncer.py

74 lines
2.3 KiB
Python
Raw Normal View History

# standard imports
import os
import logging
# external imports
import chainlib.cli
import chainsyncer.cli
2022-04-26 19:24:57 +02:00
import chaind.cli
from chaind.setup import Environment
from chaind.filter import StateFilter
from chaind.adapters.fs import ChaindFsAdapter
from chainlib.eth.block import block_latest
from hexathon import strip_0x
from chainsyncer.store.fs import SyncFsStore
from chainsyncer.driver.chain_interface import ChainInterfaceDriver
2022-04-26 19:24:57 +02:00
from chaind.eth.settings import ChaindEthSettings
# local imports
from chaind.eth.cache import EthCacheTx
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
script_dir = os.path.dirname(os.path.realpath(__file__))
config_dir = os.path.join(script_dir, '..', 'data', 'config')
env = Environment(domain='eth', env=os.environ)
arg_flags = chainlib.cli.argflag_std_base | chainlib.cli.Flag.CHAIN_SPEC
argparser = chainlib.cli.ArgumentParser(arg_flags)
local_arg_flags = chaind.cli.argflag_local_base
chaind.cli.process_flags(argparser, local_arg_flags)
sync_flags = chainsyncer.cli.SyncFlag.RANGE | chainsyncer.cli.SyncFlag.HEAD
chainsyncer.cli.process_flags(argparser, sync_flags)
args = argparser.parse_args()
base_config_dir = [
chainsyncer.cli.config_dir,
chaind.cli.config_dir,
]
config = chainlib.cli.Config.from_args(args, arg_flags, base_config_dir=base_config_dir)
config = chainsyncer.cli.process_config(config, args, sync_flags)
config = chaind.cli.process_config(config, args, local_arg_flags)
config.add('eth', 'CHAIND_ENGINE', False)
logg.debug('config loaded:\n{}'.format(config))
settings = ChaindEthSettings(include_sync=True)
2022-04-26 19:24:57 +02:00
settings.process(config)
2022-04-26 19:24:57 +02:00
logg.debug('settings:\n{}'.format(settings))
def main():
queue_adapter = ChaindFsAdapter(
2022-04-26 19:24:57 +02:00
settings.get('CHAIN_SPEC'),
settings.get('SESSION_DATA_DIR'),
EthCacheTx,
None,
)
fltr = StateFilter(queue_adapter)
2022-04-26 19:24:57 +02:00
sync_store = SyncFsStore(settings.get('SESSION_RUNTIME_DIR'), session_id=settings.get('SESSION_ID'))
sync_store.register(fltr)
2022-04-26 19:24:57 +02:00
logg.debug('session block offset {}'.format(settings.get('SYNCER_OFFSET')))
2022-04-26 19:24:57 +02:00
drv = ChainInterfaceDriver(sync_store, settings.get('SYNCER_INTERFACE'), offset=settings.get('SYNCER_OFFSET'), target=settings.get('SYNCER_LIMIT'))
drv.run(settings.get('RPC'))
if __name__ == '__main__':
main()