Skip to content

Commit bbceb85

Browse files
committed
Merge branch 'master' into patch-1
2 parents 6938ada + 9c117ab commit bbceb85

File tree

3 files changed

+138
-68
lines changed

3 files changed

+138
-68
lines changed

newsfragments/2224.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Key mapping fix to eth-tester middleware for access list storage keys

tests/core/eth-module/test_transactions.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,57 @@
1616
RECEIPT_TIMEOUT = 0.2
1717

1818

19+
@pytest.mark.parametrize(
20+
'transaction',
21+
(
22+
{
23+
'gasPrice': 10 ** 9,
24+
'accessList': (
25+
{
26+
'address': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
27+
'storageKeys': (
28+
'0x0000000000000000000000000000000000000000000000000000000000000003',
29+
'0x0000000000000000000000000000000000000000000000000000000000000007',
30+
)
31+
},
32+
{
33+
'address': '0xbb9bc244d798123fde783fcc1c72d3bb8c189413',
34+
'storageKeys': ()
35+
},
36+
),
37+
},
38+
{
39+
'maxFeePerGas': 10 ** 9,
40+
'maxPriorityFeePerGas': 10 ** 9,
41+
'accessList': (
42+
{
43+
'address': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
44+
'storageKeys': (
45+
'0x0000000000000000000000000000000000000000000000000000000000000003',
46+
'0x0000000000000000000000000000000000000000000000000000000000000007',
47+
)
48+
},
49+
{
50+
'address': '0xbb9bc244d798123fde783fcc1c72d3bb8c189413',
51+
'storageKeys': ()
52+
},
53+
),
54+
},
55+
),
56+
ids=[
57+
'storage_keys_access_list_txn',
58+
'storage_keys_dynamic_fee_txn',
59+
],
60+
)
61+
def test_eth_tester_send_transaction_validation(web3, transaction):
62+
# Test that eth-tester transaction param validation does not throw for properly formatted
63+
# transactions. This is especially important because we have key mapping differences
64+
# (camelCase to snake_case) mitigated by providers/eth-tester/middleware.
65+
txn_hash = web3.eth.send_transaction(transaction)
66+
receipt = web3.eth.wait_for_transaction_receipt(txn_hash, timeout=RECEIPT_TIMEOUT)
67+
assert receipt.get('blockNumber') is not None
68+
69+
1970
@pytest.mark.parametrize(
2071
'make_chain_id, expect_success',
2172
(

web3/providers/eth_tester/middleware.py

Lines changed: 86 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,58 @@ def is_hexstr(value: Any) -> bool:
6464
is_not_named_block = complement(is_named_block)
6565

6666

67-
TRANSACTION_KEY_MAPPINGS = {
67+
# --- Request Mapping --- #
68+
69+
TRANSACTION_REQUEST_KEY_MAPPING = {
70+
'gasPrice': 'gas_price',
71+
'maxFeePerGas': 'max_fee_per_gas',
72+
'maxPriorityFeePerGas': 'max_priority_fee_per_gas',
73+
'accessList': 'access_list',
74+
}
75+
transaction_request_remapper = apply_key_map(TRANSACTION_REQUEST_KEY_MAPPING)
76+
77+
78+
TRANSACTION_REQUEST_FORMATTERS = {
79+
'gas': to_integer_if_hex,
80+
'gasPrice': to_integer_if_hex,
81+
'value': to_integer_if_hex,
82+
'nonce': to_integer_if_hex,
83+
'maxFeePerGas': to_integer_if_hex,
84+
'maxPriorityFeePerGas': to_integer_if_hex,
85+
'accessList': apply_formatter_to_array(
86+
apply_key_map({'storageKeys': 'storage_keys'})
87+
),
88+
}
89+
transaction_request_formatter = apply_formatters_to_dict(TRANSACTION_REQUEST_FORMATTERS)
90+
91+
transaction_request_transformer = compose(
92+
transaction_request_remapper,
93+
transaction_request_formatter,
94+
)
95+
96+
97+
FILTER_REQUEST_KEY_MAPPING = {
98+
'fromBlock': 'from_block',
99+
'toBlock': 'to_block',
100+
}
101+
filter_request_remapper = apply_key_map(FILTER_REQUEST_KEY_MAPPING)
102+
103+
104+
FILTER_REQUEST_FORMATTERS = {
105+
'fromBlock': to_integer_if_hex,
106+
'toBlock': to_integer_if_hex,
107+
}
108+
filter_request_formatter = apply_formatters_to_dict(FILTER_REQUEST_FORMATTERS)
109+
110+
filter_request_transformer = compose(
111+
filter_request_remapper,
112+
filter_request_formatter,
113+
)
114+
115+
116+
# --- Result Mapping --- #
117+
118+
TRANSACTION_RESULT_KEY_MAPPING = {
68119
'access_list': 'accessList',
69120
'block_hash': 'blockHash',
70121
'block_number': 'blockNumber',
@@ -74,20 +125,29 @@ def is_hexstr(value: Any) -> bool:
74125
'transaction_hash': 'transactionHash',
75126
'transaction_index': 'transactionIndex',
76127
}
77-
transaction_key_remapper = apply_key_map(TRANSACTION_KEY_MAPPINGS)
128+
transaction_result_remapper = apply_key_map(TRANSACTION_RESULT_KEY_MAPPING)
129+
130+
131+
TRANSACTION_RESULT_FORMATTERS = {
132+
'to': apply_formatter_if(partial(operator.eq, ''), static_return(None)),
133+
'access_list': apply_formatter_to_array(
134+
apply_key_map({'storage_keys': 'storageKeys'}),
135+
),
136+
}
137+
transaction_result_formatter = apply_formatters_to_dict(TRANSACTION_RESULT_FORMATTERS)
78138

79139

80-
LOG_KEY_MAPPINGS = {
140+
LOG_RESULT_KEY_MAPPING = {
81141
'log_index': 'logIndex',
82142
'transaction_index': 'transactionIndex',
83143
'transaction_hash': 'transactionHash',
84144
'block_hash': 'blockHash',
85145
'block_number': 'blockNumber',
86146
}
87-
log_key_remapper = apply_key_map(LOG_KEY_MAPPINGS)
147+
log_result_remapper = apply_key_map(LOG_RESULT_KEY_MAPPING)
88148

89149

90-
RECEIPT_KEY_MAPPINGS = {
150+
RECEIPT_RESULT_KEY_MAPPING = {
91151
'block_hash': 'blockHash',
92152
'block_number': 'blockNumber',
93153
'contract_address': 'contractAddress',
@@ -97,10 +157,10 @@ def is_hexstr(value: Any) -> bool:
97157
'transaction_hash': 'transactionHash',
98158
'transaction_index': 'transactionIndex',
99159
}
100-
receipt_key_remapper = apply_key_map(RECEIPT_KEY_MAPPINGS)
160+
receipt_result_remapper = apply_key_map(RECEIPT_RESULT_KEY_MAPPING)
101161

102162

103-
BLOCK_KEY_MAPPINGS = {
163+
BLOCK_RESULT_KEY_MAPPING = {
104164
'gas_limit': 'gasLimit',
105165
'sha3_uncles': 'sha3Uncles',
106166
'transactions_root': 'transactionsRoot',
@@ -113,55 +173,13 @@ def is_hexstr(value: Any) -> bool:
113173
'gas_used': 'gasUsed',
114174
'base_fee_per_gas': 'baseFeePerGas',
115175
}
116-
block_key_remapper = apply_key_map(BLOCK_KEY_MAPPINGS)
117-
118-
119-
TRANSACTION_PARAMS_MAPPING = {
120-
'gasPrice': 'gas_price',
121-
'maxFeePerGas': 'max_fee_per_gas',
122-
'maxPriorityFeePerGas': 'max_priority_fee_per_gas',
123-
'accessList': 'access_list',
124-
}
125-
transaction_params_remapper = apply_key_map(TRANSACTION_PARAMS_MAPPING)
126-
127-
128-
REQUEST_TRANSACTION_FORMATTERS = {
129-
'gas': to_integer_if_hex,
130-
'gasPrice': to_integer_if_hex,
131-
'value': to_integer_if_hex,
132-
'nonce': to_integer_if_hex,
133-
'maxFeePerGas': to_integer_if_hex,
134-
'maxPriorityFeePerGas': to_integer_if_hex,
135-
}
136-
request_transaction_formatter = apply_formatters_to_dict(REQUEST_TRANSACTION_FORMATTERS)
137-
138-
139-
FILTER_PARAMS_MAPPINGS = {
140-
'fromBlock': 'from_block',
141-
'toBlock': 'to_block',
142-
}
143-
filter_params_remapper = apply_key_map(FILTER_PARAMS_MAPPINGS)
144-
145-
146-
FILTER_PARAMS_FORMATTERS = {
147-
'fromBlock': to_integer_if_hex,
148-
'toBlock': to_integer_if_hex,
149-
}
150-
filter_params_formatter = apply_formatters_to_dict(FILTER_PARAMS_FORMATTERS)
151-
filter_params_transformer = compose(filter_params_remapper, filter_params_formatter)
152-
153-
154-
RESPONSE_TRANSACTION_FORMATTERS = {
155-
'to': apply_formatter_if(partial(operator.eq, ''), static_return(None)),
156-
}
157-
response_transaction_formatter = apply_formatters_to_dict(RESPONSE_TRANSACTION_FORMATTERS)
176+
block_result_remapper = apply_key_map(BLOCK_RESULT_KEY_MAPPING)
158177

159178

160-
RECEIPT_FORMATTERS = {
161-
'logs': apply_formatter_to_array(log_key_remapper),
179+
RECEIPT_RESULT_FORMATTERS = {
180+
'logs': apply_formatter_to_array(log_result_remapper),
162181
}
163-
receipt_formatter = apply_formatters_to_dict(RECEIPT_FORMATTERS)
164-
transaction_params_transformer = compose(transaction_params_remapper, request_transaction_formatter)
182+
receipt_result_formatter = apply_formatters_to_dict(RECEIPT_RESULT_FORMATTERS)
165183

166184

167185
ethereum_tester_middleware = construct_formatting_middleware(
@@ -191,19 +209,19 @@ def is_hexstr(value: Any) -> bool:
191209
to_integer_if_hex,
192210
),
193211
RPCEndpoint('eth_newFilter'): apply_formatters_to_args(
194-
filter_params_transformer,
212+
filter_request_transformer,
195213
),
196214
RPCEndpoint('eth_getLogs'): apply_formatters_to_args(
197-
filter_params_transformer,
215+
filter_request_transformer,
198216
),
199217
RPCEndpoint('eth_sendTransaction'): apply_formatters_to_args(
200-
transaction_params_transformer,
218+
transaction_request_transformer,
201219
),
202220
RPCEndpoint('eth_estimateGas'): apply_formatters_to_args(
203-
transaction_params_transformer,
221+
transaction_request_transformer,
204222
),
205223
RPCEndpoint('eth_call'): apply_formatters_to_args(
206-
transaction_params_transformer,
224+
transaction_request_transformer,
207225
apply_formatter_if(is_not_named_block, to_integer_if_hex),
208226
),
209227
RPCEndpoint('eth_uninstallFilter'): apply_formatters_to_args(hex_to_integer),
@@ -219,49 +237,49 @@ def is_hexstr(value: Any) -> bool:
219237
RPCEndpoint('evm_revert'): apply_formatters_to_args(hex_to_integer),
220238
# Personal
221239
RPCEndpoint('personal_sendTransaction'): apply_formatters_to_args(
222-
transaction_params_transformer,
240+
transaction_request_transformer,
223241
identity,
224242
),
225243
},
226244
result_formatters={
227245
RPCEndpoint('eth_getBlockByHash'): apply_formatter_if(
228246
is_dict,
229-
block_key_remapper,
247+
block_result_remapper,
230248
),
231249
RPCEndpoint('eth_getBlockByNumber'): apply_formatter_if(
232250
is_dict,
233-
block_key_remapper,
251+
block_result_remapper,
234252
),
235253
RPCEndpoint('eth_getBlockTransactionCountByHash'): apply_formatter_if(
236254
is_dict,
237-
transaction_key_remapper,
255+
transaction_result_remapper,
238256
),
239257
RPCEndpoint('eth_getBlockTransactionCountByNumber'): apply_formatter_if(
240258
is_dict,
241-
transaction_key_remapper,
259+
transaction_result_remapper,
242260
),
243261
RPCEndpoint('eth_getTransactionByHash'): apply_formatter_if(
244262
is_dict,
245-
compose(transaction_key_remapper, response_transaction_formatter),
263+
compose(transaction_result_remapper, transaction_result_formatter),
246264
),
247265
RPCEndpoint('eth_getTransactionReceipt'): apply_formatter_if(
248266
is_dict,
249-
compose(receipt_key_remapper, receipt_formatter),
267+
compose(receipt_result_remapper, receipt_result_formatter),
250268
),
251269
RPCEndpoint('eth_newFilter'): integer_to_hex,
252270
RPCEndpoint('eth_newBlockFilter'): integer_to_hex,
253271
RPCEndpoint('eth_newPendingTransactionFilter'): integer_to_hex,
254272
RPCEndpoint('eth_getLogs'): apply_formatter_if(
255273
is_array_of_dicts,
256-
apply_formatter_to_array(log_key_remapper),
274+
apply_formatter_to_array(log_result_remapper),
257275
),
258276
RPCEndpoint('eth_getFilterChanges'): apply_formatter_if(
259277
is_array_of_dicts,
260-
apply_formatter_to_array(log_key_remapper),
278+
apply_formatter_to_array(log_result_remapper),
261279
),
262280
RPCEndpoint('eth_getFilterLogs'): apply_formatter_if(
263281
is_array_of_dicts,
264-
apply_formatter_to_array(log_key_remapper),
282+
apply_formatter_to_array(log_result_remapper),
265283
),
266284
# EVM
267285
RPCEndpoint('evm_snapshot'): integer_to_hex,

0 commit comments

Comments
 (0)