Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naive tuple support #1147

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions tests/core/contracts/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,163 @@ def CallerTesterContract(web3, CALLER_TESTER_CONTRACT):
return web3.eth.contract(**CALLER_TESTER_CONTRACT)


CONTRACT_TUPLE_SOURCE = """
pragma experimental ABIEncoderV2;

contract Tuple {
struct Struct {
int anInt;
bool aBool;
address anAddress;
}
function methodTakingStruct(Struct memory m)
public
pure
returns (Struct memory)
{
return m;
}
function methodTakingArrayOfStructs(Struct[] memory m)
public
pure
returns (Struct[] memory)
{
return m;
}
}"""

CONTRACT_TUPLE_CODE = "608060405234801561001057600080fd5b50610571806100206000396000f3fe608060405260043610610046576000357c0100000000000000000000000000000000000000000000000000000000900480635442981a1461004b578063d0723aff14610088575b600080fd5b34801561005757600080fd5b50610072600480360361006d919081019061029a565b6100c5565b60405161007f9190610410565b60405180910390f35b34801561009457600080fd5b506100af60048036036100aa91908101906102db565b6100cf565b6040516100bc9190610432565b60405180910390f35b6060819050919050565b6100d76100df565b819050919050565b60606040519081016040528060008152602001600015158152602001600073ffffffffffffffffffffffffffffffffffffffff1681525090565b6000610125823561050f565b905092915050565b600082601f830112151561014057600080fd5b813561015361014e8261047a565b61044d565b9150818183526020840193506020810190508385606084028201111561017857600080fd5b60005b838110156101a8578161018e88826101da565b84526020840193506060830192505060018101905061017b565b5050505092915050565b60006101be8235610521565b905092915050565b60006101d2823561052d565b905092915050565b6000606082840312156101ec57600080fd5b6101f6606061044d565b90506000610206848285016101c6565b600083015250602061021a848285016101b2565b602083015250604061022e84828501610119565b60408301525092915050565b60006060828403121561024c57600080fd5b610256606061044d565b90506000610266848285016101c6565b600083015250602061027a848285016101b2565b602083015250604061028e84828501610119565b60408301525092915050565b6000602082840312156102ac57600080fd5b600082013567ffffffffffffffff8111156102c657600080fd5b6102d28482850161012d565b91505092915050565b6000606082840312156102ed57600080fd5b60006102fb8482850161023a565b91505092915050565b61030d816104c7565b82525050565b600061031e826104af565b808452602084019350610330836104a2565b60005b82811015610362576103468683516103ce565b61034f826104ba565b9150606086019550600181019050610333565b50849250505092915050565b610377816104d9565b82525050565b610386816104e5565b82525050565b6060820160008201516103a2600085018261037d565b5060208201516103b5602085018261036e565b5060408201516103c86040850182610304565b50505050565b6060820160008201516103e4600085018261037d565b5060208201516103f7602085018261036e565b50604082015161040a6040850182610304565b50505050565b6000602082019050818103600083015261042a8184610313565b905092915050565b6000606082019050610447600083018461038c565b92915050565b6000604051905081810181811067ffffffffffffffff8211171561047057600080fd5b8060405250919050565b600067ffffffffffffffff82111561049157600080fd5b602082029050602081019050919050565b6000602082019050919050565b600081519050919050565b6000602082019050919050565b60006104d2826104ef565b9050919050565b60008115159050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061051a826104ef565b9050919050565b60008115159050919050565b600081905091905056fea265627a7a7230582016790dbe833dd3fd348c5d67805f5fae4b7caa58b6f6cd2e1e146e725d1b09506c6578706572696d656e74616cf50037" # noqa: E501

