chainlib-eth

Ethereum implementation of the chainlib interface
Log | Files | Refs | README | LICENSE

commit e704445ff7ca84838b240c940c1d90ac09d521c1
parent 8edcb2364057b9dadaa9928dc69560b2a10d268d
Author: lash <dev@holbrook.no>
Date:   Thu,  3 Nov 2022 21:11:48 +0000

Rehabilitate remaining cli tools for stdin arg refactor

Diffstat:
Mchainlib/eth/cli/config.py | 4++--
Mchainlib/eth/runnable/block.py | 5+++--
Mchainlib/eth/runnable/count.py | 6++++--
Mchainlib/eth/runnable/decode.py | 17+++++++++++++++--
Mchainlib/eth/runnable/encode.py | 69++++++++++++++++++++++++++++++++++++++++++++-------------------------
Mchainlib/eth/runnable/gas.py | 28+++++++++++++++-------------
Mchainlib/eth/runnable/get.py | 14++++++++++----
Mchainlib/eth/runnable/info.py | 6++++--
Mchainlib/eth/runnable/raw.py | 14++++++++++++--
Mchainlib/eth/runnable/wait.py | 14++++++++++++--
10 files changed, 121 insertions(+), 56 deletions(-)

diff --git a/chainlib/eth/cli/config.py b/chainlib/eth/cli/config.py @@ -19,8 +19,8 @@ class Config(BaseConfig): default_fee_limit = 21000 -def process_config(config, arg, args, flags, pos_arg_name=None): - config = base_process_config(config, arg, args, flags) +def process_config(config, arg, args, flags, positional_name=None): + config = base_process_config(config, arg, args, flags, positional_name=positional_name) if arg.match('provider', flags): if not bool(config.get('RPC_DIALECT')): diff --git a/chainlib/eth/runnable/block.py b/chainlib/eth/runnable/block.py @@ -61,7 +61,8 @@ script_dir = os.path.dirname(os.path.realpath(__file__)) config_dir = os.path.join(script_dir, '..', 'data', 'config') def process_config_local(config, arg, args, flags): - config.add(args.block, '_BLOCK', False) + if len(args.block) > 0: + config.add(args.block[0], '_BLOCK', False) return config @@ -95,7 +96,7 @@ arg = Arg(arg_flags) flags = arg_flags.STD_BASE_READ flags = arg_flags.less(flags, arg_flags.CHAIN_SPEC) argparser = process_args(argparser, arg, flags) -argparser.add_argument('block', nargs='?', type=str, help='Block hash or number to retrieve') +argparser.add_argument('block', type=str, help='Block hash or number to retrieve') args = argparser.parse_args() logg = process_log(args, logg) diff --git a/chainlib/eth/runnable/count.py b/chainlib/eth/runnable/count.py @@ -40,9 +40,11 @@ config_dir = os.path.join(script_dir, '..', 'data', 'config') def process_config_local(config, arg, args, flags): - config.add(args.address, '_RECIPIENT', False) + recipient = config.get('_POSARG') + config.add(recipient, '_RECIPIENT', False) return config + argparser = chainlib.eth.cli.ArgumentParser() arg_flags = ArgFlag() arg = Arg(arg_flags) @@ -56,7 +58,7 @@ logg = process_log(args, logg) logg.debug('flags {} {} {}'.format(flags, arg_flags.SEQ, flags & arg_flags.SEQ)) config = Config() -config = process_config(config, arg, args, flags) +config = process_config(config, arg, args, flags, positional_name='address') config = process_config_local(config, arg, args, flags) logg.debug('config loaded:\n{}'.format(config)) diff --git a/chainlib/eth/runnable/decode.py b/chainlib/eth/runnable/decode.py @@ -13,6 +13,9 @@ import chainlib.eth.cli from chainlib.eth.tx import unpack from chainlib.settings import ChainSettings from chainlib.chain import ChainSpec +from hexathon import ( + strip_0x, + ) # local imports import chainlib.eth.cli @@ -21,6 +24,7 @@ from chainlib.eth.cli.arg import ( Arg, ArgFlag, process_args, + stdin_arg, ) from chainlib.eth.cli.config import ( Config, @@ -38,7 +42,16 @@ config_dir = os.path.join(script_dir, '..', 'data', 'config') def process_config_local(config, arg, args, flags): - config.add(args.tx_data, '_TX_DATA', False) + data = config.get('_POSARG') + + try: + data = strip_0x(data) + except TypeError: + data = stdin_arg() + data = strip_0x(data) + + config.add(data, '_TX_DATA', False) + return config @@ -54,7 +67,7 @@ args = argparser.parse_args() logg = process_log(args, logg) config = Config() -config = process_config(config, arg, args, flags) +config = process_config(config, arg, args, flags, positional_name='tx_data') config = process_config_local(config, arg, args, flags) logg.debug('config loaded:\n{}'.format(config)) diff --git a/chainlib/eth/runnable/encode.py b/chainlib/eth/runnable/encode.py @@ -11,7 +11,8 @@ import urllib import sha3 # external imports -from chainlib.cli import flag_reset +#from chainlib.cli import flag_reset +from chainlib.settings import ChainSettings from funga.eth.signer import EIP155Signer from funga.eth.keystore.dict import DictKeystore from hexathon import ( @@ -35,6 +36,7 @@ from chainlib.eth.cli.encode import CLIEncoder from chainlib.eth.constant import ZERO_ADDRESS from chainlib.eth.address import to_checksum from chainlib.eth.connection import EthHTTPConnection +from chainlib.eth.settings import process_settings from chainlib.jsonrpc import ( JSONRPCRequest, IntSequenceGenerator, @@ -81,30 +83,36 @@ config = process_config(config, arg, args, flags) config = process_config_local(config, arg, args, flags) logg.debug('config loaded:\n{}'.format(config)) -wallet = chainlib.eth.cli.Wallet(EIP155Signer) -wallet.from_config(config) +settings = ChainSettings() +settings = process_settings(settings, config) +logg.debug('settings loaded:\n{}'.format(settings)) -rpc = chainlib.eth.cli.Rpc(wallet=wallet) -conn = rpc.connect_by_config(config) -send = config.true('_RPC_SEND') +#wallet = chainlib.eth.cli.Wallet(EIP155Signer) +#wallet.from_config(config) +# +#rpc = chainlib.eth.cli.Rpc(wallet=wallet) +#conn = rpc.connect_by_config(config) +# +#send = config.true('_RPC_SEND') -chain_spec = None -try: - chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) -except AttributeError: - pass +#chain_spec = None +#try: +# chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) +#except AttributeError: +# pass def main(): signer_address = ZERO_ADDRESS signer = None - try: - signer = rpc.get_signer() - signer_address = rpc.get_signer_address() - except SignerMissingException: - pass + #try: + #signer = rpc.get_signer() + # signer_address = rpc.get_signer_address() + #except SignerMissingException: + # pass + signer_address = settings.get('SENDER_ADDRESS') code = '0x' cli_encoder = CLIEncoder(signature=config.get('_SIGNATURE')) @@ -140,7 +148,8 @@ def main(): if config.get('RPC_PROVIDER'): logg.debug('provider {}'.format(config.get('RPC_PROVIDER'))) if not config.get('_FEE_LIMIT') or not config.get('_FEE_PRICE'): - gas_oracle = rpc.get_gas_oracle() + #gas_oracle = rpc.get_gas_oracle() + gas_oracle = settings.get('GAS_ORACLE') (price, limit) = gas_oracle.get_gas() if not config.get('_FEE_PRICE'): config.add(price, '_FEE_PRICE') @@ -149,7 +158,7 @@ def main(): if mode == 'tx': if not config.get('_NONCE'): - nonce_oracle = rpc.get_nonce_oracle() + nonce_oracle = settings.get('NONCE_ORACLE') #rpc.get_nonce_oracle() config.add(nonce_oracle.get_nonce(), '_NONCE') else: for arg in [ @@ -163,8 +172,8 @@ def main(): if mode == 'call': #signer == None or config.true('_NOTX'): - c = TxFactory(chain_spec) - j = JSONRPCRequest(id_generator=rpc.id_generator) + c = TxFactory(settings.get('CHAIN_SPEC')) + j = JSONRPCRequest(id_generator=settings.get('RPC_ID_GENERATOR')) o = j.template() gas_limit = add_0x(int.to_bytes(config.get('_FEE_LIMIT'), 8, byteorder='big').hex(), compact_value=True) gas_price = add_0x(int.to_bytes(config.get('_FEE_PRICE'), 8, byteorder='big').hex(), compact_value=True) @@ -193,21 +202,31 @@ def main(): print(o) return - if signer == None: + if settings.get('SIGNER') == None: logg.error('mode "tx" without signer does not make sense. Please specify a key file with -y.') sys.exit(1) - if chain_spec == None: + if settings.get('CHAIN_SPEC') == None: raise ValueError('chain spec must be specified') - c = TxFactory(chain_spec, signer=signer, gas_oracle=rpc.get_gas_oracle(), nonce_oracle=rpc.get_nonce_oracle()) - tx = c.template(signer_address, config.get('_EXEC_ADDRESS'), use_nonce=True) + c = TxFactory( + settings.get('CHAIN_SPEC'), + signer=settings.get('SIGNER'), + gas_oracle=settings.get('FEE_ORACLE'), + nonce_oracle=settings.get('NONCE_ORACLE'), + ) + + tx = c.template( + settings.get('SENDER_ADDRESS'), + settings.get('EXEC'), + use_nonce=True, + ) tx = c.set_code(tx, code) tx_format = TxFormat.JSONRPC if config.get('_RAW'): tx_format = TxFormat.RLP_SIGNED (tx_hash_hex, o) = c.finalize(tx, tx_format=tx_format) - if send: + if settings.get('SEND'): r = conn.do(o) print(r) else: diff --git a/chainlib/eth/runnable/gas.py b/chainlib/eth/runnable/gas.py @@ -93,7 +93,7 @@ args = argparser.parse_args() logg = process_log(args, logg) config = Config() -config = process_config(config, arg, args, flags) +config = process_config(config, arg, args, flags, positional_name='amount') config = process_config_local(config, arg, args, flags) logg.debug('config loaded:\n{}'.format(config)) @@ -130,13 +130,14 @@ def main(): settings.get('SENDER_ADDRESS'), settings.get('RPC_ID_GENERATOR'), ) - recipient_balance = balance( - settings.get('CONN'), - settings.get('RECIPIENT'), - settings.get('RPC_ID_GENERATOR'), - ) logg.debug('sender {} balance before: {}'.format(settings.get('SENDER_ADDRESS'), sender_balance)) - logg.debug('recipient {} balance before: {}'.format(settings.get('RECIPIENT'), recipient_balance)) + if settings.get('RECIPIENT') != None: + recipient_balance = balance( + settings.get('CONN'), + settings.get('RECIPIENT'), + settings.get('RPC_ID_GENERATOR'), + ) + logg.debug('recipient {} balance before: {}'.format(settings.get('RECIPIENT'), recipient_balance)) except urllib.error.URLError: pass @@ -160,13 +161,14 @@ def main(): settings.get('SENDER_ADDRESS'), settings.get('RPC_ID_GENERATOR'), ) - recipient_balance = balance( - settings.get('CONN'), - settings.get('RECIPIENT'), - settings.get('RPC_ID_GENERATOR'), - ) logg.debug('sender {} balance before: {}'.format(settings.get('SENDER_ADDRESS'), sender_balance)) - logg.debug('recipient {} balance before: {}'.format(settings.get('RECIPIENT'), recipient_balance)) + if settings.get('RECIPIENT') != None: + recipient_balance = balance( + settings.get('CONN'), + settings.get('RECIPIENT'), + settings.get('RPC_ID_GENERATOR'), + ) + logg.debug('recipient {} balance before: {}'.format(settings.get('RECIPIENT'), recipient_balance)) if r['status'] == 0: logg.critical('VM revert for {}. Wish I could tell you more'.format(tx_hash_hex)) sys.exit(1) diff --git a/chainlib/eth/runnable/get.py b/chainlib/eth/runnable/get.py @@ -48,6 +48,7 @@ from chainlib.eth.cli.arg import ( Arg, ArgFlag, process_args, + stdin_arg, ) from chainlib.eth.cli.config import ( Config, @@ -65,14 +66,19 @@ config_dir = os.path.join(script_dir, '..', 'data', 'config') def process_config_local(config, arg, args, flags): - config.add(args.item, '_ITEM', False) + item = config.get('_POSARG') + try: + item = strip_0x(item) + except TypeError: + item = stdin_arg() + item = strip_0x(item) + + config.add(item, '_ITEM', False) return config def process_settings_local(settings, config): item = config.get('_ITEM') - item = strip_0x(item) - if len(item) == 40: config.add(item, '_RECIPIENT', False) elif len(item) == 64: @@ -94,7 +100,7 @@ args = argparser.parse_args() logg = process_log(args, logg) config = Config() -config = process_config(config, arg, args, flags) +config = process_config(config, arg, args, flags, positional_name='item') config = process_config_local(config, arg, args, flags) logg.debug('config loaded:\n{}'.format(config)) diff --git a/chainlib/eth/runnable/info.py b/chainlib/eth/runnable/info.py @@ -68,12 +68,14 @@ results_translation = { def process_config_local(config, arg, args, flags): config.add(args.local, '_LOCAL', False) config.add(args.long, '_LONG', False) - config.add(args.entry, '_ENTRY', False) - if config.get('_ENTRY') != None: + config.add(None, '_ENTRY', False) + if len(args.entry) > 0: + config.add(args.entry[0], '_ENTRY', True) if config.get('_ENTRY') not in results_translation.keys(): raise ValueError('Unknown entry {}'.format(config.get('_ENTRY'))) return config + arg_flags = ArgFlag() arg = Arg(arg_flags) flags = arg_flags.STD_READ | arg_flags.ENV diff --git a/chainlib/eth/runnable/raw.py b/chainlib/eth/runnable/raw.py @@ -46,6 +46,7 @@ from chainlib.eth.cli.arg import ( Arg, ArgFlag, process_args, + stdin_arg, ) from chainlib.eth.cli.config import ( Config, @@ -64,7 +65,16 @@ config_dir = os.path.join(script_dir, '..', 'data', 'config') def process_config_local(config, arg, args, flags): config.add(args.deploy, '_DEPLOY', False) config.add(args.mode, '_MODE', False) - config.add(args.data, '_DATA', False) + data = config.get('_POSARG') + + try: + data = strip_0x(data) + except TypeError: + data = stdin_arg() + data = strip_0x(data) + + config.add(data, '_DATA', False) + return config @@ -82,7 +92,7 @@ args = argparser.parse_args() logg = process_log(args, logg) config = Config() -config = process_config(config, arg, args, flags) +config = process_config(config, arg, args, flags, positional_name='data') config = process_config_local(config, arg, args, flags) logg.debug('config loaded:\n{}'.format(config)) diff --git a/chainlib/eth/runnable/wait.py b/chainlib/eth/runnable/wait.py @@ -47,6 +47,7 @@ from chainlib.eth.cli.arg import ( Arg, ArgFlag, process_args, + stdin_arg, ) from chainlib.eth.cli.config import ( Config, @@ -64,7 +65,16 @@ config_dir = os.path.join(script_dir, '..', 'data', 'config') def process_config_local(config, arg, args, flags): config.add(args.ignore, '_IGNORE', False) config.add(args.ignore_all, '_IGNORE_ALL', False) - config.add(args.hashes, '_HASH', False) + + hsh = config.get('_POSARG') + try: + hsh = strip_0x(hsh) + except TypeError: + hsh = stdin_arg() + hsh = strip_0x(hsh) + + config.add(hsh, '_HASH', False) + return config @@ -87,7 +97,7 @@ args = argparser.parse_args() logg = process_log(args, logg) config = Config() -config = process_config(config, arg, args, flags) +config = process_config(config, arg, args, flags, positional_name='hashes') config = process_config_local(config, arg, args, flags) logg.debug('config loaded:\n{}'.format(config))