From 258ebef8efdc72991113e11e07a3653be9ba0a39 Mon Sep 17 00:00:00 2001 From: derekpierre Date: Tue, 3 Sep 2024 17:16:56 -0400 Subject: [PATCH] Remove poa error redundancy middleware now that we understand the underlying cause of the problem. It was always a stop gap measure until we could determine the exact cause of the poa error; Now that we know the error it is no longer needed. --- nucypher/blockchain/eth/clients.py | 8 --- nucypher/blockchain/middleware/poa.py | 47 --------------- .../network/test_poa_redundancy_middleware.py | 58 ------------------- 3 files changed, 113 deletions(-) delete mode 100644 nucypher/blockchain/middleware/poa.py delete mode 100644 tests/integration/network/test_poa_redundancy_middleware.py diff --git a/nucypher/blockchain/eth/clients.py b/nucypher/blockchain/eth/clients.py index 076549515..a7a3e27e3 100644 --- a/nucypher/blockchain/eth/clients.py +++ b/nucypher/blockchain/eth/clients.py @@ -15,7 +15,6 @@ from nucypher.blockchain.eth.constants import ( AVERAGE_BLOCK_TIME_IN_SECONDS, PUBLIC_CHAINS, ) -from nucypher.blockchain.middleware.poa import create_poa_error_redundancy_middleware from nucypher.blockchain.middleware.retry import ( AlchemyRetryRequestMiddleware, InfuraRetryRequestMiddleware, @@ -101,13 +100,6 @@ class EthereumClient: layer=0, name=poa_middleware_name, ) - # POA error redundancy middleware, just in case - self.log.debug("Adding POA redundancy middleware") - self.add_middleware( - create_poa_error_redundancy_middleware( - existing_poa_middleware_name=poa_middleware_name - ) - ) # simple cache middleware self.log.debug("Adding simple_cache_middleware") diff --git a/nucypher/blockchain/middleware/poa.py b/nucypher/blockchain/middleware/poa.py deleted file mode 100644 index 4e3df6bba..000000000 --- a/nucypher/blockchain/middleware/poa.py +++ /dev/null @@ -1,47 +0,0 @@ -from typing import Any, Callable - -from web3 import Web3 -from web3.exceptions import ExtraDataLengthError -from web3.middleware import geth_poa_middleware -from web3.types import Middleware, RPCEndpoint, RPCResponse - -from nucypher.utilities.logging import Logger - - -def create_poa_error_redundancy_middleware( - existing_poa_middleware_name: str = "poa", -) -> Middleware: - """ - Redundant middleware for replacing already added named poa middleware if ExtraDataLengthError - still encountered. Extra layer of defense in case random POA error is observed. - """ - - def poa_error_redundancy_middleware( - make_request: Callable[[RPCEndpoint, Any], RPCResponse], _w3: "Web3" - ) -> Callable[[RPCEndpoint, Any], RPCResponse]: - logger = Logger("POAErrorRedundancyMiddleware") - - def middleware(method: RPCEndpoint, params: Any) -> RPCResponse: - try: - response = make_request(method, params) - except ExtraDataLengthError: - logger.warn( - "RPC request failed due to extraData error; re-injecting poa middleware and retrying" - ) - # add / replace existing poa middleware; replacement seems unlikely but just in case - if _w3.middleware_onion.get(existing_poa_middleware_name): - # we can't have > 1 geth_poa_middleware added (event with different names) so the - # removal-then-add is just for safety. - _w3.middleware_onion.remove(existing_poa_middleware_name) - _w3.middleware_onion.inject( - geth_poa_middleware, layer=0, name=existing_poa_middleware_name - ) - - # try again - response = make_request(method, params) - - return response - - return middleware - - return poa_error_redundancy_middleware diff --git a/tests/integration/network/test_poa_redundancy_middleware.py b/tests/integration/network/test_poa_redundancy_middleware.py deleted file mode 100644 index fd590ed77..000000000 --- a/tests/integration/network/test_poa_redundancy_middleware.py +++ /dev/null @@ -1,58 +0,0 @@ -from unittest.mock import ANY, Mock - -from web3.exceptions import ExtraDataLengthError -from web3.middleware import geth_poa_middleware -from web3.types import RPCEndpoint, RPCResponse - -from nucypher.blockchain.middleware.poa import create_poa_error_redundancy_middleware - - -def test_request_with_poa_issues(): - make_request = Mock() - w3 = Mock() - middleware_onion = Mock() - w3.middleware_onion = middleware_onion - - poa_name = "poa_test" - - poa_redundancy_middleware = create_poa_error_redundancy_middleware( - existing_poa_middleware_name=poa_name - ) - - valid_response = RPCResponse( - jsonrpc="2.0", id=1, result="Geth/v1.14.8-stable-a9523b64/linux-amd64/go1.22.6" - ) - - redundant_middleware = poa_redundancy_middleware(make_request, w3) - - # 1. no poa error, simply return response - make_request.side_effect = [valid_response] - response = redundant_middleware(RPCEndpoint("web3_clientVersion"), None) - - assert response == valid_response - middleware_onion.get.assert_not_called() - middleware_onion.remove.assert_not_called() - middleware_onion.inject.assert_not_called() - - # 2. poa error; no prior poa middleware - make_request.side_effect = [ExtraDataLengthError(), valid_response] - middleware_onion.get.return_value = None - - response = redundant_middleware(RPCEndpoint("web3_clientVersion"), None) - - assert response == valid_response - middleware_onion.get.assert_called_once_with(poa_name) - middleware_onion.remove.assert_not_called() - middleware_onion.inject.assert_called_once_with(ANY, layer=0, name=poa_name) - - # 3. poa error; prior poa middleware - make_request.side_effect = [ExtraDataLengthError(), valid_response] - middleware_onion.get.return_value = geth_poa_middleware - response = redundant_middleware(RPCEndpoint("web3_clientVersion"), None) - - assert response == valid_response - assert middleware_onion.get.call_count == 2 - middleware_onion.get.assert_called_with(poa_name) - middleware_onion.remove.assert_called_once_with(poa_name) - assert middleware_onion.inject.call_count == 2 - middleware_onion.inject.assert_called_with(ANY, layer=0, name=poa_name)