Skip to content

Commit ea75379

Browse files
committed
Add tests for indexed and non-indexed arg filtering
1 parent 3d87d7d commit ea75379

File tree

10 files changed

+294
-49
lines changed

10 files changed

+294
-49
lines changed

tests/conftest.py

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def emitter_contract_data():
4040

4141

4242
class LogFunctions:
43+
# These appear to be for a very specific test and this doesn't need to be updated
44+
# for every event in the emitter contract. That ends up breaking that test.
4345
LogAnonymous = 0
4446
LogNoArguments = 1
4547
LogSingleArg = 2
@@ -99,6 +101,7 @@ class LogTopics:
99101
"LogAddressNotIndexed(address,address)"
100102
)
101103
LogStructArgs = event_signature_to_log_topic("LogStructArgs(uint256,tuple)")
104+
LogIndexedAndNotIndexed = event_signature_to_log_topic("LogIndexedAndNotIndexed()")
102105

103106

104107
@pytest.fixture(scope="session")

tests/core/filtering/test_contract_get_logs.py

+171-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import pytest
22

3+
from web3._utils.contract_sources.contract_data._custom_contract_data import (
4+
EMITTER_ENUM,
5+
)
6+
37

48
def test_contract_get_available_events(
59
emitter,
610
):
711
"""We can iterate over available contract events"""
812
contract = emitter
913
events = list(contract.events)
10-
assert len(events) == 19
14+
assert len(events) == len(EMITTER_ENUM)
1115

1216

