2022-04-10 11:33:10 +02:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import signal
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
import chainlib.eth.cli
|
2022-04-26 21:40:58 +02:00
|
|
|
import chaind.cli
|
2022-04-28 14:49:50 +02:00
|
|
|
import chainqueue.cli
|
2022-04-10 11:33:10 +02:00
|
|
|
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
|
2022-04-10 11:33:10 +02:00
|
|
|
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-04-10 11:33:10 +02:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from chaind.eth.cache import EthCacheTx
|
2022-04-26 21:40:58 +02:00
|
|
|
from chaind.eth.settings import ChaindEthSettings
|
2022-05-04 07:41:00 +02:00
|
|
|
from chaind.eth.dispatch import EthDispatcher
|
2022-04-10 11:33:10 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
arg_flags = chainlib.eth.cli.argflag_std_read
|
|
|
|
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
|
2022-04-26 21:40:58 +02:00
|
|
|
|
2022-04-28 14:49:50 +02:00
|
|
|
queue_arg_flags = 0
|
|
|
|
chainqueue.cli.process_flags(argparser, queue_arg_flags)
|
|
|
|
|
2022-04-26 21:40:58 +02:00
|
|
|
local_arg_flags = chaind.cli.argflag_local_base | chaind.cli.ChaindFlag.DISPATCH | chaind.cli.ChaindFlag.SOCKET
|
|
|
|
chaind.cli.process_flags(argparser, local_arg_flags)
|
|
|
|
|
2022-04-10 11:33:10 +02:00
|
|
|
args = argparser.parse_args()
|
2022-04-26 21:40:58 +02:00
|
|
|
|
2022-04-28 14:49:50 +02:00
|
|
|
base_config_dir = [chainqueue.cli.config_dir, chaind.cli.config_dir]
|
2022-04-27 08:36:01 +02:00
|
|
|
config = chainlib.eth.cli.Config.from_args(args, arg_flags, base_config_dir=base_config_dir)
|
2022-04-26 21:40:58 +02:00
|
|
|
config = chaind.cli.process_config(config, args, local_arg_flags)
|
2022-04-28 14:49:50 +02:00
|
|
|
config = chainqueue.cli.process_config(config, args, queue_arg_flags)
|
2022-04-26 21:40:58 +02:00
|
|
|
config.add('eth', 'CHAIND_ENGINE', False)
|
2022-04-27 08:36:01 +02:00
|
|
|
config.add('queue', 'CHAIND_COMPONENT', False)
|
2022-04-10 11:33:10 +02:00
|
|
|
logg.debug('config loaded:\n{}'.format(config))
|
|
|
|
|
2022-04-26 23:30:13 +02:00
|
|
|
settings = ChaindEthSettings(include_queue=True)
|
2022-04-26 21:40:58 +02:00
|
|
|
settings.process(config)
|
|
|
|
|
|
|
|
logg.debug('settings:\n{}'.format(settings))
|
|
|
|
|
2022-04-10 11:33:10 +02:00
|
|
|
rpc = chainlib.eth.cli.Rpc()
|
|
|
|
conn = rpc.connect_by_config(config)
|
|
|
|
|
|
|
|
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-04-10 11:33:10 +02:00
|
|
|
dispatcher = EthDispatcher(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)
|
2022-04-10 11:33:10 +02:00
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, ctrl.shutdown)
|
|
|
|
signal.signal(signal.SIGTERM, ctrl.shutdown)
|
|
|
|
|
2022-04-26 23:30:13 +02:00
|
|
|
logg.info('session id is ' + settings.get('SESSION_ID'))
|
|
|
|
logg.info('session socket path is ' + settings.get('SESSION_SOCKET_PATH'))
|
2022-04-10 11:33:10 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
while True:
|
2022-04-10 19:13:49 +02:00
|
|
|
v = None
|
|
|
|
client_socket = None
|
2022-04-10 11:33:10 +02:00
|
|
|
try:
|
2022-04-10 19:13:49 +02:00
|
|
|
(client_socket, v) = ctrl.get()
|
2022-04-10 11:33:10 +02:00
|
|
|
except ClientGoneError:
|
|
|
|
break
|
|
|
|
except ClientBlockError:
|
|
|
|
continue
|
|
|
|
except ClientInputError:
|
|
|
|
continue
|
|
|
|
except NothingToDoError:
|
|
|
|
pass
|
|
|
|
|
2022-04-10 19:13:49 +02:00
|
|
|
if v == None:
|
2022-04-10 11:33:10 +02:00
|
|
|
ctrl.process(conn)
|
|
|
|
continue
|
|
|
|
|
2022-05-04 07:41:00 +02:00
|
|
|
queue_adapter = ChaindFsAdapter(
|
|
|
|
settings.get('CHAIN_SPEC'),
|
|
|
|
settings.dir_for('queue'),
|
|
|
|
EthCacheTx,
|
|
|
|
dispatcher,
|
|
|
|
store_sync=False,
|
|
|
|
)
|
|
|
|
|
2022-04-10 19:13:49 +02:00
|
|
|
result_data = None
|
|
|
|
r = 0 # no error
|
2022-04-10 15:58:24 +02:00
|
|
|
try:
|
2022-04-10 19:13:49 +02:00
|
|
|
result_data = queue_adapter.put(v.hex())
|
2022-04-10 18:22:25 +02:00
|
|
|
except DuplicateTxError as e:
|
2022-04-10 19:13:49 +02:00
|
|
|
logg.error('tx already exists: {}'.format(e))
|
|
|
|
r = 1
|
2022-04-10 18:22:25 +02:00
|
|
|
except ValueError as e:
|
2022-04-10 19:13:49 +02:00
|
|
|
logg.error('adapter rejected input {}: "{}"'.format(v.hex(), e))
|
2022-04-10 15:58:24 +02:00
|
|
|
continue
|
|
|
|
|
2022-04-10 19:13:49 +02:00
|
|
|
if r == 0:
|
|
|
|
queue_adapter.enqueue(result_data)
|
2022-04-10 11:33:10 +02:00
|
|
|
|
2022-04-10 19:13:49 +02:00
|
|
|
ctrl.respond_put(client_socket, r, extra_data=result_data)
|
|
|
|
|
2022-04-10 11:33:10 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|