commit b19b948be32760d2467cee217d7eda2b4f89f569
parent f96134565257adda575d0f332e80e45eb6ddd77d
Author: lash <dev@holbrook.no>
Date: Tue, 26 Apr 2022 17:24:57 +0000
implement settings renderer
Diffstat:
2 files changed, 34 insertions(+), 73 deletions(-)
diff --git a/chaind/eth/runnable/syncer.py b/chaind/eth/runnable/syncer.py
@@ -3,20 +3,18 @@ import os
import logging
# external imports
-import chainlib.eth.cli
+import chaind.cli
from chaind.setup import Environment
from chaind.filter import StateFilter
from chaind.adapters.fs import ChaindFsAdapter
from chainlib.eth.block import block_latest
-from chainlib.eth.connection import EthHTTPConnection
-from chainlib.chain import ChainSpec
from hexathon import strip_0x
from chainsyncer.store.fs import SyncFsStore
from chainsyncer.driver.chain_interface import ChainInterfaceDriver
+from chaind.eth.settings import ChaindEthSettings
# local imports
from chaind.eth.cache import EthCacheTx
-from chaind.eth.chain import EthChainInterface
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
@@ -26,91 +24,36 @@ config_dir = os.path.join(script_dir, '..', 'data', 'config')
env = Environment(domain='eth', env=os.environ)
-arg_flags = chainlib.eth.cli.argflag_std_read
-argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
-argparser.add_argument('--data-dir', type=str, help='data directory')
-argparser.add_argument('--runtime-dir', type=str, help='runtime directory')
-argparser.add_argument('--session-id', dest='session_id', type=str, help='session identifier')
-argparser.add_argument('--dispatch-delay', dest='dispatch_delay', type=float, help='socket timeout before processing queue')
-argparser.add_argument('--offset', type=int, default=0, help='Start sync on this block')
-argparser.add_argument('--until', type=int, default=0, help='Terminate sync on this block')
-argparser.add_argument('--head', action='store_true', help='Start at current block height (overrides --offset, assumes --keep-alive)')
-argparser.add_argument('--keep-alive', action='store_true', dest='keep_alive', help='Continue to sync head after history sync complete')
+arg_flags = chaind.cli.argflag_local_sync
+argparser = chaind.cli.ArgumentParser(arg_flags)
+local_arg_flags = chaind.cli.argflag_local_sync
+argparser.process_local_flags(local_arg_flags)
args = argparser.parse_args()
-extra_args = {
- 'runtime_dir': 'SESSION_RUNTIME_DIR',
- 'data_dir': 'SESSION_DATA_DIR',
- 'session_id': 'SESSION_ID',
- 'dispatch_delay': 'SESSION_DISPATCH_DELAY',
- }
-config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, base_config_dir=config_dir)
-
-logg.debug('session id {} {}'.format(type(config.get('SESSION_ID')), config.get('SESSION_ID')))
-if config.get('SESSION_ID') == None:
- config.add(env.session, 'SESSION_ID', exists_ok=True)
-if config.get('SESSION_RUNTIME_DIR') == None:
- config.add(env.runtime_dir, 'SESSION_RUNTIME_DIR', exists_ok=True)
-if config.get('SESSION_DATA_DIR') == None:
- config.add(env.data_dir, 'SESSION_DATA_DIR', exists_ok=True)
-if not config.get('SESSION_SOCKET_PATH'):
- socket_path = os.path.join(config.get('SESSION_RUNTIME_DIR'), config.get('SESSION_ID'), 'chaind.sock')
- config.add(socket_path, 'SESSION_SOCKET_PATH', True)
-config.add(args.keep_alive, '_KEEP_ALIVE', True)
-config.add(args.head, '_HEAD', True)
-config.add(args.offset, '_SYNC_OFFSET', True)
+config = chaind.cli.Config.from_args('eth', args, arg_flags, local_arg_flags)
logg.debug('config loaded:\n{}'.format(config))
-chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
+settings = ChaindEthSettings()
+settings.process(config)
-conn = EthHTTPConnection(url=config.get('RPC_PROVIDER'), chain_spec=chain_spec)
+logg.debug('settings:\n{}'.format(settings))
def main():
- o = block_latest()
- r = conn.do(o)
- block_offset = int(strip_0x(r), 16) + 1
- logg.info('network block height is {}'.format(block_offset))
-
- keep_alive = False
- session_block_offset = 0
- block_limit = 0
- if args.head:
- session_block_offset = block_offset
- block_limit = -1
- keep_alive = True
- else:
- session_block_offset = args.offset
-
- if args.until > 0:
- if not args.head and args.until <= session_block_offset:
- raise ValueError('sync termination block number must be later than offset ({} >= {})'.format(session_block_offset, args.until))
- block_limit = args.until
- elif config.true('_KEEP_ALIVE'):
- keep_alive=True
- block_limit = -1
-
- if session_block_offset == -1:
- session_block_offset = block_offset
- elif not config.true('_KEEP_ALIVE'):
- if block_limit == 0:
- block_limit = block_offset
-
queue_adapter = ChaindFsAdapter(
- chain_spec,
- config.get('SESSION_DATA_DIR'),
+ settings.get('CHAIN_SPEC'),
+ settings.get('SESSION_DATA_DIR'),
EthCacheTx,
None,
)
fltr = StateFilter(queue_adapter)
- sync_store = SyncFsStore(config.get('SESSION_RUNTIME_DIR'), session_id=config.get('SESSION_ID'))
+ sync_store = SyncFsStore(settings.get('SESSION_RUNTIME_DIR'), session_id=settings.get('SESSION_ID'))
sync_store.register(fltr)
- logg.debug('session block ofset {}'.format(session_block_offset))
+ logg.debug('session block offset {}'.format(settings.get('SYNCER_OFFSET')))
- chain_interface = EthChainInterface()
- drv = ChainInterfaceDriver(sync_store, chain_interface, offset=session_block_offset, target=block_limit)
- drv.run(conn)
+ drv = ChainInterfaceDriver(sync_store, settings.get('SYNCER_INTERFACE'), offset=settings.get('SYNCER_OFFSET'), target=settings.get('SYNCER_LIMIT'))
+ drv.run(settings.get('RPC'))
if __name__ == '__main__':
diff --git a/chaind/eth/settings.py b/chaind/eth/settings.py
@@ -0,0 +1,18 @@
+# external imports
+from chainlib.eth.connection import EthHTTPConnection
+from chaind.settings import ChaindSettings
+from chaind.eth.chain import EthChainInterface
+
+
+class ChaindEthSettings(ChaindSettings):
+
+ def process_sync_interface(self, config):
+ self.o['SYNCER_INTERFACE'] = EthChainInterface()
+
+
+ def process_common(self, config):
+ super(ChaindEthSettings, self).process_common(config)
+ rpc_provider = config.get('RPC_PROVIDER')
+ if rpc_provider == None:
+ rpc_provider = 'http://localhost:8545'
+ self.o['RPC'] = EthHTTPConnection(url=rpc_provider, chain_spec=self.o['CHAIN_SPEC'])