Skip to content

Commit c85b7d9

Browse files
committed
Only apply to_hexbytes formatter if value is not null
1 parent ac8703c commit c85b7d9

File tree

4 files changed

+70
-30
lines changed

4 files changed

+70
-30
lines changed

newsfragments/2321.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``to_hexbytes`` block formatters no longer throw when value is ``None``
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import pytest
2+
3+
from web3._utils.rpc_abi import (
4+
RPC,
5+
)
6+
from web3.middleware import (
7+
construct_result_generator_middleware,
8+
)
9+
10+
11+
@pytest.fixture(autouse=True)
12+
def wait_for_first_block(web3, wait_for_block):
13+
wait_for_block(web3)
14+
15+
16+
def test_uses_default_block(web3, extra_accounts,
17+
wait_for_transaction):
18+
assert(web3.eth.default_block == 'latest')
19+
web3.eth.default_block = web3.eth.block_number
20+
assert(web3.eth.default_block == web3.eth.block_number)
21+
22+
23+
def test_uses_defaultBlock_with_warning(web3, extra_accounts,
24+
wait_for_transaction):
25+
with pytest.warns(DeprecationWarning):
26+
assert web3.eth.defaultBlock == 'latest'
27+
28+
with pytest.warns(DeprecationWarning):
29+
web3.eth.defaultBlock = web3.eth.block_number
30+
31+
with pytest.warns(DeprecationWarning):
32+
assert(web3.eth.defaultBlock == web3.eth.block_number)
33+
34+
35+
def test_get_block_formatters_with_null_values(web3):
36+
null_values_block = {
37+
'baseFeePerGas': None,
38+
'extraData': None,
39+
'gasLimit': None,
40+
'gasUsed': None,
41+
'size': None,
42+
'timestamp': None,
43+
'hash': None,
44+
'logsBloom': None,
45+
'miner': None,
46+
'mixHash': None,
47+
'nonce': None,
48+
'number': None,
49+
'parentHash': None,
50+
'sha3Uncles': None,
51+
'difficulty': None,
52+
'receiptsRoot': None,
53+
'statesRoot': None,
54+
'totalDifficulty': None,
55+
'transactionsRoot': None,
56+
}
57+
result_middleware = construct_result_generator_middleware({
58+
RPC.eth_getBlockByNumber: lambda *_: null_values_block,
59+
})
60+
61+
web3.middleware_onion.inject(result_middleware, 'result_middleware', layer=0)
62+
63+
received_block = web3.eth.get_block('pending')
64+
assert received_block == null_values_block

tests/core/eth-module/test_default_block_api.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

web3/_utils/method_formatters.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,29 +210,29 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
210210

211211
BLOCK_FORMATTERS = {
212212
'baseFeePerGas': to_integer_if_hex,
213-
'extraData': to_hexbytes(32, variable_length=True),
213+
'extraData': apply_formatter_if(is_not_null, to_hexbytes(32, variable_length=True)),
214214
'gasLimit': to_integer_if_hex,
215215
'gasUsed': to_integer_if_hex,
216216
'size': to_integer_if_hex,
217217
'timestamp': to_integer_if_hex,
218218
'hash': apply_formatter_if(is_not_null, to_hexbytes(32)),
219219
'logsBloom': apply_formatter_if(is_not_null, to_hexbytes(256)),
220220
'miner': apply_formatter_if(is_not_null, to_checksum_address),
221-
'mixHash': to_hexbytes(32),
221+
'mixHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
222222
'nonce': apply_formatter_if(is_not_null, to_hexbytes(8, variable_length=True)),
223223
'number': apply_formatter_if(is_not_null, to_integer_if_hex),
224224
'parentHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
225225
'sha3Uncles': apply_formatter_if(is_not_null, to_hexbytes(32)),
226226
'uncles': apply_list_to_array_formatter(to_hexbytes(32)),
227227
'difficulty': to_integer_if_hex,
228-
'receiptsRoot': to_hexbytes(32),
229-
'stateRoot': to_hexbytes(32),
228+
'receiptsRoot': apply_formatter_if(is_not_null, to_hexbytes(32)),
229+
'stateRoot': apply_formatter_if(is_not_null, to_hexbytes(32)),
230230
'totalDifficulty': to_integer_if_hex,
231231
'transactions': apply_one_of_formatters((
232232
(is_array_of_dicts, apply_list_to_array_formatter(transaction_result_formatter)),
233233
(is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
234234
)),
235-
'transactionsRoot': to_hexbytes(32),
235+
'transactionsRoot': apply_formatter_if(is_not_null, to_hexbytes(32)),
236236
}
237237

238238

0 commit comments

Comments
 (0)