WIP add lock cli tool

This commit is contained in:
lash 2022-04-27 09:43:42 +00:00
parent 6647e11df5
commit 3a2317d253
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
9 changed files with 93 additions and 5 deletions

View File

@ -1,3 +1,6 @@
* 0.3.7
- Remove hard eth dependency in settings rendering
- Add unlock cli tool
* 0.3.6 * 0.3.6
- Add cli arg processing and settings renderer - Add cli arg processing and settings renderer
* 0.3.5 * 0.3.5

View File

@ -1 +1 @@
include *requirements.txt LICENSE.txt include *requirements.txt LICENSE.txt chainsyncer/data/config/*

View File

@ -10,3 +10,5 @@ def process_flags(argparser, flags):
if flags & SyncFlag.HEAD > 0: if flags & SyncFlag.HEAD > 0:
argparser.add_argument('--head', action='store_true', help='Start from latest block as offset') 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') argparser.add_argument('--keep-alive', action='store_true', help='Do not stop syncing when caught up')
argparser.add_argument('--backend', type=str, help='Backend to use for state store')

View File

@ -4,6 +4,9 @@ from chainsyncer.cli import SyncFlag
def process_config(config, args, flags): def process_config(config, args, flags):
args_override = {} args_override = {}
args_override['SYNCER_BACKEND'] = getattr(args, 'backend')
if flags & SyncFlag.RANGE: if flags & SyncFlag.RANGE:
args_override['SYNCER_OFFSET'] = getattr(args, 'offset') args_override['SYNCER_OFFSET'] = getattr(args, 'offset')
args_override['SYNCER_LIMIT'] = getattr(args, 'until') args_override['SYNCER_LIMIT'] = getattr(args, 'until')

View File

@ -1,3 +1,4 @@
[syncer] [syncer]
offset = 0 offset = 0
limit = 0 limit = 0
backend = mem

View File

@ -0,0 +1,66 @@
# SPDX-License-Identifier: GPL-3.0-or-later
# standard imports
import os
import logging
import sys
import importlib
# external imports
import chainlib.cli
# local imports
import chainsyncer.cli
from chainsyncer.settings import ChainsyncerSettings
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
arg_flags = chainlib.cli.argflag_std_base | chainlib.cli.Flag.CHAIN_SPEC
argparser = chainlib.cli.ArgumentParser(arg_flags)
argparser.add_argument('--state-dir', type=str, dest='state_dir', help='State directory')
argparser.add_argument('--session-id', type=str, dest='session_id', help='Session id for state')
argparser.add_argument('--action', type=str, choices=['repeat', 'continue'], help='Action to take on lock. Repeat means re-run the locked filter. Continue means resume execution for next filter.')
argparser.add_positional('tx', type=str, help='Transaction hash to unlock')
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,
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.add(args.state_dir, '_STATE_DIR', False)
config.add(args.session_id, '_SESSION_ID', False)
logg.debug('config loaded:\n{}'.format(config))
settings = ChainsyncerSettings()
settings.process_sync_backend(config)
def main():
state_dir = None
if settings.get('SYNCER_BACKEND') == 'mem':
raise ValueError('cannot unlock volatile state store')
if settings.get('SYNCER_BACKEND') == 'fs':
syncer_store_module = importlib.import_module('chainsyncer.store.fs')
syncer_store_class = getattr(syncer_store_module, 'SyncFsStore')
elif settings.get('SYNCER_BACKEND') == 'rocksdb':
syncer_store_module = importlib.import_module('chainsyncer.store.rocksdb')
syncer_store_class = getattr(syncer_store_module, 'SyncRocksDbStore')
else:
syncer_store_module = importlib.import_module(settings.get('SYNCER_BACKEND'))
syncer_store_class = getattr(syncer_store_module, 'SyncStore')
state_dir = os.path.join(config.get('_STATE_DIR'), settings.get('SYNCER_BACKEND'))
sync_path = os.path.join(config.get('_SESSION_ID'), 'sync', 'filter')
sync_store = syncer_store_class(state_dir, session_id=sync_path)
logg.info('session is {}'.format(sync_store.session_id))
if __name__ == '__main__':
main()

View File

@ -2,7 +2,6 @@
import logging import logging
# external imports # external imports
from chainlib.eth.block import block_latest
from hexathon import ( from hexathon import (
to_int as hex_to_int, to_int as hex_to_int,
strip_0x, strip_0x,
@ -13,8 +12,17 @@ logg = logging.getLogger(__name__)
class ChainsyncerSettings: class ChainsyncerSettings:
def __init__(self):
self.o = {}
self.get = self.o.get
def process_sync_backend(self, config):
self.o['SYNCER_BACKEND'] = config.get('SYNCER_BACKEND')
def process_sync_range(self, config): def process_sync_range(self, config):
o = block_latest() o = self.o['SYNCER_INTERFACE'].block_latest()
r = self.o['RPC'].do(o) r = self.o['RPC'].do(o)
block_offset = int(strip_0x(r), 16) + 1 block_offset = int(strip_0x(r), 16) + 1
logg.info('network block height at startup is {}'.format(block_offset)) logg.info('network block height at startup is {}'.format(block_offset))

View File

@ -33,9 +33,13 @@ class SyncFsStore(SyncStore):
factory = SimpleFileStoreFactory(base_sync_path, binary=True) factory = SimpleFileStoreFactory(base_sync_path, binary=True)
self.setup_sync_state(factory, state_event_callback) self.setup_sync_state(factory, state_event_callback)
self.setup_filter_state(callback=filter_state_event_callback)
def setup_filter_state(self, callback=None):
base_filter_path = os.path.join(self.session_path, 'filter') base_filter_path = os.path.join(self.session_path, 'filter')
factory = SimpleFileStoreFactory(base_filter_path, binary=True) factory = SimpleFileStoreFactory(base_filter_path, binary=True)
self.setup_filter_state(factory, filter_state_event_callback) super(SyncFsStore, self).setup_filter_state(factory, callback)
def __create_path(self, base_path, default_path, session_id=None): def __create_path(self, base_path, default_path, session_id=None):

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name = chainsyncer name = chainsyncer
version = 0.3.6 version = 0.3.7
description = Generic blockchain syncer driver description = Generic blockchain syncer driver
author = Louis Holbrook author = Louis Holbrook
author_email = dev@holbrook.no author_email = dev@holbrook.no
@ -28,6 +28,7 @@ packages =
chainsyncer.driver chainsyncer.driver
chainsyncer.unittest chainsyncer.unittest
chainsyncer.store chainsyncer.store
chainsyncer.cli
#[options.package_data] #[options.package_data]
#* = #* =