Esempio n. 1
0
def test_apply_one_of_formatters(condition_formatters, value, expected):
    if isinstance(expected, type) and issubclass(expected, Exception):
        with pytest.raises(expected):
            apply_one_of_formatters(condition_formatters, value)
        with pytest.raises(expected):
            eth_utils.apply_one_of_formatters(condition_formatters, value)
    else:
        assert eth_utils.apply_one_of_formatters(condition_formatters, value) == expected

        # must be able to curry
        apply_one = apply_one_of_formatters(condition_formatters)
        assert apply_one(value) == expected
Esempio n. 2
0
    'sha3Uncles':
    apply_formatter_if(is_not_null, to_hexbytes(32)),
    'uncles':
    apply_list_to_array_formatter(to_hexbytes(32)),
    'difficulty':
    to_integer_if_hex,
    'receiptsRoot':
    apply_formatter_if(is_not_null, to_hexbytes(32)),
    'stateRoot':
    apply_formatter_if(is_not_null, to_hexbytes(32)),
    'totalDifficulty':
    to_integer_if_hex,
    'transactions':
    apply_one_of_formatters((
        (is_array_of_dicts,
         apply_list_to_array_formatter(transaction_result_formatter)),
        (is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
    )),
    'transactionsRoot':
    apply_formatter_if(is_not_null, to_hexbytes(32)),
}

block_formatter = apply_formatters_to_dict(BLOCK_FORMATTERS)

SYNCING_FORMATTERS = {
    'startingBlock': to_integer_if_hex,
    'currentBlock': to_integer_if_hex,
    'highestBlock': to_integer_if_hex,
    'knownStates': to_integer_if_hex,
    'pulledStates': to_integer_if_hex,
}
Esempio n. 3
0
    identity,
    partial,
)

from .common import (
    normalize_if,
    normalize_dict,
    normalize_array,
)
from ..utils.encoding import int_to_32byte_big_endian

normalize_account = to_checksum_address
normalize_account_list = partial(normalize_array, normalizer=normalize_account)

to_empty_or_checksum_address = apply_one_of_formatters((
    (lambda addr: addr == b'', lambda addr: ''),
    (is_canonical_address, to_checksum_address),
))


def _normalize_outbound_access_list(access_list):
    return tuple([{
        'address':
        to_checksum_address(entry[0]),
        'storage_keys':
        tuple([encode_hex(int_to_32byte_big_endian(k)) for k in entry[1]])
    } for entry in access_list])


TRANSACTION_NORMALIZERS = {
    "type":
    identity,
Esempio n. 4
0
    elif is_valid_topic_array(topics):
        yield tuple(
            normalize_topic_list(item)
            if is_list_like(item) else normalize_topic(item)
            for item in topics)
    else:
        raise TypeError(
            "Topics are not in a recognized format: {0}".format(address))


def normalize_private_key(value):
    return decode_hex(value)


to_empty_or_canonical_address = apply_one_of_formatters((
    (lambda addr: addr == '', lambda addr: b''),
    (is_hex, to_canonical_address),
))

TRANSACTION_NORMALIZERS = {
    'from': to_canonical_address,
    'to': to_empty_or_canonical_address,
    'gas': identity,
    'gas_price': identity,
    'nonce': identity,
    'value': identity,
    'data': decode_hex,
    'r': identity,
    's': identity,
    'v': identity,
}
Esempio n. 5
0
    'to': b'',
    'value': 0,
    'data': b'',
}

TRANSACTION_FORMATTERS = {
    'nonce':
    hexstr_if_str(to_int),
    'gasPrice':
    hexstr_if_str(to_int),
    'gas':
    hexstr_if_str(to_int),
    'to':
    apply_one_of_formatters((
        (is_string, hexstr_if_str(to_bytes)),
        (is_bytes, identity),
        (is_none, lambda val: b''),
    )),
    'value':
    hexstr_if_str(to_int),
    'data':
    hexstr_if_str(to_bytes),
    'v':
    hexstr_if_str(to_int),
    'r':
    hexstr_if_str(to_int),
    's':
    hexstr_if_str(to_int),
}

TRANSACTION_VALID_VALUES = {
Esempio n. 6
0
from typing import List

from eth_utils import is_list_like, is_string
from eth_utils.curried import apply_formatter_if, apply_one_of_formatters


def i_put_my_thing_down_flip_it_and_reverse_it(lyric: List[str]) -> str:
    return "".join(reversed(lyric))


CONDITION_FORMATTER_PAIRS = (
    (is_list_like, tuple),
    (is_string, i_put_my_thing_down_flip_it_and_reverse_it),
)


apply_formatter_if(is_string)
apply_formatter_if(is_string, bool)
apply_formatter_if(is_string, bool, 1)


apply_one_of_formatters(CONDITION_FORMATTER_PAIRS)
apply_one_of_formatters(CONDITION_FORMATTER_PAIRS, "my thing")
Esempio n. 7
0
    'timestamp': to_integer_if_hex,
    'hash': apply_formatter_if(is_not_null, to_hexbytes(32)),
    'logsBloom': apply_formatter_if(is_not_null, to_hexbytes(256)),
    'miner': apply_formatter_if(is_not_null, to_checksum_address),
    'mixHash': to_hexbytes(32),
    'nonce': apply_formatter_if(is_not_null, to_hexbytes(8, variable_length=True)),
    '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_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((
        (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),
}


block_formatter = apply_formatters_to_dict(BLOCK_FORMATTERS)


SYNCING_FORMATTERS = {
    'startingBlock': to_integer_if_hex,
    'currentBlock': to_integer_if_hex,
    'highestBlock': to_integer_if_hex,
    'knownStates': to_integer_if_hex,
    'pulledStates': to_integer_if_hex,
}
Esempio n. 8
0
from .validation import (
    LEGACY_TRANSACTION_FORMATTERS,
    LEGACY_TRANSACTION_VALID_VALUES,
    is_int_or_prefixed_hexstr,
    is_rpc_structured_access_list,
)

TYPED_TRANSACTION_FORMATTERS = merge(
    LEGACY_TRANSACTION_FORMATTERS, {
        'chainId': hexstr_if_str(to_int),
        'type': hexstr_if_str(to_int),
        'accessList': apply_formatter_to_array(
            apply_formatters_to_dict(
                {
                    "address": apply_one_of_formatters((
                        (is_string, hexstr_if_str(to_bytes)),
                        (is_bytes, identity),
                    )),
                    "storageKeys": apply_formatter_to_array(hexstr_if_str(to_int))
                }
            ),
        ),
        'maxPriorityFeePerGas': hexstr_if_str(to_int),
        'maxFeePerGas': hexstr_if_str(to_int),
    },
)

# Define typed transaction common sedes.
# [[{20 bytes}, [{32 bytes}...]]...], where ... means “zero or more of the thing to the left”.
access_list_sede_type = CountableList(
    List([
        Binary.fixed_length(20, allow_empty=False),