Skip to content

Commit 20b09f9

Browse files
authored
feat: add methods to test for the existence of a chain (#114)
Adds two methods `Exists` and `ExistsN` to the Blockchain struct to check if a chain exists in the database. `Exists` checks the existence of a single selector to return true. `ExistsN` checks the existence of multiple selectors, all of which must exist for the method to return true.
1 parent 91ac227 commit 20b09f9

File tree

3 files changed

+96
-9
lines changed

3 files changed

+96
-9
lines changed

.changeset/angry-trains-end.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+
Add `Exists` and `ExistsN` methods to `Blockchains` to test for the existence of a chain for the provided selector/s

chain/blockchain.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ var _ BlockChain = aptos.Chain{}
1616
var _ BlockChain = sui.Chain{}
1717
var _ BlockChain = ton.Chain{}
1818

19+
// BlockChain is an interface that represents a chain.
20+
// A chain can be an EVM chain, Solana chain Aptos chain or others.
21+
type BlockChain interface {
22+
// String returns chain name and selector "<name> (<selector>)"
23+
String() string
24+
// Name returns the name of the chain
25+
Name() string
26+
ChainSelector() uint64
27+
Family() string
28+
}
29+
1930
// BlockChains represents a collection of chains.
2031
// It provides querying capabilities for different types of chains.
2132
type BlockChains struct {
@@ -41,15 +52,22 @@ func NewBlockChains(chains map[uint64]BlockChain) BlockChains {
4152
}
4253
}
4354

44-
// BlockChain is an interface that represents a chain.
45-
// A chain can be an EVM chain, Solana chain Aptos chain or others.
46-
type BlockChain interface {
47-
// String returns chain name and selector "<name> (<selector>)"
48-
String() string
49-
// Name returns the name of the chain
50-
Name() string
51-
ChainSelector() uint64
52-
Family() string
55+
// Exists checks if a chain with the given selector exists.
56+
func (b BlockChains) Exists(selector uint64) bool {
57+
_, ok := b.chains[selector]
58+
59+
return ok
60+
}
61+
62+
// ExistsN checks if all chains with the given selectors exist.
63+
func (b BlockChains) ExistsN(selectors ...uint64) bool {
64+
for _, selector := range selectors {
65+
if !b.Exists(selector) {
66+
return false
67+
}
68+
}
69+
70+
return true
5371
}
5472

5573
// EVMChains returns a map of all EVM chains with their selectors.

chain/blockchain_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,70 @@ func TestNewBlockChains(t *testing.T) {
4747
})
4848
}
4949

50+
func Test_BlockChains_Exists(t *testing.T) {
51+
t.Parallel()
52+
53+
chains := buildBlockChains()
54+
55+
tests := []struct {
56+
name string
57+
selector uint64
58+
expected bool
59+
}{
60+
{
61+
name: "exists",
62+
selector: evmChain1.Selector,
63+
expected: true,
64+
},
65+
{
66+
name: "does not exist",
67+
selector: 99999999,
68+
expected: false,
69+
},
70+
}
71+
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
t.Parallel()
75+
76+
exists := chains.Exists(tt.selector)
77+
assert.Equal(t, tt.expected, exists)
78+
})
79+
}
80+
}
81+
82+
func Test_BlockChains_ExistsN(t *testing.T) {
83+
t.Parallel()
84+
85+
chains := buildBlockChains()
86+
87+
tests := []struct {
88+
name string
89+
selectors []uint64
90+
expected bool
91+
}{
92+
{
93+
name: "all exist",
94+
selectors: []uint64{evmChain1.Selector, solanaChain1.Selector},
95+
expected: true,
96+
},
97+
{
98+
name: "some do not exist",
99+
selectors: []uint64{evmChain1.Selector, 99999999},
100+
expected: false,
101+
},
102+
}
103+
104+
for _, tt := range tests {
105+
t.Run(tt.name, func(t *testing.T) {
106+
t.Parallel()
107+
108+
exists := chains.ExistsN(tt.selectors...)
109+
assert.Equal(t, tt.expected, exists)
110+
})
111+
}
112+
}
113+
50114
func TestBlockChainsEVMChains(t *testing.T) {
51115
t.Parallel()
52116

0 commit comments

Comments
 (0)