diff --git a/chainlib/eth/gas.py b/chainlib/eth/gas.py index ff6ef98..f646a0c 100644 --- a/chainlib/eth/gas.py +++ b/chainlib/eth/gas.py @@ -55,11 +55,13 @@ class Gas(TxFactory): return (tx_hash_hex, o) + class RPCGasOracle: - def __init__(self, conn, code_callback=None): + def __init__(self, conn, code_callback=None, min_price=1): self.conn = conn self.code_callback = code_callback + self.min_price = min_price def get_gas(self, code=None): @@ -69,7 +71,17 @@ class RPCGasOracle: fee_units = MINIMUM_FEE_UNITS if self.code_callback != None: fee_units = self.code_callback(code) - return (int(n, 16), fee_units) + price = int(n, 16) + if price < self.min_price: + logg.debug('adjusting price {} to set minimum {}'.format(price, self.min_price)) + price = self.min_price + return (price, fee_units) + + +class RPCPureGasOracle(RPCGasOracle): + + def __init__(self, conn, code_callback=None): + super(RPCPureGasOracle, self).__init__(conn, code_callback=code_callback, min_price=0) class OverrideGasOracle(RPCGasOracle): @@ -77,12 +89,14 @@ class OverrideGasOracle(RPCGasOracle): def __init__(self, price=None, limit=None, conn=None, code_callback=None): self.conn = None self.code_callback = None - if conn != None: - logg.debug('override gas oracle with rpc fallback') - super(OverrideGasOracle, self).__init__(conn, code_callback) self.limit = limit self.price = price + if self.limit == None or self.price == None: + if conn != None: + logg.debug('override gas oracle with rpc fallback') + super(OverrideGasOracle, self).__init__(conn, code_callback) + def get_gas(self, code=None): r = None diff --git a/chainlib/eth/runnable/gas.py b/chainlib/eth/runnable/gas.py index 2c75e85..7d24872 100644 --- a/chainlib/eth/runnable/gas.py +++ b/chainlib/eth/runnable/gas.py @@ -16,8 +16,9 @@ import os import json import argparse import logging +import urllib -# third-party imports +# external imports from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer from crypto_dev_signer.keystore.dict import DictKeystore from hexathon import ( @@ -57,7 +58,7 @@ argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum ke argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration') argparser.add_argument('--nonce', type=int, help='override nonce') argparser.add_argument('--price', type=int, help='override gas price') -argparser.add_argument('--gas', type=int, help='override gas limit') +argparser.add_argument('--limit', type=int, help='override gas limit') argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress') argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose') @@ -100,8 +101,8 @@ else: nonce_oracle = RPCNonceOracle(signer_address, conn) gas_oracle = None -if args.price or args.gas != None: - gas_oracle = OverrideGasOracle(price=args.price, limit=args.gas, conn=conn) +if args.price or args.limit != None: + gas_oracle = OverrideGasOracle(price=args.price, limit=args.limit, conn=conn) else: gas_oracle = RPCGasOracle(conn) @@ -129,8 +130,11 @@ def main(): logg.info('gas transfer from {} to {} value {}'.format(signer_address, recipient, value)) if logg.isEnabledFor(logging.DEBUG): - logg.debug('sender {} balance before: {}'.format(signer_address, balance(signer_address))) - logg.debug('recipient {} balance before: {}'.format(recipient, balance(recipient))) + try: + logg.debug('sender {} balance before: {}'.format(signer_address, balance(signer_address))) + logg.debug('recipient {} balance before: {}'.format(recipient, balance(recipient))) + except urllib.error.URLError: + pass (tx_hash_hex, o) = g.create(signer_address, recipient, value) diff --git a/chainlib/eth/runnable/info.py b/chainlib/eth/runnable/info.py index e9f5de9..8ea6ec8 100644 --- a/chainlib/eth/runnable/info.py +++ b/chainlib/eth/runnable/info.py @@ -10,6 +10,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later # standard imports +import datetime import sys import os import json @@ -34,7 +35,11 @@ from chainlib.jsonrpc import ( jsonrpc_template, jsonrpc_result, ) -from chainlib.eth.block import block_latest +from chainlib.eth.block import ( + block_latest, + block_by_number, + Block, + ) from chainlib.eth.tx import count from chainlib.eth.erc20 import ERC20 from chainlib.eth.connection import EthHTTPConnection @@ -45,6 +50,8 @@ from chainlib.eth.gas import ( ) from chainlib.chain import ChainSpec +BLOCK_SAMPLES = 10 + logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() @@ -56,6 +63,7 @@ argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provide argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string') argparser.add_argument('-H', '--human', dest='human', action='store_true', help='Use human-friendly formatting') argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress') +argparser.add_argument('-l', '--long', dest='l', action='store_true', help='Calculate averages through sampling of blocks and txs') argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Include summary for keyfile') @@ -71,34 +79,15 @@ elif args.v: signer = None holder_address = None if args.address != None: - if not args.u and is_checksum_address(args.address): + if not args.u and not is_checksum_address(args.address): raise ValueError('invalid checksum address {}'.format(args.address)) - holder_address = add_0x(args.address) + holder_address = add_0x(args.address) elif args.y != None: f = open(args.y, 'r') o = json.load(f) f.close() holder_address = add_0x(to_checksum_address(o['address'])) - -#if holder_address != None: -# passphrase_env = 'ETH_PASSPHRASE' -# if args.env_prefix != None: -# passphrase_env = args.env_prefix + '_' + passphrase_env -# passphrase = os.environ.get(passphrase_env) -# logg.error('pass {}'.format(passphrase_env)) -# if passphrase == None: -# logg.warning('no passphrase given') -# passphrase='' -# -# holder_address = None -# keystore = DictKeystore() -# if args.y != None: -# logg.debug('loading keystore file {}'.format(args.y)) -# signer_address = keystore.import_keystore_file(args.y, password=passphrase) -# logg.debug('now have key for signer address {}'.format(signer_address)) -# signer = EIP155Signer(keystore) - conn = EthHTTPConnection(args.p) gas_oracle = OverrideGasOracle(conn) @@ -108,15 +97,45 @@ chain_spec = ChainSpec.from_chain_str(args.i) human = args.human +longmode = args.l def main(): + o = block_latest() r = conn.do(o) n = int(r, 16) + first_block_number = n if human: n = format(n, ',') sys.stdout.write('Block: {}\n'.format(n)) + o = block_by_number(first_block_number, False) + r = conn.do(o) + last_block = Block(r) + last_timestamp = last_block.timestamp + + if longmode: + aggr_time = 0.0 + aggr_gas = 0 + for i in range(BLOCK_SAMPLES): + o = block_by_number(first_block_number-i, False) + r = conn.do(o) + block = Block(r) + aggr_time += last_block.timestamp - block.timestamp + + gas_limit = int(r['gasLimit'], 16) + aggr_gas += gas_limit + + last_block = block + last_timestamp = block.timestamp + + n = int(aggr_gas / BLOCK_SAMPLES) + if human: + n = format(n, ',') + + sys.stdout.write('Gaslimit: {}\n'.format(n)) + sys.stdout.write('Blocktime: {}\n'.format(aggr_time / BLOCK_SAMPLES)) + o = price() r = conn.do(o) n = int(r, 16) diff --git a/chainlib/eth/runnable/transfer.py b/chainlib/eth/runnable/transfer.py index 1c18713..fae11a5 100644 --- a/chainlib/eth/runnable/transfer.py +++ b/chainlib/eth/runnable/transfer.py @@ -58,8 +58,8 @@ argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFI argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress') argparser.add_argument('-s', '--send', dest='s', action='store_true', help='Send to network') argparser.add_argument('--nonce', type=int, help='Override nonce') -argparser.add_argument('--gas-price', dest='gas_price', type=int, help='Override gas price') -argparser.add_argument('--gas-limit', dest='gas_limit', type=int, help='Override gas limit') +argparser.add_argument('--price', type=int, help='Override gas price') +argparser.add_argument('--limit', type=int, help='Override gas limit') argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('recipient', type=str, help='Recipient account address') @@ -104,8 +104,8 @@ def _max_gas(code=None): return 8000000 gas_oracle = None -if args.gas_price != None: - gas_oracle = OverrideGasOracle(args.gas_price, args.gas_limit) +if args.price != None: + gas_oracle = OverrideGasOracle(args.price, args.limit) else: gas_oracle = RPCGasOracle(conn, code_callback=_max_gas)