chaind-eth

Queue server for ethereum
Info | Log | Files | Refs | README | LICENSE

resend.py (2854B)


      1 # standard imports
      2 import os
      3 import logging
      4 import sys
      5 import datetime
      6 import enum
      7 import re
      8 import stat
      9 
     10 # external imports
     11 import chainlib.eth.cli
     12 from chaind import Environment
     13 from chainlib.eth.gas import price
     14 from chainlib.chain import ChainSpec
     15 from hexathon import strip_0x
     16 from eth_token_index.index import TokenUniqueSymbolIndex
     17 
     18 # local imports
     19 from chaind_eth.cli.retry import Retrier
     20 from chaind.error import TxSourceError
     21 from chaind_eth.cli.output import (
     22         Outputter,
     23         OpMode,
     24         )
     25 
     26 logging.basicConfig(level=logging.WARNING)
     27 logg = logging.getLogger()
     28 
     29 script_dir = os.path.dirname(os.path.realpath(__file__)) 
     30 config_dir = os.path.join(script_dir, '..', 'data', 'config')
     31 
     32 
     33 arg_flags = chainlib.eth.cli.argflag_std_write
     34 argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
     35 argparser.add_argument('--socket', dest='socket', type=str, help='Socket to send transactions to')
     36 argparser.add_positional('source', required=False, type=str, help='Transaction source file')
     37 args = argparser.parse_args()
     38 
     39 extra_args = {
     40         'socket': None,
     41         'source': None,
     42         }
     43 
     44 env = Environment(domain='eth', env=os.environ)
     45 config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, base_config_dir=config_dir)
     46 
     47 wallet = chainlib.eth.cli.Wallet()
     48 wallet.from_config(config)
     49 
     50 rpc = chainlib.eth.cli.Rpc(wallet=wallet)
     51 conn = rpc.connect_by_config(config)
     52 
     53 chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
     54 
     55 
     56 mode = OpMode.STDOUT
     57 re_unix = r'^ipc://(/.+)'
     58 m = re.match(re_unix, config.get('_SOCKET', ''))
     59 if m != None:
     60     config.add(m.group(1), '_SOCKET', exists_ok=True)
     61     r = 0
     62     try:
     63         stat_info = os.stat(config.get('_SOCKET'))
     64         if not stat.S_ISSOCK(stat_info.st_mode):
     65             r = 1
     66     except FileNotFoundError:
     67         r = 1
     68 
     69     if r > 0:
     70         sys.stderr.write('{} is not a socket\n'.format(config.get('_SOCKET')))
     71         sys.exit(1)
     72     
     73     mode = OpMode.UNIX
     74 
     75 logg.info('using mode {}'.format(mode.value))
     76 
     77 if config.get('_SOURCE') == None:
     78     sys.stderr.write('source data missing\n')
     79     sys.exit(1)
     80 
     81 
     82 def main():
     83     signer = rpc.get_signer()
     84 
     85     # TODO: make resolvers pluggable
     86     processor = Retrier(wallet.get_signer_address(), wallet.get_signer(), config.get('_SOURCE'), chain_spec, rpc.get_gas_oracle())
     87 
     88     sends = None
     89     try:
     90         sends = processor.load()
     91     except TxSourceError as e:
     92         sys.stderr.write('processing error: {}. processors: {}\n'.format(str(e), str(processor)))
     93         sys.exit(1)
     94 
     95     tx_iter = iter(processor)
     96     out = Outputter(mode)
     97     while True:
     98         tx = None
     99         try:
    100             tx_bytes = next(tx_iter)
    101         except StopIteration:
    102             break
    103         tx_hex = tx_bytes.hex()
    104         print(out.do(tx_hex, socket=config.get('_SOCKET')))
    105 
    106 
    107 if __name__ == '__main__':
    108     main()