Skip to content

Commit 68a059f

Browse files
committed
refactor: use slices.Contains to simplify code
Signed-off-by: chuanshanjida <chuanshanjida@outlook.com>
1 parent 4ab7ba4 commit 68a059f

3 files changed

Lines changed: 200 additions & 14 deletions

File tree

ante/cosmos/authz.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cosmos
22

33
import (
44
"fmt"
5+
"slices"
56

67
errorsmod "cosmossdk.io/errors"
78

@@ -79,11 +80,5 @@ func (ald AuthzLimiterDecorator) checkDisabledMsgs(msgs []sdk.Msg, isAuthzInnerM
7980
// isDisabledMsg returns true if the given message is in the list of restricted
8081
// messages from the AnteHandler.
8182
func (ald AuthzLimiterDecorator) isDisabledMsg(msgTypeURL string) bool {
82-
for _, disabledType := range ald.disabledMsgTypes {
83-
if msgTypeURL == disabledType {
84-
return true
85-
}
86-
}
87-
88-
return false
83+
return slices.Contains(ald.disabledMsgTypes, msgTypeURL)
8984
}

rpc/namespaces/ethereum/eth/filters/utils.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ Logs:
3232
continue
3333
}
3434
for i, sub := range topics {
35-
match := len(sub) == 0 // empty rule set == wildcard
36-
for _, topic := range sub {
37-
if log.Topics[i] == topic {
38-
match = true
39-
break
40-
}
41-
}
35+
match := len(sub) == 0 || slices.Contains(sub, log.Topics[i])
4236
if !match {
4337
continue Logs
4438
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package filters
2+
3+
import (
4+
"math/big"
5+
"testing"
6+
7+
"github.com/ethereum/go-ethereum/common"
8+
ethtypes "github.com/ethereum/go-ethereum/core/types"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestFilterLogs(t *testing.T) {
13+
addr1 := common.HexToAddress("0x1111111111111111111111111111111111111111")
14+
addr2 := common.HexToAddress("0x2222222222222222222222222222222222222222")
15+
16+
topic1 := common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")
17+
topic2 := common.HexToHash("0x2222222222222222222222222222222222222222222222222222222222222222")
18+
topic3 := common.HexToHash("0x3333333333333333333333333333333333333333333333333333333333333333")
19+
20+
testCases := []struct {
21+
name string
22+
logs []*ethtypes.Log
23+
fromBlock *big.Int
24+
toBlock *big.Int
25+
addresses []common.Address
26+
topics [][]common.Hash
27+
expected int
28+
}{
29+
{
30+
name: "no filters - return all logs",
31+
logs: []*ethtypes.Log{
32+
{Address: addr1, Topics: []common.Hash{topic1}, BlockNumber: 1},
33+
{Address: addr2, Topics: []common.Hash{topic2}, BlockNumber: 2},
34+
},
35+
fromBlock: nil,
36+
toBlock: nil,
37+
addresses: nil,
38+
topics: nil,
39+
expected: 2,
40+
},
41+
{
42+
name: "filter by block range",
43+
logs: []*ethtypes.Log{
44+
{Address: addr1, Topics: []common.Hash{topic1}, BlockNumber: 1},
45+
{Address: addr1, Topics: []common.Hash{topic1}, BlockNumber: 5},
46+
{Address: addr1, Topics: []common.Hash{topic1}, BlockNumber: 10},
47+
},
48+
fromBlock: big.NewInt(2),
49+
toBlock: big.NewInt(8),
50+
addresses: nil,
51+
topics: nil,
52+
expected: 1, // only block 5
53+
},
54+
{
55+
name: "filter by address",
56+
logs: []*ethtypes.Log{
57+
{Address: addr1, Topics: []common.Hash{topic1}, BlockNumber: 1},
58+
{Address: addr2, Topics: []common.Hash{topic1}, BlockNumber: 1},
59+
},
60+
fromBlock: nil,
61+
toBlock: nil,
62+
addresses: []common.Address{addr1},
63+
topics: nil,
64+
expected: 1,
65+
},
66+
{
67+
name: "filter by single topic - match",
68+
logs: []*ethtypes.Log{
69+
{Address: addr1, Topics: []common.Hash{topic1, topic2}, BlockNumber: 1},
70+
{Address: addr1, Topics: []common.Hash{topic2, topic1}, BlockNumber: 1},
71+
},
72+
fromBlock: nil,
73+
toBlock: nil,
74+
addresses: nil,
75+
topics: [][]common.Hash{{topic1}},
76+
expected: 1, // only first log matches topic1 in position 0
77+
},
78+
{
79+
name: "filter by single topic - no match (tests !slices.Contains branch)",
80+
logs: []*ethtypes.Log{
81+
{Address: addr1, Topics: []common.Hash{topic1, topic2}, BlockNumber: 1},
82+
{Address: addr1, Topics: []common.Hash{topic2, topic3}, BlockNumber: 1},
83+
},
84+
fromBlock: nil,
85+
toBlock: nil,
86+
addresses: nil,
87+
topics: [][]common.Hash{{topic3}}, // topic3 not in position 0 of any log
88+
expected: 0,
89+
},
90+
{
91+
name: "filter by multiple topics - all match",
92+
logs: []*ethtypes.Log{
93+
{Address: addr1, Topics: []common.Hash{topic1, topic2}, BlockNumber: 1},
94+
{Address: addr1, Topics: []common.Hash{topic1, topic3}, BlockNumber: 1},
95+
},
96+
fromBlock: nil,
97+
toBlock: nil,
98+
addresses: nil,
99+
topics: [][]common.Hash{{topic1}, {topic2}},
100+
expected: 1, // only first log has topic1 AND topic2
101+
},
102+
{
103+
name: "filter by multiple topics - second position no match (tests !slices.Contains branch)",
104+
logs: []*ethtypes.Log{
105+
{Address: addr1, Topics: []common.Hash{topic1, topic2}, BlockNumber: 1},
106+
{Address: addr1, Topics: []common.Hash{topic1, topic3}, BlockNumber: 1},
107+
},
108+
fromBlock: nil,
109+
toBlock: nil,
110+
addresses: nil,
111+
topics: [][]common.Hash{{topic1}, {topic1}}, // second position requires topic1, but logs have topic2/topic3
112+
expected: 0,
113+
},
114+
{
115+
name: "filter by OR topics - match any",
116+
logs: []*ethtypes.Log{
117+
{Address: addr1, Topics: []common.Hash{topic1, topic2}, BlockNumber: 1},
118+
{Address: addr1, Topics: []common.Hash{topic2, topic3}, BlockNumber: 1},
119+
{Address: addr1, Topics: []common.Hash{topic3, topic1}, BlockNumber: 1},
120+
},
121+
fromBlock: nil,
122+
toBlock: nil,
123+
addresses: nil,
124+
topics: [][]common.Hash{{topic1, topic2}}, // topic1 OR topic2 in position 0
125+
expected: 2, // first two logs match
126+
},
127+
{
128+
name: "filter by OR topics in multiple positions",
129+
logs: []*ethtypes.Log{
130+
{Address: addr1, Topics: []common.Hash{topic1, topic2}, BlockNumber: 1},
131+
{Address: addr1, Topics: []common.Hash{topic2, topic1}, BlockNumber: 1},
132+
{Address: addr1, Topics: []common.Hash{topic1, topic3}, BlockNumber: 1},
133+
},
134+
fromBlock: nil,
135+
toBlock: nil,
136+
addresses: nil,
137+
topics: [][]common.Hash{{topic1, topic2}, {topic1, topic2}}, // (topic1 OR topic2) AND (topic1 OR topic2)
138+
expected: 2, // first two logs match
139+
},
140+
{
141+
name: "topics filter longer than log topics - skip",
142+
logs: []*ethtypes.Log{
143+
{Address: addr1, Topics: []common.Hash{topic1}, BlockNumber: 1},
144+
},
145+
fromBlock: nil,
146+
toBlock: nil,
147+
addresses: nil,
148+
topics: [][]common.Hash{{topic1}, {topic2}}, // requires 2 topics, but log only has 1
149+
expected: 0,
150+
},
151+
{
152+
name: "empty topic sub-array (wildcard) - matches anything in that position",
153+
logs: []*ethtypes.Log{
154+
{Address: addr1, Topics: []common.Hash{topic1, topic2}, BlockNumber: 1},
155+
{Address: addr1, Topics: []common.Hash{topic2, topic3}, BlockNumber: 1},
156+
},
157+
fromBlock: nil,
158+
toBlock: nil,
159+
addresses: nil,
160+
topics: [][]common.Hash{}, // empty topics array = no topic filtering
161+
expected: 2,
162+
},
163+
{
164+
name: "empty inner topic sub-array - wildcard matches any topic in that position",
165+
logs: []*ethtypes.Log{
166+
{Address: addr1, Topics: []common.Hash{topic1, topic2}, BlockNumber: 1},
167+
{Address: addr1, Topics: []common.Hash{topic2, topic3}, BlockNumber: 1},
168+
},
169+
fromBlock: nil,
170+
toBlock: nil,
171+
addresses: nil,
172+
topics: [][]common.Hash{{}},
173+
expected: 2,
174+
},
175+
{
176+
name: "combined filters - address, block range, and topics",
177+
logs: []*ethtypes.Log{
178+
{Address: addr1, Topics: []common.Hash{topic1, topic2}, BlockNumber: 5},
179+
{Address: addr2, Topics: []common.Hash{topic1, topic2}, BlockNumber: 5},
180+
{Address: addr1, Topics: []common.Hash{topic2, topic2}, BlockNumber: 5},
181+
{Address: addr1, Topics: []common.Hash{topic1, topic2}, BlockNumber: 10},
182+
},
183+
fromBlock: big.NewInt(1),
184+
toBlock: big.NewInt(8),
185+
addresses: []common.Address{addr1},
186+
topics: [][]common.Hash{{topic1}},
187+
expected: 1, // only first log matches all criteria
188+
},
189+
}
190+
191+
for _, tc := range testCases {
192+
t.Run(tc.name, func(t *testing.T) {
193+
result := FilterLogs(tc.logs, tc.fromBlock, tc.toBlock, tc.addresses, tc.topics)
194+
require.Len(t, result, tc.expected, "expected %d logs, got %d", tc.expected, len(result))
195+
})
196+
}
197+
}

0 commit comments

Comments
 (0)