Skip to content

Commit bb450f4

Browse files
fix(chain): ListChainSelectors filter with multiple families (#119)
- Use map instead of slice for faster lookup - WithFamily now supports more than 1 family, eg `ListChainSelectors(WithFamily("evm"), WithFamily("aptos")`
1 parent bbcfc36 commit bb450f4

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

.changeset/gold-eels-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"chainlink-deployments-framework": patch
3+
---
4+
5+
ListChainSelectors can filter multiple families

chain/blockchain.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,32 @@ func (b BlockChains) TonChains() map[uint64]ton.Chain {
144144
type ChainSelectorsOption func(*chainSelectorsOptions)
145145

146146
type chainSelectorsOptions struct {
147-
family string
148-
excluding []uint64
147+
// Use map for faster lookups
148+
includedFamilies map[string]struct{}
149+
excludedChainSels map[uint64]struct{}
149150
}
150151

151152
// WithFamily returns an option to filter chains by family (evm, solana, aptos)
152153
// Use constants from chain_selectors package eg WithFamily(chain_selectors.FamilySolana)
154+
// This can be used more than once to include multiple families.
153155
func WithFamily(family string) ChainSelectorsOption {
154156
return func(o *chainSelectorsOptions) {
155-
o.family = family
157+
if o.includedFamilies == nil {
158+
o.includedFamilies = make(map[string]struct{})
159+
}
160+
o.includedFamilies[family] = struct{}{}
156161
}
157162
}
158163

159164
// WithChainSelectorsExclusion returns an option to exclude specific chain selectors
160165
func WithChainSelectorsExclusion(chainSelectors []uint64) ChainSelectorsOption {
161166
return func(o *chainSelectorsOptions) {
162-
o.excluding = chainSelectors
167+
if o.excludedChainSels == nil {
168+
o.excludedChainSels = make(map[uint64]struct{})
169+
}
170+
for _, selector := range chainSelectors {
171+
o.excludedChainSels[selector] = struct{}{}
172+
}
163173
}
164174
}
165175

@@ -168,11 +178,7 @@ func WithChainSelectorsExclusion(chainSelectors []uint64) ChainSelectorsOption {
168178
// - WithFamily: filter by family eg WithFamily(chain_selectors.FamilySolana)
169179
// - WithChainSelectorsExclusion: exclude specific chain selectors
170180
func (b BlockChains) ListChainSelectors(options ...ChainSelectorsOption) []uint64 {
171-
// Initialize default options
172-
opts := chainSelectorsOptions{
173-
family: "",
174-
excluding: []uint64{},
175-
}
181+
opts := chainSelectorsOptions{}
176182

177183
// Apply all provided options
178184
for _, option := range options {
@@ -182,14 +188,13 @@ func (b BlockChains) ListChainSelectors(options ...ChainSelectorsOption) []uint6
182188
selectors := make([]uint64, 0, len(b.chains))
183189

184190
for selector, chain := range b.chains {
185-
// Skip if in exclusion list
186-
if slices.Contains(opts.excluding, selector) {
187-
continue
191+
if opts.excludedChainSels != nil {
192+
if _, excluded := opts.excludedChainSels[selector]; excluded {
193+
continue
194+
}
188195
}
189-
190-
// Apply family filter if specified
191-
if opts.family != "" {
192-
if opts.family != chain.Family() {
196+
if opts.includedFamilies != nil {
197+
if _, ok := opts.includedFamilies[chain.Family()]; !ok {
193198
continue
194199
}
195200
}

chain/blockchain_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ func TestBlockChainsListChainSelectors(t *testing.T) {
230230
expectedIDs: []uint64{tonChain1.Selector},
231231
description: "expected Ton chain selectors",
232232
},
233+
{
234+
name: "with multiple families",
235+
options: []chain.ChainSelectorsOption{chain.WithFamily(chain_selectors.FamilyEVM), chain.WithFamily(chain_selectors.FamilySolana)},
236+
expectedIDs: []uint64{evmChain1.Selector, evmChain2.Selector, solanaChain1.Selector},
237+
description: "expected EVM and Solana chain selectors",
238+
},
233239
{
234240
name: "with exclusion",
235241
options: []chain.ChainSelectorsOption{chain.WithChainSelectorsExclusion(
@@ -260,7 +266,7 @@ func TestBlockChainsListChainSelectors(t *testing.T) {
260266
}
261267

262268
// buildBlockChains creates a new BlockChains instance with the test chains.
263-
// 2 evm chains, 1 solana chain, 1 aptos chain, 1 sui chain
269+
// 2 evm chains, 1 solana chain, 1 aptos chain, 1 sui chain, 1 ton chain.
264270
func buildBlockChains() chain.BlockChains {
265271
chains := chain.NewBlockChains(map[uint64]chain.BlockChain{
266272
evmChain1.ChainSelector(): evmChain1,

0 commit comments

Comments
 (0)