Skip to content

Commit

Permalink
Fix tests. Black.
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Sep 29, 2023
1 parent a500549 commit 971cf47
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 20 deletions.
4 changes: 0 additions & 4 deletions eth_defi/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,18 @@
#: See GoEthereum error codes https://github.com/ethereum/go-ethereum/blob/master/rpc/errors.go
#:
DEFAULT_RETRYABLE_RPC_ERROR_CODES = (

# The node provider has corrupted database or something GoEthereum
# cannot handle gracefully.
# ValueError: {'message': 'Internal JSON-RPC error.', 'code': -32603}
-32603,

# ValueError: {'code': -32000, 'message': 'nonce too low'}.
# Might happen when we are broadcasting multiple transactions through multiple RPC providers
# using eth_sendRawTransaction
# One provide has not yet seeing a transaction broadcast through the other provider.
-32000,

# ValueError: {'code': -32003, 'message': 'nonce too low'}.
# Anvil variant for nonce too low, same as above
-32003,

# Some error we are getting from LlamaNodes eth_getLogs RPC that we do not know what it is all about
# {'code': -32043, 'message': 'Requested data is not available'}
-32043,
Expand Down
13 changes: 2 additions & 11 deletions eth_defi/provider/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def __init__(
self.backoff = backoff
self.retries = retries


#: provider number -> API name -> call count mappings.
# This tracks completed API requests.
self.api_call_counts = defaultdict(Counter)
Expand Down Expand Up @@ -178,15 +177,7 @@ def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
new_provider_name = get_provider_name(self.get_active_provider())

if i < self.retries:
logger.log(self.switchover_noisiness, "Encountered JSON-RPC retryable error %s when calling method %s.\n" "Switching providers %s -> %s\n" "Retrying in %f seconds, retry #%d / %d",
e,
method,
old_provider_name,
new_provider_name,
current_sleep,
i,
self.retries
)
logger.log(self.switchover_noisiness, "Encountered JSON-RPC retryable error %s when calling method %s.\n" "Switching providers %s -> %s\n" "Retrying in %f seconds, retry #%d / %d", e, method, old_provider_name, new_provider_name, current_sleep, i, self.retries)
time.sleep(current_sleep)
current_sleep *= self.backoff
self.retry_count += 1
Expand All @@ -197,4 +188,4 @@ def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
logger.info("Will not retry on %s, method %s, as not a retryable exception %s: %s", old_provider_name, method, e.__class__, e)
raise # Not retryable exception

raise AssertionError("Should never be reached")
raise AssertionError("Should never be reached")
10 changes: 5 additions & 5 deletions tests/rpc/test_fallback_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_fallback_double_fault(fallback_provider: FallbackProvider, provider_1,
with pytest.raises(requests.exceptions.ConnectionError):
web3.eth.block_number

assert fallback_provider.retry_count == 5
assert fallback_provider.retry_count == 6


def test_fallback_double_fault_recovery(fallback_provider: FallbackProvider, provider_1, provider_2):
Expand Down Expand Up @@ -137,13 +137,13 @@ def test_fallback_nonce_too_low(web3, deployer: str):
user = Account.create()
hot_wallet = HotWallet(user)

tx1_hash = web3.eth.send_transaction({"from": deployer, "to": user.address, "value": 5*10**18})
tx1_hash = web3.eth.send_transaction({"from": deployer, "to": user.address, "value": 5 * 10**18})
assert_transaction_success_with_explanation(web3, tx1_hash)

hot_wallet.sync_nonce(web3)

# First send a transaction with a correct nonce
tx2 = {"chainId": web3.eth.chain_id, "from": user.address, "to": deployer, "value": 1*10**18, "gas": 30_000}
tx2 = {"chainId": web3.eth.chain_id, "from": user.address, "to": deployer, "value": 1 * 10**18, "gas": 30_000}
HotWallet.fill_in_gas_price(web3, tx2)
signed_tx2 = hot_wallet.sign_transaction_with_new_nonce(tx2)
assert signed_tx2.nonce == 0
Expand All @@ -157,7 +157,7 @@ def test_fallback_nonce_too_low(web3, deployer: str):
# Then send a transaction with too low nonce.
# We are not interested that the transaction goes thru, only
# that it is retried.
tx3 = {"chainId": web3.eth.chain_id, "from": user.address, "to": deployer, "value": 1*10**18, "gas": 30_000}
tx3 = {"chainId": web3.eth.chain_id, "from": user.address, "to": deployer, "value": 1 * 10**18, "gas": 30_000}
HotWallet.fill_in_gas_price(web3, tx3)
hot_wallet.current_nonce = 0 # Spoof nonce
signed_tx3 = hot_wallet.sign_transaction_with_new_nonce(tx3)
Expand All @@ -167,4 +167,4 @@ def test_fallback_nonce_too_low(web3, deployer: str):
# nonce too low happens during RPC call
tx3_hash = web3.eth.send_raw_transaction(signed_tx3.rawTransaction)

assert fallback_provider.api_retry_counts[0]["eth_sendRawTransaction"] == 3 # 5 attempts, 3 retries, the last retry does not count
assert fallback_provider.api_retry_counts[0]["eth_sendRawTransaction"] == 3 # 5 attempts, 3 retries, the last retry does not count

0 comments on commit 971cf47

Please sign in to comment.