Skip to content
Merged
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
1 change: 1 addition & 0 deletions newsfragments/3618.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix contract event ``FilterParams`` to validate and normalize ``address`` parameters.
288 changes: 257 additions & 31 deletions tests/core/utilities/test_construct_event_filter_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,67 @@
from web3._utils.filters import (
construct_event_filter_params,
)
from web3.exceptions import (
InvalidAddress,
Web3ValueError,
)

EVENT_1_ABI = {
"anonymous": False,
"inputs": [
{"indexed": False, "name": "arg0", "type": "uint256"},
{"indexed": True, "name": "arg1", "type": "uint256"},
],
"name": "Event_1",
"type": "event",
}

def hex_and_pad(i):
unpadded_hex_value = hex(i).rstrip("L")
return "0x" + unpadded_hex_value[2:].zfill(64)


@pytest.fixture
def event_abi():
return {
"anonymous": False,
"inputs": [
{"indexed": False, "name": "arg0", "type": "uint256"},
{"indexed": True, "name": "arg1", "type": "uint256"},
],
"name": "Event_1",
"type": "event",
}


@pytest.mark.parametrize(
"event_abi,fn_kwargs,expected",
"fn_kwargs,expected",
(
(
EVENT_1_ABI,
pytest.param(
{},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
},
id="no-args",
),
(
EVENT_1_ABI,
pytest.param(
{"topics": ["should-overwrite-topics"]},
{"topics": ["should-overwrite-topics"]},
id="overwrite-topics",
),
(
EVENT_1_ABI,
pytest.param(
{"address": None},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
},
id="no-address",
),
pytest.param(
{"contract_address": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601"},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
},
id="contract_address-string",
),
(
EVENT_1_ABI,
pytest.param(
{
"contract_address": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"address": "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
Expand All @@ -57,37 +77,243 @@
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
],
},
id="address-with-contract_address",
),
(
EVENT_1_ABI,
pytest.param(
{
"contract_address": ["0xd3CdA913deB6f67967B99D67aCDFa1712C293601"],
"address": ["0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413"],
},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": [
"0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
],
},
id="single-item-address-list-with-contract_address-list",
),
pytest.param(
{"address": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601"},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
},
id="address-string",
),
pytest.param(
{"address": b"\xf2\xe2F\xbbv\xdf\x87l\xef\x8b8\xae\x84\x13\x0fOU\xde9["},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": b"\xf2\xe2F\xbbv\xdf\x87l\xef\x8b8\xae\x84\x13\x0fOU\xde9[",
},
id="address-bytes",
),
pytest.param(
{
"contract_address": b"\xf2\xe2F\xbbv\xdf\x87l\xef\x8b8\xae\x84\x13\x0fOU\xde9[", # noqa: E501
"address": [
b"\xf2\xe2F\xbbv\xdf\x87l\xef\x8b8\xae\x84\x13\x0fOU\xde9[",
b"\xf2\xe2F\xbbv\xdf\x87l\xef\x8b8\xae\x84\x13\x0fOU\xde9[",
],
},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": b"\xf2\xe2F\xbbv\xdf\x87l\xef\x8b8\xae\x84\x13\x0fOU\xde9[",
},
id="address-bytes-list",
),
pytest.param(
{"address": ["0xd3CdA913deB6f67967B99D67aCDFa1712C293601"]},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
},
id="address-list",
),
pytest.param(
{
"address": [
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"0x1234567890123456789012345678901234567890",
]
},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": [
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"0x1234567890123456789012345678901234567890",
],
},
id="multiple-address",
),
pytest.param(
{
"address": "0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"contract_address": ["0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413"],
},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": [
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
],
},
id="one-address-with-multiple-contract_address",
),
pytest.param(
{
"address": [
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"0x1234567890123456789012345678901234567890",
],
"contract_address": "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": [
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"0x1234567890123456789012345678901234567890",
"0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
],
},
id="multiple-address-with-one-contract_address",
),
pytest.param(
{
"address": [
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"0x1234567890123456789012345678901234567890",
],
"contract_address": [
"0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
"0x1234567890123456789012345678901234567890",
],
},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": [
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"0x1234567890123456789012345678901234567890",
"0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
],
},
id="multiple-address-with-multiple-contract_address",
),
pytest.param(
{
"contract_address": "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"from_block": "latest",
"to_block": "latest",
"address": [
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"0x1234567890123456789012345678901234567890",
"0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
],
},
{
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": [
"0xd3CdA913deB6f67967B99D67aCDFa1712C293601",
"0x1234567890123456789012345678901234567890",
"0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
],
"fromBlock": "latest",
"toBlock": "latest",
},
id="all-arguments-with-address-list",
),
),
)
def test_construct_event_filter_params(w3, event_abi, fn_kwargs, expected):
_, actual = construct_event_filter_params(event_abi, w3.codec, **fn_kwargs)
assert actual == expected
_, filter_params = construct_event_filter_params(event_abi, w3.codec, **fn_kwargs)
# Ensure that the filter_params contains the expected keys
assert (
filter_params.keys() == expected.keys()
), f"Keys don't match. Expected {set(expected.keys())}, got {set(filter_params.keys())}" # noqa: E501
# Verify all values in filter_params match the expected values
for key, value in expected.items():
if isinstance(value, list) and isinstance(filter_params[key], list):
assert sorted(filter_params[key]) == sorted(
value
), f"Expected {key}={value}, got {key}={filter_params.get(key)}"
else:
assert (
filter_params[key] == value
), f"Expected {key}={value}, got {key}={filter_params.get(key)}"


