implement settings renderer

This commit is contained in:
lash 2022-04-26 17:24:57 +00:00
parent f961345652
commit b19b948be3
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 34 additions and 73 deletions

View File

@ -3,20 +3,18 @@ import os
import logging import logging
# external imports # external imports
import chainlib.eth.cli import chaind.cli
from chaind.setup import Environment from chaind.setup import Environment
from chaind.filter import StateFilter from chaind.filter import StateFilter
from chaind.adapters.fs import ChaindFsAdapter from chaind.adapters.fs import ChaindFsAdapter
from chainlib.eth.block import block_latest from chainlib.eth.block import block_latest
from chainlib.eth.connection import EthHTTPConnection
from chainlib.chain import ChainSpec
from hexathon import strip_0x from hexathon import strip_0x
from chainsyncer.store.fs import SyncFsStore from chainsyncer.store.fs import SyncFsStore
from chainsyncer.driver.chain_interface import ChainInterfaceDriver from chainsyncer.driver.chain_interface import ChainInterfaceDriver
from chaind.eth.settings import ChaindEthSettings
# local imports # local imports
from chaind.eth.cache import EthCacheTx from chaind.eth.cache import EthCacheTx
from chaind.eth.chain import EthChainInterface
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
@ -26,91 +24,36 @@ config_dir = os.path.join(script_dir, '..', 'data', 'config')
env = Environment(domain='eth', env=os.environ) env = Environment(domain='eth', env=os.environ)
arg_flags = chainlib.eth.cli.argflag_std_read arg_flags = chaind.cli.argflag_local_sync
argparser = chainlib.eth.cli.ArgumentParser(arg_flags) argparser = chaind.cli.ArgumentParser(arg_flags)
argparser.add_argument('--data-dir', type=str, help='data directory') local_arg_flags = chaind.cli.argflag_local_sync
argparser.add_argument('--runtime-dir', type=str, help='runtime directory') argparser.process_local_flags(local_arg_flags)
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')
args = argparser.parse_args() args = argparser.parse_args()
extra_args = { config = chaind.cli.Config.from_args('eth', args, arg_flags, local_arg_flags)
'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)
logg.debug('config loaded:\n{}'.format(config)) 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(): 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( queue_adapter = ChaindFsAdapter(
chain_spec, settings.get('CHAIN_SPEC'),
config.get('SESSION_DATA_DIR'), settings.get('SESSION_DATA_DIR'),
EthCacheTx, EthCacheTx,
None, None,
) )
fltr = StateFilter(queue_adapter) 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) 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, settings.get('SYNCER_INTERFACE'), offset=settings.get('SYNCER_OFFSET'), target=settings.get('SYNCER_LIMIT'))
drv = ChainInterfaceDriver(sync_store, chain_interface, offset=session_block_offset, target=block_limit) drv.run(settings.get('RPC'))
drv.run(conn)
if __name__ == '__main__': if __name__ == '__main__':

18
chaind/eth/settings.py Normal file
View File

@ -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'])