Skip to content

Commit

Permalink
Eliminate ethereum_tester_middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjw committed Jan 8, 2019
1 parent 6c974f5 commit 3647a50
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 281 deletions.
285 changes: 260 additions & 25 deletions web3/providers/eth_tester/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,162 @@
from eth_utils import (
decode_hex,
encode_hex,
is_dict,
is_hex,
is_null,
is_string,
keccak,
)

from web3._utils.formatters import (
apply_formatter_if,
apply_formatter_to_array,
apply_formatters_to_args,
apply_formatters_to_dict,
apply_key_map,
hex_to_integer,
integer_to_hex,
is_array_of_dicts,
remove_key_if,
static_return,
)
from web3._utils.toolz import (
complement,
compose,
curry,
excepts,
identity,
partial,
)


def is_hexstr(value):
return is_string(value) and is_hex(value)


def is_named_block(value):
return value in {"latest", "earliest", "pending"}


to_integer_if_hex = apply_formatter_if(is_hexstr, hex_to_integer)


is_not_named_block = complement(is_named_block)


TRANSACTION_PARAMS_MAPPING = {
'gasPrice': 'gas_price',
}


transaction_params_remapper = apply_key_map(TRANSACTION_PARAMS_MAPPING)


TRANSACTION_PARAMS_FORMATTERS = {
'gas': to_integer_if_hex,
'gasPrice': to_integer_if_hex,
'value': to_integer_if_hex,
'nonce': to_integer_if_hex,
}


transaction_params_formatter = compose(
# remove nonce for now due to issue https://github.com/ethereum/eth-tester/issues/80
remove_key_if('nonce', lambda _: True),
apply_formatters_to_dict(TRANSACTION_PARAMS_FORMATTERS),
)


FILTER_PARAMS_MAPPINGS = {
'fromBlock': 'from_block',
'toBlock': 'to_block',
}

filter_params_remapper = apply_key_map(FILTER_PARAMS_MAPPINGS)

FILTER_PARAMS_FORMATTERS = {
'fromBlock': to_integer_if_hex,
'toBlock': to_integer_if_hex,
}

filter_params_formatter = apply_formatters_to_dict(FILTER_PARAMS_FORMATTERS)

filter_params_transformer = compose(filter_params_remapper, filter_params_formatter)


transaction_params_transformer = compose(transaction_params_remapper, transaction_params_formatter)


TRANSACTION_FORMATTERS = {
'to': apply_formatter_if(partial(operator.eq, ''), static_return(None)),
}


transaction_formatter = apply_formatters_to_dict(TRANSACTION_FORMATTERS)


LOG_KEY_MAPPINGS = {
'log_index': 'logIndex',
'transaction_index': 'transactionIndex',
'transaction_hash': 'transactionHash',
'block_hash': 'blockHash',
'block_number': 'blockNumber',
}


log_key_remapper = apply_key_map(LOG_KEY_MAPPINGS)


RECEIPT_KEY_MAPPINGS = {
'block_hash': 'blockHash',
'block_number': 'blockNumber',
'contract_address': 'contractAddress',
'gas_used': 'gasUsed',
'cumulative_gas_used': 'cumulativeGasUsed',
'transaction_hash': 'transactionHash',
'transaction_index': 'transactionIndex',
}


receipt_key_remapper = apply_key_map(RECEIPT_KEY_MAPPINGS)


RECEIPT_FORMATTERS = {
'logs': apply_formatter_to_array(log_key_remapper),
}


receipt_formatter = apply_formatters_to_dict(RECEIPT_FORMATTERS)


TRANSACTION_KEY_MAPPINGS = {
'block_hash': 'blockHash',
'block_number': 'blockNumber',
'gas_price': 'gasPrice',
'transaction_hash': 'transactionHash',
'transaction_index': 'transactionIndex',
}

transaction_key_remapper = apply_key_map(TRANSACTION_KEY_MAPPINGS)


