Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a2317d253
|
||
|
|
6647e11df5
|
||
|
|
4e7c0f0d73
|
@@ -1,3 +1,8 @@
|
||||
* 0.3.7
|
||||
- Remove hard eth dependency in settings rendering
|
||||
- Add unlock cli tool
|
||||
* 0.3.6
|
||||
- Add cli arg processing and settings renderer
|
||||
* 0.3.5
|
||||
- Allow memory-only shep if factory set to None in store constructor
|
||||
* 0.3.4
|
||||
|
||||
@@ -1 +1 @@
|
||||
include *requirements.txt LICENSE.txt
|
||||
include *requirements.txt LICENSE.txt chainsyncer/data/config/*
|
||||
|
||||
12
chainsyncer/cli/__init__.py
Normal file
12
chainsyncer/cli/__init__.py
Normal file
@@ -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')
|
||||
14
chainsyncer/cli/arg.py
Normal file
14
chainsyncer/cli/arg.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# 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')
|
||||
|
||||
argparser.add_argument('--backend', type=str, help='Backend to use for state store')
|
||||
7
chainsyncer/cli/base.py
Normal file
7
chainsyncer/cli/base.py
Normal file
@@ -0,0 +1,7 @@
|
||||
# standard imports
|
||||
import enum
|
||||
|
||||
|
||||
class SyncFlag(enum.IntEnum):
|
||||
RANGE = 1
|
||||
HEAD = 2
|
||||
20
chainsyncer/cli/config.py
Normal file
20
chainsyncer/cli/config.py
Normal file
@@ -0,0 +1,20 @@
|
||||
# external imports
|
||||
from chainsyncer.cli import SyncFlag
|
||||
|
||||
|
||||
def process_config(config, args, flags):
|
||||
args_override = {}
|
||||
|
||||
args_override['SYNCER_BACKEND'] = getattr(args, 'backend')
|
||||
|
||||
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
|
||||
4
chainsyncer/data/config/syncer.ini
Normal file
4
chainsyncer/data/config/syncer.ini
Normal file
@@ -0,0 +1,4 @@
|
||||
[syncer]
|
||||
offset = 0
|
||||
limit = 0
|
||||
backend = mem
|
||||
1
chainsyncer/paths.py
Normal file
1
chainsyncer/paths.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
66
chainsyncer/runnable/lock.py
Normal file
66
chainsyncer/runnable/lock.py
Normal 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()
|
||||
59
chainsyncer/settings.py
Normal file
59
chainsyncer/settings.py
Normal file
@@ -0,0 +1,59 @@
|
||||
# standard imports
|
||||
import logging
|
||||
|
||||
# external imports
|
||||
from hexathon import (
|
||||
to_int as hex_to_int,
|
||||
strip_0x,
|
||||
)
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
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):
|
||||
o = self.o['SYNCER_INTERFACE'].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
|
||||
@@ -33,9 +33,13 @@ class SyncFsStore(SyncStore):
|
||||
factory = SimpleFileStoreFactory(base_sync_path, binary=True)
|
||||
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')
|
||||
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):
|
||||
|
||||
@@ -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.')
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[metadata]
|
||||
name = chainsyncer
|
||||
version = 0.3.5
|
||||
version = 0.3.7
|
||||
description = Generic blockchain syncer driver
|
||||
author = Louis Holbrook
|
||||
author_email = dev@holbrook.no
|
||||
@@ -28,6 +28,7 @@ packages =
|
||||
chainsyncer.driver
|
||||
chainsyncer.unittest
|
||||
chainsyncer.store
|
||||
chainsyncer.cli
|
||||
|
||||
#[options.package_data]
|
||||
#* =
|
||||
|
||||
Reference in New Issue
Block a user