CONTRACT_TUPLE_RUNTIME = "608060405260043610610046576000357c0100000000000000000000000000000000000000000000000000000000900480635442981a1461004b578063d0723aff14610088575b600080fd5b34801561005757600080fd5b50610072600480360361006d919081019061029a565b6100c5565b60405161007f9190610410565b60405180910390f35b34801561009457600080fd5b506100af60048036036100aa91908101906102db565b6100cf565b6040516100bc9190610432565b60405180910390f35b6060819050919050565b6100d76100df565b819050919050565b60606040519081016040528060008152602001600015158152602001600073ffffffffffffffffffffffffffffffffffffffff1681525090565b6000610125823561050f565b905092915050565b600082601f830112151561014057600080fd5b813561015361014e8261047a565b61044d565b9150818183526020840193506020810190508385606084028201111561017857600080fd5b60005b838110156101a8578161018e88826101da565b84526020840193506060830192505060018101905061017b565b5050505092915050565b60006101be8235610521565b905092915050565b60006101d2823561052d565b905092915050565b6000606082840312156101ec57600080fd5b6101f6606061044d565b90506000610206848285016101c6565b600083015250602061021a848285016101b2565b602083015250604061022e84828501610119565b60408301525092915050565b60006060828403121561024c57600080fd5b610256606061044d565b90506000610266848285016101c6565b600083015250602061027a848285016101b2565b602083015250604061028e84828501610119565b60408301525092915050565b6000602082840312156102ac57600080fd5b600082013567ffffffffffffffff8111156102c657600080fd5b6102d28482850161012d565b91505092915050565b6000606082840312156102ed57600080fd5b60006102fb8482850161023a565b91505092915050565b61030d816104c7565b82525050565b600061031e826104af565b808452602084019350610330836104a2565b60005b82811015610362576103468683516103ce565b61034f826104ba565b9150606086019550600181019050610333565b50849250505092915050565b610377816104d9565b82525050565b610386816104e5565b82525050565b6060820160008201516103a2600085018261037d565b5060208201516103b5602085018261036e565b5060408201516103c86040850182610304565b50505050565b6060820160008201516103e4600085018261037d565b5060208201516103f7602085018261036e565b50604082015161040a6040850182610304565b50505050565b6000602082019050818103600083015261042a8184610313565b905092915050565b6000606082019050610447600083018461038c565b92915050565b6000604051905081810181811067ffffffffffffffff8211171561047057600080fd5b8060405250919050565b600067ffffffffffffffff82111561049157600080fd5b602082029050602081019050919050565b6000602082019050919050565b600081519050919050565b6000602082019050919050565b60006104d2826104ef565b9050919050565b60008115159050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061051a826104ef565b9050919050565b60008115159050919050565b600081905091905056fea265627a7a7230582016790dbe833dd3fd348c5d67805f5fae4b7caa58b6f6cd2e1e146e725d1b09506c6578706572696d656e74616cf50037" # noqa: E501

CONTRACT_TUPLE_ABI = json.loads("""
[
{
"constant": true,
"inputs": [
{
"components": [
{
"name": "anInt",
"type": "int256"
},
{
"name": "aBool",
"type": "bool"
},
{
"name": "anAddress",
"type": "address"
}
],
"name": "m",
"type": "tuple[]"
}
],
"name": "methodTakingArrayOfStructs",
"outputs": [
{
"components": [
{
"name": "anInt",
"type": "int256"
},
{
"name": "aBool",
"type": "bool"
},
{
"name": "anAddress",
"type": "address"
}
],
"name": "",
"type": "tuple[]"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"components": [
{
"name": "anInt",
"type": "int256"
},
{
"name": "aBool",
"type": "bool"
},
{
"name": "anAddress",
"type": "address"
}
],
"name": "m",
"type": "tuple"
}
],
"name": "methodTakingStruct",
"outputs": [
{
"components": [
{
"name": "anInt",
"type": "int256"
},
{
"name": "aBool",
"type": "bool"
},
{
"name": "anAddress",
"type": "address"
}
],
"name": "",
"type": "tuple"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
}
]""")


