From 6757ef18c4dee38b1d702ec63d317aea5a71268f Mon Sep 17 00:00:00 2001 From: kclowes Date: Thu, 8 Jul 2021 09:33:19 -0600 Subject: [PATCH] Don't apply geth poa field remapping if result is None --- newsfragments/2064.bugfix.rst | 1 + tests/core/eth-module/test_poa.py | 11 +++++++++++ web3/middleware/geth_poa.py | 15 ++++++++++----- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 newsfragments/2064.bugfix.rst diff --git a/newsfragments/2064.bugfix.rst b/newsfragments/2064.bugfix.rst new file mode 100644 index 0000000000..6e86ad447c --- /dev/null +++ b/newsfragments/2064.bugfix.rst @@ -0,0 +1 @@ +Fix bug in geth PoA middleware where a ``None`` response should throw a ``BlockNotFound`` error, but was instead throwing an ``AttributeError`` diff --git a/tests/core/eth-module/test_poa.py b/tests/core/eth-module/test_poa.py index aeefb9969d..8be9346dfe 100644 --- a/tests/core/eth-module/test_poa.py +++ b/tests/core/eth-module/test_poa.py @@ -1,6 +1,7 @@ import pytest from web3.exceptions import ( + BlockNotFound, ExtraDataLengthError, ) from web3.middleware import ( @@ -37,3 +38,13 @@ def test_geth_proof_of_authority(web3): block = web3.eth.get_block('latest') assert 'extraData' not in block assert block.proofOfAuthorityData == b'\xff' * 33 + + +def test_returns_none_response(web3): + return_none_response = construct_fixture_middleware({ + 'eth_getBlockByNumber': None, + }) + web3.middleware_onion.inject(geth_poa_middleware, layer=0) + web3.middleware_onion.inject(return_none_response, layer=0) + with pytest.raises(BlockNotFound): + web3.eth.get_block(100000000000) diff --git a/web3/middleware/geth_poa.py b/web3/middleware/geth_poa.py index a7e7947b32..f941b69a01 100644 --- a/web3/middleware/geth_poa.py +++ b/web3/middleware/geth_poa.py @@ -1,20 +1,25 @@ from eth_utils.curried import ( + apply_formatter_if, apply_formatters_to_dict, apply_key_map, + is_null, ) from eth_utils.toolz import ( + complement, compose, ) from hexbytes import ( HexBytes, ) +from web3._utils.rpc_abi import ( + RPC, +) from web3.middleware.formatting import ( construct_formatting_middleware, ) -from web3.types import ( - RPCEndpoint, -) + +is_not_null = complement(is_null) remap_geth_poa_fields = apply_key_map({ 'extraData': 'proofOfAuthorityData', @@ -28,7 +33,7 @@ geth_poa_middleware = construct_formatting_middleware( result_formatters={ - RPCEndpoint("eth_getBlockByHash"): geth_poa_cleanup, - RPCEndpoint("eth_getBlockByNumber"): geth_poa_cleanup, + RPC.eth_getBlockByHash: apply_formatter_if(is_not_null, geth_poa_cleanup), + RPC.eth_getBlockByNumber: apply_formatter_if(is_not_null, geth_poa_cleanup), }, )