BLOCK_KEY_MAPPINGS = {
'gas_limit': 'gasLimit',
'sha3_uncles': 'sha3Uncles',
'transactions_root': 'transactionsRoot',
'parent_hash': 'parentHash',
'bloom': 'logsBloom',
'state_root': 'stateRoot',
'receipt_root': 'receiptsRoot',
'total_difficulty': 'totalDifficulty',
'extra_data': 'extraData',
'gas_used': 'gasUsed',
}


block_key_remapper = apply_key_map(BLOCK_KEY_MAPPINGS)


def not_implemented(*args, **kwargs):
raise NotImplementedError("RPC method not implemented")

Expand Down Expand Up @@ -180,14 +322,25 @@ def personal_send_transaction(eth_tester, params):
'getStorageAt': not_implemented,
'getTransactionCount': call_eth_tester('get_nonce'),
'getBlockTransactionCountByHash': null_if_block_not_found(compose(
apply_formatter_if(
is_dict,
transaction_key_remapper
),
len,
operator.itemgetter('transactions'),
call_eth_tester('get_block_by_hash'),
)),
'getBlockTransactionCountByNumber': null_if_block_not_found(compose(
apply_formatter_if(
is_dict,
transaction_key_remapper
),
len,
operator.itemgetter('transactions'),
call_eth_tester('get_block_by_number'),
apply_formatters_to_args(
apply_formatter_if(is_not_named_block, to_integer_if_hex)
)
)),
'getUncleCountByBlockHash': null_if_block_not_found(compose(
len,
Expand All @@ -198,21 +351,69 @@ def personal_send_transaction(eth_tester, params):
len,
operator.itemgetter('uncles'),
call_eth_tester('get_block_by_number'),
apply_formatters_to_args(
apply_formatter_if(is_not_named_block, to_integer_if_hex)
)
)),
'getCode': call_eth_tester('get_code'),
'getCode': compose(
call_eth_tester('get_code'),
apply_formatters_to_args(
identity,
apply_formatter_if(is_not_named_block, to_integer_if_hex))
),
'sign': not_implemented,
'sendTransaction': call_eth_tester('send_transaction'),
'sendTransaction': compose(
call_eth_tester('send_transaction'),
apply_formatters_to_args(
transaction_params_transformer,
identity)
),
'sendRawTransaction': call_eth_tester('send_raw_transaction'),
'call': call_eth_tester('call'), # TODO: untested
'estimateGas': call_eth_tester('estimate_gas'), # TODO: untested
'getBlockByHash': null_if_block_not_found(call_eth_tester('get_block_by_hash')),
'getBlockByNumber': null_if_block_not_found(call_eth_tester('get_block_by_number')),
'getTransactionByHash': null_if_transaction_not_found(
call_eth_tester('get_transaction_by_hash')
'call': compose(
call_eth_tester('call'), # TODO: untested
apply_formatters_to_args(
transaction_params_transformer,
apply_formatter_if(is_not_named_block, to_integer_if_hex))
),
'estimateGas': compose(
call_eth_tester('estimate_gas'), # TODO: untested
apply_formatters_to_args(transaction_params_transformer)
),
'getBlockByHash': null_if_block_not_found(compose(
apply_formatter_if(
is_dict,
block_key_remapper,
),
call_eth_tester('get_block_by_hash'))),
'getBlockByNumber': compose(
apply_formatter_if(
is_dict,
block_key_remapper,
),
null_if_block_not_found(call_eth_tester('get_block_by_number')),
apply_formatters_to_args(
apply_formatter_if(is_not_named_block, to_integer_if_hex))),
'getTransactionByHash': null_if_transaction_not_found(compose(
apply_formatter_if(
is_dict,
compose(transaction_key_remapper, transaction_formatter),
),
call_eth_tester('get_transaction_by_hash'))
),
'getTransactionByBlockHashAndIndex': compose(
get_transaction_by_block_hash_and_index,
apply_formatters_to_args(identity, to_integer_if_hex)),
'getTransactionByBlockNumberAndIndex': compose(
get_transaction_by_block_number_and_index,
apply_formatters_to_args(
apply_formatter_if(is_not_named_block, to_integer_if_hex),
to_integer_if_hex
)
),
'getTransactionByBlockHashAndIndex': get_transaction_by_block_hash_and_index,
'getTransactionByBlockNumberAndIndex': get_transaction_by_block_number_and_index,
'getTransactionReceipt': null_if_transaction_not_found(compose(
apply_formatter_if(
is_dict,
compose(receipt_key_remapper, receipt_formatter)),
apply_formatter_if(
compose(is_null, operator.itemgetter('block_number')),
static_return(None),
Expand All @@ -225,20 +426,48 @@ def personal_send_transaction(eth_tester, params):
'compileLLL': not_implemented,
'compileSolidity': not_implemented,
'compileSerpent': not_implemented,
'newFilter': create_log_filter,
'newBlockFilter': call_eth_tester('create_block_filter'),
'newPendingTransactionFilter': call_eth_tester('create_pending_transaction_filter'),
'uninstallFilter': excepts(
FilterNotFound,
compose(
is_null,
call_eth_tester('delete_filter'),
'newFilter': compose(
integer_to_hex,
create_log_filter,
apply_formatters_to_args(filter_params_transformer)
),
'newBlockFilter': compose(integer_to_hex, call_eth_tester('create_block_filter')),
'newPendingTransactionFilter': compose(
integer_to_hex,
call_eth_tester('create_pending_transaction_filter')),
'uninstallFilter': compose(
excepts(
FilterNotFound,
compose(
is_null,
call_eth_tester('delete_filter'),
),
static_return(False),
),
static_return(False),
apply_formatters_to_args(hex_to_integer),
),
'getFilterChanges': compose(
apply_formatter_if(
is_array_of_dicts,
apply_formatter_to_array(log_key_remapper)),
null_if_filter_not_found(call_eth_tester('get_only_filter_changes')),
apply_formatters_to_args(hex_to_integer)
),
'getFilterLogs': compose(
apply_formatter_if(
is_array_of_dicts,
apply_formatter_to_array(log_key_remapper)),
null_if_filter_not_found(call_eth_tester('get_all_filter_logs')),
apply_formatters_to_args(hex_to_integer),
),
'getLogs': compose(
apply_formatter_if(
is_array_of_dicts,
apply_formatter_to_array(log_key_remapper)),
get_logs,
apply_formatters_to_args(
filter_params_transformer)
),
'getFilterChanges': null_if_filter_not_found(call_eth_tester('get_only_filter_changes')),
'getFilterLogs': null_if_filter_not_found(call_eth_tester('get_all_filter_logs')),
'getLogs': get_logs,
'getWork': not_implemented,
'submitWork': not_implemented,
'submitHashrate': not_implemented,
Expand Down Expand Up @@ -323,7 +552,10 @@ def personal_send_transaction(eth_tester, params):
compose(static_return(True), call_eth_tester('unlock_account')),
static_return(False),
),
'sendTransaction': personal_send_transaction,
'sendTransaction': compose(
personal_send_transaction,
apply_formatters_to_args(transaction_params_transformer)
),
'sign': not_implemented,
},
'testing': {
Expand All @@ -336,7 +568,10 @@ def personal_send_transaction(eth_tester, params):
},
'evm': {
'mine': call_eth_tester('mine_blocks'),
'revert': call_eth_tester('revert_to_snapshot'),
'snapshot': call_eth_tester('take_snapshot'),
'revert': compose(
call_eth_tester('revert_to_snapshot'),
apply_formatters_to_args(hex_to_integer)
),
'snapshot': compose(integer_to_hex, call_eth_tester('take_snapshot')),
},
}
4 changes: 1 addition & 3 deletions web3/providers/eth_tester/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

from .middleware import (
default_transaction_fields_middleware,
ethereum_tester_middleware,
)


class EthereumTesterProvider(BaseProvider):
middlewares = [
default_transaction_fields_middleware,
ethereum_tester_middleware,
]
]
ethereum_tester = None
api_endpoints = None

Expand Down
Loading

0 comments on commit 3647a50

Please sign in to comment.