Skip to content

Commit

Permalink
Use formatting methods from eth-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kclowes committed Nov 4, 2019
1 parent 6c0256a commit e086cf2
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 91 deletions.
1 change: 1 addition & 0 deletions newsfragments/1479.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use formatting functions from eth-utils instead of web3
5 changes: 4 additions & 1 deletion tests/core/utilities/test_formatters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

import pytest

from web3._utils.formatters import (
from eth_utils.curried import (
apply_formatters_to_dict,
)

from web3._utils.formatters import (
map_collection,
recursive_map,
)
Expand Down
7 changes: 4 additions & 3 deletions web3/_utils/datatypes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from eth_utils import (
apply_formatters_to_dict,
)
from eth_utils.toolz import (
concat,
)

import web3._utils.formatters


def verify_attr(class_name, key, namespace):
if key not in namespace:
Expand All @@ -28,7 +29,7 @@ def __new__(mcs, name, bases, namespace, normalizers=None):
verify_attr(name, key, all_keys)

if normalizers:
processed_namespace = web3._utils.formatters.apply_formatters_to_dict(
processed_namespace = apply_formatters_to_dict(
normalizers,
namespace,
)
Expand Down
6 changes: 3 additions & 3 deletions web3/_utils/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
to_hex,
to_tuple,
)
from eth_utils.curried import (
apply_formatter_if,
)
from eth_utils.toolz import (
complement,
compose,
Expand All @@ -34,9 +37,6 @@
hexstr_if_str,
to_bytes,
)
from web3._utils.formatters import (
apply_formatter_if,
)
from web3._utils.normalizers import (
BASE_RETURN_NORMALIZERS,
)
Expand Down
6 changes: 3 additions & 3 deletions web3/_utils/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
is_string,
is_text,
)
from eth_utils.curried import (
apply_formatter_if,
)
from eth_utils.toolz import (
complement,
curry,
Expand All @@ -14,9 +17,6 @@
HexBytes,
)

