Skip to content

Commit 2cf7c34

Browse files
committed
normalize function decoder output and update tests
1 parent d5233ad commit 2cf7c34

File tree

2 files changed

+23
-44
lines changed

2 files changed

+23
-44
lines changed

tests/core/contracts/test_contract_method_abi_decoding.py

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import json
55
import pytest
66

7-
ABI_A = json.loads('[{"constant":false,"inputs":[],"name":"a","outputs":[],"type":"function"}]')
8-
ABI_B = json.loads('[{"constant":false,"inputs":[{"name":"","type":"uint256"}],"name":"a","outputs":[],"type":"function"}]') # noqa: E501
9-
ABI_C = json.loads('[{"constant":false,"inputs":[],"name":"a","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"","type":"bytes32"}],"name":"a","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"","type":"uint256"}],"name":"a","outputs":[],"type":"function"}]') # noqa: E501
7+
ABI_A = json.loads('[{"constant":false,"inputs":[],"name":"noargfunc","outputs":[],"type":"function"}]') # noqa: E501
8+
ABI_B = json.loads('[{"constant":false,"inputs":[{"name":"uintarg","type":"uint256"}],"name":"uintfunc","outputs":[],"type":"function"}]') # noqa: E501
9+
ABI_C = json.loads('[{"constant":false,"inputs":[],"name":"namesakefunc","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"bytesarg","type":"bytes32"}],"name":"namesakefunc","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"uintarg","type":"uint256"}],"name":"namesakefunc","outputs":[],"type":"function"}]') # noqa: E501
1010
ABI_D = json.loads('[{ "constant": false, "inputs": [ { "name": "b", "type": "bytes32[]" } ], "name": "byte_array", "outputs": [], "payable": false, "type": "function" }]') # noqa: E501
1111
ABI_BYTES = json.loads('[{"constant":false,"inputs":[{"name":"bytesarg","type":"bytes"}],"name":"bytesfunc","outputs":[],"type":"function"}]') # noqa: E501
1212
ABI_STRING = json.loads('[{"constant":false,"inputs":[{"name":"stringarg","type":"string"}],"name":"stringfunc","outputs":[],"type":"function"}]') # noqa: E501
@@ -15,81 +15,59 @@
1515

1616

1717
@pytest.mark.parametrize(
18-
'abi,data,method,arguments,expected',
18+
'abi,data,method,expected',
1919
(
2020
(
2121
ABI_A,
22-
'0x0dbe671f',
23-
'a',
24-
[],
22+
'0xc4c1a40b',
23+
'noargfunc',
2524
{},
2625
),
2726
(
2827
ABI_B,
29-
'0xf0fdf8340000000000000000000000000000000000000000000000000000000000000001',
30-
'a',
31-
[1],
32-
{'': 1},
28+
'0xcc6820de0000000000000000000000000000000000000000000000000000000000000001',
29+
'uintfunc',
30+
{'uintarg': 1},
3331
),
3432
(
3533
ABI_C,
36-
'0xf0fdf8340000000000000000000000000000000000000000000000000000000000000001',
37-
'a',
38-
[1],
39-
{'': 1},
40-
),
41-
(
42-
ABI_C,
43-
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
44-
'a',
45-
[b'a'],
46-
{'': a32bytes},
47-
),
48-
(
49-
ABI_C,
50-
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
51-
'a',
52-
['0x61'],
53-
{'': a32bytes},
34+
'0x22d86fa3',
35+
'namesakefunc',
36+
{},
5437
),
5538
(
5639
ABI_C,
57-
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
58-
'a',
59-
['61'],
60-
{'': a32bytes},
40+
'0x40c05b2f0000000000000000000000000000000000000000000000000000000000000001',
41+
'namesakefunc',
42+
{'uintarg': 1},
6143
),
6244
(
6345
ABI_C,
64-
'0xf0fdf834000000000000000000000000000000000000000000000000000000000000007f',
65-
'a',
66-
[127],
67-
{'': 127},
46+
'0xf931d77c6100000000000000000000000000000000000000000000000000000000000000',
47+
'namesakefunc',
48+
{'bytesarg': a32bytes},
6849
),
6950
(
7051
ABI_BYTES,
7152
'0xb606a9f6000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000016100000000000000000000000000000000000000000000000000000000000000', # noqa: E501
7253
'bytesfunc',
73-
[],
7454
{'bytesarg': b'a'},
7555
),
7656
(
7757
ABI_STRING,
7858
'0x33b4005f000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000016100000000000000000000000000000000000000000000000000000000000000', # noqa: E501
7959
'stringfunc',
80-
[],
8160
{'stringarg': 'a'},
8261
),
8362
(
8463
ABI_ADDRESS,
8564
'0x4767be6c000000000000000000000000ffffffffffffffffffffffffffffffffffffffff',
8665
'addressfunc',
87-
[],
8866
{'addressarg': '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF'},
8967
),
9068
),
9169
)
92-
def test_contract_abi_decoding(web3, abi, data, method, arguments, expected):
70+
def test_contract_abi_decoding(web3, abi, data, method, expected):
9371
contract = web3.eth.contract(abi=abi)
9472
func, params = contract.decode_function_input(data)
9573
assert func.fn_name == method
@@ -107,10 +85,10 @@ def test_contract_abi_decoding(web3, abi, data, method, arguments, expected):
10785
ABI_D,
10886
'byte_array',
10987
{
110-
'b': (
88+
'b': [
11189
unhexlify('5595c210956e7721f9b692e702708556aa9aabb14ea163e96afa56ffbe9fa809'),
11290
unhexlify('6f8d2fa18448afbfe4f82143c384484ad09a0271f3a3c0eb9f629e703f883125'),
113-
),
91+
],
11492
},
11593
'0xf166d6f8000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000025595c210956e7721f9b692e702708556aa9aabb14ea163e96afa56ffbe9fa8096f8d2fa18448afbfe4f82143c384484ad09a0271f3a3c0eb9f629e703f883125', # noqa: E501
11694
),

web3/contract.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,8 @@ def decode_function_input(self, data):
699699
names = [x['name'] for x in func.abi['inputs']]
700700
types = [x['type'] for x in func.abi['inputs']]
701701
decoded = decode_abi(types, params)
702-
return func, dict(zip(names, decoded))
702+
normalized = map_abi_data(BASE_RETURN_NORMALIZERS, types, decoded)
703+
return func, dict(zip(names, normalized))
703704

704705
@combomethod
705706
def find_functions_by_args(self, *args):

0 commit comments

Comments
 (0)