Skip to content

Commit 6757ef1

Browse files
committed
Don't apply geth poa field remapping if result is None
1 parent f13d78a commit 6757ef1

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

newsfragments/2064.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug in geth PoA middleware where a ``None`` response should throw a ``BlockNotFound`` error, but was instead throwing an ``AttributeError``

tests/core/eth-module/test_poa.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from web3.exceptions import (
4+
BlockNotFound,
45
ExtraDataLengthError,
56
)
67
from web3.middleware import (
@@ -37,3 +38,13 @@ def test_geth_proof_of_authority(web3):
3738
block = web3.eth.get_block('latest')
3839
assert 'extraData' not in block
3940
assert block.proofOfAuthorityData == b'\xff' * 33
41+
42+
43+
def test_returns_none_response(web3):
44+
return_none_response = construct_fixture_middleware({
45+
'eth_getBlockByNumber': None,
46+
})
47+
web3.middleware_onion.inject(geth_poa_middleware, layer=0)
48+
web3.middleware_onion.inject(return_none_response, layer=0)
49+
with pytest.raises(BlockNotFound):
50+
web3.eth.get_block(100000000000)

web3/middleware/geth_poa.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
from eth_utils.curried import (
2+
apply_formatter_if,
23
apply_formatters_to_dict,
34
apply_key_map,
5+
is_null,
46
)
57
from eth_utils.toolz import (
8+
complement,
69
compose,
710
)
811
from hexbytes import (
912
HexBytes,
1013
)
1114

15+
from web3._utils.rpc_abi import (
16+
RPC,
17+
)
1218
from web3.middleware.formatting import (
1319
construct_formatting_middleware,
1420
)
15-
from web3.types import (
16-
RPCEndpoint,
17-
)
21+
22+
is_not_null = complement(is_null)
1823

1924
remap_geth_poa_fields = apply_key_map({
2025
'extraData': 'proofOfAuthorityData',
@@ -28,7 +33,7 @@
2833

2934
geth_poa_middleware = construct_formatting_middleware(
3035
result_formatters={
31-
RPCEndpoint("eth_getBlockByHash"): geth_poa_cleanup,
32-
RPCEndpoint("eth_getBlockByNumber"): geth_poa_cleanup,
36+
RPC.eth_getBlockByHash: apply_formatter_if(is_not_null, geth_poa_cleanup),
37+
RPC.eth_getBlockByNumber: apply_formatter_if(is_not_null, geth_poa_cleanup),
3338
},
3439
)

0 commit comments

Comments
 (0)