Skip to content

Commit 3647a50

Browse files
committed
Eliminate ethereum_tester_middleware
1 parent 6c974f5 commit 3647a50

File tree

3 files changed

+261
-281
lines changed

3 files changed

+261
-281
lines changed

web3/providers/eth_tester/defaults.py

Lines changed: 260 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,162 @@
1111
from eth_utils import (
1212
decode_hex,
1313
encode_hex,
14+
is_dict,
15+
is_hex,
1416
is_null,
17+
is_string,
1518
keccak,
1619
)
1720

1821
from web3._utils.formatters import (
1922
apply_formatter_if,
23+
apply_formatter_to_array,
24+
apply_formatters_to_args,
25+
apply_formatters_to_dict,
26+
apply_key_map,
27+
hex_to_integer,
28+
integer_to_hex,
29+
is_array_of_dicts,
30+
remove_key_if,
31+
static_return,
2032
)
2133
from web3._utils.toolz import (
34+
complement,
2235
compose,
2336
curry,
2437
excepts,
38+
identity,
39+
partial,
2540
)
2641

2742

43+
def is_hexstr(value):
44+
return is_string(value) and is_hex(value)
45+
46+
47+
def is_named_block(value):
48+
return value in {"latest", "earliest", "pending"}
49+
50+
51+
to_integer_if_hex = apply_formatter_if(is_hexstr, hex_to_integer)
52+
53+
54+
is_not_named_block = complement(is_named_block)
55+
56+
57+
TRANSACTION_PARAMS_MAPPING = {
58+
'gasPrice': 'gas_price',
59+
}
60+
61+
62+
transaction_params_remapper = apply_key_map(TRANSACTION_PARAMS_MAPPING)
63+
64+
65+
TRANSACTION_PARAMS_FORMATTERS = {
66+
'gas': to_integer_if_hex,
67+
'gasPrice': to_integer_if_hex,
68+
'value': to_integer_if_hex,
69+
'nonce': to_integer_if_hex,
70+
}
71+
72+
73+
transaction_params_formatter = compose(
74+
# remove nonce for now due to issue https://github.com/ethereum/eth-tester/issues/80
75+
remove_key_if('nonce', lambda _: True),
76+
apply_formatters_to_dict(TRANSACTION_PARAMS_FORMATTERS),
77+
)
78+
79+
80+
FILTER_PARAMS_MAPPINGS = {
81+
'fromBlock': 'from_block',
82+
'toBlock': 'to_block',
83+
}
84+
85+
filter_params_remapper = apply_key_map(FILTER_PARAMS_MAPPINGS)
86+
87+
FILTER_PARAMS_FORMATTERS = {
88+
'fromBlock': to_integer_if_hex,
89+
'toBlock': to_integer_if_hex,
90+
}
91+
92+
filter_params_formatter = apply_formatters_to_dict(FILTER_PARAMS_FORMATTERS)
93+
94+
filter_params_transformer = compose(filter_params_remapper, filter_params_formatter)
95+
96+
97+
transaction_params_transformer = compose(transaction_params_remapper, transaction_params_formatter)
98+
99+
100+
TRANSACTION_FORMATTERS = {
101+
'to': apply_formatter_if(partial(operator.eq, ''), static_return(None)),
102+
}
103+
104+
105+
transaction_formatter = apply_formatters_to_dict(TRANSACTION_FORMATTERS)
106+
107+
108+
LOG_KEY_MAPPINGS = {
109+
'log_index': 'logIndex',
110+
'transaction_index': 'transactionIndex',
111+
'transaction_hash': 'transactionHash',
112+
'block_hash': 'blockHash',
113+
'block_number': 'blockNumber',
114+
}
115+
116+
117+
log_key_remapper = apply_key_map(LOG_KEY_MAPPINGS)
118+
119+
120+
RECEIPT_KEY_MAPPINGS = {
121+
'block_hash': 'blockHash',
122+
'block_number': 'blockNumber',
123+
'contract_address': 'contractAddress',
124+
'gas_used': 'gasUsed',
125+
'cumulative_gas_used': 'cumulativeGasUsed',
126+
'transaction_hash': 'transactionHash',
127+
'transaction_index': 'transactionIndex',
128+
}
129+
130+
131+
receipt_key_remapper = apply_key_map(RECEIPT_KEY_MAPPINGS)
132+
133+
134+
RECEIPT_FORMATTERS = {
135+
'logs': apply_formatter_to_array(log_key_remapper),
136+
}
137+
138+
139+
receipt_formatter = apply_formatters_to_dict(RECEIPT_FORMATTERS)
140+
141+
142+
TRANSACTION_KEY_MAPPINGS = {
143+
'block_hash': 'blockHash',
144+
'block_number': 'blockNumber',
145+
'gas_price': 'gasPrice',
146+
'transaction_hash': 'transactionHash',
147+
'transaction_index': 'transactionIndex',
148+
}
149+
150+
transaction_key_remapper = apply_key_map(TRANSACTION_KEY_MAPPINGS)
151+
152+
153+
BLOCK_KEY_MAPPINGS = {
154+
'gas_limit': 'gasLimit',
155+
'sha3_uncles': 'sha3Uncles',
156+
'transactions_root': 'transactionsRoot',
157+
'parent_hash': 'parentHash',
158+
'bloom': 'logsBloom',
159+
'state_root': 'stateRoot',
160+
'receipt_root': 'receiptsRoot',
161+
'total_difficulty': 'totalDifficulty',
162+
'extra_data': 'extraData',
163+
'gas_used': 'gasUsed',
164+
}
165+
166+
167+
block_key_remapper = apply_key_map(BLOCK_KEY_MAPPINGS)
168+
169+
28170
def not_implemented(*args, **kwargs):
29171
raise NotImplementedError("RPC method not implemented")
30172

