From 1b05ab83f88328a26e78b9df580ae4dbbb04a8b2 Mon Sep 17 00:00:00 2001 From: Stuart Reed Date: Wed, 11 Sep 2024 13:35:02 -0600 Subject: [PATCH] Add test case for ``abi`` used as function name in a contract Fix contract compilation and naming --- .../test_contract_class_construction.py | 45 +++++++++++++++++++ .../contract_sources/EventContracts.sol | 2 +- .../FunctionNameTesterContract.sol | 4 ++ .../contract_data/event_contracts.py | 24 +++++----- .../function_name_tester_contract.py | 11 ++++- 5 files changed, 71 insertions(+), 15 deletions(-) diff --git a/tests/core/contracts/test_contract_class_construction.py b/tests/core/contracts/test_contract_class_construction.py index 1571b50652..e6a713b2f8 100644 --- a/tests/core/contracts/test_contract_class_construction.py +++ b/tests/core/contracts/test_contract_class_construction.py @@ -43,6 +43,51 @@ def test_abi_as_json_string(w3, math_contract_abi, some_address): assert math.abi == math_contract_abi +def test_contract_init_with_abi_function_name( + w3, + function_name_tester_contract_abi, + function_name_tester_contract, +): + # test `w3` function name does not throw when creating the contract factory + contract_factory = w3.eth.contract(abi=function_name_tester_contract_abi) + + # re-instantiate the contract + contract = contract_factory(function_name_tester_contract.address) + + # Contract w3 function should not override web3 instance + with pytest.raises(AttributeError): + contract.functions.abi.get_block("latest") + + assert contract.abi == function_name_tester_contract_abi + + # assert the `w3` function returns true when called + result = contract.functions.abi().call() + assert result is True + + +@pytest.mark.asyncio +async def test_async_contract_init_with_abi_function_name( + async_w3, + function_name_tester_contract_abi, + async_function_name_tester_contract, +): + # test `w3` function name does not throw when creating the contract factory + contract_factory = async_w3.eth.contract(abi=function_name_tester_contract_abi) + + # re-instantiate the contract + contract = contract_factory(async_function_name_tester_contract.address) + + # Contract w3 function should not override web3 instance + with pytest.raises(AttributeError): + contract.functions.abi.get_block("latest") + + assert contract.abi == function_name_tester_contract_abi + + # assert the `w3` function returns true when called + result = await contract.functions.abi().call() + assert result is True + + def test_contract_init_with_w3_function_name( w3, function_name_tester_contract_abi, diff --git a/web3/_utils/contract_sources/EventContracts.sol b/web3/_utils/contract_sources/EventContracts.sol index 6b70414e8a..7e757b6d20 100644 --- a/web3/_utils/contract_sources/EventContracts.sol +++ b/web3/_utils/contract_sources/EventContracts.sol @@ -20,7 +20,7 @@ contract IndexedEventContract { } } -contract DuplicateEventContract { +contract AmbiguousEventNameContract { event LogSingleArg(uint256 arg0); event LogSingleArg(bytes32 arg0); diff --git a/web3/_utils/contract_sources/FunctionNameTesterContract.sol b/web3/_utils/contract_sources/FunctionNameTesterContract.sol index b6a58ebc15..f3cfd8655d 100644 --- a/web3/_utils/contract_sources/FunctionNameTesterContract.sol +++ b/web3/_utils/contract_sources/FunctionNameTesterContract.sol @@ -1,6 +1,10 @@ pragma solidity ^0.8.23; contract FunctionNameTesterContract { + function abi() public returns (bool) { + return true; + } + function w3() public returns (bool) { return true; } diff --git a/web3/_utils/contract_sources/contract_data/event_contracts.py b/web3/_utils/contract_sources/contract_data/event_contracts.py index d69bf2b762..c3961f6ea1 100644 --- a/web3/_utils/contract_sources/contract_data/event_contracts.py +++ b/web3/_utils/contract_sources/contract_data/event_contracts.py @@ -4,8 +4,8 @@ """ # source: web3/_utils/contract_sources/EventContracts.sol:EventContract -EVENT_CONTRACT_BYTECODE = "0x6080604052348015600e575f5ffd5b5061017a8061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b610047600480360381019061004291906100f1565b610049565b005b7ff70fe689e290d8ce2b2a388ac28db36fbb0e16a6d89c6804c461f65a1b40bb1581604051610078919061012b565b60405180910390a17f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100af919061012b565b60405180910390a150565b5f5ffd5b5f819050919050565b6100d0816100be565b81146100da575f5ffd5b50565b5f813590506100eb816100c7565b92915050565b5f60208284031215610106576101056100ba565b5b5f610113848285016100dd565b91505092915050565b610125816100be565b82525050565b5f60208201905061013e5f83018461011c565b9291505056fea2646970667358221220b63e7400f1145576575cee7cab449aef3d4ec139b261a4a3cf0b3aa391cbc1f364736f6c634300081b0033" # noqa: E501 -EVENT_CONTRACT_RUNTIME = "0x608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b610047600480360381019061004291906100f1565b610049565b005b7ff70fe689e290d8ce2b2a388ac28db36fbb0e16a6d89c6804c461f65a1b40bb1581604051610078919061012b565b60405180910390a17f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100af919061012b565b60405180910390a150565b5f5ffd5b5f819050919050565b6100d0816100be565b81146100da575f5ffd5b50565b5f813590506100eb816100c7565b92915050565b5f60208284031215610106576101056100ba565b5b5f610113848285016100dd565b91505092915050565b610125816100be565b82525050565b5f60208201905061013e5f83018461011c565b9291505056fea2646970667358221220b63e7400f1145576575cee7cab449aef3d4ec139b261a4a3cf0b3aa391cbc1f364736f6c634300081b0033" # noqa: E501 +EVENT_CONTRACT_BYTECODE = "0x6080604052348015600e575f5ffd5b5061017a8061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b610047600480360381019061004291906100f1565b610049565b005b7ff70fe689e290d8ce2b2a388ac28db36fbb0e16a6d89c6804c461f65a1b40bb1581604051610078919061012b565b60405180910390a17f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100af919061012b565b60405180910390a150565b5f5ffd5b5f819050919050565b6100d0816100be565b81146100da575f5ffd5b50565b5f813590506100eb816100c7565b92915050565b5f60208284031215610106576101056100ba565b5b5f610113848285016100dd565b91505092915050565b610125816100be565b82525050565b5f60208201905061013e5f83018461011c565b9291505056fea2646970667358221220b0bcf8d91a48b57e8c3a13a2358baf5a199c99a22aebe7940a1dc0397f284b9064736f6c634300081b0033" # noqa: E501 +EVENT_CONTRACT_RUNTIME = "0x608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b610047600480360381019061004291906100f1565b610049565b005b7ff70fe689e290d8ce2b2a388ac28db36fbb0e16a6d89c6804c461f65a1b40bb1581604051610078919061012b565b60405180910390a17f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100af919061012b565b60405180910390a150565b5f5ffd5b5f819050919050565b6100d0816100be565b81146100da575f5ffd5b50565b5f813590506100eb816100c7565b92915050565b5f60208284031215610106576101056100ba565b5b5f610113848285016100dd565b91505092915050565b610125816100be565b82525050565b5f60208201905061013e5f83018461011c565b9291505056fea2646970667358221220b0bcf8d91a48b57e8c3a13a2358baf5a199c99a22aebe7940a1dc0397f284b9064736f6c634300081b0033" # noqa: E501 EVENT_CONTRACT_ABI = [ { "anonymous": False, @@ -49,8 +49,8 @@ # source: web3/_utils/contract_sources/EventContracts.sol:IndexedEventContract -INDEXED_EVENT_CONTRACT_BYTECODE = "0x6080604052348015600e575f5ffd5b506101708061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b610047600480360381019061004291906100e7565b610049565b005b807ff70fe689e290d8ce2b2a388ac28db36fbb0e16a6d89c6804c461f65a1b40bb1560405160405180910390a27f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100a59190610121565b60405180910390a150565b5f5ffd5b5f819050919050565b6100c6816100b4565b81146100d0575f5ffd5b50565b5f813590506100e1816100bd565b92915050565b5f602082840312156100fc576100fb6100b0565b5b5f610109848285016100d3565b91505092915050565b61011b816100b4565b82525050565b5f6020820190506101345f830184610112565b9291505056fea264697066735822122068dd945f073032eff9a457b27d95cff39ebca5d4801534cf9719d0cf1606087464736f6c634300081b0033" # noqa: E501 -INDEXED_EVENT_CONTRACT_RUNTIME = "0x608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b610047600480360381019061004291906100e7565b610049565b005b807ff70fe689e290d8ce2b2a388ac28db36fbb0e16a6d89c6804c461f65a1b40bb1560405160405180910390a27f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100a59190610121565b60405180910390a150565b5f5ffd5b5f819050919050565b6100c6816100b4565b81146100d0575f5ffd5b50565b5f813590506100e1816100bd565b92915050565b5f602082840312156100fc576100fb6100b0565b5b5f610109848285016100d3565b91505092915050565b61011b816100b4565b82525050565b5f6020820190506101345f830184610112565b9291505056fea264697066735822122068dd945f073032eff9a457b27d95cff39ebca5d4801534cf9719d0cf1606087464736f6c634300081b0033" # noqa: E501 +INDEXED_EVENT_CONTRACT_BYTECODE = "0x6080604052348015600e575f5ffd5b506101708061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b610047600480360381019061004291906100e7565b610049565b005b807ff70fe689e290d8ce2b2a388ac28db36fbb0e16a6d89c6804c461f65a1b40bb1560405160405180910390a27f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100a59190610121565b60405180910390a150565b5f5ffd5b5f819050919050565b6100c6816100b4565b81146100d0575f5ffd5b50565b5f813590506100e1816100bd565b92915050565b5f602082840312156100fc576100fb6100b0565b5b5f610109848285016100d3565b91505092915050565b61011b816100b4565b82525050565b5f6020820190506101345f830184610112565b9291505056fea2646970667358221220d6260750ec274a719dbff1334e0c10993ba5e8b99bd2807ffd773256ed41a2e164736f6c634300081b0033" # noqa: E501 +INDEXED_EVENT_CONTRACT_RUNTIME = "0x608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b610047600480360381019061004291906100e7565b610049565b005b807ff70fe689e290d8ce2b2a388ac28db36fbb0e16a6d89c6804c461f65a1b40bb1560405160405180910390a27f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100a59190610121565b60405180910390a150565b5f5ffd5b5f819050919050565b6100c6816100b4565b81146100d0575f5ffd5b50565b5f813590506100e1816100bd565b92915050565b5f602082840312156100fc576100fb6100b0565b5b5f610109848285016100d3565b91505092915050565b61011b816100b4565b82525050565b5f6020820190506101345f830184610112565b9291505056fea2646970667358221220d6260750ec274a719dbff1334e0c10993ba5e8b99bd2807ffd773256ed41a2e164736f6c634300081b0033" # noqa: E501 INDEXED_EVENT_CONTRACT_ABI = [ { "anonymous": False, @@ -93,10 +93,10 @@ } -# source: web3/_utils/contract_sources/EventContracts.sol:DuplicateEventContract -DUPLICATE_EVENT_CONTRACT_BYTECODE = "0x6080604052348015600e575f5ffd5b506102728061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b61004760048036038101906100429190610119565b610049565b005b7f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100789190610153565b60405180910390a17fe466ad4edc182e32048f6e723b179ae20d1030f298fcfa1e9ad4a759b5a63112816040516020016100b29190610153565b6040516020818303038152906040526100ca906101ae565b6040516100d79190610223565b60405180910390a150565b5f5ffd5b5f819050919050565b6100f8816100e6565b8114610102575f5ffd5b50565b5f81359050610113816100ef565b92915050565b5f6020828403121561012e5761012d6100e2565b5b5f61013b84828501610105565b91505092915050565b61014d816100e6565b82525050565b5f6020820190506101665f830184610144565b92915050565b5f81519050919050565b5f819050602082019050919050565b5f819050919050565b5f6101998251610185565b80915050919050565b5f82821b905092915050565b5f6101b88261016c565b826101c284610176565b90506101cd8161018e565b9250602082101561020d576102087fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff836020036008026101a2565b831692505b5050919050565b61021d81610185565b82525050565b5f6020820190506102365f830184610214565b9291505056fea2646970667358221220450ed815dde012387f4a948e0123039f4fc1d714fe3b1b6213cd0177c0afbb2964736f6c634300081b0033" # noqa: E501 -DUPLICATE_EVENT_CONTRACT_RUNTIME = "0x608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b61004760048036038101906100429190610119565b610049565b005b7f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100789190610153565b60405180910390a17fe466ad4edc182e32048f6e723b179ae20d1030f298fcfa1e9ad4a759b5a63112816040516020016100b29190610153565b6040516020818303038152906040526100ca906101ae565b6040516100d79190610223565b60405180910390a150565b5f5ffd5b5f819050919050565b6100f8816100e6565b8114610102575f5ffd5b50565b5f81359050610113816100ef565b92915050565b5f6020828403121561012e5761012d6100e2565b5b5f61013b84828501610105565b91505092915050565b61014d816100e6565b82525050565b5f6020820190506101665f830184610144565b92915050565b5f81519050919050565b5f819050602082019050919050565b5f819050919050565b5f6101998251610185565b80915050919050565b5f82821b905092915050565b5f6101b88261016c565b826101c284610176565b90506101cd8161018e565b9250602082101561020d576102087fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff836020036008026101a2565b831692505b5050919050565b61021d81610185565b82525050565b5f6020820190506102365f830184610214565b9291505056fea2646970667358221220450ed815dde012387f4a948e0123039f4fc1d714fe3b1b6213cd0177c0afbb2964736f6c634300081b0033" # noqa: E501 -DUPLICATE_EVENT_CONTRACT_ABI = [ +# source: web3/_utils/contract_sources/EventContracts.sol:AmbiguousEventNameContract +AMBIGUOUS_EVENT_NAME_CONTRACT_BYTECODE = "0x6080604052348015600e575f5ffd5b506102728061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b61004760048036038101906100429190610119565b610049565b005b7f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100789190610153565b60405180910390a17fe466ad4edc182e32048f6e723b179ae20d1030f298fcfa1e9ad4a759b5a63112816040516020016100b29190610153565b6040516020818303038152906040526100ca906101ae565b6040516100d79190610223565b60405180910390a150565b5f5ffd5b5f819050919050565b6100f8816100e6565b8114610102575f5ffd5b50565b5f81359050610113816100ef565b92915050565b5f6020828403121561012e5761012d6100e2565b5b5f61013b84828501610105565b91505092915050565b61014d816100e6565b82525050565b5f6020820190506101665f830184610144565b92915050565b5f81519050919050565b5f819050602082019050919050565b5f819050919050565b5f6101998251610185565b80915050919050565b5f82821b905092915050565b5f6101b88261016c565b826101c284610176565b90506101cd8161018e565b9250602082101561020d576102087fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff836020036008026101a2565b831692505b5050919050565b61021d81610185565b82525050565b5f6020820190506102365f830184610214565b9291505056fea2646970667358221220e921a9ff848699a59d4bfe4f7fe3c6ea2c735d2d7bee0857548a605a86827b3664736f6c634300081b0033" # noqa: E501 +AMBIGUOUS_EVENT_NAME_CONTRACT_RUNTIME = "0x608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80635818fad71461002d575b5f5ffd5b61004760048036038101906100429190610119565b610049565b005b7f56d2ef3c5228bf5d88573621e325a4672ab50e033749a601e4f4a5e1dce905d4816040516100789190610153565b60405180910390a17fe466ad4edc182e32048f6e723b179ae20d1030f298fcfa1e9ad4a759b5a63112816040516020016100b29190610153565b6040516020818303038152906040526100ca906101ae565b6040516100d79190610223565b60405180910390a150565b5f5ffd5b5f819050919050565b6100f8816100e6565b8114610102575f5ffd5b50565b5f81359050610113816100ef565b92915050565b5f6020828403121561012e5761012d6100e2565b5b5f61013b84828501610105565b91505092915050565b61014d816100e6565b82525050565b5f6020820190506101665f830184610144565b92915050565b5f81519050919050565b5f819050602082019050919050565b5f819050919050565b5f6101998251610185565b80915050919050565b5f82821b905092915050565b5f6101b88261016c565b826101c284610176565b90506101cd8161018e565b9250602082101561020d576102087fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff836020036008026101a2565b831692505b5050919050565b61021d81610185565b82525050565b5f6020820190506102365f830184610214565b9291505056fea2646970667358221220e921a9ff848699a59d4bfe4f7fe3c6ea2c735d2d7bee0857548a605a86827b3664736f6c634300081b0033" # noqa: E501 +AMBIGUOUS_EVENT_NAME_CONTRACT_ABI = [ { "anonymous": False, "inputs": [ @@ -131,8 +131,8 @@ "type": "function", }, ] -DUPLICATE_EVENT_CONTRACT_DATA = { - "bytecode": DUPLICATE_EVENT_CONTRACT_BYTECODE, - "bytecode_runtime": DUPLICATE_EVENT_CONTRACT_RUNTIME, - "abi": DUPLICATE_EVENT_CONTRACT_ABI, +AMBIGUOUS_EVENT_NAME_CONTRACT_DATA = { + "bytecode": AMBIGUOUS_EVENT_NAME_CONTRACT_BYTECODE, + "bytecode_runtime": AMBIGUOUS_EVENT_NAME_CONTRACT_RUNTIME, + "abi": AMBIGUOUS_EVENT_NAME_CONTRACT_ABI, } diff --git a/web3/_utils/contract_sources/contract_data/function_name_tester_contract.py b/web3/_utils/contract_sources/contract_data/function_name_tester_contract.py index f5fefd8f1a..67695c3414 100644 --- a/web3/_utils/contract_sources/contract_data/function_name_tester_contract.py +++ b/web3/_utils/contract_sources/contract_data/function_name_tester_contract.py @@ -4,9 +4,16 @@ """ # source: web3/_utils/contract_sources/FunctionNameTesterContract.sol:FunctionNameTesterContract # noqa: E501 -FUNCTION_NAME_TESTER_CONTRACT_BYTECODE = "0x6080604052348015600e575f5ffd5b5060dc80601a5f395ff3fe6080604052348015600e575f5ffd5b50600436106030575f3560e01c8063a044c987146034578063c5d7802e14604e575b5f5ffd5b603a6068565b60405160459190608f565b60405180910390f35b60546070565b604051605f9190608f565b60405180910390f35b5f6001905090565b5f5f905090565b5f8115159050919050565b6089816077565b82525050565b5f60208201905060a05f8301846082565b9291505056fea26469706673582212200a081b5e095d27bf0d18bfdb07b8618bdf1a04e1a4cf660629f12ab2b0d0bdb264736f6c634300081b0033" # noqa: E501 -FUNCTION_NAME_TESTER_CONTRACT_RUNTIME = "0x6080604052348015600e575f5ffd5b50600436106030575f3560e01c8063a044c987146034578063c5d7802e14604e575b5f5ffd5b603a6068565b60405160459190608f565b60405180910390f35b60546070565b604051605f9190608f565b60405180910390f35b5f6001905090565b5f5f905090565b5f8115159050919050565b6089816077565b82525050565b5f60208201905060a05f8301846082565b9291505056fea26469706673582212200a081b5e095d27bf0d18bfdb07b8618bdf1a04e1a4cf660629f12ab2b0d0bdb264736f6c634300081b0033" # noqa: E501 +FUNCTION_NAME_TESTER_CONTRACT_BYTECODE = "0x6080604052348015600e575f5ffd5b506101088061001c5f395ff3fe6080604052348015600e575f5ffd5b5060043610603a575f3560e01c80634887063014603e578063a044c987146058578063c5d7802e146072575b5f5ffd5b6044608c565b604051604f919060bb565b60405180910390f35b605e6094565b6040516069919060bb565b60405180910390f35b6078609c565b6040516083919060bb565b60405180910390f35b5f6001905090565b5f6001905090565b5f5f905090565b5f8115159050919050565b60b58160a3565b82525050565b5f60208201905060cc5f83018460ae565b9291505056fea26469706673582212209b66b89285ded090d95bee398163ad2759d1c426728d46eb08e5549b5e8166d264736f6c634300081b0033" # noqa: E501 +FUNCTION_NAME_TESTER_CONTRACT_RUNTIME = "0x6080604052348015600e575f5ffd5b5060043610603a575f3560e01c80634887063014603e578063a044c987146058578063c5d7802e146072575b5f5ffd5b6044608c565b604051604f919060bb565b60405180910390f35b605e6094565b6040516069919060bb565b60405180910390f35b6078609c565b6040516083919060bb565b60405180910390f35b5f6001905090565b5f6001905090565b5f5f905090565b5f8115159050919050565b60b58160a3565b82525050565b5f60208201905060cc5f83018460ae565b9291505056fea26469706673582212209b66b89285ded090d95bee398163ad2759d1c426728d46eb08e5549b5e8166d264736f6c634300081b0033" # noqa: E501 FUNCTION_NAME_TESTER_CONTRACT_ABI = [ + { + "inputs": [], + "name": "abi", + "outputs": [{"internalType": "bool", "name": "", "type": "bool"}], + "stateMutability": "nonpayable", + "type": "function", + }, { "inputs": [], "name": "w3",