chaind-eth/chaind/eth/runnable/queuer.py

166 lines
4.6 KiB
Python
Raw Normal View History

# standard imports
import os
import logging
import signal
# external imports
import chainlib.eth.cli
2022-05-13 15:47:58 +02:00
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
from chainqueue.cli.arg import (
apply_arg as apply_arg_queue,
apply_flag as apply_flag_queue,
)
from chaind.cli.arg import (
apply_arg,
apply_flag,
)
from chaind.session import SessionController
from chaind.setup import Environment
from chaind.error import (
NothingToDoError,
ClientGoneError,
ClientBlockError,
ClientInputError,
)
from chainqueue import (
Store,
Status,
)
2022-04-10 18:45:58 +02:00
from chainqueue.error import DuplicateTxError
from chainqueue.store.fs import (
IndexStore,
CounterStore,
)
from chainqueue.cache import CacheTokenTx
from chainlib.encode import TxHexNormalizer
from chainlib.chain import ChainSpec
from chaind.adapters.fs import ChaindFsAdapter
2022-05-04 07:41:00 +02:00
from chaind.dispatch import DispatchProcessor
2022-05-13 15:47:58 +02:00
from chainqueue.data import config_dir as chainqueue_config_dir
from chaind.data import config_dir as chaind_config_dir
from chainlib.eth.cli.log import process_log
2022-05-14 18:15:02 +02:00
from chaind.cli.config import process_config as process_config_local
# local imports
from chaind.eth.cache import EthCacheTx
2022-05-13 15:47:58 +02:00
from chaind.eth.settings import ChaindSettings
2022-05-04 07:41:00 +02:00
from chaind.eth.dispatch import EthDispatcher
2022-05-13 15:47:58 +02:00
from chaind.eth.settings import process_settings
from chaind.settings import (
process_queue,
process_socket,
process_dispatch,
)
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
script_dir = os.path.dirname(os.path.realpath(__file__))
config_dir = os.path.join(script_dir, '..', 'data', 'config')
env = Environment(domain='eth', env=os.environ)
2022-05-13 15:47:58 +02:00
arg_flags = ArgFlag()
arg_flags = apply_flag_queue(arg_flags)
arg_flags = apply_flag(arg_flags)
2022-05-13 15:47:58 +02:00
arg = Arg(arg_flags)
arg = apply_arg_queue(arg)
arg = apply_arg(arg)
flags = arg_flags.STD_READ | arg_flags.QUEUE | arg_flags.STATE | arg_flags.SESSION
2022-05-13 15:47:58 +02:00
argparser = chainlib.eth.cli.ArgumentParser()
argparser = process_args(argparser, arg, flags)
args = argparser.parse_args()
2022-05-13 15:47:58 +02:00
logg = process_log(args, logg)
config = Config()
config.add_schema_dir(chainqueue_config_dir)
config.add_schema_dir(chaind_config_dir)
config = process_config(config, arg, args, flags)
2022-05-14 18:15:02 +02:00
config = process_config_local(config, arg, args, flags)
config.add('eth', 'CHAIND_ENGINE', False)
2022-05-13 15:47:58 +02:00
config.add('sync', 'CHAIND_COMPONENT', False)
logg.debug('config loaded:\n{}'.format(config))
2022-05-13 15:47:58 +02:00
settings = ChaindSettings(include_sync=True)
settings = process_settings(settings, config)
settings = process_queue(settings, config)
settings = process_socket(settings, config)
settings = process_dispatch(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
tx_normalizer = TxHexNormalizer().tx_hash
2022-05-04 07:41:00 +02:00
token_cache_store = CacheTokenTx(settings.get('CHAIN_SPEC'), normalizer=tx_normalizer)
2022-05-13 15:47:58 +02:00
dispatcher = EthDispatcher(settings.get('CONN'))
2022-05-04 07:41:00 +02:00
processor = DispatchProcessor(settings.get('CHAIN_SPEC'), settings.dir_for('queue'), dispatcher)
ctrl = SessionController(settings, processor.process)
signal.signal(signal.SIGINT, ctrl.shutdown)
signal.signal(signal.SIGTERM, ctrl.shutdown)
logg.info('session id is ' + settings.get('SESSION_ID'))
logg.info('session socket path is ' + settings.get('SESSION_SOCKET_PATH'))
2022-05-05 17:08:51 +02:00
def main():
2022-05-05 17:08:51 +02:00
global dispatcher, settings
queue_adapter = ChaindFsAdapter(
settings.get('CHAIN_SPEC'),
settings.dir_for('queue'),
EthCacheTx,
dispatcher,
store_sync=False,
)
while True:
2022-04-10 19:13:49 +02:00
v = None
client_socket = None
try:
2022-04-10 19:13:49 +02:00
(client_socket, v) = ctrl.get()
except ClientGoneError:
break
except ClientBlockError:
continue
except ClientInputError:
continue
except NothingToDoError:
pass
2022-04-10 19:13:49 +02:00
if v == None:
2022-05-13 15:47:58 +02:00
ctrl.process(settings.get('CONN'))
2022-05-05 17:08:51 +02:00
#queue_adapter = create_adapter(settings, dispatcher)
continue
2022-04-10 19:13:49 +02:00
result_data = None
r = 0 # no error
try:
2022-04-10 19:13:49 +02:00
result_data = queue_adapter.put(v.hex())
except DuplicateTxError as e:
2022-04-10 19:13:49 +02:00
logg.error('tx already exists: {}'.format(e))
r = 1
except ValueError as e:
2022-04-10 19:13:49 +02:00
logg.error('adapter rejected input {}: "{}"'.format(v.hex(), e))
continue
2022-04-10 19:13:49 +02:00
if r == 0:
queue_adapter.enqueue(result_data)
2022-04-10 19:13:49 +02:00
ctrl.respond_put(client_socket, r, extra_data=result_data)
if __name__ == '__main__':
main()