@@ -180,14 +322,25 @@ def personal_send_transaction(eth_tester, params):
180322
'getStorageAt': not_implemented,
181323
'getTransactionCount': call_eth_tester('get_nonce'),
182324
'getBlockTransactionCountByHash': null_if_block_not_found(compose(
325+
apply_formatter_if(
326+
is_dict,
327+
transaction_key_remapper
328+
),
183329
len,
184330
operator.itemgetter('transactions'),
185331
call_eth_tester('get_block_by_hash'),
186332
)),
187333
'getBlockTransactionCountByNumber': null_if_block_not_found(compose(
334+
apply_formatter_if(
335+
is_dict,
336+
transaction_key_remapper
337+
),
188338
len,
189339
operator.itemgetter('transactions'),
190340
call_eth_tester('get_block_by_number'),
341+
apply_formatters_to_args(
342+
apply_formatter_if(is_not_named_block, to_integer_if_hex)
343+
)
191344
)),
192345
'getUncleCountByBlockHash': null_if_block_not_found(compose(
193346
len,
@@ -198,21 +351,69 @@ def personal_send_transaction(eth_tester, params):
198351
len,
199352
operator.itemgetter('uncles'),
200353
call_eth_tester('get_block_by_number'),
354+
apply_formatters_to_args(
355+
apply_formatter_if(is_not_named_block, to_integer_if_hex)
356+
)
201357
)),
202-
'getCode': call_eth_tester('get_code'),
358+
'getCode': compose(
359+
call_eth_tester('get_code'),
360+
apply_formatters_to_args(
361+
identity,
362+
apply_formatter_if(is_not_named_block, to_integer_if_hex))
363+
),
203364
'sign': not_implemented,
204-
'sendTransaction': call_eth_tester('send_transaction'),
365+
'sendTransaction': compose(
366+
call_eth_tester('send_transaction'),
367+
apply_formatters_to_args(
368+
transaction_params_transformer,
369+
identity)
370+
),
205371
'sendRawTransaction': call_eth_tester('send_raw_transaction'),
206-
'call': call_eth_tester('call'), # TODO: untested
207-
'estimateGas': call_eth_tester('estimate_gas'), # TODO: untested
208-
'getBlockByHash': null_if_block_not_found(call_eth_tester('get_block_by_hash')),
209-
'getBlockByNumber': null_if_block_not_found(call_eth_tester('get_block_by_number')),
210-
'getTransactionByHash': null_if_transaction_not_found(
211-
call_eth_tester('get_transaction_by_hash')
372+
'call': compose(
373+
call_eth_tester('call'), # TODO: untested
374+
apply_formatters_to_args(
375+
transaction_params_transformer,
376+
apply_formatter_if(is_not_named_block, to_integer_if_hex))
377+
),
378+
'estimateGas': compose(
379+
call_eth_tester('estimate_gas'), # TODO: untested
380+
apply_formatters_to_args(transaction_params_transformer)
381+
),
382+
'getBlockByHash': null_if_block_not_found(compose(
383+
apply_formatter_if(
384+
is_dict,
385+
block_key_remapper,
386+
),
387+
call_eth_tester('get_block_by_hash'))),
388+
'getBlockByNumber': compose(
389+
apply_formatter_if(
390+
is_dict,
391+
block_key_remapper,
392+
),
393+
null_if_block_not_found(call_eth_tester('get_block_by_number')),
394+
apply_formatters_to_args(
395+
apply_formatter_if(is_not_named_block, to_integer_if_hex))),
396+
'getTransactionByHash': null_if_transaction_not_found(compose(
397+
apply_formatter_if(
398+
is_dict,
399+
compose(transaction_key_remapper, transaction_formatter),
400+
),
401+
call_eth_tester('get_transaction_by_hash'))
402+
),
403+
'getTransactionByBlockHashAndIndex': compose(
404+
get_transaction_by_block_hash_and_index,
405+
apply_formatters_to_args(identity, to_integer_if_hex)),
406+
'getTransactionByBlockNumberAndIndex': compose(
407+
get_transaction_by_block_number_and_index,
408+
apply_formatters_to_args(
409+
apply_formatter_if(is_not_named_block, to_integer_if_hex),
410+
to_integer_if_hex
411+
)
212412
),
213-
'getTransactionByBlockHashAndIndex': get_transaction_by_block_hash_and_index,
214-
'getTransactionByBlockNumberAndIndex': get_transaction_by_block_number_and_index,
215413
'getTransactionReceipt': null_if_transaction_not_found(compose(
414+
apply_formatter_if(
415+
is_dict,
416+
compose(receipt_key_remapper, receipt_formatter)),
216417
apply_formatter_if(
217418
compose(is_null, operator.itemgetter('block_number')),
218419
static_return(None),
@@ -225,20 +426,48 @@ def personal_send_transaction(eth_tester, params):
225426
'compileLLL': not_implemented,
226427
'compileSolidity': not_implemented,
227428
'compileSerpent': not_implemented,
228-
'newFilter': create_log_filter,
229-
'newBlockFilter': call_eth_tester('create_block_filter'),
230-
'newPendingTransactionFilter': call_eth_tester('create_pending_transaction_filter'),
231-
'uninstallFilter': excepts(
232-
FilterNotFound,
233-
compose(
234-
is_null,
235-
call_eth_tester('delete_filter'),
429+
'newFilter': compose(
430+
integer_to_hex,
431+
create_log_filter,
432+
apply_formatters_to_args(filter_params_transformer)
433+
),
434+
'newBlockFilter': compose(integer_to_hex, call_eth_tester('create_block_filter')),
435+
'newPendingTransactionFilter': compose(
436+
integer_to_hex,
437+
call_eth_tester('create_pending_transaction_filter')),
438+
'uninstallFilter': compose(
439+
excepts(
440+
FilterNotFound,
441+
compose(
442+
is_null,
443+
call_eth_tester('delete_filter'),
444+
),
445+
static_return(False),
236446
),
237-
static_return(False),
447+
apply_formatters_to_args(hex_to_integer),
448+
),
449+
'getFilterChanges': compose(
450+
apply_formatter_if(
451+
is_array_of_dicts,
452+
apply_formatter_to_array(log_key_remapper)),
453+
null_if_filter_not_found(call_eth_tester('get_only_filter_changes')),
454+
apply_formatters_to_args(hex_to_integer)
455+
),
456+
'getFilterLogs': compose(
457+
apply_formatter_if(
458+
is_array_of_dicts,
459+
apply_formatter_to_array(log_key_remapper)),
460+
null_if_filter_not_found(call_eth_tester('get_all_filter_logs')),
461+
apply_formatters_to_args(hex_to_integer),
462+
),
463+
'getLogs': compose(
464+
apply_formatter_if(
465+
is_array_of_dicts,
466+
apply_formatter_to_array(log_key_remapper)),
467+
get_logs,
468+
apply_formatters_to_args(
469+
filter_params_transformer)
238470
),
239-
'getFilterChanges': null_if_filter_not_found(call_eth_tester('get_only_filter_changes')),
240-
'getFilterLogs': null_if_filter_not_found(call_eth_tester('get_all_filter_logs')),
241-
'getLogs': get_logs,
242471
'getWork': not_implemented,
243472
'submitWork': not_implemented,
244473
'submitHashrate': not_implemented,
@@ -323,7 +552,10 @@ def personal_send_transaction(eth_tester, params):
323552
compose(static_return(True), call_eth_tester('unlock_account')),
324553
static_return(False),
325554
),
326-
'sendTransaction': personal_send_transaction,
555+
'sendTransaction': compose(
556+
personal_send_transaction,
557+
apply_formatters_to_args(transaction_params_transformer)
558+
),
327559
'sign': not_implemented,
328560
},
329561
'testing': {
@@ -336,7 +568,10 @@ def personal_send_transaction(eth_tester, params):
336568
},
337569
'evm': {
338570
'mine': call_eth_tester('mine_blocks'),
339-
'revert': call_eth_tester('revert_to_snapshot'),
340-
'snapshot': call_eth_tester('take_snapshot'),
571+
'revert': compose(
572+
call_eth_tester('revert_to_snapshot'),
573+
apply_formatters_to_args(hex_to_integer)
574+
),
575+
'snapshot': compose(integer_to_hex, call_eth_tester('take_snapshot')),
341576
},
342577
}

web3/providers/eth_tester/main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
from .middleware import (
66
default_transaction_fields_middleware,
7-
ethereum_tester_middleware,
87
)
98

109

1110
class EthereumTesterProvider(BaseProvider):
1211
middlewares = [
1312
default_transaction_fields_middleware,
14-
ethereum_tester_middleware,
15-
]
13+
]
1614
ethereum_tester = None
1715
api_endpoints = None
1816

0 commit comments

Comments
 (0)