from web3._utils.formatters import (
apply_formatter_if,
)
from web3._utils.threads import (
TimerClass,
)
Expand Down
48 changes: 3 additions & 45 deletions web3/_utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
to_dict,
to_list,
)
from eth_utils.curried import (
apply_formatter_at_index,
)
from eth_utils.toolz import (
compose,
curry,
Expand All @@ -28,21 +31,6 @@ def hex_to_integer(value):
integer_to_hex = hex


@curry
@to_list
def apply_formatter_at_index(formatter, at_index, value):
if at_index + 1 > len(value):
raise IndexError(
"Not enough values in iterable to apply formatter. Got: {0}. "
"Need: {1}".format(len(value), at_index + 1)
)
for index, item in enumerate(value):
if index == at_index:
yield formatter(item)
else:
yield item


def apply_formatters_to_args(*formatters):
return compose(*(
apply_formatter_at_index(formatter, index)
Expand All @@ -51,43 +39,13 @@ def apply_formatters_to_args(*formatters):
))


@curry
def apply_formatter_if(condition, formatter, value):
if condition(value):
return formatter(value)
else:
return value


@curry
@to_dict
def apply_formatters_to_dict(formatters, value):
for key, item in value.items():
if key in formatters:
try:
yield key, formatters[key](item)
except (TypeError, ValueError) as exc:
raise type(exc)("Could not format value %r as field %r" % (item, key)) from exc
else:
yield key, item


@curry
@to_list
def apply_formatter_to_array(formatter, value):
for item in value:
yield formatter(item)


@curry
def apply_one_of_formatters(formatter_condition_pairs, value):
for formatter, condition in formatter_condition_pairs:
if condition(value):
return formatter(value)
else:
raise ValueError("The provided value did not satisfy any of the formatter conditions")


def map_collection(func, collection):
"""
Apply func to each element of a collection, or value of a dictionary.
Expand Down
6 changes: 3 additions & 3 deletions web3/_utils/rpc_abi.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from eth_utils import (
to_dict,
)
from eth_utils.curried import (
apply_formatter_at_index,
)
from eth_utils.toolz import (
curry,
)

from web3._utils.abi import (
map_abi_data,
)
from web3._utils.formatters import (
apply_formatter_at_index,
)

TRANSACTION_PARAMS_ABIS = {
'data': 'bytes',
Expand Down
6 changes: 3 additions & 3 deletions web3/_utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
is_list_like,
is_string,
)
from eth_utils.curried import (
apply_formatter_to_array,
)
from eth_utils.hexadecimal import (
encode_hex,
)
Expand All @@ -37,9 +40,6 @@
length_of_array_type,
sub_type_of_array_type,
)
from web3._utils.formatters import (
apply_formatter_to_array,
)
from web3.exceptions import (
InvalidAddress,
)
Expand Down
3 changes: 1 addition & 2 deletions web3/middleware/normalize_request_parameters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from eth_utils import (
is_string,
)

from web3._utils.formatters import (
from eth_utils.curried import (
apply_formatter_at_index,
apply_formatter_if,
apply_formatters_to_dict,
Expand Down
45 changes: 26 additions & 19 deletions web3/middleware/pythonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import operator

from eth_utils.curried import (
apply_formatter_at_index,
apply_formatter_if,
apply_formatters_to_dict,
apply_formatters_to_sequence,
apply_one_of_formatters,
is_address,
is_bytes,
is_integer,
Expand All @@ -11,6 +15,7 @@
remove_0x_prefix,
text_if_str,
to_checksum_address,
to_list,
)
from eth_utils.toolz import (
complement,
Expand All @@ -31,11 +36,7 @@
to_hex,
)
from web3._utils.formatters import (
apply_formatter_at_index,
apply_formatter_if,
apply_formatter_to_array,
apply_formatters_to_dict,
apply_one_of_formatters,
hex_to_integer,
integer_to_hex,
is_array_of_dicts,
Expand Down Expand Up @@ -128,14 +129,18 @@ def to_hexbytes(num_bytes, val, variable_length=False):
whisper_log_formatter = apply_formatters_to_dict(WHISPER_LOG_FORMATTERS)


def apply_list_to_array_formatter(formatter):
return to_list(apply_formatter_to_array(formatter))


LOG_ENTRY_FORMATTERS = {
'blockHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
'blockNumber': apply_formatter_if(is_not_null, to_integer_if_hex),
'transactionIndex': apply_formatter_if(is_not_null, to_integer_if_hex),
'transactionHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
'logIndex': to_integer_if_hex,
'address': to_checksum_address,
'topics': apply_formatter_to_array(to_hexbytes(32)),
'topics': apply_list_to_array_formatter(to_hexbytes(32)),
'data': to_ascii_if_bytes,
}

Expand All @@ -152,7 +157,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
'status': to_integer_if_hex,
'gasUsed': to_integer_if_hex,
'contractAddress': apply_formatter_if(is_not_null, to_checksum_address),
'logs': apply_formatter_to_array(log_entry_formatter),
'logs': apply_list_to_array_formatter(log_entry_formatter),
'logsBloom': to_hexbytes(256),
}

Expand All @@ -173,14 +178,14 @@ def to_hexbytes(num_bytes, val, variable_length=False):
'number': apply_formatter_if(is_not_null, to_integer_if_hex),
'parentHash': apply_formatter_if(is_not_null, to_hexbytes(32)),
'sha3Uncles': apply_formatter_if(is_not_null, to_hexbytes(32)),
'uncles': apply_formatter_to_array(to_hexbytes(32)),
'uncles': apply_list_to_array_formatter(to_hexbytes(32)),
'difficulty': to_integer_if_hex,
'receiptsRoot': to_hexbytes(32),
'stateRoot': to_hexbytes(32),
'totalDifficulty': to_integer_if_hex,
'transactions': apply_one_of_formatters((
(apply_formatter_to_array(transaction_formatter), is_array_of_dicts),
(apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings),
(is_array_of_dicts, apply_list_to_array_formatter(transaction_formatter)),
(is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
)),
'transactionsRoot': to_hexbytes(32),
}
Expand All @@ -192,17 +197,19 @@ def to_hexbytes(num_bytes, val, variable_length=False):
STORAGE_PROOF_FORMATTERS = {
'key': HexBytes,
'value': HexBytes,
'proof': apply_formatter_to_array(HexBytes),
'proof': apply_list_to_array_formatter(HexBytes),
}

ACCOUNT_PROOF_FORMATTERS = {
'address': to_checksum_address,
'accountProof': apply_formatter_to_array(HexBytes),
'accountProof': apply_list_to_array_formatter(HexBytes),
'balance': to_integer_if_hex,
'codeHash': to_hexbytes(32),
'nonce': to_integer_if_hex,
'storageHash': to_hexbytes(32),
'storageProof': apply_formatter_to_array(apply_formatters_to_dict(STORAGE_PROOF_FORMATTERS))
'storageProof': apply_list_to_array_formatter(
apply_formatters_to_dict(STORAGE_PROOF_FORMATTERS)
)
}

proof_formatter = apply_formatters_to_dict(ACCOUNT_PROOF_FORMATTERS)
Expand Down Expand Up @@ -258,8 +265,8 @@ def to_hexbytes(num_bytes, val, variable_length=False):


filter_result_formatter = apply_one_of_formatters((
(apply_formatter_to_array(log_entry_formatter), is_array_of_dicts),
(apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings),
(is_array_of_dicts, apply_list_to_array_formatter(log_entry_formatter)),
(is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
))


Expand Down Expand Up @@ -305,8 +312,8 @@ def to_hexbytes(num_bytes, val, variable_length=False):
block_number_formatter,
]),
'eth_estimateGas': apply_one_of_formatters((
(estimate_gas_without_block_id, is_length(1)),
(estimate_gas_with_block_id, is_length(2)),
(is_length(1), estimate_gas_without_block_id),
(is_length(2), estimate_gas_with_block_id),
)),
'eth_sendTransaction': apply_formatter_at_index(transaction_param_formatter, 0),
# personal
Expand All @@ -328,7 +335,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
},
result_formatters={
# Eth
'eth_accounts': apply_formatter_to_array(to_checksum_address),
'eth_accounts': apply_list_to_array_formatter(to_checksum_address),
'eth_blockNumber': to_integer_if_hex,
'eth_chainId': to_integer_if_hex,
'eth_coinbase': to_checksum_address,
Expand Down Expand Up @@ -374,12 +381,12 @@ def to_hexbytes(num_bytes, val, variable_length=False):
'eth_syncing': apply_formatter_if(is_not_false, syncing_formatter),
# personal
'personal_importRawKey': to_checksum_address,
'personal_listAccounts': apply_formatter_to_array(to_checksum_address),
'personal_listAccounts': apply_list_to_array_formatter(to_checksum_address),
'personal_newAccount': to_checksum_address,
'personal_signTypedData': HexBytes,
'personal_sendTransaction': to_hexbytes(32),
# SHH
'shh_getFilterMessages': apply_formatter_to_array(whisper_log_formatter),
'shh_getFilterMessages': apply_list_to_array_formatter(whisper_log_formatter),
# Transaction Pool
'txpool_content': transaction_pool_content_formatter,
'txpool_inspect': transaction_pool_inspect_formatter,
Expand Down
6 changes: 3 additions & 3 deletions web3/middleware/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
from eth_utils import (
to_dict,
)
from eth_utils.curried import (
apply_formatter_if,
)
from eth_utils.toolz import (
compose,
)

from web3._utils.formatters import (
apply_formatter_if,
)
from web3._utils.rpc_abi import (
TRANSACTION_PARAMS_ABIS,
apply_abi_formatters_to_dict,
Expand Down
7 changes: 3 additions & 4 deletions web3/providers/eth_tester/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
is_null,
keccak,
)
from eth_utils.curried import (
apply_formatter_if,
)
from eth_utils.toolz import (
compose,
curry,
excepts,
)

from web3._utils.formatters import (
apply_formatter_if,
)


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

0 comments on commit e086cf2

Please sign in to comment.