Implement minimum gas return of 1 in default gas oracle rpc

This commit is contained in:
nolash 2021-04-09 16:06:19 +02:00
parent 72f49841f8
commit bff46748be
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 74 additions and 37 deletions

View File

@ -55,11 +55,13 @@ class Gas(TxFactory):
return (tx_hash_hex, o) return (tx_hash_hex, o)
class RPCGasOracle: class RPCGasOracle:
def __init__(self, conn, code_callback=None): def __init__(self, conn, code_callback=None, min_price=1):
self.conn = conn self.conn = conn
self.code_callback = code_callback self.code_callback = code_callback
self.min_price = min_price
def get_gas(self, code=None): def get_gas(self, code=None):
@ -69,7 +71,17 @@ class RPCGasOracle:
fee_units = MINIMUM_FEE_UNITS fee_units = MINIMUM_FEE_UNITS
if self.code_callback != None: if self.code_callback != None:
fee_units = self.code_callback(code) 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): class OverrideGasOracle(RPCGasOracle):
@ -77,12 +89,14 @@ class OverrideGasOracle(RPCGasOracle):
def __init__(self, price=None, limit=None, conn=None, code_callback=None): def __init__(self, price=None, limit=None, conn=None, code_callback=None):
self.conn = None self.conn = None
self.code_callback = 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.limit = limit
self.price = price 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): def get_gas(self, code=None):
r = None r = None

View File

@ -16,8 +16,9 @@ import os
import json import json
import argparse import argparse
import logging import logging
import urllib
# third-party imports # external imports
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore.dict import DictKeystore from crypto_dev_signer.keystore.dict import DictKeystore
from hexathon import ( 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('--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('--nonce', type=int, help='override nonce')
argparser.add_argument('--price', type=int, help='override gas price') 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('-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('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose')
@ -100,8 +101,8 @@ else:
nonce_oracle = RPCNonceOracle(signer_address, conn) nonce_oracle = RPCNonceOracle(signer_address, conn)
gas_oracle = None gas_oracle = None
if args.price or args.gas != None: if args.price or args.limit != None:
gas_oracle = OverrideGasOracle(price=args.price, limit=args.gas, conn=conn) gas_oracle = OverrideGasOracle(price=args.price, limit=args.limit, conn=conn)
else: else:
gas_oracle = RPCGasOracle(conn) gas_oracle = RPCGasOracle(conn)
@ -129,8 +130,11 @@ def main():
logg.info('gas transfer from {} to {} value {}'.format(signer_address, recipient, value)) logg.info('gas transfer from {} to {} value {}'.format(signer_address, recipient, value))
if logg.isEnabledFor(logging.DEBUG): if logg.isEnabledFor(logging.DEBUG):
logg.debug('sender {} balance before: {}'.format(signer_address, balance(signer_address))) try:
logg.debug('recipient {} balance before: {}'.format(recipient, balance(recipient))) 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) (tx_hash_hex, o) = g.create(signer_address, recipient, value)

View File

@ -10,6 +10,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
# standard imports # standard imports
import datetime
import sys import sys
import os import os
import json import json
@ -34,7 +35,11 @@ from chainlib.jsonrpc import (
jsonrpc_template, jsonrpc_template,
jsonrpc_result, 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.tx import count
from chainlib.eth.erc20 import ERC20 from chainlib.eth.erc20 import ERC20
from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.connection import EthHTTPConnection
@ -45,6 +50,8 @@ from chainlib.eth.gas import (
) )
from chainlib.chain import ChainSpec from chainlib.chain import ChainSpec
BLOCK_SAMPLES = 10
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() 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('-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('-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('-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('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more 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') argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Include summary for keyfile')
@ -71,34 +79,15 @@ elif args.v:
signer = None signer = None
holder_address = None holder_address = None
if args.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)) raise ValueError('invalid checksum address {}'.format(args.address))
holder_address = add_0x(args.address) holder_address = add_0x(args.address)
elif args.y != None: elif args.y != None:
f = open(args.y, 'r') f = open(args.y, 'r')
o = json.load(f) o = json.load(f)
f.close() f.close()
holder_address = add_0x(to_checksum_address(o['address'])) 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) conn = EthHTTPConnection(args.p)
gas_oracle = OverrideGasOracle(conn) gas_oracle = OverrideGasOracle(conn)
@ -108,15 +97,45 @@ chain_spec = ChainSpec.from_chain_str(args.i)
human = args.human human = args.human
longmode = args.l
def main(): def main():
o = block_latest() o = block_latest()
r = conn.do(o) r = conn.do(o)
n = int(r, 16) n = int(r, 16)
first_block_number = n
if human: if human:
n = format(n, ',') n = format(n, ',')
sys.stdout.write('Block: {}\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() o = price()
r = conn.do(o) r = conn.do(o)
n = int(r, 16) n = int(r, 16)

View File

@ -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('-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('-s', '--send', dest='s', action='store_true', help='Send to network')
argparser.add_argument('--nonce', type=int, help='Override nonce') 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('--price', type=int, help='Override gas price')
argparser.add_argument('--gas-limit', dest='gas_limit', type=int, help='Override gas limit') argparser.add_argument('--limit', type=int, help='Override gas limit')
argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('recipient', type=str, help='Recipient account address') argparser.add_argument('recipient', type=str, help='Recipient account address')
@ -104,8 +104,8 @@ def _max_gas(code=None):
return 8000000 return 8000000
gas_oracle = None gas_oracle = None
if args.gas_price != None: if args.price != None:
gas_oracle = OverrideGasOracle(args.gas_price, args.gas_limit) gas_oracle = OverrideGasOracle(args.price, args.limit)
else: else:
gas_oracle = RPCGasOracle(conn, code_callback=_max_gas) gas_oracle = RPCGasOracle(conn, code_callback=_max_gas)