sync.py (3440B)
1 # standard imports 2 import sys 3 import signal 4 import argparse 5 import confini 6 import logging 7 import os 8 import importlib 9 import uuid 10 import datetime 11 12 # external imports 13 import chainlib.cli 14 import chainsyncer.cli 15 from chainlib.chain import ChainSpec 16 from chainlib.eth.connection import EthHTTPConnection 17 from chainlib.eth.block import block_latest 18 from hexathon import ( 19 strip_0x, 20 add_0x, 21 ) 22 from chainsyncer.cli.arg import ( 23 apply_arg as apply_arg_sync, 24 apply_flag as apply_flag_sync, 25 ) 26 from chainsyncer.cli.config import process_config as process_config_sync 27 from chainsyncer.driver.chain_interface import ChainInterfaceDriver 28 from chainsyncer.error import SyncDone 29 from chainsyncer.data import config_dir as chainsyncer_config_dir 30 from chainlib.settings import ChainSettings 31 from chainlib.eth.settings import process_settings 32 from chainlib.eth.cli.arg import ( 33 Arg, 34 ArgFlag, 35 process_args, 36 ) 37 from chainlib.eth.cli.config import ( 38 Config, 39 process_config, 40 ) 41 42 # local imports 43 from eth_monitor.callback import ( 44 pre_callback, 45 post_callback, 46 ) 47 import eth_monitor.cli 48 from eth_monitor.cli.log import process_log 49 from eth_monitor.settings import process_settings as process_settings_local 50 from eth_monitor.run import cleanup 51 52 logg = logging.getLogger() 53 54 script_dir = os.path.realpath(os.path.dirname(__file__)) 55 config_dir = os.path.join(script_dir, '..', 'data', 'config') 56 57 arg_flags = ArgFlag() 58 arg_flags = apply_flag_sync(arg_flags) 59 60 arg = Arg(arg_flags) 61 arg = apply_arg_sync(arg) 62 63 64 flags = arg_flags.STD_BASE_READ | arg_flags.PROVIDER | arg_flags.CHAIN_SPEC | arg_flags.VERYVERBOSE | arg_flags.SYNC_RANGE_EXT | arg_flags.STATE 65 argparser = chainlib.eth.cli.ArgumentParser() 66 argparser = process_args(argparser, arg, flags) 67 argparser.add_argument('--list-backends', dest='list_backends', action='store_true', help='List built-in store backends') 68 eth_monitor.cli.process_args(argparser, arg, flags) 69 70 args = argparser.parse_args(sys.argv[1:]) 71 72 if args.list_backends: 73 for v in [ 74 'fs', 75 'rocksdb', 76 'mem', 77 ]: 78 print(v) 79 sys.exit(0) 80 81 logg = process_log(args, logg) 82 83 config = Config() 84 config.add_schema_dir(config_dir) 85 config.add_schema_dir(chainsyncer_config_dir) 86 config = process_config(config, arg, args, flags) 87 config = process_config_sync(config, arg, args, flags) 88 config = eth_monitor.cli.process_config(config, arg, args, flags) 89 logg.debug('loaded config:\n{}'.format(config)) 90 91 settings = ChainSettings() 92 settings = process_settings(settings, config) 93 settings = process_settings_local(settings, config) 94 logg.debug('loaded settings:\n{}'.format(settings)) 95 96 97 def main(): 98 logg.info('session is {}'.format(settings.get('SESSION_ID'))) 99 100 drv = ChainInterfaceDriver( 101 settings.get('SYNC_STORE'), 102 settings.get('SYNCER_INTERFACE'), 103 offset=settings.get('SYNCER_OFFSET'), 104 target=settings.get('SYNCER_LIMIT'), 105 pre_callback=pre_callback, 106 post_callback=post_callback, 107 block_callback=settings.get('BLOCK_HANDLER').filter, 108 ) 109 110 try: 111 r = drv.run(settings.get('CONN'), ctx=settings.get('SYNCER_CONTEXT')) 112 except SyncDone as e: 113 sys.stderr.write("sync {} done at block {}\n".format(drv, e)) 114 115 cleanup(settings) 116 117 118 if __name__ == '__main__': 119 main()