From 7039a8ddac22347d1a3a44700bbcc8feb95bbc76 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Sun, 25 Jul 2021 10:37:15 -0400 Subject: [PATCH 01/16] update web3 --- requirements.txt | 2 +- setup.py | 2 +- tests/manual_test_async_tx.py | 5 +---- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 539a47a9..f013da48 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ pytz == 2017.3 -web3 == 5.12.0 +web3 == 5.21.0 requests == 2.22.0 eth-keys<0.3.0,>=0.2.1 jsonnet == 0.9.5 diff --git a/setup.py b/setup.py index c6b78633..52ac1a60 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ # For a discussion on single-sourcing the version across setup.py and the # project code, see # https://packaging.python.org/en/latest/single_source_version.html - version='1.1.3', # Required + version='1.9.0', # Required description='Python API for Maker contracts', license='COPYING', long_description=long_description, diff --git a/tests/manual_test_async_tx.py b/tests/manual_test_async_tx.py index 26db2e64..c8d8f141 100644 --- a/tests/manual_test_async_tx.py +++ b/tests/manual_test_async_tx.py @@ -51,7 +51,7 @@ class TestApp: def main(self): - self.test_replacement() + # self.test_replacement() self.test_simultaneous() self.shutdown() @@ -79,9 +79,6 @@ def shutdown(self): if Wad(0) < balance < Wad(100): # this account's tiny WETH balance came from this test logging.info(f"Unwrapping {balance} WETH") assert weth.withdraw(balance).transact(gas_price=fast_gas) - elif balance >= Wad(22): # user already had a balance, so unwrap what a successful test would have consumed - logging.info(f"Unwrapping 12 WETH") - assert weth.withdraw(Wad(22)).transact(gas_price=fast_gas) @staticmethod def _run_future(future): From 233e9f02e9283d42aa66293a0f689e130ecaa160 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Mon, 26 Jul 2021 11:08:35 -0400 Subject: [PATCH 02/16] handled event type change --- pymaker/logging.py | 6 +++--- tests/test_general2.py | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pymaker/logging.py b/pymaker/logging.py index 7907d7c9..4d14ece6 100644 --- a/pymaker/logging.py +++ b/pymaker/logging.py @@ -18,7 +18,7 @@ import logging from pprint import pformat from web3 import Web3 -from web3._utils.events import get_event_data +from web3._utils.events import AttributeDict, get_event_data from eth_abi.codec import ABICodec from eth_abi.registry import registry as default_registry @@ -37,8 +37,8 @@ def __init__(self, log): self._data = args['data'] @classmethod - def from_event(cls, event: dict, contract_abi: list): - assert isinstance(event, dict) + def from_event(cls, event: AttributeDict, contract_abi: list): + assert isinstance(event, AttributeDict) assert isinstance(contract_abi, list) log_note_abi = [abi for abi in contract_abi if abi.get('name') == 'LogNote'][0] diff --git a/tests/test_general2.py b/tests/test_general2.py index 83a4bd9d..424db949 100644 --- a/tests/test_general2.py +++ b/tests/test_general2.py @@ -221,6 +221,7 @@ def setup_method(self): self.token = DSToken.deploy(self.web3, 'ABC') self.token.mint(Wad(1000000)).transact() + @pytest.mark.skip("Using Web3 5.21.0, transactions sent to Ganache succeed immediately, thus cannot be replaced") @pytest.mark.asyncio async def test_transaction_replace(self): # given From 2d85f25dc4f749f871d8fbe001ec7704bbe51340 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Tue, 27 Jul 2021 22:45:17 -0400 Subject: [PATCH 03/16] fixed issue with contract deployment, skip tests we cannot do with latest Web3 and Ganache --- docker-compose.yml | 4 +-- pymaker/__init__.py | 15 ++++++++-- pymaker/deployment.py | 13 ++++----- tests/manual_test_token.py | 60 ++++++++++++++++++++++++++++++++++++++ tests/test_general2.py | 12 ++++---- tests/test_token.py | 10 ++++--- 6 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 tests/manual_test_token.py diff --git a/docker-compose.yml b/docker-compose.yml index b038f326..a8fcc762 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,14 +13,14 @@ services: working_dir: /home/parity ganache: - image: trufflesuite/ganache-cli:v6.9.1 + image: trufflesuite/ganache-cli:v6.12.2 container_name: ganache ports: - "8555:8555" expose: - "8555" command: "--gasLimit 10000000 - -p 8555 + -p 8555 --blockTime 1 --account=\"0x91cf2cc3671a365fcbf38010ff97ee31a5b7e674842663c56769e41600696ead,1000000000000000000000000\" --account=\"0xc0a550404067ce46a51283e0cc99ec3ba832940064587147a8db9a7ba355ef27,1000000000000000000000000\", --account=\"0x6ca1cfaba9715aa485504cb8a3d3fe54191e0991b5f47eb982e8fb40d1b8e8d8,1000000000000000000000000\", diff --git a/pymaker/__init__.py b/pymaker/__init__.py index b1f0805b..442e5fc8 100644 --- a/pymaker/__init__.py +++ b/pymaker/__init__.py @@ -189,7 +189,8 @@ class Contract: logger = logging.getLogger() @staticmethod - def _deploy(web3: Web3, abi: list, bytecode: str, args: list) -> Address: + def _deploy(web3: Web3, abi: list, bytecode: str, args: list = [], timeout=60) -> Address: + """Meant to be called by a subclass, deploy the contract to the connected chain""" assert(isinstance(web3, Web3)) assert(isinstance(abi, list)) assert(isinstance(bytecode, str)) @@ -198,8 +199,16 @@ def _deploy(web3: Web3, abi: list, bytecode: str, args: list) -> Address: contract = web3.eth.contract(abi=abi, bytecode=bytecode) tx_hash = contract.constructor(*args).transact( transaction={'from': eth_utils.to_checksum_address(web3.eth.defaultAccount)}) - receipt = web3.eth.getTransactionReceipt(tx_hash) - return Address(receipt['contractAddress']) + + submitted = time.time() + while time.time() - submitted < timeout: + try: + receipt = web3.eth.getTransactionReceipt(tx_hash) + return Address(receipt['contractAddress']) + except TransactionNotFound: + time.sleep(1) + + raise RuntimeError("Timeout out awaiting receipt for contract deployment") @staticmethod def _get_contract(web3: Web3, abi: list, address: Address): diff --git a/pymaker/deployment.py b/pymaker/deployment.py index 7ef3260f..bd99c03f 100644 --- a/pymaker/deployment.py +++ b/pymaker/deployment.py @@ -18,13 +18,15 @@ import json import os import re +import time +import warnings from typing import Dict, List, Optional import pkg_resources from pymaker.auctions import Clipper, Flapper, Flipper, Flopper from web3 import Web3, HTTPProvider -from pymaker import Address +from pymaker import Address, Contract from pymaker.approval import directly, hope_directly from pymaker.auth import DSGuard from pymaker.etherdelta import EtherDelta @@ -46,6 +48,8 @@ def deploy_contract(web3: Web3, contract_name: str, args: Optional[list] = None) -> Address: + warnings.warn("DEPRECATED: Please subclass Contract and call Contract._deploy instead.", + category=DeprecationWarning, stacklevel=2) """Deploys a new contract. Args: @@ -62,12 +66,7 @@ def deploy_contract(web3: Web3, contract_name: str, args: Optional[list] = None) abi = json.loads(pkg_resources.resource_string('pymaker.deployment', f'abi/{contract_name}.abi')) bytecode = str(pkg_resources.resource_string('pymaker.deployment', f'abi/{contract_name}.bin'), 'utf-8') - if args is not None: - tx_hash = web3.eth.contract(abi=abi, bytecode=bytecode).constructor(*args).transact() - else: - tx_hash = web3.eth.contract(abi=abi, bytecode=bytecode).constructor().transact() - receipt = web3.eth.getTransactionReceipt(tx_hash) - return Address(receipt['contractAddress']) + return Contract._deploy(web3, abi, bytecode, args if args else []) class Deployment: diff --git a/tests/manual_test_token.py b/tests/manual_test_token.py new file mode 100644 index 00000000..c2d1949f --- /dev/null +++ b/tests/manual_test_token.py @@ -0,0 +1,60 @@ +# This file is part of Maker Keeper Framework. +# +# Copyright (C) 2021 EdNoepel +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import logging +import os +import sys +import time + +from pymaker import Address, web3_via_http +from pymaker.keys import register_keys +from pymaker.numeric import Wad +from pymaker.token import DSToken + +logging.basicConfig(format='%(asctime)-15s %(levelname)-8s %(message)s', level=logging.DEBUG) +# reduce logspew +logging.getLogger('urllib3').setLevel(logging.INFO) +logging.getLogger("web3").setLevel(logging.INFO) +logging.getLogger("asyncio").setLevel(logging.INFO) +logging.getLogger("requests").setLevel(logging.INFO) + +endpoint_uri = os.environ['ETH_RPC_URL'] +web3 = web3_via_http(endpoint_uri, timeout=10) +print(web3.clientVersion) + +""" +Please set environment ETH_RPC_URL to your Ethereum node URI + +Argument: Reqd? Example: +Ethereum address yes 0x0000000000000000000000000000000aBcdef123 +Private key yes key_file=~keys/default-account.json,pass_file=~keys/default-account.pass +Action yes token address to mint existing DSToken, symbol to deploy a new token +""" + +web3.eth.defaultAccount = sys.argv[1] +register_keys(web3, [sys.argv[2]]) +our_address = Address(web3.eth.defaultAccount) +action = sys.argv[3] + +if action.startswith("0x"): + token = DSToken(web3, Address(action)) + token.mint_to(our_address, Wad.from_number(100)).transact() +else: + symbol = action + assert len(symbol) < 6 # Most token symbols are under 6 characters; were you really trying to deploy a new token? + token = DSToken.deploy(web3, symbol) + print(f"{symbol} token deployed to {token.address.address}") diff --git a/tests/test_general2.py b/tests/test_general2.py index 424db949..679a30f1 100644 --- a/tests/test_general2.py +++ b/tests/test_general2.py @@ -20,7 +20,8 @@ from mock import MagicMock from web3 import Web3, HTTPProvider -from pymaker import Address, eth_transfer, get_pending_transactions, RecoveredTransact, TransactStatus, Calldata, Receipt +from pymaker import Address, Calldata, eth_transfer, get_pending_transactions, \ + Receipt, RecoveredTransact, TransactStatus from pymaker.gas import FixedGasPrice from pymaker.numeric import Wad from pymaker.proxy import DSProxy, DSProxyCache @@ -221,7 +222,7 @@ def setup_method(self): self.token = DSToken.deploy(self.web3, 'ABC') self.token.mint(Wad(1000000)).transact() - @pytest.mark.skip("Using Web3 5.21.0, transactions sent to Ganache succeed immediately, thus cannot be replaced") + @pytest.mark.skip("Using Web3 5.21.0, transactions sent to Ganache cannot be replaced") @pytest.mark.asyncio async def test_transaction_replace(self): # given @@ -234,9 +235,9 @@ async def test_transaction_replace(self): self.web3.eth.getTransaction = MagicMock(return_value={'nonce': nonce}) # and transact_1 = self.token.transfer(self.second_address, Wad(500)) - future_receipt_1 = asyncio.ensure_future(transact_1.transact_async(gas_price=FixedGasPrice(100000))) + future_receipt_1 = asyncio.ensure_future(transact_1.transact_async(gas_price=FixedGasPrice(1))) # and - await asyncio.sleep(2) + await asyncio.sleep(0.2) # then assert future_receipt_1.done() is False assert self.token.balance_of(self.second_address) == Wad(0) @@ -246,10 +247,11 @@ async def test_transaction_replace(self): self.web3.eth.getTransaction = original_get_transaction # and transact_2 = self.token.transfer(self.third_address, Wad(700)) + # FIXME: Ganache produces a "the tx doesn't have the correct nonce" error. future_receipt_2 = asyncio.ensure_future(transact_2.transact_async(replace=transact_1, gas_price=FixedGasPrice(150000))) # and - await asyncio.sleep(10) + await asyncio.sleep(2) # then assert transact_1.status == TransactStatus.FINISHED assert future_receipt_1.done() diff --git a/tests/test_token.py b/tests/test_token.py index 5ce9bcc5..20ed7788 100644 --- a/tests/test_token.py +++ b/tests/test_token.py @@ -102,21 +102,23 @@ def test_transfer_failed_async(self): assert self.token.balance_of(self.our_address) == Wad(1000000) assert self.token.balance_of(self.second_address) == Wad(0) + @pytest.mark.timeout(10) def test_transfer_out_of_gas(self): # when - with pytest.raises(Exception): - self.token.transfer(self.second_address, Wad(500)).transact(gas=26000) + receipt = self.token.transfer(self.second_address, Wad(500)).transact(gas=26000) # then + assert receipt is None assert self.token.balance_of(self.our_address) == Wad(1000000) assert self.token.balance_of(self.second_address) == Wad(0) + @pytest.mark.timeout(10) def test_transfer_out_of_gas_async(self): # when - with pytest.raises(Exception): - synchronize([self.token.transfer(self.second_address, Wad(500)).transact_async(gas=26000)])[0] + receipt = synchronize([self.token.transfer(self.second_address, Wad(500)).transact_async(gas=26000)])[0] # then + assert receipt is None assert self.token.balance_of(self.our_address) == Wad(1000000) assert self.token.balance_of(self.second_address) == Wad(0) From ca284bfbfe415494ec01ada62a9dc110e7818603 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Fri, 13 Aug 2021 09:47:27 -0400 Subject: [PATCH 04/16] naive yet functional implementation of type 2 gas scaling --- pymaker/__init__.py | 152 ++++++++++++++++++--------- pymaker/gas.py | 178 ++++++++++++++++++------------- requirements.txt | 5 +- tests/manual_test_goerli.py | 12 ++- tests/test_gas.py | 204 ++++++++++++++++++------------------ tests/test_general2.py | 12 +-- 6 files changed, 326 insertions(+), 237 deletions(-) diff --git a/pymaker/__init__.py b/pymaker/__init__.py index 442e5fc8..dda21f30 100644 --- a/pymaker/__init__.py +++ b/pymaker/__init__.py @@ -18,6 +18,7 @@ import asyncio import json import logging +import pprint import re import requests import sys @@ -41,12 +42,12 @@ from eth_abi.codec import ABICodec from eth_abi.registry import registry as default_registry -from pymaker.gas import DefaultGasPrice, GasPrice +from pymaker.gas import DefaultGasPrice, GasStrategy from pymaker.numeric import Wad from pymaker.util import synchronize, bytes_to_hexstring, is_contract_at filter_threads = [] -nonce_calc = WeakKeyDictionary() +endpoint_behavior = WeakKeyDictionary() next_nonce = {} transaction_lock = Lock() logger = logging.getLogger() @@ -76,24 +77,46 @@ class NonceCalculation(Enum): PARITY_SERIAL = auto() -def _get_nonce_calc(web3: Web3) -> NonceCalculation: +class EndpointBehavior: + def __init__(self, nonce_calc: NonceCalculation, supports_london: bool): + assert isinstance(nonce_calc, NonceCalculation) + assert isinstance(supports_london, bool) + self.nonce_calc = nonce_calc + self.supports_london = supports_london + + def __repr__(self): + if self.supports_london: + return f"{self.nonce_calc} with EIP 1559 support" + else: + return f"{self.nonce_calc} without EIP 1559 support" + + +def _get_endpoint_behavior(web3: Web3) -> EndpointBehavior: assert isinstance(web3, Web3) - global nonce_calc - if web3 not in nonce_calc: + global endpoint_behavior + if web3 not in endpoint_behavior: + + # Determine nonce calculation providers_without_nonce_calculation = ['infura', 'quiknode'] requires_serial_nonce = any(provider in web3.manager.provider.endpoint_uri for provider in providers_without_nonce_calculation) is_parity = "parity" in web3.clientVersion.lower() or "openethereum" in web3.clientVersion.lower() if is_parity and requires_serial_nonce: - nonce_calc[web3] = NonceCalculation.PARITY_SERIAL + nonce_calc = NonceCalculation.PARITY_SERIAL elif requires_serial_nonce: - nonce_calc[web3] = NonceCalculation.SERIAL + nonce_calc = NonceCalculation.SERIAL elif is_parity: - nonce_calc[web3] = NonceCalculation.PARITY_NEXTNONCE + nonce_calc = NonceCalculation.PARITY_NEXTNONCE else: - nonce_calc[web3] = NonceCalculation.TX_COUNT - logger.debug(f"node clientVersion={web3.clientVersion}, will use {nonce_calc[web3]}") - return nonce_calc[web3] + nonce_calc = NonceCalculation.TX_COUNT + + # Check for EIP 1559 gas parameters support + supports_london = 'baseFeePerGas' in web3.eth.get_block('latest') + + behavior = EndpointBehavior(nonce_calc, supports_london) + endpoint_behavior[web3] = behavior + logger.debug(f"node clientVersion={web3.clientVersion}, will use {behavior}") + return endpoint_behavior[web3] def register_filter_thread(filter_thread): @@ -414,7 +437,8 @@ def get_pending_transactions(web3: Web3, address: Address = None) -> list: address = Address(web3.eth.defaultAccount) # Get the list of pending transactions and their details from specified sources - if _get_nonce_calc(web3) in (NonceCalculation.PARITY_NEXTNONCE, NonceCalculation.PARITY_SERIAL): + nonce_calc = _get_endpoint_behavior(web3).nonce_calc + if False and nonce_calc in (NonceCalculation.PARITY_NEXTNONCE, NonceCalculation.PARITY_SERIAL): items = web3.manager.request_blocking("parity_pendingTransactions", []) items = filter(lambda item: item['from'].lower() == address.address.lower(), items) items = filter(lambda item: item['blockNumber'] is None, items) @@ -471,8 +495,8 @@ def __init__(self, self.status = TransactStatus.NEW self.nonce = None self.replaced = False - self.gas_price = None - self.gas_price_last = 0 + self.gas_strategy = None + self.gas_strategy_last = 0 self.tx_hashes = [] def _get_receipt(self, transaction_hash: str) -> Optional[Receipt]: @@ -503,26 +527,51 @@ def _gas(self, gas_estimate: int, **kwargs) -> int: else: return gas_estimate + 100000 - def _func(self, from_account: str, gas: int, gas_price: Optional[int], nonce: Optional[int]): - gas_price_dict = {'gasPrice': gas_price} if gas_price is not None else {} - nonce_dict = {'nonce': nonce} if nonce is not None else {} + def _gas_params(self, seconds_elapsed: int, gas_strategy: GasStrategy) -> dict: + assert isinstance(seconds_elapsed, int) + assert isinstance(gas_strategy, GasStrategy) + gas_price = gas_strategy.get_gas_price(seconds_elapsed) + gas_feecap, gas_tip = gas_strategy.get_gas_fees(seconds_elapsed) + + if _get_endpoint_behavior(self.web3).supports_london and gas_feecap and gas_tip: + params = {'maxFeePerGas': gas_feecap, + 'maxPriorityFeePerGas': gas_tip} + elif gas_price: + params = {'gasPrice': gas_price} + else: + params = {} + return params + + @staticmethod + def _gas_exceeds_replacement_threshold(prev_gas_params: dict, current_gas_params: dict): + # TODO: Can a type 0 TX be replaced with a type 2 TX? Vice-versa? + if 'gasPrice' in prev_gas_params and 'gasPrice' in current_gas_params: + return current_gas_params['gasPrice'] > prev_gas_params['gasPrice'] * 1.125 + # TODO: Implement EIP-1559 support + return False + + def _func(self, from_account: str, gas: int, gas_price_params: dict, nonce: Optional[int]): + assert isinstance(from_account, str) + assert isinstance(gas_price_params, dict) + assert isinstance(nonce, int) or nonce is None + + nonce_dict = {'nonce': nonce} if nonce is not None else {} transaction_params = {**{'from': from_account, 'gas': gas}, - **gas_price_dict, + **gas_price_params, **nonce_dict, **self._as_dict(self.extra)} - if self.contract is not None: if self.function_name is None: - return bytes_to_hexstring(self.web3.eth.sendTransaction({**transaction_params, - **{'to': self.address.address, - 'data': self.parameters[0]}})) + return bytes_to_hexstring(self.web3.eth.send_transaction({**transaction_params, + **{'to': self.address.address, + 'data': self.parameters[0]}})) else: return bytes_to_hexstring(self._contract_function().transact(transaction_params)) else: - return bytes_to_hexstring(self.web3.eth.sendTransaction({**transaction_params, - **{'to': self.address.address}})) + return bytes_to_hexstring(self.web3.eth.send_transaction({**transaction_params, + **{'to': self.address.address}})) def _contract_function(self): if '(' in self.function_name: @@ -629,7 +678,7 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: global next_nonce self.initial_time = time.time() - unknown_kwargs = set(kwargs.keys()) - {'from_address', 'replace', 'gas', 'gas_buffer', 'gas_price'} + unknown_kwargs = set(kwargs.keys()) - {'from_address', 'replace', 'gas', 'gas_buffer', 'gas_strategy'} if len(unknown_kwargs) > 0: raise ValueError(f"Unknown kwargs: {unknown_kwargs}") @@ -653,10 +702,11 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: self.logger.warning(f"Transaction {self.name()} will fail, refusing to send ({sys.exc_info()[1]})") return None - # Get or calculate `gas`. Get `gas_price`, which in fact refers to a gas pricing algorithm. + # Get or calculate `gas`. Get `gas_strategy`, which in fact refers to a gas pricing algorithm. gas = self._gas(gas_estimate, **kwargs) - self.gas_price = kwargs['gas_price'] if ('gas_price' in kwargs) else DefaultGasPrice() - assert(isinstance(self.gas_price, GasPrice)) + self.gas_strategy = kwargs['gas_strategy'] if ('gas_strategy' in kwargs) else DefaultGasPrice() + assert(isinstance(self.gas_strategy, GasStrategy)) + gas_params_last = None # Get the transaction this one is supposed to replace. # If there is one, try to borrow the nonce from it as long as that transaction isn't finished. @@ -670,9 +720,9 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: # Gas should be calculated from the original time of submission self.initial_time = replaced_tx.initial_time if replaced_tx.initial_time else time.time() # Use gas strategy from the original transaction if one was not provided - if 'gas_price' not in kwargs: - self.gas_price = replaced_tx.gas_price if replaced_tx.gas_price else DefaultGasPrice() - self.gas_price_last = replaced_tx.gas_price_last + if 'gas_strategy' not in kwargs: + self.gas_strategy = replaced_tx.gas_strategy if replaced_tx.gas_strategy else DefaultGasPrice() + self.gas_strategy_last = replaced_tx.gas_strategy_last # Detain replacement until gas strategy produces a price acceptable to the node if replaced_tx.tx_hashes: most_recent_tx = replaced_tx.tx_hashes[-1] @@ -680,6 +730,7 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: while True: seconds_elapsed = int(time.time() - self.initial_time) + gas_params = self._gas_params(seconds_elapsed, self.gas_strategy) # CAUTION: if transact_async is called rapidly, we will hammer the node with these JSON-RPC requests if self.nonce is not None and self.web3.eth.getTransactionCount(from_account) > self.nonce: @@ -721,27 +772,27 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: # - no transaction has been sent yet, or # - the requested gas price has changed enough since the last transaction has been sent # - the gas price on a replacement has sufficiently exceeded that of the original transaction - gas_price_value = self.gas_price.get_gas_price(seconds_elapsed) transaction_was_sent = len(self.tx_hashes) > 0 or (replaced_tx is not None and len(replaced_tx.tx_hashes) > 0) # Uncomment this to debug state during transaction submission - # self.logger.debug(f"Transaction {self.name()} is churning: was_sent={transaction_was_sent}, gas_price_value={gas_price_value} gas_price_last={self.gas_price_last}") - if not transaction_was_sent or (gas_price_value is not None and gas_price_value > self.gas_price_last * 1.125): - self.gas_price_last = gas_price_value + # self.logger.debug(f"Transaction {self.name()} is churning: was_sent={transaction_was_sent}") + # TODO: For EIP-1559 transactions, both maxFeePerGas and maxPriorityFeePerGas need to be bumped 12.5% to replace. + if not transaction_was_sent or (gas_params_last and self._gas_exceeds_replacement_threshold(gas_params_last, gas_params)): + gas_params_last = gas_params try: # We need the lock in order to not try to send two transactions with the same nonce. with transaction_lock: if self.nonce is None: - nonce_calculation = _get_nonce_calc(self.web3) - if nonce_calculation == NonceCalculation.PARITY_NEXTNONCE: + nonce_calc = _get_endpoint_behavior(self.web3).nonce_calc + if nonce_calc == NonceCalculation.PARITY_NEXTNONCE: self.nonce = int(self.web3.manager.request_blocking("parity_nextNonce", [from_account]), 16) - elif nonce_calculation == NonceCalculation.TX_COUNT: + elif nonce_calc == NonceCalculation.TX_COUNT: self.nonce = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') - elif nonce_calculation == NonceCalculation.SERIAL: + elif nonce_calc == NonceCalculation.SERIAL: tx_count = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') next_serial = next_nonce[from_account] self.nonce = max(tx_count, next_serial) - elif nonce_calculation == NonceCalculation.PARITY_SERIAL: + elif nonce_calc == NonceCalculation.PARITY_SERIAL: tx_count = int(self.web3.manager.request_blocking("parity_nextNonce", [from_account]), 16) next_serial = next_nonce[from_account] self.nonce = max(tx_count, next_serial) @@ -752,16 +803,15 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: self.logger.info(f"Transaction {self.name()} with nonce={self.nonce} was replaced") return None - tx_hash = self._func(from_account, gas, gas_price_value, self.nonce) + tx_hash = self._func(from_account, gas, gas_params, self.nonce) self.tx_hashes.append(tx_hash) self.logger.info(f"Sent transaction {self.name()} with nonce={self.nonce}, gas={gas}," - f" gas_price={gas_price_value if gas_price_value is not None else 'default'}" + f" gas_params={gas_params if gas_params else 'default'}" f" (tx_hash={tx_hash})") except Exception as e: self.logger.warning(f"Failed to send transaction {self.name()} with nonce={self.nonce}, gas={gas}," - f" gas_price={gas_price_value if gas_price_value is not None else 'default'}" - f" ({e})") + f" gas_params={gas_params if gas_params else 'default'} ({e})") if len(self.tx_hashes) == 0: raise @@ -782,6 +832,7 @@ def invocation(self) -> Invocation: return Invocation(self.address, Calldata(self._contract_function()._encode_transaction_data())) +# TODO: Add EIP-1559 support. class RecoveredTransact(Transact): """ Models a pending transaction retrieved from the mempool. @@ -804,6 +855,7 @@ def __init__(self, web3: Web3, self.nonce = nonce self.tx_hashes.append(latest_tx_hash) self.current_gas = current_gas + self.gas_price_last = None def name(self): return f"Recovered tx with nonce {self.nonce}" @@ -813,22 +865,22 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: # TODO: Read transaction data from chain, create a new state machine to manage gas for the transaction. raise NotImplementedError() - def cancel(self, gas_price: GasPrice): - return synchronize([self.cancel_async(gas_price)])[0] + def cancel(self, gas_strategy: GasStrategy): + return synchronize([self.cancel_async(gas_strategy)])[0] - async def cancel_async(self, gas_price: GasPrice): - assert isinstance(gas_price, GasPrice) + async def cancel_async(self, gas_strategy: GasStrategy): + assert isinstance(gas_strategy, GasStrategy) initial_time = time.time() self.gas_price_last = self.current_gas self.tx_hashes.clear() - if gas_price.get_gas_price(0) <= self.current_gas * 1.125: + if gas_strategy.get_gas_price(0) <= self.current_gas * 1.125: self.logger.warning(f"Recovery gas price is less than current gas price {self.current_gas}; " "cancellation will be deferred until the strategy produces an acceptable price.") while True: seconds_elapsed = int(time.time() - initial_time) - gas_price_value = gas_price.get_gas_price(seconds_elapsed) + gas_price_value = gas_strategy.get_gas_price(seconds_elapsed) if gas_price_value > self.gas_price_last * 1.125: self.gas_price_last = gas_price_value # Transaction lock isn't needed here, as we are replacing an existing nonce diff --git a/pymaker/gas.py b/pymaker/gas.py index a7e85c33..1f4db731 100644 --- a/pymaker/gas.py +++ b/pymaker/gas.py @@ -1,6 +1,6 @@ # This file is part of Maker Keeper Framework. # -# Copyright (C) 2017-2018 reverendus +# Copyright (C) 2017-2021 reverendus, EdNoepel # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by @@ -16,11 +16,11 @@ # along with this program. If not, see . import math -from typing import Optional +from typing import Optional, Tuple from web3 import Web3 -class GasPrice(object): +class GasStrategy(object): GWEI = 1000000000 """Abstract class, which can be inherited for implementing different gas price strategies. @@ -57,8 +57,12 @@ def get_gas_price(self, time_elapsed: int) -> Optional[int]: """ raise NotImplementedError("Please implement this method") + def get_gas_fees(self, time_elapsed: int) -> Tuple[int, int]: + """Return fee cap (max fee) and tip for type 2 (EIP-1559) transactions""" + raise NotImplementedError("Please implement this method") + -class DefaultGasPrice(GasPrice): +class DefaultGasPrice(GasStrategy): """Default gas price. Uses the default gas price i.e. gas price will be decided by the Ethereum node @@ -68,29 +72,47 @@ class DefaultGasPrice(GasPrice): def get_gas_price(self, time_elapsed: int) -> Optional[int]: return None + def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: + return None, None + -class NodeAwareGasPrice(GasPrice): +class NodeAwareGasStrategy(GasStrategy): """Abstract baseclass which is Web3-aware. Retrieves the default gas price provided by the Ethereum node to be consumed by subclasses. """ - def __init__(self, web3: Web3): + def __init__(self, web3: Web3, base_fee_multiplier=1.125, initial_tip=2 * GasStrategy.GWEI): assert isinstance(web3, Web3) - if self.__class__ == NodeAwareGasPrice: + if self.__class__ == NodeAwareGasStrategy: raise NotImplementedError('This class is not intended to be used directly') self.web3 = web3 + self.base_fee_multiplier = base_fee_multiplier + self.initial_tip = initial_tip def get_gas_price(self, time_elapsed: int) -> Optional[int]: """If user wants node to choose gas price, they should use DefaultGasPrice for the same functionality without an additional HTTP request. This baseclass exists to let a subclass manipulate the node price.""" raise NotImplementedError("Please implement this method") - def get_node_gas_price(self): + def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: + """Implementation of tip is subjective. For August 2021, the following implementation is a reasonable example: + return int(self.get_next_base_fee(self)*1.5), 2 * self.GWEI""" + raise NotImplementedError("Please implement this method") + + def get_node_gas_price(self) -> int: return max(self.web3.manager.request_blocking("eth_gasPrice", []), 1 * self.GWEI) + def get_next_base_fee(self) -> Optional[int]: + """Useful for calculating maxfee; a multiple of this value is suggested""" + next_block = self.web3.eth.get_block('pending') + if 'baseFeePerGas' in next_block: + return int(next_block['baseFeePerGas']) + else: + return None -class FixedGasPrice(GasPrice): + +class FixedGasPrice(GasStrategy): """Fixed gas price. Uses specified gas price instead of the default price suggested by the Ethereum @@ -98,109 +120,119 @@ class FixedGasPrice(GasPrice): is still in progress) by calling the `update_gas_price` method. Attributes: - gas_price: Gas price to be used (in Wei). + gas_price: Gas price to be used (in Wei) for legacy transactions + max_fee: Maximum fee (in Wei) for EIP-1559 transactions, should be >= (base_fee + tip) + tip: Priority fee (in Wei) for EIP-1559 transactions """ - def __init__(self, gas_price: int): - assert(isinstance(gas_price, int)) + def __init__(self, gas_price: Optional[int], max_fee: Optional[int], tip: Optional[int]): + assert isinstance(gas_price, int) or gas_price is None + assert isinstance(max_fee, int) or max_fee is None + assert isinstance(tip, int) or tip is None + assert gas_price or (max_fee and tip) self.gas_price = gas_price + self.max_fee = max_fee + self.tip = tip - def update_gas_price(self, new_gas_price: int): + def update_gas_price(self, new_gas_price: int, new_max_fee: int, new_tip: int): """Changes the initial gas price to a higher value, preferably higher. The only reason when calling this function makes sense is when an async transaction is in progress. In this case, the loop waiting for the transaction to be mined (see :py:class:`pymaker.Transact`) will resend the pending transaction again with the new gas price. - As Parity excepts the gas price to rise by at least 10% in replacement transactions, the price + As OpenEthereum excepts the gas price to rise by at least 12.5% in replacement transactions, the price argument supplied to this method should be accordingly higher. Args: - new_gas_price: New gas price to be set (in Wei). + new_gas_price: New gas price to be set (in Wei). + new_max_fee: New maximum fee (in Wei) appropriate for subsequent block(s). + new_tip: New prioritization fee (in Wei). """ - assert(isinstance(new_gas_price, int)) - + assert isinstance(new_gas_price, int) or new_gas_price is None + assert isinstance(new_max_fee, int) or new_max_fee is None + assert isinstance(new_tip, int) or new_tip is None + assert new_gas_price or (new_max_fee and new_tip) self.gas_price = new_gas_price + self.max_fee = new_max_fee + self.tip = new_tip def get_gas_price(self, time_elapsed: int) -> Optional[int]: - assert(isinstance(time_elapsed, int)) return self.gas_price + def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: + return self.max_fee, self.tip -class IncreasingGasPrice(GasPrice): - """Constantly increasing gas price. - - Start with `initial_price`, then increase it by fixed amount `increase_by` every `every_secs` seconds - until the transaction gets confirmed. There is an optional upper limit. - - Attributes: - initial_price: The initial gas price in Wei i.e. the price the transaction - is originally sent with. - increase_by: Gas price increase in Wei, which will happen every `every_secs` seconds. - every_secs: Gas price increase interval (in seconds). - max_price: Optional upper limit. - """ - def __init__(self, initial_price: int, increase_by: int, every_secs: int, max_price: Optional[int]): - assert(isinstance(initial_price, int)) - assert(isinstance(increase_by, int)) - assert(isinstance(every_secs, int)) - assert(isinstance(max_price, int) or max_price is None) - assert(initial_price > 0) - assert(increase_by > 0) - assert(every_secs > 0) - if max_price is not None: - assert(max_price > 0) - - self.initial_price = initial_price - self.increase_by = increase_by - self.every_secs = every_secs - self.max_price = max_price - - def get_gas_price(self, time_elapsed: int) -> Optional[int]: - assert(isinstance(time_elapsed, int)) - result = self.initial_price + int(time_elapsed/self.every_secs)*self.increase_by - if self.max_price is not None: - result = min(result, self.max_price) - - return result - - -class GeometricGasPrice(GasPrice): +class GeometricGasPrice(GasStrategy): """Geometrically increasing gas price. Start with `initial_price`, then increase it every 'every_secs' seconds by a fixed coefficient. - Coefficient defaults to 1.125 (12.5%), the minimum increase for Parity to replace a transaction. + Coefficient defaults to 1.125 (12.5%), the minimum increase for OpenEthereum to replace a transaction. Coefficient can be adjusted, and there is an optional upper limit. + To disable legacy (type 0) transactions, set initial_price None. + To disable EIP-1559 (type 2) transactions, set initial_feecap and initial_tip None. + Other parameters apply to both transaction types. + Attributes: - initial_price: The initial gas price in Wei i.e. the price the transaction is originally sent with. + initial_price: The initial gas price in Wei, used only for legacy transactions. + initial_feecap: Set this >= current basefee+tip, keeping in mind basefee can rise each block. + initial_tip: Initial priority fee paid on top of a base fee. every_secs: Gas price increase interval (in seconds). coefficient: Gas price multiplier, defaults to 1.125. max_price: Optional upper limit, defaults to None. """ - def __init__(self, initial_price: int, every_secs: int, coefficient=1.125, max_price: Optional[int] = None): - assert (isinstance(initial_price, int)) - assert (isinstance(every_secs, int)) - assert (isinstance(max_price, int) or max_price is None) - assert (initial_price > 0) - assert (every_secs > 0) - assert (coefficient > 1) - if max_price is not None: - assert(max_price >= initial_price) + def __init__(self, initial_price: Optional[int], initial_feecap: Optional[int], initial_tip: Optional[int], + every_secs: int, coefficient=1.125, max_price: Optional[int] = None): + assert (isinstance(initial_price, int) and initial_price > 0) or initial_price is None + assert isinstance(initial_feecap, int) or initial_feecap is None + assert isinstance(initial_tip, int) or initial_tip is None + assert isinstance(every_secs, int) + assert isinstance(coefficient, float) + assert (isinstance(max_price, int) and max_price > 0) or max_price is None + assert initial_price or (initial_tip is not None and initial_feecap > initial_tip >= 0) + assert every_secs > 0 + assert coefficient > 1 + if initial_price and max_price: + assert initial_price <= max_price + if initial_feecap and max_price: + assert initial_feecap <= max_price self.initial_price = initial_price + self.initial_feecap = initial_feecap + self.initial_tip = initial_tip self.every_secs = every_secs self.coefficient = coefficient self.max_price = max_price - def get_gas_price(self, time_elapsed: int) -> Optional[int]: - assert(isinstance(time_elapsed, int)) - - result = self.initial_price + def scale_by_time(self, value: int, time_elapsed: int): + result = value if time_elapsed >= self.every_secs: - for second in range(math.floor(time_elapsed/self.every_secs)): + for second in range(math.floor(time_elapsed / self.every_secs)): result *= self.coefficient + return result + + def get_gas_price(self, time_elapsed: int) -> Optional[int]: + assert isinstance(time_elapsed, int) + if not self.initial_price: + return None + + result = self.scale_by_time(self.initial_price, time_elapsed) if self.max_price is not None: result = min(result, self.max_price) return math.ceil(result) + + def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: + assert isinstance(time_elapsed, int) + if not self.initial_feecap: + return None + + feecap = self.scale_by_time(self.initial_feecap, time_elapsed) + tip = self.scale_by_time(self.initial_tip, time_elapsed) + if self.max_price is not None: + feecap = min(feecap, self.max_price) + # TODO: Instead of asserting, apply a meaningful limit. + assert tip < feecap # basefee is > 0, and tip can't exceed feecap + + return math.ceil(feecap), tip diff --git a/requirements.txt b/requirements.txt index f013da48..ac562ff4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ pytz == 2017.3 -web3 == 5.21.0 +web3 == 5.22.0 requests == 2.22.0 -eth-keys<0.3.0,>=0.2.1 +eth-account >= 0.5.5 +eth-keys <0.3.0, >=0.2.1 jsonnet == 0.9.5 diff --git a/tests/manual_test_goerli.py b/tests/manual_test_goerli.py index cfd20524..9db2d531 100644 --- a/tests/manual_test_goerli.py +++ b/tests/manual_test_goerli.py @@ -21,7 +21,7 @@ from web3 import Web3, HTTPProvider from pymaker import Address, eth_transfer, web3_via_http -from pymaker.gas import GeometricGasPrice +from pymaker.gas import DefaultGasPrice, GeometricGasPrice from pymaker.lifecycle import Lifecycle from pymaker.keys import register_keys from pymaker.numeric import Wad @@ -59,9 +59,11 @@ our_address = None run_transactions = False -gas_price = None if len(sys.argv) <= 4 else \ - GeometricGasPrice(initial_price=int(float(sys.argv[4]) * GeometricGasPrice.GWEI), - every_secs=15, +gas_strategy = DefaultGasPrice() if len(sys.argv) <= 4 else \ + GeometricGasPrice(initial_price=None, # int(float(sys.argv[4]) * GeometricGasPrice.GWEI), + initial_feecap=int(60 * GeometricGasPrice.GWEI), + initial_tip=int(2 * GeometricGasPrice.GWEI), + every_secs=2, max_price=100 * GeometricGasPrice.GWEI) eth = EthToken(web3, Address.zero()) @@ -79,7 +81,7 @@ def on_block(self): if run_transactions and block % 3 == 0: # dummy transaction: send 0 ETH to ourself eth_transfer(web3=web3, to=our_address, amount=Wad(0)).transact( - from_address=our_address, gas=21000, gas_price=gas_price) + from_address=our_address, gas=21000, gas_strategy=gas_strategy) if our_address: logging.info(f"Eth balance is {eth.balance_of(our_address)}") diff --git a/tests/test_gas.py b/tests/test_gas.py index dc431a81..dd3dcce6 100644 --- a/tests/test_gas.py +++ b/tests/test_gas.py @@ -16,20 +16,23 @@ # along with this program. If not, see . import pytest -from typing import Optional +from typing import Optional, Tuple from web3 import Web3 +from web3._utils.events import AttributeDict -from pymaker.gas import DefaultGasPrice, FixedGasPrice, GasPrice, GeometricGasPrice, IncreasingGasPrice, NodeAwareGasPrice +from pymaker.gas import DefaultGasPrice, FixedGasPrice, GasStrategy, GeometricGasPrice, NodeAwareGasStrategy from tests.conftest import web3 class TestGasPrice: def test_not_implemented(self): - with pytest.raises(Exception): - GasPrice().get_gas_price(0) + with pytest.raises(NotImplementedError): + GasStrategy().get_gas_price(0) + with pytest.raises(NotImplementedError): + GasStrategy().get_gas_fees(0) def test_gwei(self): - assert GasPrice.GWEI == 1000000000 + assert GasStrategy.GWEI == 1000000000 class TestDefaultGasPrice: @@ -42,130 +45,110 @@ def test_should_always_be_default(self): assert default_gas_price.get_gas_price(1) is None assert default_gas_price.get_gas_price(1000000) is None + # expect + assert default_gas_price.get_gas_fees(0) == (None, None) + assert default_gas_price.get_gas_fees(1) == (None, None) + assert default_gas_price.get_gas_fees(1000000) == (None, None) + class TestNodeAwareGasPrice: - class DumbSampleImplementation(NodeAwareGasPrice): + class DumbSampleImplementation(NodeAwareGasStrategy): def get_gas_price(self, time_elapsed: int) -> Optional[int]: return self.get_node_gas_price() * max(time_elapsed, 1) - class BadImplementation(NodeAwareGasPrice): + def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: + return int(self.get_next_base_fee()*1.5), 2 * self.GWEI + + class BadImplementation(NodeAwareGasStrategy): pass - def test_retrieve_node_gas_price(self, web3): + @staticmethod + def patch_web3_block_data(web3, mocker, base_fee): + # TODO: Build a new testchain with a node which provides EIP-1559 baseFee in getBlock response. + block_data = dict(web3.eth.get_block('pending')) + block_data['baseFeePerGas'] = base_fee + mocker.patch.object(web3.eth, 'get_block', return_value=AttributeDict(block_data)) + + def test_retrieve_node_gas_price(self, web3, mocker): strategy = TestNodeAwareGasPrice.DumbSampleImplementation(web3) assert strategy.get_gas_price(0) > 0 assert strategy.get_gas_price(60) < strategy.get_gas_price(120) + base_fee = 7 * GasStrategy.GWEI + self.patch_web3_block_data(web3, mocker, base_fee) + feecap, tip = strategy.get_gas_fees(90) + assert feecap == base_fee * 1.5 + assert tip == 2 * GasStrategy.GWEI + def test_not_implemented(self, web3): with pytest.raises(NotImplementedError): - NodeAwareGasPrice(web3) + NodeAwareGasStrategy(web3) bad = TestNodeAwareGasPrice.BadImplementation(web3) with pytest.raises(NotImplementedError): bad.get_gas_price(0) + with pytest.raises(NotImplementedError): + bad.get_gas_fees(0) class TestFixedGasPrice: def test_gas_price_should_stay_the_same(self): # given - value = 9000000000 - fixed_gas_price = FixedGasPrice(value) + price = 9 * GasStrategy.GWEI + feecap = 6 * GasStrategy.GWEI + tip = 3 * GasStrategy.GWEI + fixed_gas_price = FixedGasPrice(price, feecap, tip) # expect - assert fixed_gas_price.get_gas_price(0) == value - assert fixed_gas_price.get_gas_price(1) == value - assert fixed_gas_price.get_gas_price(2) == value - assert fixed_gas_price.get_gas_price(5) == value - assert fixed_gas_price.get_gas_price(60) == value - assert fixed_gas_price.get_gas_price(120) == value - assert fixed_gas_price.get_gas_price(600) == value - assert fixed_gas_price.get_gas_price(1000000) == value + assert fixed_gas_price.get_gas_price(0) == price + assert fixed_gas_price.get_gas_price(1) == price + assert fixed_gas_price.get_gas_price(2) == price + assert fixed_gas_price.get_gas_price(5) == price + assert fixed_gas_price.get_gas_price(60) == price + assert fixed_gas_price.get_gas_price(120) == price + assert fixed_gas_price.get_gas_price(600) == price + assert fixed_gas_price.get_gas_price(1000000) == price + + # expect + assert fixed_gas_price.get_gas_fees(0) == (feecap, tip) + assert fixed_gas_price.get_gas_fees(120) == (feecap, tip) + assert fixed_gas_price.get_gas_fees(1000000) == (feecap, tip) def test_gas_price_should_be_updated_by_update_gas_price_method(self): # given - value1 = 9000000000 - value2 = 16000000000 + price1 = 9 * GasStrategy.GWEI + feecap1 = 6 * GasStrategy.GWEI + tip1 = 3 * GasStrategy.GWEI + price2 = 16 * GasStrategy.GWEI + feecap2 = 10 * GasStrategy.GWEI + tip2 = 2 * GasStrategy.GWEI # and - fixed_gas_price = FixedGasPrice(value1) + fixed_gas_price = FixedGasPrice(price1, feecap1, tip1) # and - assert fixed_gas_price.get_gas_price(0) == value1 - assert fixed_gas_price.get_gas_price(1) == value1 - assert fixed_gas_price.get_gas_price(2) == value1 - assert fixed_gas_price.get_gas_price(5) == value1 + assert fixed_gas_price.get_gas_price(0) == price1 + assert fixed_gas_price.get_gas_price(1) == price1 + assert fixed_gas_price.get_gas_price(2) == price1 + assert fixed_gas_price.get_gas_price(5) == price1 + assert fixed_gas_price.get_gas_fees(0) == (feecap1, tip1) + assert fixed_gas_price.get_gas_fees(30) == (feecap1, tip1) # when - fixed_gas_price.update_gas_price(value2) + fixed_gas_price.update_gas_price(price2, feecap2, tip2) # then - assert fixed_gas_price.get_gas_price(60) == value2 - assert fixed_gas_price.get_gas_price(120) == value2 - assert fixed_gas_price.get_gas_price(600) == value2 - - -class TestIncreasingGasPrice: - def test_gas_price_should_increase_with_time(self): - # given - increasing_gas_price = IncreasingGasPrice(1000, 100, 60, None) - - # expect - assert increasing_gas_price.get_gas_price(0) == 1000 - assert increasing_gas_price.get_gas_price(1) == 1000 - assert increasing_gas_price.get_gas_price(59) == 1000 - assert increasing_gas_price.get_gas_price(60) == 1100 - assert increasing_gas_price.get_gas_price(119) == 1100 - assert increasing_gas_price.get_gas_price(120) == 1200 - assert increasing_gas_price.get_gas_price(1200) == 3000 - - def test_gas_price_should_obey_max_value(self): - # given - increasing_gas_price = IncreasingGasPrice(1000, 100, 60, 2500) - - # expect - assert increasing_gas_price.get_gas_price(0) == 1000 - assert increasing_gas_price.get_gas_price(1) == 1000 - assert increasing_gas_price.get_gas_price(59) == 1000 - assert increasing_gas_price.get_gas_price(60) == 1100 - assert increasing_gas_price.get_gas_price(119) == 1100 - assert increasing_gas_price.get_gas_price(120) == 1200 - assert increasing_gas_price.get_gas_price(1200) == 2500 - assert increasing_gas_price.get_gas_price(3000) == 2500 - assert increasing_gas_price.get_gas_price(1000000) == 2500 - - def test_should_require_positive_initial_price(self): - with pytest.raises(Exception): - IncreasingGasPrice(0, 1000, 60, None) - - with pytest.raises(Exception): - IncreasingGasPrice(-1, 1000, 60, None) - - def test_should_require_positive_increase_by_value(self): - with pytest.raises(Exception): - IncreasingGasPrice(1000, 0, 60, None) - - with pytest.raises(Exception): - IncreasingGasPrice(1000, -1, 60, None) - - def test_should_require_positive_every_secs_value(self): - with pytest.raises(Exception): - IncreasingGasPrice(1000, 100, 0, None) - - with pytest.raises(Exception): - IncreasingGasPrice(1000, 100, -1, None) - - def test_should_require_positive_max_price_if_provided(self): - with pytest.raises(Exception): - IncreasingGasPrice(1000, 1000, 60, 0) - - with pytest.raises(Exception): - IncreasingGasPrice(1000, 1000, 60, -1) + assert fixed_gas_price.get_gas_price(60) == price2 + assert fixed_gas_price.get_gas_price(120) == price2 + assert fixed_gas_price.get_gas_price(600) == price2 + assert fixed_gas_price.get_gas_fees(90) == (feecap2, tip2) + assert fixed_gas_price.get_gas_fees(360) == (feecap2, tip2) class TestGeometricGasPrice: def test_gas_price_should_increase_with_time(self): # given - geometric_gas_price = GeometricGasPrice(100, 10) + geometric_gas_price = GeometricGasPrice(initial_price=100, initial_feecap=200, initial_tip=1, every_secs=10) # expect assert geometric_gas_price.get_gas_price(0) == 100 @@ -177,9 +160,12 @@ def test_gas_price_should_increase_with_time(self): assert geometric_gas_price.get_gas_price(50) == 181 assert geometric_gas_price.get_gas_price(100) == 325 + # TODO: test geometric_gas_price.get_gas_fees() + def test_gas_price_should_obey_max_value(self): # given - geometric_gas_price = GeometricGasPrice(1000, 60, 1.125, 2500) + geometric_gas_price = GeometricGasPrice(initial_price=1000, initial_feecap=2000, initial_tip=10, + every_secs=60, coefficient=1.125, max_price=2500) # expect assert geometric_gas_price.get_gas_price(0) == 1000 @@ -192,10 +178,13 @@ def test_gas_price_should_obey_max_value(self): assert geometric_gas_price.get_gas_price(3000) == 2500 assert geometric_gas_price.get_gas_price(1000000) == 2500 + # TODO: test geometric_gas_price.get_gas_fees() + def test_behaves_with_realistic_values(self): # given GWEI = 1000000000 - geometric_gas_price = GeometricGasPrice(100*GWEI, 10, 1+(0.125*2)) + geometric_gas_price = GeometricGasPrice(initial_price=100*GWEI, initial_feecap=200*GWEI, initial_tip=1*GWEI, + every_secs=10, coefficient=1+(0.125*2)) for seconds in [0,1,10,12,30,60]: print(f"gas price after {seconds} seconds is {geometric_gas_price.get_gas_price(seconds)/GWEI}") @@ -207,37 +196,50 @@ def test_behaves_with_realistic_values(self): assert round(geometric_gas_price.get_gas_price(30) / GWEI, 1) == 195.3 assert round(geometric_gas_price.get_gas_price(60) / GWEI, 1) == 381.5 + # TODO: test geometric_gas_price.get_gas_fees() + def test_should_require_positive_initial_price(self): with pytest.raises(AssertionError): - GeometricGasPrice(0, 60) + GeometricGasPrice(0, None, None, 60) + with pytest.raises(AssertionError): + GeometricGasPrice(None, 0, 0, 60) with pytest.raises(AssertionError): - GeometricGasPrice(-1, 60) + GeometricGasPrice(-1, None, None, 60) + with pytest.raises(AssertionError): + GeometricGasPrice(None, -1, -1, 60) def test_should_require_positive_every_secs_value(self): with pytest.raises(AssertionError): - GeometricGasPrice(1000, 0) + GeometricGasPrice(1000, None, None, 0) + with pytest.raises(AssertionError): + GeometricGasPrice(None, 600, 50, 0) with pytest.raises(AssertionError): - GeometricGasPrice(1000, -1) + GeometricGasPrice(1000, None, None, -1) + with pytest.raises(AssertionError): + GeometricGasPrice(None, 600, 50, -1) def test_should_require_positive_coefficient(self): with pytest.raises(AssertionError): - GeometricGasPrice(1000, 60, 0) + GeometricGasPrice(1000, 600, 50, 60, 0) with pytest.raises(AssertionError): - GeometricGasPrice(1000, 60, 1) + GeometricGasPrice(1000, 600, 50, 60, 1) with pytest.raises(AssertionError): - GeometricGasPrice(1000, 60, -1) + GeometricGasPrice(1000, 600, 50, 60, -1) def test_should_require_positive_max_price_if_provided(self): with pytest.raises(AssertionError): - GeometricGasPrice(1000, 60, 1.125, 0) + GeometricGasPrice(1000, 600, 50, 60, 1.125, 0) with pytest.raises(AssertionError): - GeometricGasPrice(1000, 60, 1.125, -1) + GeometricGasPrice(1000, 600, 50, 60, 1.125, -1) def test_max_price_should_exceed_initial_price(self): with pytest.raises(AssertionError): - GeometricGasPrice(6000, 30, 2.25, 5000) + GeometricGasPrice(6000, 600, 50, 30, 2.25, 5000) + + with pytest.raises(AssertionError): + GeometricGasPrice(None, 300, 5, 30, 1.424, 200) diff --git a/tests/test_general2.py b/tests/test_general2.py index 679a30f1..a8b73d13 100644 --- a/tests/test_general2.py +++ b/tests/test_general2.py @@ -134,20 +134,20 @@ def test_gas_and_gas_buffer_not_allowed_at_the_same_time_async(self): def test_custom_gas_price(self): # given - gas_price = FixedGasPrice(25000000100) + gas_price = FixedGasPrice(25000000100, None, None) # when - self.token.transfer(self.second_address, Wad(500)).transact(gas_price=gas_price) + self.token.transfer(self.second_address, Wad(500)).transact(gas_strategy=gas_price) # then assert self.web3.eth.getBlock('latest', full_transactions=True).transactions[0].gasPrice == gas_price.gas_price def test_custom_gas_price_async(self): # given - gas_price = FixedGasPrice(25000000200) + gas_price = FixedGasPrice(25000000200, None, None) # when - synchronize([self.token.transfer(self.second_address, Wad(500)).transact_async(gas_price=gas_price)]) + synchronize([self.token.transfer(self.second_address, Wad(500)).transact_async(gas_strategy=gas_price)]) # then assert self.web3.eth.getBlock('latest', full_transactions=True).transactions[0].gasPrice == gas_price.gas_price @@ -235,7 +235,7 @@ async def test_transaction_replace(self): self.web3.eth.getTransaction = MagicMock(return_value={'nonce': nonce}) # and transact_1 = self.token.transfer(self.second_address, Wad(500)) - future_receipt_1 = asyncio.ensure_future(transact_1.transact_async(gas_price=FixedGasPrice(1))) + future_receipt_1 = asyncio.ensure_future(transact_1.transact_async(gas_strategy=FixedGasPrice(1, None, None))) # and await asyncio.sleep(0.2) # then @@ -249,7 +249,7 @@ async def test_transaction_replace(self): transact_2 = self.token.transfer(self.third_address, Wad(700)) # FIXME: Ganache produces a "the tx doesn't have the correct nonce" error. future_receipt_2 = asyncio.ensure_future(transact_2.transact_async(replace=transact_1, - gas_price=FixedGasPrice(150000))) + gas_price=FixedGasPrice(150000, None, None))) # and await asyncio.sleep(2) # then From 74cfade2dde94bfeb203f29ff4d2744c7534a1a1 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Fri, 13 Aug 2021 23:54:51 -0400 Subject: [PATCH 05/16] functional replacement using geth; needed to pass web3 to GeometricGasPrice --- pymaker/__init__.py | 52 ++++++++----- pymaker/gas.py | 67 +++++++++------- requirements.txt | 2 +- tests/conftest.py | 9 +++ ...al_test_goerli.py => manual_test_nomcd.py} | 0 tests/test_gas.py | 77 ++++++++++--------- tests/test_general2.py | 15 ++++ 7 files changed, 135 insertions(+), 87 deletions(-) rename tests/{manual_test_goerli.py => manual_test_nomcd.py} (100%) diff --git a/pymaker/__init__.py b/pymaker/__init__.py index dda21f30..8c7d6b7f 100644 --- a/pymaker/__init__.py +++ b/pymaker/__init__.py @@ -18,13 +18,13 @@ import asyncio import json import logging -import pprint import re import requests import sys import time from enum import Enum, auto from functools import total_ordering, wraps +from pprint import pprint from threading import Lock from typing import Optional from weakref import WeakKeyDictionary @@ -527,29 +527,40 @@ def _gas(self, gas_estimate: int, **kwargs) -> int: else: return gas_estimate + 100000 - def _gas_params(self, seconds_elapsed: int, gas_strategy: GasStrategy) -> dict: + def _gas_fees(self, seconds_elapsed: int, gas_strategy: GasStrategy) -> dict: assert isinstance(seconds_elapsed, int) assert isinstance(gas_strategy, GasStrategy) gas_price = gas_strategy.get_gas_price(seconds_elapsed) gas_feecap, gas_tip = gas_strategy.get_gas_fees(seconds_elapsed) - if _get_endpoint_behavior(self.web3).supports_london and gas_feecap and gas_tip: - params = {'maxFeePerGas': gas_feecap, - 'maxPriorityFeePerGas': gas_tip} - elif gas_price: + if _get_endpoint_behavior(self.web3).supports_london and gas_feecap and gas_tip: # prefer type 2 TXes + params = {'maxFeePerGas': gas_feecap, 'maxPriorityFeePerGas': gas_tip} + elif gas_price: # fallback to type 0 if not supported or params not specified params = {'gasPrice': gas_price} - else: + else: # let the node determine gas params = {} return params - @staticmethod - def _gas_exceeds_replacement_threshold(prev_gas_params: dict, current_gas_params: dict): + def _gas_exceeds_replacement_threshold(self, prev_gas_params: dict, curr_gas_params: dict): # TODO: Can a type 0 TX be replaced with a type 2 TX? Vice-versa? - if 'gasPrice' in prev_gas_params and 'gasPrice' in current_gas_params: - return current_gas_params['gasPrice'] > prev_gas_params['gasPrice'] * 1.125 - # TODO: Implement EIP-1559 support - return False + + # Determine if a type 0 transaction would be replaced + if 'gasPrice' in prev_gas_params and 'gasPrice' in curr_gas_params: + return curr_gas_params['gasPrice'] > prev_gas_params['gasPrice'] * 1.125 + # Determine if a type 2 transaction would be replaced + elif 'maxFeePerGas' in prev_gas_params and 'maxFeePerGas' in curr_gas_params: + # This is how it should work, but doesn't; read here: https://github.com/ethereum/go-ethereum/issues/23311 + # base_fee = int(self.web3.eth.get_block('pending')['baseFeePerGas']) + # prev_effective_price = base_fee + prev_gas_params['maxPriorityFeePerGas'] + # curr_effective_price = base_fee + curr_gas_params['maxPriorityFeePerGas'] + # print(f"base={base_fee} prev_eff={prev_effective_price} curr_eff={curr_effective_price}") + # return curr_effective_price > prev_effective_price * 1.125 + feecap_bumped = curr_gas_params['maxFeePerGas'] > prev_gas_params['maxFeePerGas'] * 1.125 + tip_bumped = curr_gas_params['maxPriorityFeePerGas'] > prev_gas_params['maxPriorityFeePerGas'] * 1.125 + return feecap_bumped and tip_bumped + else: # Replacement impossible if no parameters were offered + return False def _func(self, from_account: str, gas: int, gas_price_params: dict, nonce: Optional[int]): assert isinstance(from_account, str) @@ -561,6 +572,7 @@ def _func(self, from_account: str, gas: int, gas_price_params: dict, nonce: Opti **gas_price_params, **nonce_dict, **self._as_dict(self.extra)} + pprint(transaction_params) if self.contract is not None: if self.function_name is None: @@ -706,7 +718,7 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: gas = self._gas(gas_estimate, **kwargs) self.gas_strategy = kwargs['gas_strategy'] if ('gas_strategy' in kwargs) else DefaultGasPrice() assert(isinstance(self.gas_strategy, GasStrategy)) - gas_params_last = None + gas_fees_last = None # Get the transaction this one is supposed to replace. # If there is one, try to borrow the nonce from it as long as that transaction isn't finished. @@ -730,7 +742,7 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: while True: seconds_elapsed = int(time.time() - self.initial_time) - gas_params = self._gas_params(seconds_elapsed, self.gas_strategy) + gas_fees = self._gas_fees(seconds_elapsed, self.gas_strategy) # CAUTION: if transact_async is called rapidly, we will hammer the node with these JSON-RPC requests if self.nonce is not None and self.web3.eth.getTransactionCount(from_account) > self.nonce: @@ -776,8 +788,8 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: # Uncomment this to debug state during transaction submission # self.logger.debug(f"Transaction {self.name()} is churning: was_sent={transaction_was_sent}") # TODO: For EIP-1559 transactions, both maxFeePerGas and maxPriorityFeePerGas need to be bumped 12.5% to replace. - if not transaction_was_sent or (gas_params_last and self._gas_exceeds_replacement_threshold(gas_params_last, gas_params)): - gas_params_last = gas_params + if not transaction_was_sent or (gas_fees_last and self._gas_exceeds_replacement_threshold(gas_fees_last, gas_fees)): + gas_fees_last = gas_fees try: # We need the lock in order to not try to send two transactions with the same nonce. @@ -803,15 +815,15 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: self.logger.info(f"Transaction {self.name()} with nonce={self.nonce} was replaced") return None - tx_hash = self._func(from_account, gas, gas_params, self.nonce) + tx_hash = self._func(from_account, gas, gas_fees, self.nonce) self.tx_hashes.append(tx_hash) self.logger.info(f"Sent transaction {self.name()} with nonce={self.nonce}, gas={gas}," - f" gas_params={gas_params if gas_params else 'default'}" + f" gas_fees={gas_fees if gas_fees else 'default'}" f" (tx_hash={tx_hash})") except Exception as e: self.logger.warning(f"Failed to send transaction {self.name()} with nonce={self.nonce}, gas={gas}," - f" gas_params={gas_params if gas_params else 'default'} ({e})") + f" gas_fees={gas_fees if gas_fees else 'default'} ({e})") if len(self.tx_hashes) == 0: raise diff --git a/pymaker/gas.py b/pymaker/gas.py index 1f4db731..a2b1b3a4 100644 --- a/pymaker/gas.py +++ b/pymaker/gas.py @@ -82,13 +82,11 @@ class NodeAwareGasStrategy(GasStrategy): Retrieves the default gas price provided by the Ethereum node to be consumed by subclasses. """ - def __init__(self, web3: Web3, base_fee_multiplier=1.125, initial_tip=2 * GasStrategy.GWEI): + def __init__(self, web3: Web3): assert isinstance(web3, Web3) if self.__class__ == NodeAwareGasStrategy: raise NotImplementedError('This class is not intended to be used directly') self.web3 = web3 - self.base_fee_multiplier = base_fee_multiplier - self.initial_tip = initial_tip def get_gas_price(self, time_elapsed: int) -> Optional[int]: """If user wants node to choose gas price, they should use DefaultGasPrice for the same functionality @@ -101,13 +99,13 @@ def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: raise NotImplementedError("Please implement this method") def get_node_gas_price(self) -> int: - return max(self.web3.manager.request_blocking("eth_gasPrice", []), 1 * self.GWEI) + return max(self.web3.manager.request_blocking("eth_gasPrice", []), 1) def get_next_base_fee(self) -> Optional[int]: """Useful for calculating maxfee; a multiple of this value is suggested""" next_block = self.web3.eth.get_block('pending') if 'baseFeePerGas' in next_block: - return int(next_block['baseFeePerGas']) + return max(int(next_block['baseFeePerGas']), 1) else: return None @@ -163,7 +161,7 @@ def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: return self.max_fee, self.tip -class GeometricGasPrice(GasStrategy): +class GeometricGasPrice(NodeAwareGasStrategy): """Geometrically increasing gas price. Start with `initial_price`, then increase it every 'every_secs' seconds by a fixed coefficient. @@ -171,46 +169,48 @@ class GeometricGasPrice(GasStrategy): Coefficient can be adjusted, and there is an optional upper limit. To disable legacy (type 0) transactions, set initial_price None. - To disable EIP-1559 (type 2) transactions, set initial_feecap and initial_tip None. + To disable EIP-1559 (type 2) transactions, set initial_tip None. Other parameters apply to both transaction types. Attributes: initial_price: The initial gas price in Wei, used only for legacy transactions. - initial_feecap: Set this >= current basefee+tip, keeping in mind basefee can rise each block. - initial_tip: Initial priority fee paid on top of a base fee. - every_secs: Gas price increase interval (in seconds). - coefficient: Gas price multiplier, defaults to 1.125. - max_price: Optional upper limit, defaults to None. + initial_tip: Initial priority fee paid on top of a base fee (recommend 1 GWEI minimum). + every_secs: Gas increase interval (in seconds). + coefficient: Gas price and tip multiplier, defaults to 1.125. + max_price: Optional upper limit and fee cap, defaults to None. """ - def __init__(self, initial_price: Optional[int], initial_feecap: Optional[int], initial_tip: Optional[int], + def __init__(self, web3: Web3, initial_price: Optional[int], initial_tip: Optional[int], every_secs: int, coefficient=1.125, max_price: Optional[int] = None): + assert isinstance(web3, Web3) assert (isinstance(initial_price, int) and initial_price > 0) or initial_price is None - assert isinstance(initial_feecap, int) or initial_feecap is None assert isinstance(initial_tip, int) or initial_tip is None + assert initial_price or (initial_tip is not None and initial_tip > 0) assert isinstance(every_secs, int) assert isinstance(coefficient, float) assert (isinstance(max_price, int) and max_price > 0) or max_price is None - assert initial_price or (initial_tip is not None and initial_feecap > initial_tip >= 0) assert every_secs > 0 assert coefficient > 1 if initial_price and max_price: assert initial_price <= max_price - if initial_feecap and max_price: - assert initial_feecap <= max_price + if initial_tip and max_price: + assert initial_tip < max_price + super().__init__(web3) self.initial_price = initial_price - self.initial_feecap = initial_feecap self.initial_tip = initial_tip self.every_secs = every_secs self.coefficient = coefficient self.max_price = max_price - def scale_by_time(self, value: int, time_elapsed: int): + def scale_by_time(self, value: int, time_elapsed: int) -> int: + assert isinstance(value, int) + assert isinstance(time_elapsed, int) result = value if time_elapsed >= self.every_secs: for second in range(math.floor(time_elapsed / self.every_secs)): + # print(f"result={result} coeff={self.coefficient}") result *= self.coefficient - return result + return math.ceil(result) def get_gas_price(self, time_elapsed: int) -> Optional[int]: assert isinstance(time_elapsed, int) @@ -221,18 +221,29 @@ def get_gas_price(self, time_elapsed: int) -> Optional[int]: if self.max_price is not None: result = min(result, self.max_price) - return math.ceil(result) + return result def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: assert isinstance(time_elapsed, int) - if not self.initial_feecap: + if not self.initial_tip: return None - feecap = self.scale_by_time(self.initial_feecap, time_elapsed) + base_fee = self.get_next_base_fee() tip = self.scale_by_time(self.initial_tip, time_elapsed) - if self.max_price is not None: - feecap = min(feecap, self.max_price) - # TODO: Instead of asserting, apply a meaningful limit. - assert tip < feecap # basefee is > 0, and tip can't exceed feecap - return math.ceil(feecap), tip + # This is how it should work, but doesn't; read more here: https://github.com/ethereum/go-ethereum/issues/23311 + # if self.max_price: + # # If the scaled tip would exceed our fee cap, reduce tip to largest possible + # if base_fee + tip > self.max_price: + # tip = max(0, self.max_price - base_fee) + # # Honor the max_price, even if it does not exceed base fee + # return self.max_price, tip + # else: + # # If not limited by user, set a standard fee cap of twice the base fee with tip included + # return (base_fee * 2) + tip, tip + + # HACK: Ensure both feecap and tip are scaled, satisfying geth's current replacement logic. + feecap = self.scale_by_time(int(base_fee * 1.2), time_elapsed) + tip + if self.max_price and feecap > self.max_price: + feecap = self.max_price + return feecap, tip diff --git a/requirements.txt b/requirements.txt index ac562ff4..f2136e62 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ pytz == 2017.3 -web3 == 5.22.0 +web3 == 5.23.0 requests == 2.22.0 eth-account >= 0.5.5 eth-keys <0.3.0, >=0.2.1 diff --git a/tests/conftest.py b/tests/conftest.py index 6e80fc3b..fa5a5841 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,6 +19,8 @@ import pytest from web3 import Web3, HTTPProvider +from web3._utils.events import AttributeDict + from pymaker import Address, web3_via_http from pymaker.auctions import Flipper, Flapper, Flopper @@ -59,6 +61,13 @@ def web3() -> Web3: return web3 +def patch_web3_block_data(web3, mocker, base_fee): + # TODO: Build a new testchain with a node which provides EIP-1559 baseFee in getBlock response. + block_data = dict(web3.eth.get_block('pending')) + block_data['baseFeePerGas'] = base_fee + mocker.patch.object(web3.eth, 'get_block', return_value=AttributeDict(block_data)) + + @pytest.fixture(scope="session") def our_address(web3) -> Address: return Address(web3.eth.accounts[0]) diff --git a/tests/manual_test_goerli.py b/tests/manual_test_nomcd.py similarity index 100% rename from tests/manual_test_goerli.py rename to tests/manual_test_nomcd.py diff --git a/tests/test_gas.py b/tests/test_gas.py index dd3dcce6..70826b4c 100644 --- a/tests/test_gas.py +++ b/tests/test_gas.py @@ -17,11 +17,9 @@ import pytest from typing import Optional, Tuple -from web3 import Web3 -from web3._utils.events import AttributeDict from pymaker.gas import DefaultGasPrice, FixedGasPrice, GasStrategy, GeometricGasPrice, NodeAwareGasStrategy -from tests.conftest import web3 +from tests.conftest import patch_web3_block_data class TestGasPrice: @@ -62,20 +60,13 @@ def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: class BadImplementation(NodeAwareGasStrategy): pass - @staticmethod - def patch_web3_block_data(web3, mocker, base_fee): - # TODO: Build a new testchain with a node which provides EIP-1559 baseFee in getBlock response. - block_data = dict(web3.eth.get_block('pending')) - block_data['baseFeePerGas'] = base_fee - mocker.patch.object(web3.eth, 'get_block', return_value=AttributeDict(block_data)) - def test_retrieve_node_gas_price(self, web3, mocker): strategy = TestNodeAwareGasPrice.DumbSampleImplementation(web3) assert strategy.get_gas_price(0) > 0 assert strategy.get_gas_price(60) < strategy.get_gas_price(120) base_fee = 7 * GasStrategy.GWEI - self.patch_web3_block_data(web3, mocker, base_fee) + patch_web3_block_data(web3, mocker, base_fee) feecap, tip = strategy.get_gas_fees(90) assert feecap == base_fee * 1.5 assert tip == 2 * GasStrategy.GWEI @@ -144,11 +135,20 @@ def test_gas_price_should_be_updated_by_update_gas_price_method(self): assert fixed_gas_price.get_gas_fees(90) == (feecap2, tip2) assert fixed_gas_price.get_gas_fees(360) == (feecap2, tip2) + def test_gas_price_requires_type0_or_type2_params(self): + with pytest.raises(AssertionError): + FixedGasPrice(None, None, None) + + with pytest.raises(AssertionError): + FixedGasPrice(None, 20 * GasStrategy.GWEI, None) + with pytest.raises(AssertionError): + FixedGasPrice(None, None, 1 * GasStrategy.GWEI) + class TestGeometricGasPrice: - def test_gas_price_should_increase_with_time(self): + def test_gas_price_should_increase_with_time(self, web3): # given - geometric_gas_price = GeometricGasPrice(initial_price=100, initial_feecap=200, initial_tip=1, every_secs=10) + geometric_gas_price = GeometricGasPrice(web3=web3, initial_price=100, initial_tip=1, every_secs=10) # expect assert geometric_gas_price.get_gas_price(0) == 100 @@ -162,9 +162,9 @@ def test_gas_price_should_increase_with_time(self): # TODO: test geometric_gas_price.get_gas_fees() - def test_gas_price_should_obey_max_value(self): + def test_gas_price_should_obey_max_value(self, web3): # given - geometric_gas_price = GeometricGasPrice(initial_price=1000, initial_feecap=2000, initial_tip=10, + geometric_gas_price = GeometricGasPrice(web3=web3, initial_price=1000, initial_tip=10, every_secs=60, coefficient=1.125, max_price=2500) # expect @@ -176,14 +176,15 @@ def test_gas_price_should_obey_max_value(self): assert geometric_gas_price.get_gas_price(120) == 1266 assert geometric_gas_price.get_gas_price(1200) == 2500 assert geometric_gas_price.get_gas_price(3000) == 2500 - assert geometric_gas_price.get_gas_price(1000000) == 2500 + assert geometric_gas_price.get_gas_price(100000) == 2500 + # assert geometric_gas_price.get_gas_price(1000000) == 2500 # 277 hours produces overflow # TODO: test geometric_gas_price.get_gas_fees() - def test_behaves_with_realistic_values(self): + def test_behaves_with_realistic_values(self, web3): # given GWEI = 1000000000 - geometric_gas_price = GeometricGasPrice(initial_price=100*GWEI, initial_feecap=200*GWEI, initial_tip=1*GWEI, + geometric_gas_price = GeometricGasPrice(web3=web3, initial_price=100*GWEI, initial_tip=1*GWEI, every_secs=10, coefficient=1+(0.125*2)) for seconds in [0,1,10,12,30,60]: @@ -198,48 +199,48 @@ def test_behaves_with_realistic_values(self): # TODO: test geometric_gas_price.get_gas_fees() - def test_should_require_positive_initial_price(self): + def test_should_require_positive_initial_price(self, web3): with pytest.raises(AssertionError): - GeometricGasPrice(0, None, None, 60) + GeometricGasPrice(web3, 0, None, 60) with pytest.raises(AssertionError): - GeometricGasPrice(None, 0, 0, 60) + GeometricGasPrice(web3, None, 0, 0, 60) with pytest.raises(AssertionError): - GeometricGasPrice(-1, None, None, 60) + GeometricGasPrice(web3, -1, None, 60) with pytest.raises(AssertionError): - GeometricGasPrice(None, -1, -1, 60) + GeometricGasPrice(web3, None, -1, -1, 60) - def test_should_require_positive_every_secs_value(self): + def test_should_require_positive_every_secs_value(self, web3): with pytest.raises(AssertionError): - GeometricGasPrice(1000, None, None, 0) + GeometricGasPrice(web3, 1000, None, 0) with pytest.raises(AssertionError): - GeometricGasPrice(None, 600, 50, 0) + GeometricGasPrice(web3, None, 600, 50, 0) with pytest.raises(AssertionError): - GeometricGasPrice(1000, None, None, -1) + GeometricGasPrice(web3, 1000, None, -1) with pytest.raises(AssertionError): - GeometricGasPrice(None, 600, 50, -1) + GeometricGasPrice(web3, None, 600, 50, -1) - def test_should_require_positive_coefficient(self): + def test_should_require_positive_coefficient(self, web3): with pytest.raises(AssertionError): - GeometricGasPrice(1000, 600, 50, 60, 0) + GeometricGasPrice(web3, 1000, 600, 50, 60, 0) with pytest.raises(AssertionError): - GeometricGasPrice(1000, 600, 50, 60, 1) + GeometricGasPrice(web3, 1000, 600, 50, 60, 1) with pytest.raises(AssertionError): - GeometricGasPrice(1000, 600, 50, 60, -1) + GeometricGasPrice(web3, 1000, 600, 50, 60, -1) - def test_should_require_positive_max_price_if_provided(self): + def test_should_require_positive_max_price_if_provided(self, web3): with pytest.raises(AssertionError): - GeometricGasPrice(1000, 600, 50, 60, 1.125, 0) + GeometricGasPrice(web3, 1000, 50, 60, 1.125, 0) with pytest.raises(AssertionError): - GeometricGasPrice(1000, 600, 50, 60, 1.125, -1) + GeometricGasPrice(web3, 1000, 50, 60, 1.125, -1) - def test_max_price_should_exceed_initial_price(self): + def test_max_price_should_exceed_initial_price(self, web3): with pytest.raises(AssertionError): - GeometricGasPrice(6000, 600, 50, 30, 2.25, 5000) + GeometricGasPrice(web3, 6000, 50, 30, 2.25, 5000) with pytest.raises(AssertionError): - GeometricGasPrice(None, 300, 5, 30, 1.424, 200) + GeometricGasPrice(web3, None, 201, 30, 1.424, 200) diff --git a/tests/test_general2.py b/tests/test_general2.py index a8b73d13..3aea9edd 100644 --- a/tests/test_general2.py +++ b/tests/test_general2.py @@ -27,6 +27,7 @@ from pymaker.proxy import DSProxy, DSProxyCache from pymaker.token import DSToken from pymaker.util import synchronize, eth_balance +from tests.conftest import patch_web3_block_data class TestTransact: @@ -299,6 +300,20 @@ def second_send_transaction(transaction): # and assert self.token.balance_of(self.second_address) == Wad(500) + def test_gas_to_replace_calculation(self, mocker): + dummy_tx = self.token.transfer(self.second_address, Wad(0)) + type0_prev_gas_params = {'gasPrice': 20} + type0_curr_gas_params = {'gasPrice': 21} + assert not dummy_tx._gas_exceeds_replacement_threshold(type0_prev_gas_params, type0_curr_gas_params) + type0_curr_gas_params = {'gasPrice': 23} + assert dummy_tx._gas_exceeds_replacement_threshold(type0_prev_gas_params, type0_curr_gas_params) + + patch_web3_block_data(dummy_tx.web3, mocker, 7 * FixedGasPrice.GWEI) + type2_prev_gas_params = {'maxFeePerGas': 100000000000, 'maxPriorityFeePerGas': 1000000000} + type2_curr_gas_params = {'maxFeePerGas': 100000000000, 'maxPriorityFeePerGas': 1265625000} + assert not dummy_tx._gas_exceeds_replacement_threshold(type2_prev_gas_params, type2_curr_gas_params) + type2_curr_gas_params = {'maxFeePerGas': 130000000000, 'maxPriorityFeePerGas': 1265625000} + assert dummy_tx._gas_exceeds_replacement_threshold(type2_prev_gas_params, type2_curr_gas_params) class TestTransactRecover: def setup_method(self): From e24c87d19d070017298e0537acd33e67123ff3ee Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Mon, 16 Aug 2021 13:41:48 -0400 Subject: [PATCH 06/16] prevent tip from exceeding feecap, geometric unit tests --- pymaker/gas.py | 2 +- tests/gas_sandbox.ods | Bin 0 -> 25807 bytes tests/manual_test_nomcd.py | 14 ++++++---- tests/test_gas.py | 56 ++++++++++++++++++++++++++++--------- 4 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 tests/gas_sandbox.ods diff --git a/pymaker/gas.py b/pymaker/gas.py index a2b1b3a4..cbb7b3ca 100644 --- a/pymaker/gas.py +++ b/pymaker/gas.py @@ -246,4 +246,4 @@ def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: feecap = self.scale_by_time(int(base_fee * 1.2), time_elapsed) + tip if self.max_price and feecap > self.max_price: feecap = self.max_price - return feecap, tip + return feecap, min(tip, feecap) diff --git a/tests/gas_sandbox.ods b/tests/gas_sandbox.ods new file mode 100644 index 0000000000000000000000000000000000000000..102e90994d3de3350ccc9a51e75dd0c564b9e2de GIT binary patch literal 25807 zcmb4p18}C#((fDFwl~Qp8{4)w+}QRTYh&BCZQI${wr%6?|J?e%Tes?b=hmI7sb?Pi zdV0EhrswHt1!*vFbN~Pv0ATPz{uyY+8Ab;H0RE|e9|5e*t&M?hcETN@K| zQzwUiQFCOX*EcjYwle;EYwJM&KO>3$4bt3N-_+QVUdSA1t#9Y}AHcZ(1`M>dwX)N< zF}C^-xPQm4orA5ZgR!Hdf&PDD_uuvC2-F8U{X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U%@gE;BDC-WF;f0DMJ#m()#7V5Q_dX!QdEI@^MyC?$AFvaruAqv+xlQVz z;1i#5$VQvD0D4k$>2IK8Ji}zzZq#?eS*%LOXFj}c3xaouj*APFnUpqd zcgN>W(nWRKgl6B!v3JccioS=c6)2l0=#(;F@}X_1y}kP}WWjOxEDUQ}QyF6vtIu7K zaot0NiIgQeX02`KsQ zaI}*f)7gY@CV8;Sq?R)lA;F1G82Tl2pbx)}}!7KZn$C)4-!nri1jfMbG(MRd^J94*dUKxRI6m13lF3bf@tpZA^$U^~ zma&;eZ*LmCE9?Y(jy8ZnSb6p_1oHVI0<@~==!PSAOj)K$iCQRX*Ce(~oM2Gs6&>lJ zfilJtGIGV)#kXOiN(~#dV(t^#38KIxT@TdwPZ!`oAL@#SVnM9!)}jFW!uNG_UGK&} zez|&VuTST)HEC0I zM+b_rU}P{neCf)S5J4v23GYvqA5!)+pH_K z!6;)ij|O{Dy)o&EFOlx5Ke`=r>d*a5i^G}Tro+V58B>e#2Zl5QtwWHcRcAQ$Xu-Su zCoP(!;puDvW}KRnL4}Md)@7xAR4){9Tdjgp&%fy zAx1A>d4`~RBXAi&e>XqMb0Cn3Ua>^Rq1%7@%zw9rKfq#1a8UX497e^JW+1?87Y!Jkq79@Eypp!}{{@MoOA$3%+wzgPK} ziot)Q{6)n7J4OFbE{R|E>>lNRa>u=@!Cyb(zdV1Zg8xb3|I1+a&D;0l8vh~m|EJgQ z%(g$fzJHl9%I~D{|MdHv4dW;8?Jpa=pSIaA7TkY2{@$bh_W(z!|4yCpm*<~)*neHp sp5f0O?SG;CwM@TfkDqz@mu(vUAxo<&qM-g9hJXLDxKBD7#y`*g3zV-hXaE2J literal 0 HcmV?d00001 diff --git a/tests/manual_test_nomcd.py b/tests/manual_test_nomcd.py index 9db2d531..6ce117d7 100644 --- a/tests/manual_test_nomcd.py +++ b/tests/manual_test_nomcd.py @@ -39,11 +39,13 @@ print(web3.clientVersion) """ +Purpose: Tests pymaker on chains or layer-2s where multi-collateral Dai is not deployed. + Argument: Reqd? Example: Ethereum node URI yes https://localhost:8545 Ethereum address no 0x0000000000000000000000000000000aBcdef123 Private key no key_file=~keys/default-account.json,pass_file=~keys/default-account.pass -Gas price (GWEI) no 9 +Gas tip (GWEI) no 9 """ @@ -60,11 +62,11 @@ run_transactions = False gas_strategy = DefaultGasPrice() if len(sys.argv) <= 4 else \ - GeometricGasPrice(initial_price=None, # int(float(sys.argv[4]) * GeometricGasPrice.GWEI), - initial_feecap=int(60 * GeometricGasPrice.GWEI), - initial_tip=int(2 * GeometricGasPrice.GWEI), - every_secs=2, - max_price=100 * GeometricGasPrice.GWEI) + GeometricGasPrice(web3=web3, + initial_price=None, + initial_tip=int(float(sys.argv[4]) * GeometricGasPrice.GWEI), + every_secs=5, + max_price=50 * GeometricGasPrice.GWEI) eth = EthToken(web3, Address.zero()) diff --git a/tests/test_gas.py b/tests/test_gas.py index 70826b4c..9a57383f 100644 --- a/tests/test_gas.py +++ b/tests/test_gas.py @@ -21,6 +21,8 @@ from pymaker.gas import DefaultGasPrice, FixedGasPrice, GasStrategy, GeometricGasPrice, NodeAwareGasStrategy from tests.conftest import patch_web3_block_data +GWEI = GasStrategy.GWEI + class TestGasPrice: def test_not_implemented(self): @@ -146,9 +148,9 @@ def test_gas_price_requires_type0_or_type2_params(self): class TestGeometricGasPrice: - def test_gas_price_should_increase_with_time(self, web3): + def test_gas_price_should_increase_with_time(self, web3, mocker): # given - geometric_gas_price = GeometricGasPrice(web3=web3, initial_price=100, initial_tip=1, every_secs=10) + geometric_gas_price = GeometricGasPrice(web3=web3, initial_price=100, initial_tip=None, every_secs=10) # expect assert geometric_gas_price.get_gas_price(0) == 100 @@ -160,12 +162,20 @@ def test_gas_price_should_increase_with_time(self, web3): assert geometric_gas_price.get_gas_price(50) == 181 assert geometric_gas_price.get_gas_price(100) == 325 - # TODO: test geometric_gas_price.get_gas_fees() + geometric_gas_price = GeometricGasPrice(web3=web3, initial_price=None, initial_tip=1 * GWEI, every_secs=10) + patch_web3_block_data(web3, mocker, base_fee=10 * GWEI) + last_fees = (0, 0) + for i in [0, 10, 20, 30, 50, 100, 300, 1800, 3600]: + current_fees = geometric_gas_price.get_gas_fees(i) + assert current_fees[1] > last_fees[1] + last_fees = current_fees - def test_gas_price_should_obey_max_value(self, web3): + def test_gas_price_should_obey_max_value(self, web3, mocker): # given + max_price = 2500 geometric_gas_price = GeometricGasPrice(web3=web3, initial_price=1000, initial_tip=10, - every_secs=60, coefficient=1.125, max_price=2500) + every_secs=60, coefficient=1.125, max_price=max_price) + patch_web3_block_data(web3, mocker, base_fee=10 * GWEI) # expect assert geometric_gas_price.get_gas_price(0) == 1000 @@ -179,16 +189,32 @@ def test_gas_price_should_obey_max_value(self, web3): assert geometric_gas_price.get_gas_price(100000) == 2500 # assert geometric_gas_price.get_gas_price(1000000) == 2500 # 277 hours produces overflow - # TODO: test geometric_gas_price.get_gas_fees() - - def test_behaves_with_realistic_values(self, web3): + for i in [0, 120, 3600, 100000]: + print(f"checking {i} seconds") + current_fees = geometric_gas_price.get_gas_fees(i) + assert current_fees[0] <= max_price + assert current_fees[1] <= current_fees[0] + + @staticmethod + def assert_gas_fees_equivalent(lhs: Tuple, rhs: Tuple, decimals=2): + assert isinstance(lhs, Tuple) + assert isinstance(rhs, Tuple) + left_feecap = lhs[0] / GasStrategy.GWEI + left_tip = lhs[1] / GasStrategy.GWEI + right_feecap = rhs[0] / GasStrategy.GWEI + right_tip = rhs[1] / GasStrategy.GWEI + assert round(left_feecap, decimals) == round(right_feecap, decimals) + assert round(left_tip, decimals) == round(right_tip, decimals) + + def test_behaves_with_realistic_values(self, web3, mocker): # given GWEI = 1000000000 - geometric_gas_price = GeometricGasPrice(web3=web3, initial_price=100*GWEI, initial_tip=1*GWEI, - every_secs=10, coefficient=1+(0.125*2)) + base_fee = 50*GWEI + geometric_gas_price = GeometricGasPrice(web3=web3, initial_price=100*GWEI, initial_tip=15*GWEI, + every_secs=10, coefficient=1.25, max_price=4000*GWEI) + patch_web3_block_data(web3, mocker, base_fee) - for seconds in [0,1,10,12,30,60]: - print(f"gas price after {seconds} seconds is {geometric_gas_price.get_gas_price(seconds)/GWEI}") + # See gas sandbox spreadsheet in test folder to validate calculations assert round(geometric_gas_price.get_gas_price(0) / GWEI, 1) == 100.0 assert round(geometric_gas_price.get_gas_price(1) / GWEI, 1) == 100.0 @@ -196,8 +222,12 @@ def test_behaves_with_realistic_values(self, web3): assert round(geometric_gas_price.get_gas_price(12) / GWEI, 1) == 125.0 assert round(geometric_gas_price.get_gas_price(30) / GWEI, 1) == 195.3 assert round(geometric_gas_price.get_gas_price(60) / GWEI, 1) == 381.5 + assert round(geometric_gas_price.get_gas_price(180) / GWEI, 1) == 4000.0 - # TODO: test geometric_gas_price.get_gas_fees() + self.assert_gas_fees_equivalent(geometric_gas_price.get_gas_fees(0), (75 * GWEI, 15 * GWEI)) + self.assert_gas_fees_equivalent(geometric_gas_price.get_gas_fees(30), (146.48 * GWEI, 29.30 * GWEI)) + self.assert_gas_fees_equivalent(geometric_gas_price.get_gas_fees(60), (286.10 * GWEI, 57.22 * GWEI)) + self.assert_gas_fees_equivalent(geometric_gas_price.get_gas_fees(300), (4000 * GWEI, 4000 * GWEI)) def test_should_require_positive_initial_price(self, web3): with pytest.raises(AssertionError): From 4e841823aaae571b81697d8603bbdab96f4e1317 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Mon, 16 Aug 2021 17:48:51 -0400 Subject: [PATCH 07/16] fixed bug management replacement gas --- pymaker/__init__.py | 99 +++++++++++++++++----------------- pymaker/gas.py | 13 +++-- tests/gas_sandbox.ods | Bin 25807 -> 25835 bytes tests/manual_test_async_tx.py | 22 ++++---- tests/test_gas.py | 2 +- 5 files changed, 71 insertions(+), 65 deletions(-) diff --git a/pymaker/__init__.py b/pymaker/__init__.py index 8c7d6b7f..5556c47a 100644 --- a/pymaker/__init__.py +++ b/pymaker/__init__.py @@ -496,7 +496,7 @@ def __init__(self, self.nonce = None self.replaced = False self.gas_strategy = None - self.gas_strategy_last = 0 + self.gas_fees_last = None self.tx_hashes = [] def _get_receipt(self, transaction_hash: str) -> Optional[Receipt]: @@ -558,6 +558,8 @@ def _gas_exceeds_replacement_threshold(self, prev_gas_params: dict, curr_gas_par # return curr_effective_price > prev_effective_price * 1.125 feecap_bumped = curr_gas_params['maxFeePerGas'] > prev_gas_params['maxFeePerGas'] * 1.125 tip_bumped = curr_gas_params['maxPriorityFeePerGas'] > prev_gas_params['maxPriorityFeePerGas'] * 1.125 + # print(f"feecap={curr_gas_params['maxFeePerGas']} tip={curr_gas_params['maxPriorityFeePerGas']} " + # f"feecap_bumped={feecap_bumped} tip_bumped={tip_bumped}") return feecap_bumped and tip_bumped else: # Replacement impossible if no parameters were offered return False @@ -572,7 +574,6 @@ def _func(self, from_account: str, gas: int, gas_price_params: dict, nonce: Opti **gas_price_params, **nonce_dict, **self._as_dict(self.extra)} - pprint(transaction_params) if self.contract is not None: if self.function_name is None: @@ -594,6 +595,47 @@ def _contract_function(self): return function_factory(*self.parameters) + def _interlocked_choose_nonce_and_send(self, from_account: str, gas: int, gas_fees: dict): + assert isinstance(from_account, str) # address of the sender + assert isinstance(gas, int) # gas amount + assert isinstance(gas_fees, dict) # gas fee parameters + try: + # We need the lock in order to not try to send two transactions with the same nonce. + with transaction_lock: + if self.nonce is None: + nonce_calc = _get_endpoint_behavior(self.web3).nonce_calc + if nonce_calc == NonceCalculation.PARITY_NEXTNONCE: + self.nonce = int(self.web3.manager.request_blocking("parity_nextNonce", [from_account]), 16) + elif nonce_calc == NonceCalculation.TX_COUNT: + self.nonce = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') + elif nonce_calc == NonceCalculation.SERIAL: + tx_count = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') + next_serial = next_nonce[from_account] + self.nonce = max(tx_count, next_serial) + elif nonce_calc == NonceCalculation.PARITY_SERIAL: + tx_count = int(self.web3.manager.request_blocking("parity_nextNonce", [from_account]), 16) + next_serial = next_nonce[from_account] + self.nonce = max(tx_count, next_serial) + next_nonce[from_account] = self.nonce + 1 + + # Trap replacement while original is holding the lock awaiting nonce assignment + if self.replaced: + self.logger.info(f"Transaction {self.name()} with nonce={self.nonce} was replaced") + return None + + tx_hash = self._func(from_account, gas, gas_fees, self.nonce) + self.tx_hashes.append(tx_hash) + + self.logger.info(f"Sent transaction {self.name()} with nonce={self.nonce}, gas={gas}," + f" gas_fees={gas_fees if gas_fees else 'default'}" + f" (tx_hash={tx_hash})") + except Exception as e: + self.logger.warning(f"Failed to send transaction {self.name()} with nonce={self.nonce}, gas={gas}," + f" gas_fees={gas_fees if gas_fees else 'default'} ({e})") + + if len(self.tx_hashes) == 0: + raise + def name(self) -> str: """Returns the nicely formatted name of this pending Ethereum transaction. @@ -718,7 +760,6 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: gas = self._gas(gas_estimate, **kwargs) self.gas_strategy = kwargs['gas_strategy'] if ('gas_strategy' in kwargs) else DefaultGasPrice() assert(isinstance(self.gas_strategy, GasStrategy)) - gas_fees_last = None # Get the transaction this one is supposed to replace. # If there is one, try to borrow the nonce from it as long as that transaction isn't finished. @@ -734,7 +775,7 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: # Use gas strategy from the original transaction if one was not provided if 'gas_strategy' not in kwargs: self.gas_strategy = replaced_tx.gas_strategy if replaced_tx.gas_strategy else DefaultGasPrice() - self.gas_strategy_last = replaced_tx.gas_strategy_last + self.gas_fees_last = replaced_tx.gas_fees_last # Detain replacement until gas strategy produces a price acceptable to the node if replaced_tx.tx_hashes: most_recent_tx = replaced_tx.tx_hashes[-1] @@ -757,6 +798,8 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: receipt = self._get_receipt(tx_hash) if receipt: if receipt.successful: + # CAUTION: If original transaction is being replaced, this will print details of the + # replacement transaction even if the receipt was generated from the original. self.logger.info(f"Transaction {self.name()} was successful (tx_hash={tx_hash})") return receipt else: @@ -777,7 +820,7 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: # Trap replacement after the tx has entered the mempool and before it has been mined if self.replaced: - self.logger.info(f"Transaction {self.name()} with nonce={self.nonce} is being replaced") + self.logger.info(f"Attempting to replace transaction {self.name()} with nonce={self.nonce}") return None # Send a transaction if: @@ -785,49 +828,9 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: # - the requested gas price has changed enough since the last transaction has been sent # - the gas price on a replacement has sufficiently exceeded that of the original transaction transaction_was_sent = len(self.tx_hashes) > 0 or (replaced_tx is not None and len(replaced_tx.tx_hashes) > 0) - # Uncomment this to debug state during transaction submission - # self.logger.debug(f"Transaction {self.name()} is churning: was_sent={transaction_was_sent}") - # TODO: For EIP-1559 transactions, both maxFeePerGas and maxPriorityFeePerGas need to be bumped 12.5% to replace. - if not transaction_was_sent or (gas_fees_last and self._gas_exceeds_replacement_threshold(gas_fees_last, gas_fees)): - gas_fees_last = gas_fees - - try: - # We need the lock in order to not try to send two transactions with the same nonce. - with transaction_lock: - if self.nonce is None: - nonce_calc = _get_endpoint_behavior(self.web3).nonce_calc - if nonce_calc == NonceCalculation.PARITY_NEXTNONCE: - self.nonce = int(self.web3.manager.request_blocking("parity_nextNonce", [from_account]), 16) - elif nonce_calc == NonceCalculation.TX_COUNT: - self.nonce = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') - elif nonce_calc == NonceCalculation.SERIAL: - tx_count = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') - next_serial = next_nonce[from_account] - self.nonce = max(tx_count, next_serial) - elif nonce_calc == NonceCalculation.PARITY_SERIAL: - tx_count = int(self.web3.manager.request_blocking("parity_nextNonce", [from_account]), 16) - next_serial = next_nonce[from_account] - self.nonce = max(tx_count, next_serial) - next_nonce[from_account] = self.nonce + 1 - - # Trap replacement while original is holding the lock awaiting nonce assignment - if self.replaced: - self.logger.info(f"Transaction {self.name()} with nonce={self.nonce} was replaced") - return None - - tx_hash = self._func(from_account, gas, gas_fees, self.nonce) - self.tx_hashes.append(tx_hash) - - self.logger.info(f"Sent transaction {self.name()} with nonce={self.nonce}, gas={gas}," - f" gas_fees={gas_fees if gas_fees else 'default'}" - f" (tx_hash={tx_hash})") - except Exception as e: - self.logger.warning(f"Failed to send transaction {self.name()} with nonce={self.nonce}, gas={gas}," - f" gas_fees={gas_fees if gas_fees else 'default'} ({e})") - - if len(self.tx_hashes) == 0: - raise - + if not transaction_was_sent or (self.gas_fees_last and self._gas_exceeds_replacement_threshold(self.gas_fees_last, gas_fees)): + self.gas_fees_last = gas_fees + self._interlocked_choose_nonce_and_send(from_account, gas, gas_fees) await asyncio.sleep(0.25) def invocation(self) -> Invocation: diff --git a/pymaker/gas.py b/pymaker/gas.py index cbb7b3ca..6fa71524 100644 --- a/pymaker/gas.py +++ b/pymaker/gas.py @@ -101,11 +101,11 @@ def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: def get_node_gas_price(self) -> int: return max(self.web3.manager.request_blocking("eth_gasPrice", []), 1) - def get_next_base_fee(self) -> Optional[int]: + def get_base_fee(self) -> Optional[int]: """Useful for calculating maxfee; a multiple of this value is suggested""" - next_block = self.web3.eth.get_block('pending') - if 'baseFeePerGas' in next_block: - return max(int(next_block['baseFeePerGas']), 1) + pending_block = self.web3.eth.get_block('pending') + if 'baseFeePerGas' in pending_block: + return max(int(pending_block['baseFeePerGas']), 1) else: return None @@ -228,7 +228,10 @@ def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: if not self.initial_tip: return None - base_fee = self.get_next_base_fee() + base_fee = self.get_base_fee() + if not base_fee: + raise RuntimeError("Node does not provide baseFeePerGas; type 2 transactions are not available") + tip = self.scale_by_time(self.initial_tip, time_elapsed) # This is how it should work, but doesn't; read more here: https://github.com/ethereum/go-ethereum/issues/23311 diff --git a/tests/gas_sandbox.ods b/tests/gas_sandbox.ods index 102e90994d3de3350ccc9a51e75dd0c564b9e2de..9b3ef591d7ea3c5cf5b8ff83b424cee09ddb2a72 100644 GIT binary patch delta 7315 zcmZWuWmuI&%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~GFpo@NeE|pTyo2Q}DYeTz6v=Gjp#!&AO|+v}WV%2e&bzY;t&G)m z>Zdn2*Sc1c^!qTbe|ZPeU0~5JGX6Q)S_%AFBK@b}cA}|Skk{K@7N+&LDk~_{_B!an z=z$%y$nG)P#jh#GQM&Wk?S^N{ss*XpxR-7jKNit9w)9^MN!Qf2Nm)oWPU+DGC3JO( zJF5QOsRVSro6GfiqnRpcf0rxHCUC!_X5U#qo7VAoHAFMe@0wtQKB95sDX9JX77zFbk=cZP!++eu4#3z#p0b2Au`^M?Bx%v0@9WkztjgQrfGNyZ;mFL zZ0Z7zbX-b_qj_ny?WvIbLQhb`D`3=YGWfI;ssI?|&wfl>;QQLey9++mgC>224KKmx zPEikMR0lWhg>xg#PZ@7fn!*0bE&7a&Xho!VMNkGLdccv-@EmH;IQtBN1y6Hm5i;$Oc9`dY4@5TfVM#u;k0& zLsJ+e=uh-5F`_v4leix1vQIbU@F{~ zl~%+V3dK##s&<_h*?s#1{ZI7P&b>EsxtB3pa4u*{%?m%MW5Zmr5rqNud!=4^$L&F` z@wv3(qsjAqlz6&tW{U_1r%ek7_YV%MCYixS`f!2l*49N5{(!_vuv)5p<4 yQ{~@*{F|b|ee;jp{bS<@MBIYn*{SyylprUFw*}L)kn<$0LfR~tk#$U;)&CC;c(mXE diff --git a/tests/manual_test_async_tx.py b/tests/manual_test_async_tx.py index c8d8f141..656aa732 100644 --- a/tests/manual_test_async_tx.py +++ b/tests/manual_test_async_tx.py @@ -45,40 +45,40 @@ weth = DssDeployment.from_node(web3).collaterals['ETH-A'].gem GWEI = 1000000000 -slow_gas = GeometricGasPrice(initial_price=int(15 * GWEI), every_secs=42, max_price=200 * GWEI) -fast_gas = GeometricGasPrice(initial_price=int(30 * GWEI), every_secs=42, max_price=200 * GWEI) +slow_gas = GeometricGasPrice(web3=web3, initial_price=None, initial_tip=int(0.1 * GWEI), every_secs=42, max_price=200 * GWEI) +fast_gas = GeometricGasPrice(web3=web3, initial_price=None, initial_tip=int(4.5 * GWEI), every_secs=42, max_price=200 * GWEI) class TestApp: def main(self): - # self.test_replacement() + self.test_replacement() self.test_simultaneous() self.shutdown() def test_replacement(self): first_tx = weth.deposit(Wad(4)) logging.info(f"Submitting first TX with gas price deliberately too low") - self._run_future(first_tx.transact_async(gas_price=slow_gas)) - time.sleep(0.5) + self._run_future(first_tx.transact_async(gas_strategy=slow_gas)) + time.sleep(0.1) second_tx = weth.deposit(Wad(6)) logging.info(f"Replacing first TX with legitimate gas price") - second_tx.transact(replace=first_tx, gas_price=fast_gas) + second_tx.transact(replace=first_tx, gas_strategy=fast_gas) assert first_tx.replaced def test_simultaneous(self): - self._run_future(weth.deposit(Wad(1)).transact_async(gas_price=fast_gas)) - self._run_future(weth.deposit(Wad(3)).transact_async(gas_price=fast_gas)) - self._run_future(weth.deposit(Wad(5)).transact_async(gas_price=fast_gas)) - self._run_future(weth.deposit(Wad(7)).transact_async(gas_price=fast_gas)) + self._run_future(weth.deposit(Wad(1)).transact_async(gas_strategy=fast_gas)) + self._run_future(weth.deposit(Wad(3)).transact_async(gas_strategy=fast_gas)) + self._run_future(weth.deposit(Wad(5)).transact_async(gas_strategy=fast_gas)) + self._run_future(weth.deposit(Wad(7)).transact_async(gas_strategy=fast_gas)) time.sleep(33) def shutdown(self): balance = weth.balance_of(our_address) if Wad(0) < balance < Wad(100): # this account's tiny WETH balance came from this test logging.info(f"Unwrapping {balance} WETH") - assert weth.withdraw(balance).transact(gas_price=fast_gas) + assert weth.withdraw(balance).transact(gas_strategy=fast_gas) @staticmethod def _run_future(future): diff --git a/tests/test_gas.py b/tests/test_gas.py index 9a57383f..a67c0584 100644 --- a/tests/test_gas.py +++ b/tests/test_gas.py @@ -57,7 +57,7 @@ def get_gas_price(self, time_elapsed: int) -> Optional[int]: return self.get_node_gas_price() * max(time_elapsed, 1) def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: - return int(self.get_next_base_fee()*1.5), 2 * self.GWEI + return int(self.get_base_fee() * 1.5), 2 * self.GWEI class BadImplementation(NodeAwareGasStrategy): pass From 5cc34cae038f0545d0c9d3221eb7565af3f7a338 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Mon, 16 Aug 2021 22:13:30 -0400 Subject: [PATCH 08/16] unit tests all pass --- pymaker/collateral.py | 6 +++--- pymaker/deployment.py | 6 +++--- pymaker/gas.py | 10 ++++------ tests/manual_test_tx_recovery.py | 8 +++++--- tests/test_approval.py | 4 ++-- tests/test_auctions.py | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pymaker/collateral.py b/pymaker/collateral.py index f61d2c24..958da9a2 100644 --- a/pymaker/collateral.py +++ b/pymaker/collateral.py @@ -63,6 +63,6 @@ def approve(self, usr: Address, **kwargs): Args usr: User making transactions with this collateral """ - gas_price = kwargs['gas_price'] if 'gas_price' in kwargs else DefaultGasPrice() - self.adapter.approve(hope_directly(from_address=usr, gas_price=gas_price), self.vat.address) - self.adapter.approve_token(directly(from_address=usr, gas_price=gas_price)) + gas_strategy = kwargs['gas_strategy'] if 'gas_strategy' in kwargs else DefaultGasPrice() + self.adapter.approve(hope_directly(from_address=usr, gas_strategy=gas_strategy), self.vat.address) + self.adapter.approve_token(directly(from_address=usr, gas_strategy=gas_strategy)) diff --git a/pymaker/deployment.py b/pymaker/deployment.py index bd99c03f..906cd4ab 100644 --- a/pymaker/deployment.py +++ b/pymaker/deployment.py @@ -382,10 +382,10 @@ def approve_dai(self, usr: Address, **kwargs): """ assert isinstance(usr, Address) - gas_price = kwargs['gas_price'] if 'gas_price' in kwargs else DefaultGasPrice() - self.dai_adapter.approve(approval_function=hope_directly(from_address=usr, gas_price=gas_price), + gas_strategy = kwargs['gas_strategy'] if 'gas_strategy' in kwargs else DefaultGasPrice() + self.dai_adapter.approve(approval_function=hope_directly(from_address=usr, gas_strategy=gas_strategy), source=self.vat.address) - self.dai.approve(self.dai_adapter.address).transact(from_address=usr, gas_price=gas_price) + self.dai.approve(self.dai_adapter.address).transact(from_address=usr, gas_strategy=gas_strategy) def active_auctions(self) -> dict: flips = {} diff --git a/pymaker/gas.py b/pymaker/gas.py index 6fa71524..afb811ad 100644 --- a/pymaker/gas.py +++ b/pymaker/gas.py @@ -25,10 +25,8 @@ class GasStrategy(object): """Abstract class, which can be inherited for implementing different gas price strategies. - `GasPrice` class contains only one method, `get_gas_price`, which is responsible for - returning the gas price (in Wei) for a specific point in time. It is possible to build - custom gas price strategies by implementing this method so the gas price returned - increases over time. The piece of code responsible for sending Ethereum transactions + To build custom gas price strategies, override methods within such that gas fees returned + increase over time. The piece of code responsible for sending Ethereum transactions (please see :py:class:`pymaker.Transact`) will in this case overwrite the transaction with another one, using the same `nonce` but increasing gas price. If the value returned by `get_gas_price` does not go up, no new transaction gets submitted to the network. @@ -58,7 +56,7 @@ def get_gas_price(self, time_elapsed: int) -> Optional[int]: raise NotImplementedError("Please implement this method") def get_gas_fees(self, time_elapsed: int) -> Tuple[int, int]: - """Return fee cap (max fee) and tip for type 2 (EIP-1559) transactions""" + """Return max fee (fee cap) and priority fee (tip) for type 2 (EIP-1559) transactions""" raise NotImplementedError("Please implement this method") @@ -122,7 +120,7 @@ class FixedGasPrice(GasStrategy): max_fee: Maximum fee (in Wei) for EIP-1559 transactions, should be >= (base_fee + tip) tip: Priority fee (in Wei) for EIP-1559 transactions """ - def __init__(self, gas_price: Optional[int], max_fee: Optional[int], tip: Optional[int]): + def __init__(self, gas_price: Optional[int], max_fee: Optional[int] = None, tip: Optional[int] = None): assert isinstance(gas_price, int) or gas_price is None assert isinstance(max_fee, int) or max_fee is None assert isinstance(tip, int) or tip is None diff --git a/tests/manual_test_tx_recovery.py b/tests/manual_test_tx_recovery.py index 7deb8cd8..af00cda0 100644 --- a/tests/manual_test_tx_recovery.py +++ b/tests/manual_test_tx_recovery.py @@ -43,7 +43,9 @@ stuck_txes_to_submit = int(sys.argv[3]) if len(sys.argv) > 3 else 1 GWEI = 1000000000 -increasing_gas = GeometricGasPrice(initial_price=int(1 * GWEI), every_secs=30, coefficient=1.5, max_price=100 * GWEI) +too_low_gas = FixedGasPrice(gas_price=int(0.4 * GWEI), max_fee=None, tip=None) +increasing_gas = GeometricGasPrice(web3=web3, initial_price=1*GWEI, initial_tip=None, + every_secs=30, coefficient=1.5, max_price=200*GWEI) class TestApp: @@ -55,14 +57,14 @@ def main(self): if len(pending_txes) > 0: while len(pending_txes) > 0: - pending_txes[0].cancel(gas_price=increasing_gas) + pending_txes[0].cancel(gas_strategy=increasing_gas) # After the synchronous cancel, wait to see if subsequent transactions get mined time.sleep(15) pending_txes = get_pending_transactions(web3) else: logging.info(f"No pending transactions were found; submitting {stuck_txes_to_submit}") for i in range(1, stuck_txes_to_submit+1): - self._run_future(weth.deposit(Wad(i)).transact_async(gas_price=FixedGasPrice(int(0.4 * i * GWEI)))) + self._run_future(weth.deposit(Wad(i)).transact_async(gas_strategy=too_low_gas)) time.sleep(2) self.shutdown() diff --git a/tests/test_approval.py b/tests/test_approval.py index 2cc1d978..88f753ac 100644 --- a/tests/test_approval.py +++ b/tests/test_approval.py @@ -83,7 +83,7 @@ def test_direct_approval_should_obey_gas_price(): global web3, our_address, second_address, token # when - directly(gas_price=FixedGasPrice(25000000000))(token, second_address, "some-name") + directly(gas_strategy=FixedGasPrice(25000000000))(token, second_address, "some-name") # then assert web3.eth.getBlock('latest', full_transactions=True).transactions[0].gasPrice == 25000000000 @@ -129,7 +129,7 @@ def test_via_tx_manager_approval_should_obey_gas_price(): tx = TxManager.deploy(web3) # when - via_tx_manager(tx, gas_price=FixedGasPrice(15000000000))(token, second_address, "some-name") + via_tx_manager(tx, gas_strategy=FixedGasPrice(15000000000))(token, second_address, "some-name") # then assert web3.eth.getBlock('latest', full_transactions=True).transactions[0].gasPrice == 15000000000 diff --git a/tests/test_auctions.py b/tests/test_auctions.py index fdb1e9b6..e31cefc8 100644 --- a/tests/test_auctions.py +++ b/tests/test_auctions.py @@ -44,7 +44,7 @@ def create_surplus(mcd: DssDeployment, flapper: Flapper, deployment_address: Add assert collateral.adapter.join(deployment_address, ink).transact( from_address=deployment_address) # CAUTION: dart needs to be adjusted over time to keep tests happy - frob(mcd, collateral, deployment_address, dink=ink, dart=Wad.from_number(7500)) + frob(mcd, collateral, deployment_address, dink=ink, dart=Wad.from_number(5000)) assert mcd.jug.drip(collateral.ilk).transact(from_address=deployment_address) joy = mcd.vat.dai(mcd.vow.address) # total surplus > total debt + surplus auction lot size + surplus buffer From 6ff170d560f48ee421bbe54a93626d919fc83774 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Thu, 19 Aug 2021 11:52:01 -0400 Subject: [PATCH 09/16] removed obsolete tx recovery facility, fixed bug handling non-eip1559 nodes --- pymaker/__init__.py | 111 +--------------- tests/manual_test_mempool.py | 216 +++++++++++++++++++++++++++++++ tests/manual_test_tx_recovery.py | 93 ------------- tests/test_general2.py | 36 +----- 4 files changed, 224 insertions(+), 232 deletions(-) create mode 100644 tests/manual_test_mempool.py delete mode 100644 tests/manual_test_tx_recovery.py diff --git a/pymaker/__init__.py b/pymaker/__init__.py index 5556c47a..f41308da 100644 --- a/pymaker/__init__.py +++ b/pymaker/__init__.py @@ -78,14 +78,14 @@ class NonceCalculation(Enum): class EndpointBehavior: - def __init__(self, nonce_calc: NonceCalculation, supports_london: bool): + def __init__(self, nonce_calc: NonceCalculation, supports_eip1559: bool): assert isinstance(nonce_calc, NonceCalculation) - assert isinstance(supports_london, bool) + assert isinstance(supports_eip1559, bool) self.nonce_calc = nonce_calc - self.supports_london = supports_london + self.supports_eip1559 = supports_eip1559 def __repr__(self): - if self.supports_london: + if self.supports_eip1559: return f"{self.nonce_calc} with EIP 1559 support" else: return f"{self.nonce_calc} without EIP 1559 support" @@ -428,34 +428,6 @@ class TransactStatus(Enum): FINISHED = auto() -def get_pending_transactions(web3: Web3, address: Address = None) -> list: - """Retrieves a list of pending transactions from the mempool.""" - assert isinstance(web3, Web3) - assert isinstance(address, Address) or address is None - - if address is None: - address = Address(web3.eth.defaultAccount) - - # Get the list of pending transactions and their details from specified sources - nonce_calc = _get_endpoint_behavior(web3).nonce_calc - if False and nonce_calc in (NonceCalculation.PARITY_NEXTNONCE, NonceCalculation.PARITY_SERIAL): - items = web3.manager.request_blocking("parity_pendingTransactions", []) - items = filter(lambda item: item['from'].lower() == address.address.lower(), items) - items = filter(lambda item: item['blockNumber'] is None, items) - txes = map(lambda item: RecoveredTransact(web3=web3, address=address, nonce=int(item['nonce'], 16), - latest_tx_hash=item['hash'], current_gas=int(item['gasPrice'], 16)), - items) - else: - items = web3.manager.request_blocking("eth_getBlockByNumber", ["pending", True])['transactions'] - items = filter(lambda item: item['from'].lower() == address.address.lower(), items) - list(items) # Unsure why this is required - txes = map(lambda item: RecoveredTransact(web3=web3, address=address, nonce=item['nonce'], - latest_tx_hash=item['hash'], current_gas=item['gasPrice']), - items) - - return list(txes) - - class Transact: """Represents an Ethereum transaction before it gets executed.""" @@ -531,10 +503,11 @@ def _gas_fees(self, seconds_elapsed: int, gas_strategy: GasStrategy) -> dict: assert isinstance(seconds_elapsed, int) assert isinstance(gas_strategy, GasStrategy) + supports_eip1559 = _get_endpoint_behavior(self.web3).supports_eip1559 gas_price = gas_strategy.get_gas_price(seconds_elapsed) - gas_feecap, gas_tip = gas_strategy.get_gas_fees(seconds_elapsed) + gas_feecap, gas_tip = gas_strategy.get_gas_fees(seconds_elapsed) if supports_eip1559 else (None, None) - if _get_endpoint_behavior(self.web3).supports_london and gas_feecap and gas_tip: # prefer type 2 TXes + if supports_eip1559 and gas_feecap and gas_tip: # prefer type 2 TXes params = {'maxFeePerGas': gas_feecap, 'maxPriorityFeePerGas': gas_tip} elif gas_price: # fallback to type 0 if not supported or params not specified params = {'gasPrice': gas_price} @@ -847,76 +820,6 @@ def invocation(self) -> Invocation: return Invocation(self.address, Calldata(self._contract_function()._encode_transaction_data())) -# TODO: Add EIP-1559 support. -class RecoveredTransact(Transact): - """ Models a pending transaction retrieved from the mempool. - - These can be created by a call to `get_pending_transactions`, enabling the consumer to implement logic which - cancels pending transactions upon keeper/bot startup. - """ - def __init__(self, web3: Web3, - address: Address, - nonce: int, - latest_tx_hash: str, - current_gas: int): - assert isinstance(current_gas, int) - super().__init__(origin=None, - web3=web3, - abi=None, - address=address, - contract=None, - function_name=None, - parameters=None) - self.nonce = nonce - self.tx_hashes.append(latest_tx_hash) - self.current_gas = current_gas - self.gas_price_last = None - - def name(self): - return f"Recovered tx with nonce {self.nonce}" - - @_track_status - async def transact_async(self, **kwargs) -> Optional[Receipt]: - # TODO: Read transaction data from chain, create a new state machine to manage gas for the transaction. - raise NotImplementedError() - - def cancel(self, gas_strategy: GasStrategy): - return synchronize([self.cancel_async(gas_strategy)])[0] - - async def cancel_async(self, gas_strategy: GasStrategy): - assert isinstance(gas_strategy, GasStrategy) - initial_time = time.time() - self.gas_price_last = self.current_gas - self.tx_hashes.clear() - - if gas_strategy.get_gas_price(0) <= self.current_gas * 1.125: - self.logger.warning(f"Recovery gas price is less than current gas price {self.current_gas}; " - "cancellation will be deferred until the strategy produces an acceptable price.") - - while True: - seconds_elapsed = int(time.time() - initial_time) - gas_price_value = gas_strategy.get_gas_price(seconds_elapsed) - if gas_price_value > self.gas_price_last * 1.125: - self.gas_price_last = gas_price_value - # Transaction lock isn't needed here, as we are replacing an existing nonce - tx_hash = bytes_to_hexstring(self.web3.eth.sendTransaction({'from': self.address.address, - 'to': self.address.address, - 'gasPrice': gas_price_value, - 'nonce': self.nonce, - 'value': 0})) - self.tx_hashes.append(tx_hash) - self.logger.info(f"Attempting to cancel recovered tx with nonce={self.nonce}, " - f"gas_price={gas_price_value} (tx_hash={tx_hash})") - - for tx_hash in self.tx_hashes: - receipt = self._get_receipt(tx_hash) - if receipt: - self.logger.info(f"{self.name()} was cancelled (tx_hash={tx_hash})") - return - - await asyncio.sleep(0.75) - - class Transfer: """Represents an ERC20 token transfer. diff --git a/tests/manual_test_mempool.py b/tests/manual_test_mempool.py new file mode 100644 index 00000000..9fef255a --- /dev/null +++ b/tests/manual_test_mempool.py @@ -0,0 +1,216 @@ +# This file is part of Maker Keeper Framework. +# +# Copyright (C) 2020 EdNoepel +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import asyncio +import logging +import os +import sys +import time +import threading +from pprint import pprint +from typing import Optional +from web3 import Web3 + +from pymaker import _get_endpoint_behavior, _track_status, Address, eth_transfer, NonceCalculation, Receipt, \ + Transact, Wad, web3_via_http +from pymaker.deployment import DssDeployment +from pymaker.gas import FixedGasPrice, GasStrategy, GeometricGasPrice +from pymaker.keys import register_keys +from pymaker.util import synchronize, bytes_to_hexstring + + +logging.basicConfig(format='%(asctime)-15s %(levelname)-8s %(message)s', level=logging.DEBUG) +# reduce logspew +logging.getLogger('urllib3').setLevel(logging.INFO) +logging.getLogger("web3").setLevel(logging.INFO) +logging.getLogger("asyncio").setLevel(logging.INFO) +logging.getLogger("requests").setLevel(logging.INFO) + +web3 = web3_via_http(endpoint_uri=os.environ['ETH_RPC_URL']) +if len(sys.argv) > 2: + web3.eth.defaultAccount = sys.argv[1] # ex: 0x0000000000000000000000000000000aBcdef123 + register_keys(web3, [sys.argv[2]]) # ex: key_file=~keys/default-account.json,pass_file=~keys/default-account.pass + our_address = Address(web3.eth.defaultAccount) + stuck_txes_to_submit = int(sys.argv[3]) if len(sys.argv) > 3 else 1 +else: + our_address = None + stuck_txes_to_submit = 0 + +GWEI = 1000000000 +too_low_gas = FixedGasPrice(gas_price=int(0.4 * GWEI), max_fee=None, tip=None) +increasing_gas = GeometricGasPrice(web3=web3, initial_price=1*GWEI, initial_tip=None, + every_secs=30, coefficient=1.5, max_price=200*GWEI) + + +def get_pending_transactions(web3: Web3, address: Address = None) -> list: + """Retrieves a list of pending transactions from the mempool.""" + assert isinstance(web3, Web3) + assert isinstance(address, Address) or address is None + + # Get the list of pending transactions and their details from specified sources + nonce_calc = _get_endpoint_behavior(web3).nonce_calc + if nonce_calc == NonceCalculation.PARITY_NEXTNONCE: + items = web3.manager.request_blocking("parity_pendingTransactions", []) + if address: + items = filter(lambda item: item['from'].lower() == address.address.lower(), items) + return list(map(lambda item: RecoveredTransact(web3=web3, + address=Address(item['from']), + nonce=int(item['nonce'], 16), + latest_tx_hash=item['hash'], + current_gas=int(item['gasPrice'], 16)), items)) + else: + summarize_transactions(items) + else: + items = web3.manager.request_blocking("eth_getBlockByNumber", ["pending", True])['transactions'] + summarize_transactions(items) + if address: + items = filter(lambda item: item['from'].lower() == address.address.lower(), items) + return list(map(lambda item: RecoveredTransact(web3=web3, + address=Address(item['from']), + nonce=item['nonce'], + latest_tx_hash=item['hash'], + current_gas=item['gasPrice']), items)) + else: + summarize_transactions(items) + return [] + + +def summarize_transactions(txes): + if len(txes) == 0: + print("No transactions found") + return + lowest_gas = None + highest_gas = None + addresses = set() + for tx in txes: + if isinstance(tx['gasPrice'], int): + gas_price = tx['gasPrice'] / GasStrategy.GWEI + else: + gas_price = int(tx['gasPrice'], 16) / GasStrategy.GWEI + lowest_gas = min(lowest_gas, gas_price) if lowest_gas else gas_price + highest_gas = max(highest_gas, gas_price) if highest_gas else gas_price + addresses.add(tx['from']) + # pprint(tx) + print(f"Found {len(txes)} TXes from {len(addresses)} unique addresses " + f"with gas from {lowest_gas} to {highest_gas} gwei") + + +class RecoveredTransact(Transact): + """ Models a pending transaction retrieved from the mempool. + + These can be created by a call to `get_pending_transactions`, enabling the consumer to implement logic which + cancels pending transactions upon keeper/bot startup. + """ + def __init__(self, web3: Web3, + address: Address, + nonce: int, + latest_tx_hash: str, + current_gas: int): + assert isinstance(current_gas, int) + super().__init__(origin=None, + web3=web3, + abi=None, + address=address, + contract=None, + function_name=None, + parameters=None) + self.nonce = nonce + self.tx_hashes.append(latest_tx_hash) + self.current_gas = current_gas + self.gas_price_last = None + + def name(self): + return f"Recovered tx with nonce {self.nonce}" + + @_track_status + async def transact_async(self, **kwargs) -> Optional[Receipt]: + # TODO: Read transaction data from chain, create a new state machine to manage gas for the transaction. + raise NotImplementedError() + + def cancel(self, gas_strategy: GasStrategy): + return synchronize([self.cancel_async(gas_strategy)])[0] + + async def cancel_async(self, gas_strategy: GasStrategy): + assert isinstance(gas_strategy, GasStrategy) + initial_time = time.time() + self.gas_price_last = self.current_gas + self.tx_hashes.clear() + + if gas_strategy.get_gas_price(0) <= self.current_gas * 1.125: + self.logger.warning(f"Recovery gas price is less than current gas price {self.current_gas}; " + "cancellation will be deferred until the strategy produces an acceptable price.") + + while True: + seconds_elapsed = int(time.time() - initial_time) + gas_price_value = gas_strategy.get_gas_price(seconds_elapsed) + if gas_price_value > self.gas_price_last * 1.125: + self.gas_price_last = gas_price_value + # Transaction lock isn't needed here, as we are replacing an existing nonce + tx_hash = bytes_to_hexstring(self.web3.eth.sendTransaction({'from': self.address.address, + 'to': self.address.address, + 'gasPrice': gas_price_value, + 'nonce': self.nonce, + 'value': 0})) + self.tx_hashes.append(tx_hash) + self.logger.info(f"Attempting to cancel recovered tx with nonce={self.nonce}, " + f"gas_price={gas_price_value} (tx_hash={tx_hash})") + + for tx_hash in self.tx_hashes: + receipt = self._get_receipt(tx_hash) + if receipt: + self.logger.info(f"{self.name()} was cancelled (tx_hash={tx_hash})") + return + + await asyncio.sleep(0.75) + + +class TestApp: + def main(self): + print(f"Connected to {os.environ['ETH_RPC_URL']}") + pending_txes = get_pending_transactions(web3, our_address) + + if our_address: + pprint(list(map(lambda t: f"{t.name()} with gas {t.current_gas}", pending_txes))) + if len(pending_txes) > 0: + while len(pending_txes) > 0: + pending_txes[0].cancel(gas_strategy=increasing_gas) + # After the synchronous cancel, wait to see if subsequent transactions get mined + time.sleep(15) + pending_txes = get_pending_transactions(web3) + else: + logging.info(f"No pending transactions were found; submitting {stuck_txes_to_submit}") + for i in range(1, stuck_txes_to_submit+1): + self._run_future(eth_transfer(web3=web3, to=our_address, amount=Wad(0)).transact_async( + gas_strategy=too_low_gas)) + time.sleep(2) + + @staticmethod + def _run_future(future): + def worker(): + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + try: + asyncio.get_event_loop().run_until_complete(future) + finally: + loop.close() + + thread = threading.Thread(target=worker, daemon=True) + thread.start() + + +if __name__ == '__main__': + TestApp().main() diff --git a/tests/manual_test_tx_recovery.py b/tests/manual_test_tx_recovery.py deleted file mode 100644 index af00cda0..00000000 --- a/tests/manual_test_tx_recovery.py +++ /dev/null @@ -1,93 +0,0 @@ -# This file is part of Maker Keeper Framework. -# -# Copyright (C) 2020 EdNoepel -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . - -import asyncio -import logging -import os -import sys -import time -import threading -from pprint import pprint - -from pymaker import Address, get_pending_transactions, Wad, web3_via_http -from pymaker.deployment import DssDeployment -from pymaker.gas import FixedGasPrice, GeometricGasPrice -from pymaker.keys import register_keys - -logging.basicConfig(format='%(asctime)-15s %(levelname)-8s %(message)s', level=logging.DEBUG) -# reduce logspew -logging.getLogger('urllib3').setLevel(logging.INFO) -logging.getLogger("web3").setLevel(logging.INFO) -logging.getLogger("asyncio").setLevel(logging.INFO) -logging.getLogger("requests").setLevel(logging.INFO) - -web3 = web3_via_http(endpoint_uri=os.environ['ETH_RPC_URL']) -web3.eth.defaultAccount = sys.argv[1] # ex: 0x0000000000000000000000000000000aBcdef123 -register_keys(web3, [sys.argv[2]]) # ex: key_file=~keys/default-account.json,pass_file=~keys/default-account.pass -our_address = Address(web3.eth.defaultAccount) -weth = DssDeployment.from_node(web3).collaterals['ETH-A'].gem -stuck_txes_to_submit = int(sys.argv[3]) if len(sys.argv) > 3 else 1 - -GWEI = 1000000000 -too_low_gas = FixedGasPrice(gas_price=int(0.4 * GWEI), max_fee=None, tip=None) -increasing_gas = GeometricGasPrice(web3=web3, initial_price=1*GWEI, initial_tip=None, - every_secs=30, coefficient=1.5, max_price=200*GWEI) - - -class TestApp: - def main(self): - self.startup() - - pending_txes = get_pending_transactions(web3) - pprint(list(map(lambda t: f"{t.name()} with gas {t.current_gas}", pending_txes))) - - if len(pending_txes) > 0: - while len(pending_txes) > 0: - pending_txes[0].cancel(gas_strategy=increasing_gas) - # After the synchronous cancel, wait to see if subsequent transactions get mined - time.sleep(15) - pending_txes = get_pending_transactions(web3) - else: - logging.info(f"No pending transactions were found; submitting {stuck_txes_to_submit}") - for i in range(1, stuck_txes_to_submit+1): - self._run_future(weth.deposit(Wad(i)).transact_async(gas_strategy=too_low_gas)) - time.sleep(2) - - self.shutdown() - - def startup(self): - pass - - def shutdown(self): - pass - - @staticmethod - def _run_future(future): - def worker(): - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - try: - asyncio.get_event_loop().run_until_complete(future) - finally: - loop.close() - - thread = threading.Thread(target=worker, daemon=True) - thread.start() - - -if __name__ == '__main__': - TestApp().main() diff --git a/tests/test_general2.py b/tests/test_general2.py index 3aea9edd..f2cdf3ee 100644 --- a/tests/test_general2.py +++ b/tests/test_general2.py @@ -20,8 +20,7 @@ from mock import MagicMock from web3 import Web3, HTTPProvider -from pymaker import Address, Calldata, eth_transfer, get_pending_transactions, \ - Receipt, RecoveredTransact, TransactStatus +from pymaker import Address, Calldata, eth_transfer, Receipt, TransactStatus from pymaker.gas import FixedGasPrice from pymaker.numeric import Wad from pymaker.proxy import DSProxy, DSProxyCache @@ -314,36 +313,3 @@ def test_gas_to_replace_calculation(self, mocker): assert not dummy_tx._gas_exceeds_replacement_threshold(type2_prev_gas_params, type2_curr_gas_params) type2_curr_gas_params = {'maxFeePerGas': 130000000000, 'maxPriorityFeePerGas': 1265625000} assert dummy_tx._gas_exceeds_replacement_threshold(type2_prev_gas_params, type2_curr_gas_params) - -class TestTransactRecover: - def setup_method(self): - self.web3 = Web3(HTTPProvider("http://localhost:8555")) - self.web3.eth.defaultAccount = self.web3.eth.accounts[0] - self.token = DSToken.deploy(self.web3, 'ABC') - assert self.token.mint(Wad(100)).transact() - - def test_nothing_pending(self): - # given no pending transactions created by prior tests - - # then - assert get_pending_transactions(self.web3) == [] - - @pytest.mark.skip("Ganache and Parity testchains don't seem to simulate pending transactions in the mempool") - @pytest.mark.asyncio - async def test_recover_pending_tx(self, other_address): - # given - low_gas = FixedGasPrice(1) - await self.token.transfer(other_address, Wad(5)).transact_async(gas_price=low_gas) - await asyncio.sleep(0.5) - - # when - pending = get_pending_transactions(self.web3) - - # and - assert len(pending) == 1 - recovered: RecoveredTransact = pending[0] - high_gas = FixedGasPrice(int(1 * FixedGasPrice.GWEI)) - recovered.cancel(high_gas) - - # then - assert get_pending_transactions(self.web3) == [] From a36f1313d49e64eb58b43b7444baac41e437c88b Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Thu, 19 Aug 2021 17:27:58 -0400 Subject: [PATCH 10/16] improvements to mempool test --- pymaker/__init__.py | 2 +- tests/manual_test_mempool.py | 129 ++++++++++++++++------------------- 2 files changed, 58 insertions(+), 73 deletions(-) diff --git a/pymaker/__init__.py b/pymaker/__init__.py index f41308da..dd9a9da7 100644 --- a/pymaker/__init__.py +++ b/pymaker/__init__.py @@ -516,7 +516,7 @@ def _gas_fees(self, seconds_elapsed: int, gas_strategy: GasStrategy) -> dict: return params def _gas_exceeds_replacement_threshold(self, prev_gas_params: dict, curr_gas_params: dict): - # TODO: Can a type 0 TX be replaced with a type 2 TX? Vice-versa? + # NOTE: Experimentally (on OpenEthereum), I discovered a type 0 TX cannot be replaced with a type 2 TX. # Determine if a type 0 transaction would be replaced if 'gasPrice' in prev_gas_params and 'gasPrice' in curr_gas_params: diff --git a/tests/manual_test_mempool.py b/tests/manual_test_mempool.py index 9fef255a..11898d33 100644 --- a/tests/manual_test_mempool.py +++ b/tests/manual_test_mempool.py @@ -17,6 +17,7 @@ import asyncio import logging +import math import os import sys import time @@ -27,7 +28,6 @@ from pymaker import _get_endpoint_behavior, _track_status, Address, eth_transfer, NonceCalculation, Receipt, \ Transact, Wad, web3_via_http -from pymaker.deployment import DssDeployment from pymaker.gas import FixedGasPrice, GasStrategy, GeometricGasPrice from pymaker.keys import register_keys from pymaker.util import synchronize, bytes_to_hexstring @@ -45,15 +45,14 @@ web3.eth.defaultAccount = sys.argv[1] # ex: 0x0000000000000000000000000000000aBcdef123 register_keys(web3, [sys.argv[2]]) # ex: key_file=~keys/default-account.json,pass_file=~keys/default-account.pass our_address = Address(web3.eth.defaultAccount) - stuck_txes_to_submit = int(sys.argv[3]) if len(sys.argv) > 3 else 1 + stuck_txes_to_submit = int(sys.argv[3]) if len(sys.argv) > 3 else 0 else: our_address = None stuck_txes_to_submit = 0 GWEI = 1000000000 -too_low_gas = FixedGasPrice(gas_price=int(0.4 * GWEI), max_fee=None, tip=None) -increasing_gas = GeometricGasPrice(web3=web3, initial_price=1*GWEI, initial_tip=None, - every_secs=30, coefficient=1.5, max_price=200*GWEI) +# TODO: Dynamically choose prices based upon current block's base fee +too_low_gas = FixedGasPrice(gas_price=int(24 * GWEI), max_fee=None, tip=None) def get_pending_transactions(web3: Web3, address: Address = None) -> list: @@ -67,11 +66,10 @@ def get_pending_transactions(web3: Web3, address: Address = None) -> list: items = web3.manager.request_blocking("parity_pendingTransactions", []) if address: items = filter(lambda item: item['from'].lower() == address.address.lower(), items) - return list(map(lambda item: RecoveredTransact(web3=web3, - address=Address(item['from']), - nonce=int(item['nonce'], 16), - latest_tx_hash=item['hash'], - current_gas=int(item['gasPrice'], 16)), items)) + return list(map(lambda item: PendingTransact(web3=web3, + address=Address(item['from']), + nonce=int(item['nonce'], 16), + current_gas=int(item['gasPrice'], 16)), items)) else: summarize_transactions(items) else: @@ -79,11 +77,10 @@ def get_pending_transactions(web3: Web3, address: Address = None) -> list: summarize_transactions(items) if address: items = filter(lambda item: item['from'].lower() == address.address.lower(), items) - return list(map(lambda item: RecoveredTransact(web3=web3, - address=Address(item['from']), - nonce=item['nonce'], - latest_tx_hash=item['hash'], - current_gas=item['gasPrice']), items)) + return list(map(lambda item: PendingTransact(web3=web3, + address=Address(item['from']), + nonce=item['nonce'], + current_gas=item['gasPrice']), items)) else: summarize_transactions(items) return [] @@ -109,90 +106,78 @@ def summarize_transactions(txes): f"with gas from {lowest_gas} to {highest_gas} gwei") -class RecoveredTransact(Transact): +class PendingTransact(Transact): """ Models a pending transaction retrieved from the mempool. These can be created by a call to `get_pending_transactions`, enabling the consumer to implement logic which cancels pending transactions upon keeper/bot startup. """ - def __init__(self, web3: Web3, - address: Address, - nonce: int, - latest_tx_hash: str, - current_gas: int): + def __init__(self, web3: Web3, address: Address, nonce: int, current_gas: int): assert isinstance(current_gas, int) - super().__init__(origin=None, - web3=web3, - abi=None, - address=address, - contract=None, - function_name=None, - parameters=None) + super().__init__(origin=None, web3=web3, abi=None, address=address, contract=None, + function_name=None, parameters=None) self.nonce = nonce - self.tx_hashes.append(latest_tx_hash) self.current_gas = current_gas - self.gas_price_last = None def name(self): - return f"Recovered tx with nonce {self.nonce}" + return f"Pending TX with nonce {self.nonce} and gas at {self.current_gas/GWEI} gwei" @_track_status async def transact_async(self, **kwargs) -> Optional[Receipt]: # TODO: Read transaction data from chain, create a new state machine to manage gas for the transaction. raise NotImplementedError() - def cancel(self, gas_strategy: GasStrategy): - return synchronize([self.cancel_async(gas_strategy)])[0] + def cancel(self): + return synchronize([self.cancel_async()])[0] - async def cancel_async(self, gas_strategy: GasStrategy): - assert isinstance(gas_strategy, GasStrategy) + async def cancel_async(self): initial_time = time.time() - self.gas_price_last = self.current_gas - self.tx_hashes.clear() - - if gas_strategy.get_gas_price(0) <= self.current_gas * 1.125: - self.logger.warning(f"Recovery gas price is less than current gas price {self.current_gas}; " - "cancellation will be deferred until the strategy produces an acceptable price.") - - while True: - seconds_elapsed = int(time.time() - initial_time) - gas_price_value = gas_strategy.get_gas_price(seconds_elapsed) - if gas_price_value > self.gas_price_last * 1.125: - self.gas_price_last = gas_price_value - # Transaction lock isn't needed here, as we are replacing an existing nonce - tx_hash = bytes_to_hexstring(self.web3.eth.sendTransaction({'from': self.address.address, - 'to': self.address.address, - 'gasPrice': gas_price_value, - 'nonce': self.nonce, - 'value': 0})) - self.tx_hashes.append(tx_hash) - self.logger.info(f"Attempting to cancel recovered tx with nonce={self.nonce}, " - f"gas_price={gas_price_value} (tx_hash={tx_hash})") - - for tx_hash in self.tx_hashes: - receipt = self._get_receipt(tx_hash) - if receipt: - self.logger.info(f"{self.name()} was cancelled (tx_hash={tx_hash})") - return - - await asyncio.sleep(0.75) + + supports_eip1559 = _get_endpoint_behavior(web3).supports_eip1559 + tx_type = 0 # TODO: Pass gas details into ctor so we know the TX type. + + # Transaction lock isn't needed here, as we are replacing an existing nonce + if supports_eip1559 and tx_type == 2: + # TODO: Consider multiplying base_fee by 1.2 here to mitigate potential increase in subsequent blocks. + base_fee = int(self.web3.eth.get_block('pending')['baseFeePerGas']) + bumped_tip = math.ceil(min(1*GWEI, self.current_gas-base_fee) * 1.125) + gas_fees = {'maxFeePerGas': base_fee + bumped_tip, 'maxPriorityFeePerGas': bumped_tip} + tx_hash = bytes_to_hexstring(self.web3.eth.sendTransaction({'from': self.address.address, + 'to': self.address.address, + **gas_fees, + 'nonce': self.nonce, + 'value': 0})) + else: + bumped_gas = math.ceil(self.current_gas * 1.125) + gas_fees = {'gasPrice': bumped_gas} + tx_hash = bytes_to_hexstring(self.web3.eth.sendTransaction({'from': self.address.address, + 'to': self.address.address, + **gas_fees, + 'nonce': self.nonce, + 'value': 0})) + self.logger.info(f"Cancelling tx with nonce={self.nonce}, gas_fees={gas_fees} (tx_hash={tx_hash})") class TestApp: def main(self): - print(f"Connected to {os.environ['ETH_RPC_URL']}") + print(f"Connected to {os.environ['ETH_RPC_URL']} at block {web3.eth.get_block('latest').number}") pending_txes = get_pending_transactions(web3, our_address) if our_address: + print(f"{our_address} TX count is {web3.eth.getTransactionCount(our_address.address, block_identifier='pending')}") pprint(list(map(lambda t: f"{t.name()} with gas {t.current_gas}", pending_txes))) if len(pending_txes) > 0: - while len(pending_txes) > 0: - pending_txes[0].cancel(gas_strategy=increasing_gas) - # After the synchronous cancel, wait to see if subsequent transactions get mined - time.sleep(15) - pending_txes = get_pending_transactions(web3) - else: - logging.info(f"No pending transactions were found; submitting {stuck_txes_to_submit}") + # User would implement their own cancellation logic here, which could involve waiting before + # submitting subsequent cancels. + for tx in pending_txes: + if tx.current_gas < 20 * GWEI: + print(f"Attempting to cancel TX with nonce={tx.nonce}") + tx.cancel() + else: + print(f"Gas for TX with nonce={tx.nonce} is too high; leaving alone") + + if stuck_txes_to_submit: + logging.info(f"Submitting {stuck_txes_to_submit} transactions with low gas") for i in range(1, stuck_txes_to_submit+1): self._run_future(eth_transfer(web3=web3, to=our_address, amount=Wad(0)).transact_async( gas_strategy=too_low_gas)) From 65fe5b7ac777a282befd116fb6a206df664705c6 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Sun, 22 Aug 2021 15:28:29 -0400 Subject: [PATCH 11/16] fixed nonce synchronization bug --- pymaker/__init__.py | 68 +++++++++++-------- pymaker/gas.py | 28 ++++++-- tests/manual_test_mempool.py | 127 +++++++++++++++++++++-------------- tests/test_gas.py | 6 +- 4 files changed, 142 insertions(+), 87 deletions(-) diff --git a/pymaker/__init__.py b/pymaker/__init__.py index dd9a9da7..e6471889 100644 --- a/pymaker/__init__.py +++ b/pymaker/__init__.py @@ -569,35 +569,45 @@ def _contract_function(self): return function_factory(*self.parameters) def _interlocked_choose_nonce_and_send(self, from_account: str, gas: int, gas_fees: dict): + global next_nonce assert isinstance(from_account, str) # address of the sender assert isinstance(gas, int) # gas amount assert isinstance(gas_fees, dict) # gas fee parameters + + # We need the lock in order to not try to send two transactions with the same nonce. + transaction_lock.acquire() + # self.logger.debug(f"lock {id(transaction_lock)} acquired") + + if from_account not in next_nonce: + # logging.debug(f"Initializing nonce for {from_account}") + next_nonce[from_account] = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') + try: - # We need the lock in order to not try to send two transactions with the same nonce. - with transaction_lock: - if self.nonce is None: - nonce_calc = _get_endpoint_behavior(self.web3).nonce_calc - if nonce_calc == NonceCalculation.PARITY_NEXTNONCE: - self.nonce = int(self.web3.manager.request_blocking("parity_nextNonce", [from_account]), 16) - elif nonce_calc == NonceCalculation.TX_COUNT: - self.nonce = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') - elif nonce_calc == NonceCalculation.SERIAL: - tx_count = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') - next_serial = next_nonce[from_account] - self.nonce = max(tx_count, next_serial) - elif nonce_calc == NonceCalculation.PARITY_SERIAL: - tx_count = int(self.web3.manager.request_blocking("parity_nextNonce", [from_account]), 16) - next_serial = next_nonce[from_account] - self.nonce = max(tx_count, next_serial) - next_nonce[from_account] = self.nonce + 1 - - # Trap replacement while original is holding the lock awaiting nonce assignment - if self.replaced: - self.logger.info(f"Transaction {self.name()} with nonce={self.nonce} was replaced") - return None - - tx_hash = self._func(from_account, gas, gas_fees, self.nonce) - self.tx_hashes.append(tx_hash) + if self.nonce is None: + nonce_calc = _get_endpoint_behavior(self.web3).nonce_calc + if nonce_calc == NonceCalculation.PARITY_NEXTNONCE: + self.nonce = int(self.web3.manager.request_blocking("parity_nextNonce", [from_account]), 16) + elif nonce_calc == NonceCalculation.TX_COUNT: + self.nonce = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') + elif nonce_calc == NonceCalculation.SERIAL: + tx_count = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') + next_serial = next_nonce[from_account] + self.nonce = max(tx_count, next_serial) + elif nonce_calc == NonceCalculation.PARITY_SERIAL: + tx_count = int(self.web3.manager.request_blocking("parity_nextNonce", [from_account]), 16) + next_serial = next_nonce[from_account] + self.nonce = max(tx_count, next_serial) + next_nonce[from_account] = self.nonce + 1 + # self.logger.debug(f"Chose nonce {self.nonce} with tx_count={tx_count} and " + # f"next_serial={next_serial}; next is {next_nonce[from_account]}") + + # Trap replacement while original is holding the lock awaiting nonce assignment + if self.replaced: + self.logger.info(f"Transaction {self.name()} with nonce={self.nonce} was replaced") + return None + + tx_hash = self._func(from_account, gas, gas_fees, self.nonce) + self.tx_hashes.append(tx_hash) self.logger.info(f"Sent transaction {self.name()} with nonce={self.nonce}, gas={gas}," f" gas_fees={gas_fees if gas_fees else 'default'}" @@ -608,6 +618,9 @@ def _interlocked_choose_nonce_and_send(self, from_account: str, gas: int, gas_fe if len(self.tx_hashes) == 0: raise + finally: + transaction_lock.release() + # self.logger.debug(f"lock {id(transaction_lock)} released with next_nonce={next_nonce[from_account]}") def name(self) -> str: """Returns the nicely formatted name of this pending Ethereum transaction. @@ -703,16 +716,13 @@ async def transact_async(self, **kwargs) -> Optional[Receipt]: invocation was successful, or `None` if it failed. """ - global next_nonce self.initial_time = time.time() unknown_kwargs = set(kwargs.keys()) - {'from_address', 'replace', 'gas', 'gas_buffer', 'gas_strategy'} if len(unknown_kwargs) > 0: raise ValueError(f"Unknown kwargs: {unknown_kwargs}") - # Get the from account; initialize the first nonce for the account. + # Get the account from which the transaction will be submitted from_account = kwargs['from_address'].address if ('from_address' in kwargs) else self.web3.eth.defaultAccount - if not next_nonce or from_account not in next_nonce: - next_nonce[from_account] = self.web3.eth.getTransactionCount(from_account, block_identifier='pending') # First we try to estimate the gas usage of the transaction. If gas estimation fails # it means there is no point in sending the transaction, thus we fail instantly and diff --git a/pymaker/gas.py b/pymaker/gas.py index afb811ad..ecdbd161 100644 --- a/pymaker/gas.py +++ b/pymaker/gas.py @@ -16,6 +16,7 @@ # along with this program. If not, see . import math +from pprint import pformat from typing import Optional, Tuple from web3 import Web3 @@ -37,13 +38,13 @@ class GasStrategy(object): """ def get_gas_price(self, time_elapsed: int) -> Optional[int]: - """Return gas price applicable for a given point in time. + """Return gas price applicable for type 0 transactions. - Bear in mind that Parity (don't know about other Ethereum nodes) requires the gas - price for overwritten transactions to go up by at least 10%. Also, you may return + Bear in mind that Geth requires the gas price for overwritten transactions to increase by at + least 10%, while OpenEthereum requires a gas price bump of 12.5%. Also, you may return `None` which will make the node use the default gas price, but once you returned a numeric value (gas price in Wei), you shouldn't switch back to `None` as such - transaction also may not get properly overwritten. + transaction will likely not get overwritten. Args: time_elapsed: Number of seconds since this specific Ethereum transaction @@ -56,9 +57,24 @@ def get_gas_price(self, time_elapsed: int) -> Optional[int]: raise NotImplementedError("Please implement this method") def get_gas_fees(self, time_elapsed: int) -> Tuple[int, int]: - """Return max fee (fee cap) and priority fee (tip) for type 2 (EIP-1559) transactions""" + """Return max fee (fee cap) and priority fee (tip) for type 2 (EIP-1559) transactions. + + Note that Web3 currently requires specifying both `maxFeePerGas` and `maxPriorityFeePerGas` on a type 2 + transaction. This is inconsistent with the EIP-1559 spec. + + Args: + time_elapsed: Number of seconds since this specific Ethereum transaction + has been originally sent for the first time. + + Returns: + Gas price in Wei, or `None` if default gas price should be used. Default gas price + means it's the Ethereum node the keeper is connected to will decide on the gas price. + """ raise NotImplementedError("Please implement this method") + def __repr__(self): + return f"{__name__}({pformat(vars(self))})" + class DefaultGasPrice(GasStrategy): """Default gas price. @@ -71,7 +87,7 @@ def get_gas_price(self, time_elapsed: int) -> Optional[int]: return None def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: - return None, None + return None class NodeAwareGasStrategy(GasStrategy): diff --git a/tests/manual_test_mempool.py b/tests/manual_test_mempool.py index 11898d33..efc59860 100644 --- a/tests/manual_test_mempool.py +++ b/tests/manual_test_mempool.py @@ -33,17 +33,20 @@ from pymaker.util import synchronize, bytes_to_hexstring -logging.basicConfig(format='%(asctime)-15s %(levelname)-8s %(message)s', level=logging.DEBUG) +logging.basicConfig(format='%(asctime)-15s [%(thread)d] %(levelname)-8s %(message)s', level=logging.DEBUG) # reduce logspew logging.getLogger('urllib3').setLevel(logging.INFO) logging.getLogger("web3").setLevel(logging.INFO) logging.getLogger("asyncio").setLevel(logging.INFO) logging.getLogger("requests").setLevel(logging.INFO) +transact = False web3 = web3_via_http(endpoint_uri=os.environ['ETH_RPC_URL']) -if len(sys.argv) > 2: +if len(sys.argv) > 1: web3.eth.defaultAccount = sys.argv[1] # ex: 0x0000000000000000000000000000000aBcdef123 - register_keys(web3, [sys.argv[2]]) # ex: key_file=~keys/default-account.json,pass_file=~keys/default-account.pass + if len(sys.argv) > 2: + register_keys(web3, [sys.argv[2]]) # ex: key_file=~keys/default-account.json,pass_file=~keys/default-account.pass + transact = True our_address = Address(web3.eth.defaultAccount) stuck_txes_to_submit = int(sys.argv[3]) if len(sys.argv) > 3 else 0 else: @@ -51,12 +54,25 @@ stuck_txes_to_submit = 0 GWEI = 1000000000 -# TODO: Dynamically choose prices based upon current block's base fee -too_low_gas = FixedGasPrice(gas_price=int(24 * GWEI), max_fee=None, tip=None) +base_fee = int(web3.eth.get_block('pending')['baseFeePerGas']) +# Uses a type 0 TX +low_gas_type0 = FixedGasPrice(gas_price=base_fee, max_fee=None, tip=None) +# Forces a type 2 TX (erroring out if not supported by node) +tip = 1*GWEI +low_gas_type2 = FixedGasPrice(gas_price=None, max_fee=int(base_fee * 0.9) + tip, tip=tip) +# Favors a type 2 TX if the node supports it, otherwise falls back to a type 0 TX +low_gas_nodechoice = FixedGasPrice(low_gas_type0.gas_price, low_gas_type2.max_fee, low_gas_type2.tip) +low_gas = low_gas_nodechoice +print(f"Base fee is {base_fee/GWEI}; using {low_gas} for low gas") def get_pending_transactions(web3: Web3, address: Address = None) -> list: - """Retrieves a list of pending transactions from the mempool.""" + """Retrieves a list of pending transactions from the mempool. + + Default OpenEthereum configurations gossip and then drop transactions which do not exceed the base fee. + Third-party node providers (such as Infura) assign endpoints round-robin, such that the mempool on the node you've + connected to has no relationship to the node where your TX was submitted. + """ assert isinstance(web3, Web3) assert isinstance(address, Address) or address is None @@ -66,10 +82,13 @@ def get_pending_transactions(web3: Web3, address: Address = None) -> list: items = web3.manager.request_blocking("parity_pendingTransactions", []) if address: items = filter(lambda item: item['from'].lower() == address.address.lower(), items) - return list(map(lambda item: PendingTransact(web3=web3, - address=Address(item['from']), - nonce=int(item['nonce'], 16), - current_gas=int(item['gasPrice'], 16)), items)) + return list(map(lambda item: + PendingTransact(web3=web3, + address=Address(item['from']), + nonce=int(item['nonce'], 16), + gas_price=int(item['gasPrice'], 16), + gas_feecap=int(item['maxFeePerGas'], 16) if 'maxFeePerGas' in item else None, + gas_tip=int(item['maxPriorityFeePerGas'], 16) if 'maxPriorityFeePerGas' in item else None), items)) else: summarize_transactions(items) else: @@ -77,10 +96,14 @@ def get_pending_transactions(web3: Web3, address: Address = None) -> list: summarize_transactions(items) if address: items = filter(lambda item: item['from'].lower() == address.address.lower(), items) - return list(map(lambda item: PendingTransact(web3=web3, - address=Address(item['from']), - nonce=item['nonce'], - current_gas=item['gasPrice']), items)) + + return list(map(lambda item: + PendingTransact(web3=web3, + address=Address(item['from']), + nonce=item['nonce'], + gas_price=item['gasPrice'], + gas_feecap=item['maxFeePerGas'] if 'maxFeePerGas' in item else None, + gas_tip=item['maxPriorityFeePerGas'] if 'maxPriorityFeePerGas' in item else None), items)) else: summarize_transactions(items) return [] @@ -102,7 +125,7 @@ def summarize_transactions(txes): highest_gas = max(highest_gas, gas_price) if highest_gas else gas_price addresses.add(tx['from']) # pprint(tx) - print(f"Found {len(txes)} TXes from {len(addresses)} unique addresses " + print(f"This node's mempool contains {len(txes)} TXes from {len(addresses)} unique addresses " f"with gas from {lowest_gas} to {highest_gas} gwei") @@ -112,15 +135,23 @@ class PendingTransact(Transact): These can be created by a call to `get_pending_transactions`, enabling the consumer to implement logic which cancels pending transactions upon keeper/bot startup. """ - def __init__(self, web3: Web3, address: Address, nonce: int, current_gas: int): - assert isinstance(current_gas, int) + def __init__(self, web3: Web3, address: Address, nonce: int, gas_price: int, gas_feecap: int = None, gas_tip: int = None): + assert isinstance(web3, Web3) + assert isinstance(address, Address) + assert isinstance(nonce, int) + assert isinstance(gas_price, int) + assert isinstance(gas_feecap, int) or gas_feecap is None + assert isinstance(gas_tip, int) or gas_tip is None + super().__init__(origin=None, web3=web3, abi=None, address=address, contract=None, function_name=None, parameters=None) self.nonce = nonce - self.current_gas = current_gas + self.gas_price = gas_price + self.gas_feecap = gas_feecap + self.gas_tip = gas_tip def name(self): - return f"Pending TX with nonce {self.nonce} and gas at {self.current_gas/GWEI} gwei" + return f"Pending TX with nonce {self.nonce} and gas_price={self.gas_price} gas_feecap={self.gas_feecap} gas_tip={self.gas_tip}" @_track_status async def transact_async(self, **kwargs) -> Optional[Receipt]: @@ -131,32 +162,31 @@ def cancel(self): return synchronize([self.cancel_async()])[0] async def cancel_async(self): - initial_time = time.time() - supports_eip1559 = _get_endpoint_behavior(web3).supports_eip1559 - tx_type = 0 # TODO: Pass gas details into ctor so we know the TX type. - # Transaction lock isn't needed here, as we are replacing an existing nonce - if supports_eip1559 and tx_type == 2: - # TODO: Consider multiplying base_fee by 1.2 here to mitigate potential increase in subsequent blocks. + if self.gas_feecap and self.gas_tip: + assert supports_eip1559 base_fee = int(self.web3.eth.get_block('pending')['baseFeePerGas']) - bumped_tip = math.ceil(min(1*GWEI, self.current_gas-base_fee) * 1.125) - gas_fees = {'maxFeePerGas': base_fee + bumped_tip, 'maxPriorityFeePerGas': bumped_tip} - tx_hash = bytes_to_hexstring(self.web3.eth.sendTransaction({'from': self.address.address, - 'to': self.address.address, - **gas_fees, - 'nonce': self.nonce, - 'value': 0})) + bumped_tip = math.ceil(min(1 * GWEI, self.gas_tip) * 1.125) + bumped_feecap = max(base_fee + bumped_tip, math.ceil((self.gas_feecap + bumped_tip) * 1.125)) + gas_fees = {'maxFeePerGas': bumped_feecap, 'maxPriorityFeePerGas': bumped_tip} + # CAUTION: On OpenEthereum//v3.3.0-rc.4, this produces an underpriced gas error; even when multiplying by 2 else: - bumped_gas = math.ceil(self.current_gas * 1.125) - gas_fees = {'gasPrice': bumped_gas} - tx_hash = bytes_to_hexstring(self.web3.eth.sendTransaction({'from': self.address.address, - 'to': self.address.address, - **gas_fees, - 'nonce': self.nonce, - 'value': 0})) - self.logger.info(f"Cancelling tx with nonce={self.nonce}, gas_fees={gas_fees} (tx_hash={tx_hash})") - + assert False + if supports_eip1559: + base_fee = math.ceil(self.web3.eth.get_block('pending')['baseFeePerGas']) + bumped_tip = math.ceil(min(1 * GWEI, self.gas_price - base_fee) * 1.125) + gas_fees = {'maxFeePerGas': math.ceil((self.gas_price + bumped_tip) * 1.25), 'maxPriorityFeePerGas': bumped_tip} + else: + bumped_gas = math.ceil(self.gas_price * 1.125) + gas_fees = {'gasPrice': bumped_gas} + self.logger.info(f"Attempting to cancel TX with nonce={self.nonce} using gas_fees={gas_fees}") + tx_hash = bytes_to_hexstring(self.web3.eth.sendTransaction({'from': self.address.address, + 'to': self.address.address, + **gas_fees, + 'nonce': self.nonce, + 'value': 0})) + self.logger.info(f"Cancelled TX with nonce={self.nonce}; TX hash: {tx_hash}") class TestApp: def main(self): @@ -165,23 +195,22 @@ def main(self): if our_address: print(f"{our_address} TX count is {web3.eth.getTransactionCount(our_address.address, block_identifier='pending')}") - pprint(list(map(lambda t: f"{t.name()} with gas {t.current_gas}", pending_txes))) - if len(pending_txes) > 0: + pprint(list(map(lambda t: f"{t.name()}", pending_txes))) + if transact and len(pending_txes) > 0: # User would implement their own cancellation logic here, which could involve waiting before # submitting subsequent cancels. for tx in pending_txes: - if tx.current_gas < 20 * GWEI: - print(f"Attempting to cancel TX with nonce={tx.nonce}") + if tx.gas_price < 100 * GWEI: tx.cancel() else: print(f"Gas for TX with nonce={tx.nonce} is too high; leaving alone") - if stuck_txes_to_submit: + if transact and stuck_txes_to_submit: logging.info(f"Submitting {stuck_txes_to_submit} transactions with low gas") for i in range(1, stuck_txes_to_submit+1): - self._run_future(eth_transfer(web3=web3, to=our_address, amount=Wad(0)).transact_async( - gas_strategy=too_low_gas)) - time.sleep(2) + self._run_future(eth_transfer(web3=web3, to=our_address, amount=Wad(i*10)).transact_async( + gas_strategy=low_gas)) + time.sleep(2) # Give event loop a chance to send the transactions @staticmethod def _run_future(future): diff --git a/tests/test_gas.py b/tests/test_gas.py index a67c0584..6e0171a2 100644 --- a/tests/test_gas.py +++ b/tests/test_gas.py @@ -46,9 +46,9 @@ def test_should_always_be_default(self): assert default_gas_price.get_gas_price(1000000) is None # expect - assert default_gas_price.get_gas_fees(0) == (None, None) - assert default_gas_price.get_gas_fees(1) == (None, None) - assert default_gas_price.get_gas_fees(1000000) == (None, None) + assert default_gas_price.get_gas_fees(0) == None + assert default_gas_price.get_gas_fees(1) == None + assert default_gas_price.get_gas_fees(1000000) == None class TestNodeAwareGasPrice: From 5da3ed800c8fdb96f2bff5c6ec2ab786fec7c67f Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Tue, 24 Aug 2021 09:35:09 -0400 Subject: [PATCH 12/16] made pot.rho consistent with jug.rho --- pymaker/dss.py | 5 ++--- pymaker/gas.py | 1 - tests/test_dss.py | 10 ++++++++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pymaker/dss.py b/pymaker/dss.py index b3f6b9f2..ce140c71 100644 --- a/pymaker/dss.py +++ b/pymaker/dss.py @@ -938,9 +938,8 @@ def chi(self) -> Ray: chi = self._contract.functions.chi().call() return Ray(chi) - def rho(self) -> datetime: - rho = self._contract.functions.rho().call() - return datetime.fromtimestamp(rho) + def rho(self) -> int: + return Web3.toInt(self._contract.functions.rho().call()) def drip(self) -> Transact: return Transact(self, self.web3, self.abi, self.address, self._contract, 'drip', []) diff --git a/pymaker/gas.py b/pymaker/gas.py index ecdbd161..7b7436ea 100644 --- a/pymaker/gas.py +++ b/pymaker/gas.py @@ -222,7 +222,6 @@ def scale_by_time(self, value: int, time_elapsed: int) -> int: result = value if time_elapsed >= self.every_secs: for second in range(math.floor(time_elapsed / self.every_secs)): - # print(f"result={result} coeff={self.coefficient}") result *= self.coefficient return math.ceil(result) diff --git a/tests/test_dss.py b/tests/test_dss.py index 111ed647..77fdc132 100644 --- a/tests/test_dss.py +++ b/tests/test_dss.py @@ -637,24 +637,30 @@ def test_drip(self, mcd): class TestPot: + def setup_class(self): + self.test_started = int(time.time()) + def test_getters(self, mcd): assert isinstance(mcd.pot.pie(), Wad) assert isinstance(mcd.pot.dsr(), Ray) - assert isinstance(mcd.pot.rho(), datetime) + assert isinstance(mcd.pot.rho(), int) assert mcd.pot.pie() >= Wad(0) assert mcd.pot.dsr() > Ray.from_number(1) - assert datetime.fromtimestamp(0) < mcd.pot.rho() < datetime.utcnow() + assert 0 < mcd.pot.rho() < self.test_started def test_drip(self, mcd): chi_before = mcd.pot.chi() assert isinstance(chi_before, Ray) + time.sleep(1) assert mcd.pot.drip().transact() + time.sleep(1) chi_after = mcd.pot.chi() if mcd.pot.dsr() == Ray.from_number(1): assert chi_before == chi_after else: assert chi_before < chi_after + assert self.test_started < mcd.pot.rho() < int(time.time()) class TestOsm: From 9905aff0a30b89a44937c0832467ce4b807f779a Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Fri, 27 Aug 2021 12:02:38 -0400 Subject: [PATCH 13/16] placate certain versions of pip --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index f2136e62..f56be573 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,5 @@ pytz == 2017.3 web3 == 5.23.0 requests == 2.22.0 eth-account >= 0.5.5 -eth-keys <0.3.0, >=0.2.1 +eth-keys >=0.2.1,<0.3.0 jsonnet == 0.9.5 From df78512448833e189487832a4f6f55db64841d41 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Fri, 27 Aug 2021 19:23:55 -0400 Subject: [PATCH 14/16] Transact expects a tuple --- pymaker/gas.py | 4 ++-- tests/test_gas.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pymaker/gas.py b/pymaker/gas.py index 7b7436ea..c11024d5 100644 --- a/pymaker/gas.py +++ b/pymaker/gas.py @@ -87,7 +87,7 @@ def get_gas_price(self, time_elapsed: int) -> Optional[int]: return None def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: - return None + return None, None class NodeAwareGasStrategy(GasStrategy): @@ -239,7 +239,7 @@ def get_gas_price(self, time_elapsed: int) -> Optional[int]: def get_gas_fees(self, time_elapsed: int) -> Optional[Tuple[int, int]]: assert isinstance(time_elapsed, int) if not self.initial_tip: - return None + return None, None base_fee = self.get_base_fee() if not base_fee: diff --git a/tests/test_gas.py b/tests/test_gas.py index 6e0171a2..a67c0584 100644 --- a/tests/test_gas.py +++ b/tests/test_gas.py @@ -46,9 +46,9 @@ def test_should_always_be_default(self): assert default_gas_price.get_gas_price(1000000) is None # expect - assert default_gas_price.get_gas_fees(0) == None - assert default_gas_price.get_gas_fees(1) == None - assert default_gas_price.get_gas_fees(1000000) == None + assert default_gas_price.get_gas_fees(0) == (None, None) + assert default_gas_price.get_gas_fees(1) == (None, None) + assert default_gas_price.get_gas_fees(1000000) == (None, None) class TestNodeAwareGasPrice: From f34210620d396bd07060b006952192a9db1d5dea Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Thu, 2 Sep 2021 13:21:24 -0400 Subject: [PATCH 15/16] try to make travis happy with the proxy unittest --- tests/test_proxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_proxy.py b/tests/test_proxy.py index 786151a0..799f0bdd 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -91,7 +91,7 @@ def test_past_build(self, proxy_factory: DSProxyFactory, our_address): assert proxy_factory.build().transact() # then - past_build = proxy_factory.past_build(1) + past_build = proxy_factory.past_build(10) assert past_build assert len(past_build) == past_build_count + 1 From 54073537fb3494dd183ed89940c194191e81322f Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Tue, 7 Sep 2021 12:23:24 -0400 Subject: [PATCH 16/16] NonceCalculation.PARITY_SERIAL no longer works for Infura; add Alchemy support --- pymaker/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pymaker/__init__.py b/pymaker/__init__.py index e6471889..f5e81528 100644 --- a/pymaker/__init__.py +++ b/pymaker/__init__.py @@ -97,13 +97,11 @@ def _get_endpoint_behavior(web3: Web3) -> EndpointBehavior: if web3 not in endpoint_behavior: # Determine nonce calculation - providers_without_nonce_calculation = ['infura', 'quiknode'] + providers_without_nonce_calculation = ['alchemy', 'infura', 'quiknode'] requires_serial_nonce = any(provider in web3.manager.provider.endpoint_uri for provider in providers_without_nonce_calculation) is_parity = "parity" in web3.clientVersion.lower() or "openethereum" in web3.clientVersion.lower() - if is_parity and requires_serial_nonce: - nonce_calc = NonceCalculation.PARITY_SERIAL - elif requires_serial_nonce: + if requires_serial_nonce: nonce_calc = NonceCalculation.SERIAL elif is_parity: nonce_calc = NonceCalculation.PARITY_NEXTNONCE