chainsyncer

Blockchain syncer driver
Log | Files | Refs | LICENSE

commit 4e7c0f0d73f9a399fa14ce0bb05f54debf1d4037
parent b5617fc9fb6f738c2eecf78de31a0ac5ae428933
Author: lash <dev@holbrook.no>
Date:   Tue, 26 Apr 2022 19:07:34 +0000

Add settings renderer, cli flag and config handling

Diffstat:
Achainsyncer/cli/__init__.py | 12++++++++++++
Achainsyncer/cli/arg.py | 12++++++++++++
Achainsyncer/cli/base.py | 7+++++++
Achainsyncer/cli/config.py | 17+++++++++++++++++
Achainsyncer/data/config/syncer.ini | 3+++
Achainsyncer/paths.py | 1+
Achainsyncer/settings.py | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mchainsyncer/store/mem.py | 6++++++
8 files changed, 109 insertions(+), 0 deletions(-)

diff --git a/chainsyncer/cli/__init__.py 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 @@ -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 @@ -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 @@ -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 @@ -0,0 +1,3 @@ +[syncer] +offset = 0 +limit = 0 diff --git a/chainsyncer/paths.py b/chainsyncer/paths.py @@ -0,0 +1 @@ + diff --git a/chainsyncer/settings.py 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 @@ -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.')