mirror of https://github.com/nucypher/nucypher.git
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.
parent
2158136c6f
commit
258ebef8ef
|
@ -15,7 +15,6 @@ from nucypher.blockchain.eth.constants import (
|
||||||
AVERAGE_BLOCK_TIME_IN_SECONDS,
|
AVERAGE_BLOCK_TIME_IN_SECONDS,
|
||||||
PUBLIC_CHAINS,
|
PUBLIC_CHAINS,
|
||||||
)
|
)
|
||||||
from nucypher.blockchain.middleware.poa import create_poa_error_redundancy_middleware
|
|
||||||
from nucypher.blockchain.middleware.retry import (
|
from nucypher.blockchain.middleware.retry import (
|
||||||
AlchemyRetryRequestMiddleware,
|
AlchemyRetryRequestMiddleware,
|
||||||
InfuraRetryRequestMiddleware,
|
InfuraRetryRequestMiddleware,
|
||||||
|
@ -101,13 +100,6 @@ class EthereumClient:
|
||||||
layer=0,
|
layer=0,
|
||||||
name=poa_middleware_name,
|
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
|
# simple cache middleware
|
||||||
self.log.debug("Adding simple_cache_middleware")
|
self.log.debug("Adding simple_cache_middleware")
|
||||||
|
|
|
@ -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
|
|
|
@ -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)
|
|
Loading…
Reference in New Issue