2021-09-08 17:17:49 +02:00
|
|
|
|
# standard imports
|
|
|
|
|
import os
|
|
|
|
|
import logging
|
|
|
|
|
import sys
|
|
|
|
|
import datetime
|
|
|
|
|
import enum
|
|
|
|
|
import re
|
|
|
|
|
import stat
|
2022-04-10 21:03:23 +02:00
|
|
|
|
import socket
|
2021-09-08 17:17:49 +02:00
|
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
|
import chainlib.eth.cli
|
2022-04-28 14:49:50 +02:00
|
|
|
|
import chainqueue.cli
|
2022-04-27 08:36:01 +02:00
|
|
|
|
import chaind.cli
|
2022-04-10 21:03:23 +02:00
|
|
|
|
from chaind.setup import Environment
|
2021-09-08 17:17:49 +02:00
|
|
|
|
from chainlib.eth.gas import price
|
|
|
|
|
from chainlib.chain import ChainSpec
|
|
|
|
|
from hexathon import strip_0x
|
|
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
|
from chaind.error import TxSourceError
|
2022-04-10 21:03:23 +02:00
|
|
|
|
from chaind.eth.token.process import Processor
|
|
|
|
|
from chaind.eth.token.gas import GasTokenResolver
|
|
|
|
|
from chaind.eth.cli.csv import CSVProcessor
|
|
|
|
|
from chaind.eth.cli.output import (
|
2021-09-08 17:17:49 +02:00
|
|
|
|
Outputter,
|
|
|
|
|
OpMode,
|
|
|
|
|
)
|
2022-04-27 08:36:01 +02:00
|
|
|
|
from chaind.eth.settings import ChaindEthSettings
|
2021-09-08 17:17:49 +02:00
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.WARNING)
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
arg_flags = chainlib.eth.cli.argflag_std_write
|
2022-04-27 08:36:01 +02:00
|
|
|
|
argparser = chainlib.eth.cli.ArgumentParser(arg_flags, arg_long={'-s': '--send-rpc'})
|
2021-09-08 17:17:49 +02:00
|
|
|
|
argparser.add_positional('source', required=False, type=str, help='Transaction source file')
|
2022-04-27 08:36:01 +02:00
|
|
|
|
|
2022-04-27 11:01:46 +02:00
|
|
|
|
local_arg_flags = chaind.cli.argflag_local_socket_client | chaind.cli.ChaindFlag.TOKEN
|
2022-04-27 08:36:01 +02:00
|
|
|
|
chaind.cli.process_flags(argparser, local_arg_flags)
|
|
|
|
|
|
2022-04-28 14:49:50 +02:00
|
|
|
|
chainqueue.cli.process_flags(argparser, 0)
|
|
|
|
|
|
2021-09-08 17:17:49 +02:00
|
|
|
|
args = argparser.parse_args()
|
|
|
|
|
|
|
|
|
|
env = Environment(domain='eth', env=os.environ)
|
2022-04-27 08:36:01 +02:00
|
|
|
|
|
2022-04-28 14:49:50 +02:00
|
|
|
|
base_config_dir = [chaind.cli.config_dir, chainqueue.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-28 14:49:50 +02:00
|
|
|
|
config = chainqueue.cli.process_config(config, args, 0)
|
2022-04-27 08:36:01 +02:00
|
|
|
|
config = chaind.cli.process_config(config, args, local_arg_flags)
|
|
|
|
|
config.add(args.source, '_SOURCE', False)
|
|
|
|
|
config.add('eth', 'CHAIND_ENGINE', False)
|
|
|
|
|
config.add('queue', 'CHAIND_COMPONENT', False)
|
|
|
|
|
logg.debug('config loaded:\n{}'.format(config))
|
2021-09-08 17:17:49 +02:00
|
|
|
|
|
|
|
|
|
wallet = chainlib.eth.cli.Wallet()
|
|
|
|
|
wallet.from_config(config)
|
|
|
|
|
|
2022-04-27 08:36:01 +02:00
|
|
|
|
settings = ChaindEthSettings(include_queue=True)
|
|
|
|
|
settings.process(config)
|
|
|
|
|
|
|
|
|
|
logg.debug('settings:\n{}'.format(settings))
|
|
|
|
|
|
2021-09-08 17:17:49 +02:00
|
|
|
|
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
|
|
|
|
|
conn = rpc.connect_by_config(config)
|
|
|
|
|
|
|
|
|
|
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
|
|
|
|
|
|
|
|
|
|
mode = OpMode.STDOUT
|
2022-04-10 21:03:23 +02:00
|
|
|
|
|
2021-09-08 17:17:49 +02:00
|
|
|
|
re_unix = r'^ipc://(/.+)'
|
2022-04-27 08:36:01 +02:00
|
|
|
|
m = re.match(re_unix, config.get('SESSION_SOCKET_PATH', ''))
|
2021-09-08 17:17:49 +02:00
|
|
|
|
if m != None:
|
2022-04-27 08:36:01 +02:00
|
|
|
|
config.add(m.group(1), 'SESSION_SOCKET_PATH', exists_ok=True)
|
2021-09-08 17:17:49 +02:00
|
|
|
|
r = 0
|
|
|
|
|
try:
|
2022-04-27 08:36:01 +02:00
|
|
|
|
stat_info = os.stat(config.get('SESSION_SOCKET_PATH'))
|
2021-09-08 17:17:49 +02:00
|
|
|
|
if not stat.S_ISSOCK(stat_info.st_mode):
|
|
|
|
|
r = 1
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
r = 1
|
|
|
|
|
|
|
|
|
|
if r > 0:
|
2022-04-27 08:36:01 +02:00
|
|
|
|
sys.stderr.write('{} is not a socket\n'.format(config.get('SESSION_SOCKET_PATH')))
|
2021-09-08 17:17:49 +02:00
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
mode = OpMode.UNIX
|
|
|
|
|
|
|
|
|
|
logg.info('using mode {}'.format(mode.value))
|
|
|
|
|
|
|
|
|
|
if config.get('_SOURCE') == None:
|
2021-09-08 17:47:49 +02:00
|
|
|
|
sys.stderr.write('source data missing\n')
|
2021-09-08 17:17:49 +02:00
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
2022-04-26 17:43:18 +02:00
|
|
|
|
class SocketSender:
|
|
|
|
|
|
2022-04-27 08:36:01 +02:00
|
|
|
|
def __init__(self, settings):
|
|
|
|
|
self.path = settings.get('SESSION_SOCKET_PATH')
|
2022-04-26 17:43:18 +02:00
|
|
|
|
|
|
|
|
|
def send(self, tx):
|
|
|
|
|
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
|
s.connect(self.path)
|
|
|
|
|
s.sendall(tx.encode('utf-8'))
|
|
|
|
|
r = s.recv(68)
|
|
|
|
|
s.close()
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
2021-09-08 17:17:49 +02:00
|
|
|
|
def main():
|
2022-04-10 21:03:23 +02:00
|
|
|
|
token_resolver = None
|
2022-04-27 11:01:46 +02:00
|
|
|
|
if settings.get('TOKEN_MODULE') != None:
|
2022-04-10 21:03:23 +02:00
|
|
|
|
import importlib
|
2022-04-27 11:01:46 +02:00
|
|
|
|
m = importlib.import_module(settings.get('TOKEN_MODULE'))
|
2022-04-10 21:03:23 +02:00
|
|
|
|
m = m.TokenResolver
|
|
|
|
|
else:
|
|
|
|
|
from chaind.eth.token.gas import GasTokenResolver
|
|
|
|
|
m = GasTokenResolver
|
|
|
|
|
token_resolver = m(chain_spec, rpc.get_sender_address(), rpc.get_signer(), rpc.get_gas_oracle(), rpc.get_nonce_oracle())
|
|
|
|
|
|
|
|
|
|
processor = Processor(token_resolver, config.get('_SOURCE'))
|
|
|
|
|
processor.add_processor(CSVProcessor())
|
2021-09-08 17:17:49 +02:00
|
|
|
|
|
|
|
|
|
sends = None
|
|
|
|
|
try:
|
2022-04-10 23:27:36 +02:00
|
|
|
|
sends = processor.load(conn)
|
2021-09-08 17:17:49 +02:00
|
|
|
|
except TxSourceError as e:
|
|
|
|
|
sys.stderr.write('processing error: {}. processors: {}\n'.format(str(e), str(processor)))
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
2022-04-26 17:43:18 +02:00
|
|
|
|
sender = None
|
2022-04-27 08:36:01 +02:00
|
|
|
|
if config.true('_SOCKET_SEND'):
|
|
|
|
|
if settings.get('SESSION_SOCKET_PATH') != None:
|
|
|
|
|
sender = SocketSender(settings)
|
2022-04-26 17:43:18 +02:00
|
|
|
|
|
2021-09-08 17:17:49 +02:00
|
|
|
|
tx_iter = iter(processor)
|
|
|
|
|
out = Outputter(mode)
|
|
|
|
|
while True:
|
|
|
|
|
tx = None
|
|
|
|
|
try:
|
|
|
|
|
tx_bytes = next(tx_iter)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
break
|
|
|
|
|
tx_hex = tx_bytes.hex()
|
2022-04-26 17:43:18 +02:00
|
|
|
|
if sender != None:
|
|
|
|
|
r = sender.send(tx_hex)
|
|
|
|
|
logg.info('sent {} result {}'.format(tx_hex, r))
|
2022-04-10 21:03:23 +02:00
|
|
|
|
print(out.do(tx_hex))
|
2021-09-08 17:17:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|