From 4e7c0f0d73f9a399fa14ce0bb05f54debf1d4037 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 26 Apr 2022 19:07:34 +0000 Subject: [PATCH] Add settings renderer, cli flag and config handling --- chainsyncer/cli/__init__.py | 12 +++++++ chainsyncer/cli/arg.py | 12 +++++++ chainsyncer/cli/base.py | 7 ++++ chainsyncer/cli/config.py | 17 ++++++++++ chainsyncer/data/config/syncer.ini | 3 ++ chainsyncer/paths.py | 1 + chainsyncer/settings.py | 51 ++++++++++++++++++++++++++++++ chainsyncer/store/mem.py | 6 ++++ 8 files changed, 109 insertions(+) create mode 100644 chainsyncer/cli/__init__.py create mode 100644 chainsyncer/cli/arg.py create mode 100644 chainsyncer/cli/base.py create mode 100644 chainsyncer/cli/config.py create mode 100644 chainsyncer/data/config/syncer.ini create mode 100644 chainsyncer/paths.py create mode 100644 chainsyncer/settings.py diff --git a/chainsyncer/cli/__init__.py b/chainsyncer/cli/__init__.py new file mode 100644 index 0000000..0caa7c5 --- /dev/null +++ b/chainsyncer/cli/__init__.py @@ -0,0 +1,12 @@ +# standard imports +import os + +# local imports +from .base import * +from .arg import process_flags +from .config import process_config + + +__script_dir = os.path.dirname(os.path.realpath(__file__)) +data_dir = os.path.join(os.path.dirname(__script_dir), 'data') +config_dir = os.path.join(data_dir, 'config') diff --git a/chainsyncer/cli/arg.py b/chainsyncer/cli/arg.py new file mode 100644 index 0000000..ed367e9 --- /dev/null +++ b/chainsyncer/cli/arg.py @@ -0,0 +1,12 @@ +# local imports +from .base import SyncFlag + + +def process_flags(argparser, flags): + + if flags & SyncFlag.RANGE > 0: + argparser.add_argument('--offset', type=int, help='Block to start sync from. Default is start of history (0).') + argparser.add_argument('--until', type=int, default=-1, help='Block to stop sync on. Default is stop at block height of first run.') + if flags & SyncFlag.HEAD > 0: + argparser.add_argument('--head', action='store_true', help='Start from latest block as offset') + argparser.add_argument('--keep-alive', action='store_true', help='Do not stop syncing when caught up') diff --git a/chainsyncer/cli/base.py b/chainsyncer/cli/base.py new file mode 100644 index 0000000..0144132 --- /dev/null +++ b/chainsyncer/cli/base.py @@ -0,0 +1,7 @@ +# standard imports +import enum + + +class SyncFlag(enum.IntEnum): + RANGE = 1 + HEAD = 2 diff --git a/chainsyncer/cli/config.py b/chainsyncer/cli/config.py new file mode 100644 index 0000000..614561d --- /dev/null +++ b/chainsyncer/cli/config.py @@ -0,0 +1,17 @@ +# external imports +from chainsyncer.cli import SyncFlag + + +def process_config(config, args, flags): + args_override = {} + if flags & SyncFlag.RANGE: + args_override['SYNCER_OFFSET'] = getattr(args, 'offset') + args_override['SYNCER_LIMIT'] = getattr(args, 'until') + + config.dict_override(args_override, 'local cli args') + + if flags & SyncFlag.HEAD: + config.add(getattr(args, 'keep_alive'), '_KEEP_ALIVE') + config.add(getattr(args, 'head'), '_HEAD') + + return config diff --git a/chainsyncer/data/config/syncer.ini b/chainsyncer/data/config/syncer.ini new file mode 100644 index 0000000..3ce5021 --- /dev/null +++ b/chainsyncer/data/config/syncer.ini @@ -0,0 +1,3 @@ +[syncer] +offset = 0 +limit = 0 diff --git a/chainsyncer/paths.py b/chainsyncer/paths.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/chainsyncer/paths.py @@ -0,0 +1 @@ + diff --git a/chainsyncer/settings.py b/chainsyncer/settings.py new file mode 100644 index 0000000..d2439d7 --- /dev/null +++ b/chainsyncer/settings.py @@ -0,0 +1,51 @@ +# standard imports +import logging + +# external imports +from chainlib.eth.block import block_latest +from hexathon import ( + to_int as hex_to_int, + strip_0x, + ) + +logg = logging.getLogger(__name__) + + +class ChainsyncerSettings: + + 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 + until = 0 + + if config.true('_HEAD'): + self.o['SYNCER_OFFSET'] = block_offset + self.o['SYNCER_LIMIT'] = -1 + return + + session_block_offset = int(config.get('SYNCER_OFFSET')) + until = int(config.get('SYNCER_LIMIT')) + + 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 + elif until == -1: + keep_alive = True + + if session_block_offset == -1: + session_block_offset = block_offset + elif config.true('_KEEP_ALIVE'): + block_limit = -1 + else: + if block_limit == 0: + block_limit = block_offset + + self.o['SYNCER_OFFSET'] = session_block_offset + self.o['SYNCER_LIMIT'] = block_limit diff --git a/chainsyncer/store/mem.py b/chainsyncer/store/mem.py index 86c6ce2..c5090ef 100644 --- a/chainsyncer/store/mem.py +++ b/chainsyncer/store/mem.py @@ -32,3 +32,9 @@ class SyncMemStore(SyncStore): def get_target(self): return self.target + + + def stop(self, item): + if item != None: + super(SyncRocksDbStore, self).stop(item) + logg.info('I am an in-memory only state store. I am shutting down now, so all state will now be discarded.')