|
| 1 | +import pytest |
| 2 | + |
1 | 3 | from eth_abi.codec import (
|
2 | 4 | ABICodec,
|
3 | 5 | )
|
|
11 | 13 | foldable_namedtuple,
|
12 | 14 | named_tree,
|
13 | 15 | )
|
| 16 | +from web3.exceptions import MismatchedABI |
14 | 17 |
|
15 | 18 | from .test_abi import (
|
16 | 19 | TEST_FUNCTION_ABI,
|
17 | 20 | )
|
18 | 21 |
|
19 |
| -abi = TEST_FUNCTION_ABI["inputs"] |
20 |
| -inputs = ( |
| 22 | +full_abi_inputs = TEST_FUNCTION_ABI["inputs"] |
| 23 | +full_values = ( |
21 | 24 | (1, [2, 3, 4], [(5, 6), (7, 8), (9, 10)]), # Value for s
|
22 | 25 | (11, 12), # Value for t
|
23 | 26 | 13, # Value for a
|
24 | 27 | [[(14, 15), (16, 17)], [(18, 19)]], # Value for b
|
25 | 28 | )
|
26 | 29 |
|
27 |
| -sample_abi_inputs = { |
28 |
| - "inputs": [ |
29 |
| - { |
30 |
| - "components": [ |
31 |
| - {"name": "a", "type": "uint256"}, |
32 |
| - {"name": "b", "type": "uint256[]"}, |
33 |
| - { |
34 |
| - "components": [ |
35 |
| - {"name": "x", "type": "uint256"}, |
36 |
| - {"name": "y", "type": "uint256"}, |
37 |
| - ], |
38 |
| - "name": "c", |
39 |
| - "type": "tuple[]", |
40 |
| - }, |
41 |
| - ], |
42 |
| - "name": "s", |
43 |
| - "type": "tuple", |
44 |
| - }, |
45 |
| - { |
46 |
| - "components": [ |
47 |
| - {"name": "x", "type": "uint256"}, |
48 |
| - {"name": "y", "type": "uint256"}, |
49 |
| - ], |
50 |
| - "name": "t", |
51 |
| - "type": "tuple", |
52 |
| - }, |
53 |
| - {"name": "a", "type": "uint256"}, |
54 |
| - { |
55 |
| - "components": [ |
56 |
| - {"name": "x", "type": "uint256"}, |
57 |
| - {"name": "y", "type": "uint256"}, |
58 |
| - ], |
59 |
| - "name": "b", |
60 |
| - "type": "tuple[][]", |
61 |
| - }, |
62 |
| - ] |
63 |
| -} |
64 | 30 |
|
65 | 31 | def test_named_arguments_decode():
|
66 |
| - decoded = named_tree(abi, inputs) |
| 32 | + decoded = named_tree(full_abi_inputs, full_values) |
67 | 33 | data = dict_to_namedtuple(decoded)
|
68 |
| - assert data == inputs |
| 34 | + assert data == full_values |
69 | 35 | assert data.s.c[2].y == 10
|
70 | 36 | assert data.t.x == 11
|
71 | 37 | assert data.a == 13
|
72 | 38 |
|
73 | 39 |
|
| 40 | +short_abi_inputs_with_disallowed_names = [ |
| 41 | + { |
| 42 | + "components": [ |
| 43 | + {"name": "from", "type": "uint256"}, |
| 44 | + {"name": "to", "type": "uint256[]"}, |
| 45 | + { |
| 46 | + "components": [ |
| 47 | + {"name": "_x", "type": "uint256"}, |
| 48 | + {"name": "_y", "type": "uint256"}, |
| 49 | + ], |
| 50 | + "name": "c", |
| 51 | + "type": "tuple[]", |
| 52 | + }, |
| 53 | + ], |
| 54 | + "name": "s", |
| 55 | + "type": "tuple", |
| 56 | + }, |
| 57 | +] |
| 58 | + |
| 59 | +short_values = ((1, [2, 3, 4], [(5, 6), (7, 8), (9, 10)]),) |
| 60 | + |
| 61 | + |
| 62 | +def test_named_arguments_decode_rename(): |
| 63 | + decoded = named_tree(short_abi_inputs_with_disallowed_names, short_values) |
| 64 | + data = dict_to_namedtuple(decoded) |
| 65 | + assert data == short_values |
| 66 | + assert data._fields == ("s",) |
| 67 | + |
| 68 | + # python keyword "from" renamed to "_0" |
| 69 | + assert data.s._fields == ("_0", "to", "c") |
| 70 | + |
| 71 | + # field names starting with "_" - "_x" and "_y" - renamed to "_0" and "_1" |
| 72 | + assert data.s.c[0]._fields == ("_0", "_1") |
| 73 | + assert data.s.c[2]._1 == 10 |
| 74 | + assert data.s.to[1] == 3 |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize( |
| 78 | + "values", |
| 79 | + ( |
| 80 | + ((1, [2, 3, 4], [(5,), (7, 8), (9, 10)]),), |
| 81 | + ((1, [2, 3, 4], [(5, 6, 11), (7, 8), (9, 10)]),), |
| 82 | + ((1, [(5, 6), (7, 8), (9, 10)]),), |
| 83 | + ), |
| 84 | +) |
| 85 | +def test_named_arguments_decode_with_misshapen_inputs(values): |
| 86 | + with pytest.raises(MismatchedABI): |
| 87 | + named_tree(short_abi_inputs_with_disallowed_names, values) |
| 88 | + |
| 89 | + |
74 | 90 | def test_namedtuples_encodable():
|
75 | 91 | registry = default_registry.copy()
|
76 | 92 | codec = ABICodec(registry)
|
77 |
| - kwargs = named_tree(abi, inputs) |
| 93 | + kwargs = named_tree(full_abi_inputs, full_values) |
78 | 94 | args = dict_to_namedtuple(kwargs)
|
79 | 95 | assert check_if_arguments_can_be_encoded(TEST_FUNCTION_ABI, codec, (), kwargs)
|
80 | 96 | assert check_if_arguments_can_be_encoded(TEST_FUNCTION_ABI, codec, args, {})
|
|
0 commit comments