1317
def test_contract_get_logs_all(
@@ -87,6 +91,81 @@ def test_contract_get_logs_argument_filter(
8791
assert len(partial_logs) == 4
8892

8993

94+
def test_get_logs_argument_filters_indexed_and_non_indexed_args(emitter):
95+
emitter.functions.logIndexedAndNotIndexedArgs(
96+
f"0xdead{'00' * 17}01", # indexed address
97+
101, # indexed uint256
98+
f"0xBEEf{'00' * 17}01", # non-indexed address
99+
201, # non-indexed uint256
100+
"This is the FIRST string arg.", # non-indexed string
101+
).transact()
102+
emitter.functions.logIndexedAndNotIndexedArgs(
103+
f"0xdEaD{'00' * 17}02",
104+
102,
105+
f"0xbeeF{'00' * 17}02",
106+
202,
107+
"This is the SECOND string arg.",
108+
).transact()
109+
110+
# get all logs
111+
logs_no_filter = emitter.events.LogIndexedAndNotIndexed.get_logs(fromBlock=1)
112+
assert len(logs_no_filter) == 2
113+
114+
# filter for the second log by each of the indexed args and compare
115+
logs_filter_indexed_address = emitter.events.LogIndexedAndNotIndexed.get_logs(
116+
fromBlock=1,
117+
argument_filters={"indexedAddress": f"0xdEaD{'00' * 17}02"},
118+
)
119+
logs_filter_indexed_uint256 = emitter.events.LogIndexedAndNotIndexed.get_logs(
120+
fromBlock=1,
121+
argument_filters={"indexedUint256": 102},
122+
)
123+
assert len(logs_filter_indexed_address) == len(logs_filter_indexed_uint256) == 1
124+
assert (
125+
logs_filter_indexed_address[0]
126+
== logs_filter_indexed_uint256[0]
127+
== logs_no_filter[1]
128+
)
129+
130+
# filter for the first log by non-indexed address
131+
logs_filter_non_indexed_address = emitter.events.LogIndexedAndNotIndexed.get_logs(
132+
fromBlock=1,
133+
argument_filters={"nonIndexedAddress": f"0xBEEf{'00' * 17}01"},
134+
)
135+
assert len(logs_filter_non_indexed_address) == 1
136+
assert logs_filter_non_indexed_address[0] == logs_no_filter[0]
137+
138+
# filter for the second log by non-indexed uint256 and string separately
139+
logs_filter_non_indexed_uint256 = emitter.events.LogIndexedAndNotIndexed.get_logs(
140+
fromBlock=1,
141+
argument_filters={"nonIndexedUint256": 202},
142+
)
143+
logs_filter_non_indexed_string = emitter.events.LogIndexedAndNotIndexed.get_logs(
144+
fromBlock=1,
145+
argument_filters={"nonIndexedString": "SECOND"},
146+
)
147+
assert len(logs_filter_non_indexed_uint256) == 1
148+
assert len(logs_filter_non_indexed_string) == 1
149+
assert (
150+
logs_filter_non_indexed_uint256[0]
151+
== logs_filter_non_indexed_string[0]
152+
== logs_no_filter[1]
153+
)
154+
155+
# filter by both string and uint256, non-indexed
156+
logs_filter_non_indexed_uint256_and_string = (
157+
emitter.events.LogIndexedAndNotIndexed.get_logs(
158+
fromBlock=1,
159+
argument_filters={
160+
"nonIndexedUint256": 201,
161+
"nonIndexedString": "FIRST",
162+
},
163+
)
164+
)
165+
assert len(logs_filter_non_indexed_uint256_and_string) == 1
166+
assert logs_filter_non_indexed_uint256_and_string[0] == logs_no_filter[0]
167+
168+
90169
# --- async --- #
91170

92171

@@ -96,7 +175,7 @@ def test_async_contract_get_available_events(
96175
"""We can iterate over available contract events"""
97176
contract = async_emitter
98177
events = list(contract.events)
99-
assert len(events) == 19
178+
assert len(events) == len(EMITTER_ENUM)
100179

101180

102181
@pytest.mark.asyncio
@@ -195,3 +274,93 @@ async def test_async_contract_get_logs_argument_filter(
195274
argument_filters={"arg0": 1},
196275
)
197276
assert len(partial_logs) == 4
277+
278+
279+
@pytest.mark.asyncio
280+
async def test_async_get_logs_argument_filters_indexed_and_non_indexed_args(
281+
async_emitter,
282+
):
283+
await async_emitter.functions.logIndexedAndNotIndexedArgs(
284+
f"0xdead{'00' * 17}01", # indexed address
285+
101, # indexed uint256
286+
f"0xBEEf{'00' * 17}01", # non-indexed address
287+
201, # non-indexed uint256
288+
"This is the FIRST string arg.", # non-indexed string
289+
).transact()
290+
await async_emitter.functions.logIndexedAndNotIndexedArgs(
291+
f"0xdEaD{'00' * 17}02",
292+
102,
293+
f"0xbeeF{'00' * 17}02",
294+
202,
295+
"This is the SECOND string arg.",
296+
).transact()
297+
298+
# get all logs
299+
logs_no_filter = await async_emitter.events.LogIndexedAndNotIndexed.get_logs(
300+
fromBlock=1
301+
)
302+
assert len(logs_no_filter) == 2
303+
304+
# filter for the second log by each of the indexed args and compare
305+
logs_filter_indexed_address = (
306+
await async_emitter.events.LogIndexedAndNotIndexed.get_logs(
307+
fromBlock=1,
308+
argument_filters={"indexedAddress": f"0xdEaD{'00' * 17}02"},
309+
)
310+
)
311+
logs_filter_indexed_uint256 = (
312+
await async_emitter.events.LogIndexedAndNotIndexed.get_logs(
313+
fromBlock=1,
314+
argument_filters={"indexedUint256": 102},
315+
)
316+
)
317+
assert len(logs_filter_indexed_address) == len(logs_filter_indexed_uint256) == 1
318+
assert (
319+
logs_filter_indexed_address[0]
320+
== logs_filter_indexed_uint256[0]
321+
== logs_no_filter[1]
322+
)
323+
324+
# filter for the first log by non-indexed address
325+
logs_filter_non_indexed_address = (
326+
await async_emitter.events.LogIndexedAndNotIndexed.get_logs(
327+
fromBlock=1,
328+
argument_filters={"nonIndexedAddress": f"0xBEEf{'00' * 17}01"},
329+
)
330+
)
331+
assert len(logs_filter_non_indexed_address) == 1
332+
assert logs_filter_non_indexed_address[0] == logs_no_filter[0]
333+
334+
# filter for the second log by non-indexed uint256 and string separately
335+
logs_filter_non_indexed_uint256 = (
336+
await async_emitter.events.LogIndexedAndNotIndexed.get_logs(
337+
fromBlock=1,
338+
argument_filters={"nonIndexedUint256": 202},
339+
)
340+
)
341+
logs_filter_non_indexed_string = (
342+
await async_emitter.events.LogIndexedAndNotIndexed.get_logs(
343+
fromBlock=1,
344+
argument_filters={"nonIndexedString": "SECOND"},
345+
)
346+
)
347+
assert len(logs_filter_non_indexed_uint256) == 1
348+
assert len(logs_filter_non_indexed_string) == 1
349+
assert (
350+
logs_filter_non_indexed_uint256[0]
351+
== logs_filter_non_indexed_string[0]
352+
== logs_no_filter[1]
353+
)
354+
355+
# filter by both string and uint256, non-indexed
356+
logs_filter_non_indexed_uint256_and_string = (
357+
await async_emitter.events.LogIndexedAndNotIndexed.get_logs(
358+
fromBlock=1,
359+
argument_filters={
360+
"nonIndexedUint256": 201,
361+
"nonIndexedString": "FIRST",
362+
},
363+
)
364+
)
365+
assert len(logs_filter_non_indexed_uint256_and_string) == 1
366+
assert logs_filter_non_indexed_uint256_and_string[0] == logs_no_filter[0]

web3/_utils/contract_sources/EmitterContract.sol

+38-13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ contract EmitterContract {
3131
}
3232
event LogStructArgs(uint arg0, TestTuple arg1);
3333

34+
event LogIndexedAndNotIndexed(
35+
address indexed indexedAddress,
36+
uint256 indexed indexedUint256,
37+
address nonIndexedAddress,
38+
uint256 nonIndexedUint256,
39+
string nonIndexedString
40+
);
41+
3442
enum WhichEvent {
3543
LogAnonymous,
3644
LogNoArguments,
@@ -50,67 +58,84 @@ contract EmitterContract {
5058
LogListArgs,
5159
LogAddressIndexed,
5260
LogAddressNotIndexed,
53-
LogStructArgs
61+
LogStructArgs,
62+
LogIndexedAndNotIndexed
5463
}
5564

56-
function logNoArgs(WhichEvent which) public {
65+
function logNoArgs(WhichEvent which) external {
5766
if (which == WhichEvent.LogNoArguments) emit LogNoArguments();
5867
else if (which == WhichEvent.LogAnonymous) emit LogAnonymous();
5968
else revert("Didn't match any allowable event index");
6069
}
6170

62-
function logSingle(WhichEvent which, uint arg0) public {
71+
function logSingle(WhichEvent which, uint arg0) external {
6372
if (which == WhichEvent.LogSingleArg) emit LogSingleArg(arg0);
6473
else if (which == WhichEvent.LogSingleWithIndex) emit LogSingleWithIndex(arg0);
6574
else if (which == WhichEvent.LogSingleAnonymous) emit LogSingleAnonymous(arg0);
6675
else revert("Didn't match any allowable event index");
6776
}
6877

69-
function logDouble(WhichEvent which, uint arg0, uint arg1) public {
78+
function logDouble(WhichEvent which, uint arg0, uint arg1) external {
7079
if (which == WhichEvent.LogDoubleArg) emit LogDoubleArg(arg0, arg1);
7180
else if (which == WhichEvent.LogDoubleWithIndex) emit LogDoubleWithIndex(arg0, arg1);
7281
else if (which == WhichEvent.LogDoubleAnonymous) emit LogDoubleAnonymous(arg0, arg1);
7382
else revert("Didn't match any allowable event index");
7483
}
7584

76-
function logTriple(WhichEvent which, uint arg0, uint arg1, uint arg2) public {
85+
function logTriple(WhichEvent which, uint arg0, uint arg1, uint arg2) external {
7786
if (which == WhichEvent.LogTripleArg) emit LogTripleArg(arg0, arg1, arg2);
7887
else if (which == WhichEvent.LogTripleWithIndex) emit LogTripleWithIndex(arg0, arg1, arg2);
7988
else revert("Didn't match any allowable event index");
8089
}
8190

82-
function logQuadruple(WhichEvent which, uint arg0, uint arg1, uint arg2, uint arg3) public {
91+
function logQuadruple(WhichEvent which, uint arg0, uint arg1, uint arg2, uint arg3) external {
8392
if (which == WhichEvent.LogQuadrupleArg) emit LogQuadrupleArg(arg0, arg1, arg2, arg3);
8493
else if (which == WhichEvent.LogQuadrupleWithIndex) emit LogQuadrupleWithIndex(arg0, arg1, arg2, arg3);
8594
else revert("Didn't match any allowable event index");
8695
}
8796

88-
function logDynamicArgs(string memory arg0, string memory arg1) public {
97+
function logDynamicArgs(string memory arg0, string memory arg1) external {
8998
emit LogDynamicArgs(arg0, arg1);
9099
}
91100

92-
function logListArgs(bytes2[] memory arg0, bytes2[] memory arg1) public {
101+
function logListArgs(bytes2[] memory arg0, bytes2[] memory arg1) external {
93102
emit LogListArgs(arg0, arg1);
94103
}
95104

96-
function logAddressIndexedArgs(address arg0, address arg1) public {
105+
function logAddressIndexedArgs(address arg0, address arg1) external {
97106
emit LogAddressIndexed(arg0, arg1);
98107
}
99108

100-
function logAddressNotIndexedArgs(address arg0, address arg1) public {
109+
function logAddressNotIndexedArgs(address arg0, address arg1) external {
101110
emit LogAddressNotIndexed(arg0, arg1);
102111
}
103112

104-
function logBytes(bytes memory v) public {
113+
function logBytes(bytes memory v) external {
105114
emit LogBytes(v);
106115
}
107116

108-
function logString(string memory v) public {
117+
function logString(string memory v) external {
109118
emit LogString(v);
110119
}
111120

112-
function logStruct(uint arg0, TestTuple memory arg1) public {
121+
function logStruct(uint arg0, TestTuple memory arg1) external {
113122
emit LogStructArgs(arg0, arg1);
114123
}
124+
125+
function logIndexedAndNotIndexedArgs(
126+
address indexedAddress,
127+
uint256 indexedUint256,
128+
address nonIndexedAddress,
129+
uint256 nonIndexedUint256,
130+
string memory nonIndexedString
131+
) external {
132+
emit LogIndexedAndNotIndexed(
133+
indexedAddress,
134+
indexedUint256,
135+
nonIndexedAddress,
136+
nonIndexedUint256,
137+
nonIndexedString
138+
);
139+
}
115140
}
116141

web3/_utils/contract_sources/EventContracts.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ contract EventContract {
44
event LogSingleArg(uint256 arg0);
55
event LogSingleWithIndex(uint256 arg0);
66

7-
function logTwoEvents(uint256 _arg0) public {
7+
function logTwoEvents(uint256 _arg0) external {
88
emit LogSingleWithIndex(_arg0);
99
emit LogSingleArg(_arg0);
1010
}
@@ -14,7 +14,7 @@ contract IndexedEventContract {
1414
event LogSingleArg(uint256 arg0);
1515
event LogSingleWithIndex(uint256 indexed arg0);
1616

17-
function logTwoEvents(uint256 _arg0) public {
17+
function logTwoEvents(uint256 _arg0) external {
1818
emit LogSingleWithIndex(_arg0);
1919
emit LogSingleArg(_arg0);
2020
}

web3/_utils/contract_sources/contract_data/_custom_contract_data.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
"LogAddressIndexed": 16,
1919
"LogAddressNotIndexed": 17,
2020
"LogStructArgs": 18,
21+
"LogIndexedAndNotIndexed": 19,
2122
}

0 commit comments

Comments
 (0)