def hex_and_pad(i):
unpadded_hex_value = hex(i).rstrip("L")
return "0x" + unpadded_hex_value[2:].zfill(64)
@pytest.mark.parametrize(
"fn_kwargs, expected_exception",
[
pytest.param(
{
"contract_address": "0xbb9bc244d798123fde783fcc1c72d3bb8c189413",
"topics": [
"0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6"
],
"address": [
"0xd3cda913deb6f67967b99d67acdfa1712c293601",
"0x1234567890123456789012345678901234567890",
"0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
],
"from_block": 1,
"to_block": 2,
},
InvalidAddress,
id="invalid-checksum-address",
),
pytest.param(
{
"contract_address": "0xbb9bc244d798123fde783fcc1c72d3bb8c189413",
},
InvalidAddress,
id="invalid-checksum-contract-address",
),
pytest.param(
{"address": {"invalid": "0x1234567890123456789012345678901234567890"}},
Web3ValueError,
id="unsupported-type-exception",
),
],
)
def test_construct_event_filter_params_exceptions(
w3, event_abi, fn_kwargs, expected_exception
):
with pytest.raises(expected_exception):
construct_event_filter_params(event_abi, w3.codec, **fn_kwargs)


@pytest.mark.parametrize(
"event_abi,fn_kwargs,expected",
"fn_kwargs,expected",
(
(EVENT_1_ABI, {}, [[]]),
(EVENT_1_ABI, {"argument_filters": {"arg0": 1}}, [[hex_and_pad(1)]]),
(EVENT_1_ABI, {"argument_filters": {"arg0": [1]}}, [[hex_and_pad(1)]]),
({}, [[]]),
({"argument_filters": {"arg0": 1}}, [[hex_and_pad(1)]]),
({"argument_filters": {"arg0": [1]}}, [[hex_and_pad(1)]]),
(
EVENT_1_ABI,
{"argument_filters": {"arg0": [1, 2]}},
[
[hex_and_pad(1)],
Expand All @@ -97,7 +323,7 @@ def hex_and_pad(i):
),
)
def test_construct_event_filter_params_for_data_filters(
event_abi, w3, fn_kwargs, expected
w3, event_abi, fn_kwargs, expected
):
actual, _ = construct_event_filter_params(event_abi, w3.codec, **fn_kwargs)
assert actual == expected
Loading