示例#1
0
def test_format_dict_error():
    with pytest.raises(ValueError) as exc_info:
        apply_formatters_to_dict(
            {'myfield': int},
            {'myfield': 'a'},
        )
    with pytest.raises(ValueError) as exc_info:
        eth_utils.apply_formatters_to_dict(
            {'myfield': int},
            {'myfield': 'a'},
        )
    assert 'myfield' in str(exc_info.value)
示例#2
0
文件: common.py 项目: ArtObr/indy-scp
def _expect(post_state: Dict[str, Any], networks: Any,
            transaction: TransactionDict, filler: Dict[str,
                                                       Any]) -> Dict[str, Any]:

    test_name = get_test_name(filler)
    test = filler[test_name]
    test_update: Dict[str, Dict[Any, Any]] = {test_name: {}}

    pre_state = test.get("pre", {})
    post_state = normalize_state(post_state or {})
    defaults = {
        address: {
            "balance": 0,
            "nonce": 0,
            "code": b"",
            "storage": {},
        }
        for address in post_state
    }
    result = deep_merge(defaults, pre_state, normalize_state(post_state))
    new_expect = {"result": result}

    if transaction is not None:
        transaction = normalize_transaction(
            merge(get_default_transaction(networks), transaction))
        if "transaction" not in test:
            transaction_group = apply_formatters_to_dict(
                {
                    "data": wrap_in_list,
                    "gasLimit": wrap_in_list,
                    "value": wrap_in_list,
                }, transaction)
            indexes = {
                index_key: 0
                for transaction_key, index_key in [
                    ("gasLimit", "gas"),
                    ("value", "value"),
                    ("data", "data"),
                ] if transaction_key in transaction_group
            }
        else:
            transaction_group, indexes = add_transaction_to_group(
                test["transaction"], transaction)
        new_expect = assoc(new_expect, "indexes", indexes)
        test_update = assoc_in(test_update, [test_name, "transaction"],
                               transaction_group)

    if networks is not None:
        networks = normalize_networks(networks)
        new_expect = assoc(new_expect, "networks", networks)

    existing_expects = test.get("expect", [])
    expect = existing_expects + [new_expect]
    test_update = assoc_in(test_update, [test_name, "expect"], expect)

    return deep_merge(filler, test_update)
示例#3
0
    def normalizer(d: Dict[Any, Any]) -> Dict[str, Any]:
        keys = set(d.keys())
        missing_keys = required_set_form - keys
        superfluous_keys = keys - all_keys
        if missing_keys:
            raise KeyError("Missing required keys: {}".format(", ".join(missing_keys)))
        if superfluous_keys:
            raise KeyError("Superfluous keys: {}".format(", ".join(superfluous_keys)))

        return apply_formatters_to_dict(formatters, d)
示例#4
0
 def sign_transaction(self, transaction_dict: dict) -> HexBytes:
     formatters = {
         'nonce': Web3.toHex,
         'gasPrice': Web3.toHex,
         'gas': Web3.toHex,
         'value': Web3.toHex,
         'chainId': Web3.toHex,
         'from': to_checksum_address
     }
     transaction_dict = apply_formatters_to_dict(formatters,
                                                 transaction_dict)
     signed = self.__ipc_request("account_signTransaction",
                                 transaction_dict)
     return HexBytes(signed.raw)
示例#5
0
    def __new__(mcs, name, bases, namespace, normalizers=None):
        all_bases = set(concat(base.__mro__ for base in bases))
        all_keys = set(concat(base.__dict__.keys() for base in all_bases))

        for key in namespace:
            verify_attr(name, key, all_keys)

        if normalizers:
            processed_namespace = apply_formatters_to_dict(
                normalizers,
                namespace,
            )
        else:
            processed_namespace = namespace

        return super().__new__(mcs, name, bases, processed_namespace)
示例#6
0
    def sign_transaction(self, transaction_dict: dict) -> HexBytes:
        formatters = {
            'nonce': Web3.toHex,
            'gasPrice': Web3.toHex,
            'gas': Web3.toHex,
            'value': Web3.toHex,
            'chainId': Web3.toHex,
            'from': to_checksum_address
        }

        # Workaround for contract creation TXs
        if transaction_dict['to'] == b'':
            transaction_dict['to'] = None
        elif transaction_dict['to']:
            formatters['to'] = to_checksum_address

        formatted_transaction = apply_formatters_to_dict(
            formatters, transaction_dict)
        signed = self.__ipc_request("account_signTransaction",
                                    formatted_transaction)
        return HexBytes(signed.raw)
示例#7
0
    def __new__(  # type: ignore
        mcs,
        name: str,
        bases: Tuple[type],
        namespace: Dict[str, Any],
        normalizers: Optional[Dict[str, Any]] = None
    ) -> Type['PropertyCheckingFactory']:
        all_bases = set(concat(base.__mro__ for base in bases))
        all_keys = set(concat(base.__dict__.keys() for base in all_bases))

        for key in namespace:
            verify_attr(name, key, all_keys)

        if normalizers:
            processed_namespace = apply_formatters_to_dict(
                normalizers,
                namespace,
            )
        else:
            processed_namespace = namespace

        return super().__new__(mcs, name, bases, processed_namespace)
示例#8
0
def test_apply_formatters_to_dict(formatter, value, expected):
    assert eth_utils.apply_formatters_to_dict(formatter, value) == expected

    mapper = apply_formatters_to_dict(formatter)
    assert mapper(value) == expected
示例#9
0
文件: format.py 项目: vardan10/py-evm
def normalize_transaction_dict(
        transaction_dict: Dict[str, str]) -> Dict[str, Any]:
    normalized_dict = apply_formatters_to_dict(TRANSACTION_NORMALIZER,
                                               transaction_dict)
    return merge(SAFE_TRANSACTION_DEFAULTS, normalized_dict)
示例#10
0
def test_format_dict_error():
    with pytest.raises(ValueError) as exc_info:
        apply_formatters_to_dict({"myfield": int}, {"myfield": "a"})
    with pytest.raises(ValueError) as exc_info:
        eth_utils.apply_formatters_to_dict({"myfield": int}, {"myfield": "a"})
    assert "myfield" in str(exc_info.value)