From 2cf7c346de9a0f6a0508548816da1dcf061fb597 Mon Sep 17 00:00:00 2001 From: banteg Date: Wed, 8 Aug 2018 13:04:59 +0700 Subject: [PATCH] normalize function decoder output and update tests --- .../test_contract_method_abi_decoding.py | 64 ++++++------------- web3/contract.py | 3 +- 2 files changed, 23 insertions(+), 44 deletions(-) diff --git a/tests/core/contracts/test_contract_method_abi_decoding.py b/tests/core/contracts/test_contract_method_abi_decoding.py index e26a8a2bed..97efc46d2c 100644 --- a/tests/core/contracts/test_contract_method_abi_decoding.py +++ b/tests/core/contracts/test_contract_method_abi_decoding.py @@ -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 @@ -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 @@ -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 ), diff --git a/web3/contract.py b/web3/contract.py index 8136b9e2f9..ec40344ac2 100644 --- a/web3/contract.py +++ b/web3/contract.py @@ -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):