2021-09-07 09:59:54 +02:00
|
|
|
|
# standard imports
|
|
|
|
|
import os
|
|
|
|
|
import logging
|
|
|
|
|
import sys
|
|
|
|
|
import datetime
|
|
|
|
|
import enum
|
|
|
|
|
import re
|
|
|
|
|
import stat
|
2021-09-08 08:50:38 +02:00
|
|
|
|
import socket
|
2021-09-07 09:59:54 +02:00
|
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
|
import chainlib.eth.cli
|
|
|
|
|
from chaind import Environment
|
2021-09-07 16:46:53 +02:00
|
|
|
|
from chainlib.eth.gas import price
|
|
|
|
|
from chainlib.chain import ChainSpec
|
|
|
|
|
from hexathon import strip_0x
|
2021-09-07 09:59:54 +02:00
|
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
|
from chaind_eth.cli.process import Processor
|
|
|
|
|
from chaind_eth.cli.csv import CSVProcessor
|
|
|
|
|
from chaind.error import TxSourceError
|
2021-09-09 08:40:20 +02:00
|
|
|
|
from chaind_eth.cli.resolver import (
|
|
|
|
|
DefaultResolver,
|
|
|
|
|
LookNoop,
|
|
|
|
|
TokenIndexLookup,
|
|
|
|
|
)
|
2021-09-07 09:59:54 +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')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
arg_flags = chainlib.eth.cli.argflag_std_write
|
|
|
|
|
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
|
|
|
|
|
argparser.add_argument('--socket', dest='socket', type=str, help='Socket to send transactions to')
|
2021-09-07 16:46:53 +02:00
|
|
|
|
argparser.add_argument('--token-index', dest='token_index', type=str, help='Token resolver index')
|
2021-09-07 09:59:54 +02:00
|
|
|
|
argparser.add_positional('source', required=False, type=str, help='Transaction source file')
|
|
|
|
|
args = argparser.parse_args()
|
|
|
|
|
|
|
|
|
|
extra_args = {
|
|
|
|
|
'socket': None,
|
|
|
|
|
'source': None,
|
2021-09-07 16:46:53 +02:00
|
|
|
|
'token_index': None,
|
2021-09-07 09:59:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
env = Environment(domain='eth', env=os.environ)
|
|
|
|
|
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, base_config_dir=config_dir)
|
|
|
|
|
|
2021-09-07 16:46:53 +02:00
|
|
|
|
wallet = chainlib.eth.cli.Wallet()
|
|
|
|
|
wallet.from_config(config)
|
|
|
|
|
|
|
|
|
|
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
|
|
|
|
|
conn = rpc.connect_by_config(config)
|
|
|
|
|
|
|
|
|
|
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
|
|
|
|
|
|
2021-09-07 09:59:54 +02:00
|
|
|
|
class OpMode(enum.Enum):
|
2021-09-08 08:50:38 +02:00
|
|
|
|
STDOUT = 'standard_output'
|
|
|
|
|
UNIX = 'unix_socket'
|
2021-09-07 09:59:54 +02:00
|
|
|
|
mode = OpMode.STDOUT
|
|
|
|
|
|
2021-09-08 08:50:38 +02:00
|
|
|
|
re_unix = r'^ipc://(/.+)'
|
|
|
|
|
m = re.match(re_unix, config.get('_SOCKET', ''))
|
|
|
|
|
if m != None:
|
|
|
|
|
config.add(m.group(1), '_SOCKET', exists_ok=True)
|
2021-09-07 09:59:54 +02:00
|
|
|
|
r = 0
|
|
|
|
|
try:
|
|
|
|
|
stat_info = os.stat(config.get('_SOCKET'))
|
|
|
|
|
if not stat.S_ISSOCK(stat_info.st_mode):
|
|
|
|
|
r = 1
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
r = 1
|
|
|
|
|
|
|
|
|
|
if r > 0:
|
|
|
|
|
sys.stderr.write('{} is not a socket\n'.format(config.get('_SOCKET')))
|
|
|
|
|
sys.exit(1)
|
2021-09-08 08:50:38 +02:00
|
|
|
|
|
2021-09-07 09:59:54 +02:00
|
|
|
|
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-07 09:59:54 +02:00
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
2021-09-08 08:50:38 +02:00
|
|
|
|
class Outputter:
|
|
|
|
|
|
|
|
|
|
def __init__(self, mode):
|
|
|
|
|
self.out = getattr(self, 'do_' + mode.value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def do(self, hx):
|
2021-09-08 16:18:19 +02:00
|
|
|
|
return self.out(hx)
|
2021-09-08 08:50:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def do_standard_output(self, hx):
|
2021-09-08 16:18:19 +02:00
|
|
|
|
#sys.stdout.write(hx + '\n')
|
|
|
|
|
return hx
|
2021-09-08 08:50:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def do_unix_socket(self, hx):
|
|
|
|
|
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
|
s.connect(config.get('_SOCKET'))
|
|
|
|
|
s.send(hx.encode('utf-8'))
|
2021-09-08 16:18:19 +02:00
|
|
|
|
r = s.recv(64+4)
|
|
|
|
|
logg.debug('r {}'.format(r))
|
2021-09-08 08:50:38 +02:00
|
|
|
|
s.close()
|
2021-09-08 16:18:19 +02:00
|
|
|
|
return r[4:].decode('utf-8')
|
2021-09-08 08:50:38 +02:00
|
|
|
|
|
|
|
|
|
|
2021-09-07 09:59:54 +02:00
|
|
|
|
def main():
|
2021-09-07 16:46:53 +02:00
|
|
|
|
signer = rpc.get_signer()
|
|
|
|
|
|
2021-09-09 08:40:20 +02:00
|
|
|
|
|
2021-09-08 08:50:38 +02:00
|
|
|
|
# TODO: make resolvers pluggable
|
2021-09-07 16:46:53 +02:00
|
|
|
|
token_resolver = DefaultResolver(chain_spec, conn, sender_address=rpc.get_sender_address())
|
2021-09-09 08:40:20 +02:00
|
|
|
|
|
2021-09-09 09:05:59 +02:00
|
|
|
|
noop_lookup = LookNoop(check=not config.true('_UNSAFE'))
|
2021-09-09 08:40:20 +02:00
|
|
|
|
token_resolver.add_lookup(noop_lookup, 'noop')
|
|
|
|
|
|
|
|
|
|
if config.get('_TOKEN_INDEX') != None:
|
|
|
|
|
token_index_lookup = TokenIndexLookup(chain_spec, signer, rpc.get_gas_oracle(), rpc.get_nonce_oracle(), config.get('_TOKEN_INDEX'))
|
|
|
|
|
token_resolver.add_lookup(token_index_lookup, reverse=config.get('_TOKEN_INDEX'))
|
2021-09-07 16:46:53 +02:00
|
|
|
|
|
|
|
|
|
processor = Processor(wallet.get_signer_address(), wallet.get_signer(), config.get('_SOURCE'), chain_spec, rpc.get_gas_oracle(), rpc.get_nonce_oracle(), resolver=token_resolver)
|
2021-09-07 09:59:54 +02:00
|
|
|
|
processor.add_processor(CSVProcessor())
|
2021-09-07 16:46:53 +02:00
|
|
|
|
|
|
|
|
|
sends = None
|
2021-09-07 09:59:54 +02:00
|
|
|
|
try:
|
2021-09-07 16:46:53 +02:00
|
|
|
|
sends = processor.load()
|
2021-09-07 09:59:54 +02:00
|
|
|
|
except TxSourceError as e:
|
2021-09-07 16:46:53 +02:00
|
|
|
|
sys.stderr.write('processing error: {}. processors: {}\n'.format(str(e), str(processor)))
|
2021-09-07 09:59:54 +02:00
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
2021-09-07 16:46:53 +02:00
|
|
|
|
|
|
|
|
|
tx_iter = iter(processor)
|
2021-09-08 08:50:38 +02:00
|
|
|
|
out = Outputter(mode)
|
2021-09-07 16:46:53 +02:00
|
|
|
|
while True:
|
|
|
|
|
tx = None
|
|
|
|
|
try:
|
|
|
|
|
tx_bytes = next(tx_iter)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
break
|
2021-09-08 08:50:38 +02:00
|
|
|
|
tx_hex = tx_bytes.hex()
|
2021-09-08 16:18:19 +02:00
|
|
|
|
print(out.do(tx_hex))
|
2021-09-07 16:46:53 +02:00
|
|
|
|
|
|
|
|
|
|
2021-09-07 09:59:54 +02:00
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|