Skip to content

Commit

Permalink
Adding solana test selectors (#86)
Browse files Browse the repository at this point in the history
* adding solana test selectors

* cleaning

* changing to all selectors

* changing test chain ids
  • Loading branch information
yashnevatia authored Jan 8, 2025
1 parent 21b636a commit 0cca99a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
11 changes: 9 additions & 2 deletions genchains_solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"sort"
"strconv"
"strings"
"unicode"

"github.com/mr-tron/base58"
chain_selectors "github.com/smartcontractkit/chain-selectors"
)

Expand Down Expand Up @@ -104,7 +104,14 @@ func toVarName(name string, chainSel uint64) string {
const unnamed = "TEST"
x := strings.ReplaceAll(name, "-", "_")
x = strings.ToUpper(x)
if len(x) > 0 && unicode.IsDigit(rune(x[0])) {

// if len(x) > 0 && unicode.IsDigit(rune(x[0]))
// for evm, the above condition is used to detect if name == chainId == (some number) -> which means its a test chain
// for solana, as chainId is not a number but a base58 encoded hash, we cannot use the above condition
// we need to check if the name == chainId == a valid base58 encoded hash

_, err := base58.Decode(name)
if err == nil {
x = unnamed + "_" + x
}
if len(x) == 0 {
Expand Down
12 changes: 9 additions & 3 deletions generated_chains_solana.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func getChainInfo(selector uint64) (chainInfo, error) {
return chainInfo{}, fmt.Errorf("failed to get %s chain ID from selector %d: %w", chainID, selector, err)
}

details, exist := solanaSelectorsMap[chainID]
details, exist := solanaChainIdToChainSelector[chainID]
if !exist {
return chainInfo{}, fmt.Errorf("invalid chain id %s for %s", chainID, family)
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func GetChainDetailsByChainIDAndFamily(chainID string, family string) (ChainDeta

return details, nil
case FamilySolana:
details, exist := solanaSelectorsMap[chainID]
details, exist := solanaChainIdToChainSelector[chainID]
if !exist {
return ChainDetails{}, fmt.Errorf("invalid chain id %s for %s", chainID, family)
}
Expand Down
29 changes: 24 additions & 5 deletions solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,34 @@ import (
//go:embed selectors_solana.yml
var solanaSelectorsYml []byte

//go:embed test_selectors_solana.yml
var testSelectorsSolanaYml []byte

var (
solanaSelectorsMap = parseSolanaYml(solanaSelectorsYml)
solanaChainIdBySelector = make(map[uint64]string)
solanaSelectorsMap = parseSolanaYml(solanaSelectorsYml)
solanaTestSelectorsMap = parseSolanaYml(testSelectorsSolanaYml)
solanaChainIdToChainSelector = loadAllSolanaSelectors()
solanaChainIdBySelector = make(map[uint64]string)
)

func init() {
for k, v := range solanaSelectorsMap {
solanaChainIdBySelector[v.ChainSelector] = k
}
for k, v := range solanaTestSelectorsMap {
solanaChainIdBySelector[v.ChainSelector] = k
}
}

func loadAllSolanaSelectors() map[string]ChainDetails {
output := make(map[string]ChainDetails, len(solanaSelectorsMap)+len(solanaTestSelectorsMap))
for k, v := range solanaSelectorsMap {
output[k] = v
}
for k, v := range solanaTestSelectorsMap {
output[k] = v
}
return output
}

func parseSolanaYml(ymlFile []byte) map[string]ChainDetails {
Expand Down Expand Up @@ -52,15 +71,15 @@ func validateSolanaChainID(data map[string]ChainDetails) {
}

func SolanaChainIdToChainSelector() map[string]uint64 {
copyMap := make(map[string]uint64, len(solanaSelectorsMap))
for k, v := range solanaSelectorsMap {
copyMap := make(map[string]uint64, len(solanaChainIdToChainSelector))
for k, v := range solanaChainIdToChainSelector {
copyMap[k] = v.ChainSelector
}
return copyMap
}

func SolanaNameFromChainId(chainId string) (string, error) {
details, exist := solanaSelectorsMap[chainId]
details, exist := solanaChainIdToChainSelector[chainId]
if !exist {
return "", fmt.Errorf("chain name not found for chain %v", chainId)
}
Expand Down
7 changes: 7 additions & 0 deletions solana_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ func Test_SolanaGetChainIDByChainSelector(t *testing.T) {
assert.Equal(t, chainID, fmt.Sprintf("%v", k))
}
}

func Test_SolanaNoOverlapBetweenRealAndTestChains(t *testing.T) {
for k, _ := range solanaSelectorsMap {
_, exist := solanaTestSelectorsMap[k]
assert.False(t, exist, "Chain %d is duplicated between real and test chains", k)
}
}
9 changes: 9 additions & 0 deletions test_selectors_solana.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
selectors:
# "11111111111111111111111111111111111111111111" -> system program address
"22222222222222222222222222222222222222222222":
selector: 12463857294658392847
"33333333333333333333333333333333333333333333":
selector: 9837465928374658293
"44444444444444444444444444444444444444444444":
selector: 16574839267584930184

0 comments on commit 0cca99a

Please sign in to comment.