syncer.py (3221B)
1 # standard imports 2 import os 3 import logging 4 5 # external imports 6 import chainlib.eth.cli 7 from chaind.setup import Environment 8 from chaind.filter import StateFilter 9 from chainlib.eth.block import block_latest 10 from hexathon import strip_0x 11 from chainsyncer.store.fs import SyncFsStore 12 from chainsyncer.driver.chain_interface import ChainInterfaceDriver 13 from chainsyncer.error import SyncDone 14 from chainlib.eth.cli.arg import ( 15 Arg, 16 ArgFlag, 17 process_args, 18 ) 19 from chainlib.eth.cli.config import ( 20 Config, 21 process_config, 22 ) 23 from chainsyncer.cli.arg import ( 24 apply_arg as apply_arg_sync, 25 apply_flag as apply_flag_sync, 26 ) 27 from chainsyncer.data import config_dir as chainsyncer_config_dir 28 from chaind.data import config_dir as chaind_config_dir 29 from chaind.cli.arg import ( 30 apply_arg, 31 apply_flag, 32 ) 33 from chainlib.eth.cli.log import process_log 34 from chaind.settings import ChaindSettings 35 from chaind.cli.config import process_config as process_config_local 36 from chainsyncer.cli.config import process_config as process_config_syncer 37 38 # local imports 39 from chaind.eth.cache import EthCacheTx 40 from chaind.eth.settings import ( 41 process_settings, 42 process_sync, 43 ) 44 45 46 logg = logging.getLogger() 47 48 script_dir = os.path.dirname(os.path.realpath(__file__)) 49 config_dir = os.path.join(script_dir, '..', 'data', 'config') 50 51 env = Environment(domain='eth', env=os.environ) 52 53 arg_flags = ArgFlag() 54 arg_flags = apply_flag_sync(arg_flags) 55 arg_flags = apply_flag(arg_flags) 56 57 arg = Arg(arg_flags) 58 arg = apply_arg_sync(arg) 59 arg = apply_arg(arg) 60 61 flags = arg_flags.STD_BASE | arg_flags.CHAIN_SPEC | arg_flags.PROVIDER | arg_flags.SEQ | arg_flags.STATE 62 flags = arg_flags.more(flags, arg_flags.SYNC_RANGE_EXT) 63 flags = arg_flags.more(flags, arg_flags.CHAIND_BASE) 64 65 argparser = chainlib.eth.cli.ArgumentParser() 66 argparser = process_args(argparser, arg, flags) 67 args = argparser.parse_args() 68 69 logg = process_log(args, logg) 70 71 config = Config() 72 config.add_schema_dir(chainsyncer_config_dir) 73 config.add_schema_dir(chaind_config_dir) 74 config = process_config(config, arg, args, flags) 75 config = process_config_local(config, arg, args, flags) 76 config = process_config_syncer(config, arg, args, flags) 77 config.add('eth', 'CHAIND_ENGINE', False) 78 config.add('sync', 'CHAIND_COMPONENT', False) 79 logg.debug('config loaded:\n{}'.format(config)) 80 81 settings = ChaindSettings(include_sync=True) 82 settings = process_settings(settings, config) 83 settings = process_sync(settings, config) 84 logg.debug('settings loaded:\n{}'.format(settings)) 85 86 87 def main(): 88 fltr = StateFilter(settings.get('CHAIN_SPEC'), settings.dir_for('queue'), EthCacheTx) 89 sync_store = SyncFsStore(settings.get('SESSION_DATA_PATH'), session_id=settings.get('SESSION_ID')) 90 sync_store.register(fltr) 91 92 logg.debug('session block offset {}'.format(settings.get('SYNCER_OFFSET'))) 93 94 drv = ChainInterfaceDriver(sync_store, settings.get('SYNCER_INTERFACE'), offset=settings.get('SYNCER_OFFSET'), target=settings.get('SYNCER_LIMIT')) 95 try: 96 drv.run(settings.get('CONN')) 97 except SyncDone as e: 98 logg.info('sync done: {}'.format(e)) 99 100 101 if __name__ == '__main__': 102 main()