Skip to content

Commit

Permalink
normalize function decoder output and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
banteg committed Aug 8, 2018
1 parent d5233ad commit 2cf7c34
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
64 changes: 21 additions & 43 deletions tests/core/contracts/test_contract_method_abi_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import json
import pytest

ABI_A = json.loads('[{"constant":false,"inputs":[],"name":"a","outputs":[],"type":"function"}]')
ABI_B = json.loads('[{"constant":false,"inputs":[{"name":"","type":"uint256"}],"name":"a","outputs":[],"type":"function"}]') # noqa: E501
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
ABI_A = json.loads('[{"constant":false,"inputs":[],"name":"noargfunc","outputs":[],"type":"function"}]') # noqa: E501
ABI_B = json.loads('[{"constant":false,"inputs":[{"name":"uintarg","type":"uint256"}],"name":"uintfunc","outputs":[],"type":"function"}]') # noqa: E501
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
ABI_D = json.loads('[{ "constant": false, "inputs": [ { "name": "b", "type": "bytes32[]" } ], "name": "byte_array", "outputs": [], "payable": false, "type": "function" }]') # noqa: E501
ABI_BYTES = json.loads('[{"constant":false,"inputs":[{"name":"bytesarg","type":"bytes"}],"name":"bytesfunc","outputs":[],"type":"function"}]') # noqa: E501
ABI_STRING = json.loads('[{"constant":false,"inputs":[{"name":"stringarg","type":"string"}],"name":"stringfunc","outputs":[],"type":"function"}]') # noqa: E501
Expand All @@ -15,81 +15,59 @@


@pytest.mark.parametrize(
'abi,data,method,arguments,expected',
'abi,data,method,expected',
(
(
ABI_A,
'0x0dbe671f',
'a',
[],
'0xc4c1a40b',
'noargfunc',
{},
),
(
ABI_B,
'0xf0fdf8340000000000000000000000000000000000000000000000000000000000000001',
'a',
[1],
{'': 1},
'0xcc6820de0000000000000000000000000000000000000000000000000000000000000001',
'uintfunc',
{'uintarg': 1},
),
(
ABI_C,
'0xf0fdf8340000000000000000000000000000000000000000000000000000000000000001',
'a',
[1],
{'': 1},
),
(
ABI_C,
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
'a',
[b'a'],
{'': a32bytes},
),
(
ABI_C,
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
'a',
['0x61'],
{'': a32bytes},
'0x22d86fa3',
'namesakefunc',
{},
),
(
ABI_C,
'0x9f3fab586100000000000000000000000000000000000000000000000000000000000000',
'a',
['61'],
{'': a32bytes},
'0x40c05b2f0000000000000000000000000000000000000000000000000000000000000001',
'namesakefunc',
{'uintarg': 1},
),
(
ABI_C,
'0xf0fdf834000000000000000000000000000000000000000000000000000000000000007f',
'a',
[127],
{'': 127},
'0xf931d77c6100000000000000000000000000000000000000000000000000000000000000',
'namesakefunc',
{'bytesarg': a32bytes},
),
(
ABI_BYTES,
'0xb606a9f6000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000016100000000000000000000000000000000000000000000000000000000000000', # noqa: E501
'bytesfunc',
[],
{'bytesarg': b'a'},
),
(
ABI_STRING,
'0x33b4005f000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000016100000000000000000000000000000000000000000000000000000000000000', # noqa: E501
'stringfunc',
[],
{'stringarg': 'a'},
),
(
ABI_ADDRESS,
'0x4767be6c000000000000000000000000ffffffffffffffffffffffffffffffffffffffff',
'addressfunc',
[],
{'addressarg': '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF'},
),
),
)
def test_contract_abi_decoding(web3, abi, data, method, arguments, expected):
def test_contract_abi_decoding(web3, abi, data, method, expected):
contract = web3.eth.contract(abi=abi)
func, params = contract.decode_function_input(data)
assert func.fn_name == method
Expand All @@ -107,10 +85,10 @@ def test_contract_abi_decoding(web3, abi, data, method, arguments, expected):
ABI_D,
'byte_array',
{
'b': (
'b': [
unhexlify('5595c210956e7721f9b692e702708556aa9aabb14ea163e96afa56ffbe9fa809'),
unhexlify('6f8d2fa18448afbfe4f82143c384484ad09a0271f3a3c0eb9f629e703f883125'),
),
],
},
'0xf166d6f8000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000025595c210956e7721f9b692e702708556aa9aabb14ea163e96afa56ffbe9fa8096f8d2fa18448afbfe4f82143c384484ad09a0271f3a3c0eb9f629e703f883125', # noqa: E501
),
Expand Down
3 changes: 2 additions & 1 deletion web3/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,8 @@ def decode_function_input(self, data):
names = [x['name'] for x in func.abi['inputs']]
types = [x['type'] for x in func.abi['inputs']]
decoded = decode_abi(types, params)
return func, dict(zip(names, decoded))
normalized = map_abi_data(BASE_RETURN_NORMALIZERS, types, decoded)
return func, dict(zip(names, normalized))

@combomethod
def find_functions_by_args(self, *args):
Expand Down

0 comments on commit 2cf7c34

Please sign in to comment.