@pytest.fixture()
def TUPLE_CODE():
return CONTRACT_TUPLE_CODE


@pytest.fixture()
def TUPLE_RUNTIME():
return CONTRACT_TUPLE_RUNTIME


@pytest.fixture()
def TUPLE_ABI():
return CONTRACT_TUPLE_ABI


@pytest.fixture()
def TUPLE_CONTRACT(TUPLE_CODE, TUPLE_RUNTIME, TUPLE_ABI):
return {
'bytecode': TUPLE_CODE,
'bytecode_runtime': TUPLE_RUNTIME,
'abi': TUPLE_ABI,
}


@pytest.fixture()
def TupleContract(web3, TUPLE_CONTRACT):
return web3.eth.contract(**TUPLE_CONTRACT)


class LogFunctions:
LogAnonymous = 0
LogNoArguments = 1
Expand Down
41 changes: 41 additions & 0 deletions tests/core/contracts/test_contract_call_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ def fallback_function_contract(web3, FallballFunctionContract, address_conversio
return deploy(web3, FallballFunctionContract, address_conversion_func)


@pytest.fixture()
def tuple_contract(web3, TupleContract, address_conversion_func):
return deploy(web3, TupleContract, address_conversion_func)


def test_invalid_address_in_deploy_arg(web3, WithConstructorAddressArgumentsContract):
with pytest.raises(InvalidAddress):
WithConstructorAddressArgumentsContract.constructor(
Expand Down Expand Up @@ -611,3 +616,39 @@ def test_invalid_fixed_value_reflections(web3, fixed_reflection_contract, functi
contract_func = fixed_reflection_contract.functions[function]
with pytest.raises(ValidationError, match=error):
contract_func(value).call({'gas': 420000})


@pytest.mark.parametrize(
'method_input, expected',
(
(
{'anInt': 0, 'aBool': True, 'anAddress': '0x' + 'f' * 40},
(0, True, '0x' + 'f' * 40)
),
(
(0, True, '0x' + 'f' * 40),
(0, True, '0x' + 'f' * 40),
),
)
)
def test_call_tuple_contract_struct(tuple_contract, method_input, expected):
result = tuple_contract.functions.methodTakingStruct(method_input).call()
assert result == expected


@pytest.mark.parametrize(
'method_input, expected',
(
(
[{'anInt': 0, 'aBool': True, 'anAddress': '0x' + 'f' * 40}],
((0, True, '0x' + 'f' * 40),)
),
(
[(0, True, '0x' + 'f' * 40)],
((0, True, '0x' + 'f' * 40),)
),
)
)
def test_call_tuple_contract_struct_array(tuple_contract, method_input, expected):
result = tuple_contract.functions.methodTakingArrayOfStructs(method_input).call()
assert result == expected
39 changes: 39 additions & 0 deletions tests/core/contracts/test_contract_util_functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pytest

from web3.contract import (
find_matching_fn_abi,
parse_block_identifier_int,
)

Expand All @@ -11,3 +14,39 @@
def test_parse_block_identifier_int(web3):
last_num = web3.eth.getBlock('latest').number
assert 0 == parse_block_identifier_int(web3, -1 - last_num)


@pytest.mark.parametrize(
'contract_abi, fn_name, args, kwargs, expected',
(
(
[
{
'inputs': [],
'type': 'function',
'name': 'a',
},
{
'inputs': [{'type': 'bytes32'}],
'type': 'function',
'name': 'a',
},
{
'inputs': [{'type': 'uint256'}],
'type': 'function',
'name': 'a',
},
],
'a',
[1],
None,
{
'inputs': [{'type': 'uint256'}],
'type': 'function',
'name': 'a',
},
),
),
)
def test_find_matching_fn_abi(fn_name, contract_abi, args, kwargs, expected):
assert expected == find_matching_fn_abi(contract_abi, fn_name, args, kwargs)
Loading