Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing chain names together with selectors #9

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ module github.com/smartcontractkit/chain-selectors

go 1.20

require gopkg.in/yaml.v3 v3.0.1
require (
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
46 changes: 39 additions & 7 deletions selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chain_selectors
import (
_ "embed"
"fmt"
"strconv"

"gopkg.in/yaml.v3"
)
Expand All @@ -13,13 +14,18 @@ var selectorsYml []byte
//go:embed test_selectors.yml
var testSelectorsYml []byte

type chainDetails struct {
ChainSelector uint64 `yaml:"selector"`
ChainName string `yaml:"name"`
}

var selectorsMap = parseYml(selectorsYml)
var testSelectorsMap = parseYml(testSelectorsYml)

var evmChainIdToChainSelector = loadAllSelectors()

func loadAllSelectors() map[uint64]uint64 {
output := make(map[uint64]uint64, len(selectorsMap)+len(testSelectorsMap))
func loadAllSelectors() map[uint64]chainDetails {
output := make(map[uint64]chainDetails, len(selectorsMap)+len(testSelectorsMap))
for k, v := range selectorsMap {
output[k] = v
}
Expand All @@ -29,9 +35,9 @@ func loadAllSelectors() map[uint64]uint64 {
return output
}

func parseYml(ymlFile []byte) map[uint64]uint64 {
func parseYml(ymlFile []byte) map[uint64]chainDetails {
type ymlData struct {
Selectors map[uint64]uint64 `yaml:"selectors"`
Selectors map[uint64]chainDetails `yaml:"selectors"`
}

var data ymlData
Expand All @@ -46,14 +52,14 @@ func parseYml(ymlFile []byte) map[uint64]uint64 {
func EvmChainIdToChainSelector() map[uint64]uint64 {
copyMap := make(map[uint64]uint64, len(evmChainIdToChainSelector))
for k, v := range evmChainIdToChainSelector {
copyMap[k] = v
copyMap[k] = v.ChainSelector
}
return copyMap
}

func ChainIdFromSelector(chainSelectorId uint64) (uint64, error) {
for k, v := range evmChainIdToChainSelector {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since these mappings are all internal/private anyway, should we pre-calculate a chainSelectorToEvmChainId at init time, and use here? The memory increase should be minimal, but those may be accessed very often

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think lib users should use accessing functions like ChainIdFromSelector instead of accessing the map directly. However, if they for some reason need that map then it's rebuilt every time requested because this is a defensive way to prevent altering the internal state. Returning a single precomputed map may lead to a problem in which if users want to add something to this map, they update it for all. A map is passed by a pointer/reference, not a value.

Copy link
Contributor

@andrevmatos andrevmatos Dec 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I expect this to be accessed from the functions. I was not telling about exposing/returning the map, but just pre-calculating a private one from selectors to chainIds (and maybe also chainIds to names for the other function), instead of having to for-loop it on every function call. It's the classic memory-cpu tradeoff.

if v == chainSelectorId {
if v.ChainSelector == chainSelectorId {
return k, nil
}
}
Expand All @@ -62,11 +68,37 @@ func ChainIdFromSelector(chainSelectorId uint64) (uint64, error) {

func SelectorFromChainId(chainId uint64) (uint64, error) {
if chainSelectorId, exist := evmChainIdToChainSelector[chainId]; exist {
return chainSelectorId, nil
return chainSelectorId.ChainSelector, nil
}
return 0, fmt.Errorf("chain selector not found for chain %d", chainId)
}

func NameFromChainId(chainId uint64) (string, error) {
mateusz-sekara marked this conversation as resolved.
Show resolved Hide resolved
details, exist := evmChainIdToChainSelector[chainId]
if !exist {
return "", fmt.Errorf("chain name not found for chain %d", chainId)
}
if details.ChainName == "" {
return strconv.FormatUint(chainId, 10), nil
}
return details.ChainName, nil
}

func ChainIdFromName(name string) (uint64, error) {
for k, v := range evmChainIdToChainSelector {
if v.ChainName == name {
return k, nil
}
}
chainId, err := strconv.ParseUint(name, 10, 64)
if err == nil {
if details, exist := evmChainIdToChainSelector[chainId]; exist && details.ChainName == "" {
return chainId, nil
}
}
return 0, fmt.Errorf("chain not found for name %s", name)
}

func TestChainIds() []uint64 {
chainIds := make([]uint64, 0, len(testSelectorsMap))
for k := range testSelectorsMap {
Expand Down
111 changes: 83 additions & 28 deletions selectors.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,86 @@
selectors:
# Testnets
97: 13264668187771770619 # BSC Testnet
280: 6802309497652714138 # ZKSync Era
420: 2664363617261496610 # Optimism Goerli
1112: 9284632837123596123 # Wemix testnet
1337: 3379446385462418246 # Dev network
1442: 11059667695644972511 # Polygon zkEVM Testnet
2358: 5990477251245693094 # Kroma Sepolia
43113: 14767482510784806043 # Avalanche Fuji
76578: 781901677223027175
84531: 5790810961207155433 # BASE Goerli
84532: 10344971235874465080 # BASE Sepolia
80001: 12532609583862916517 # Polygon Mumbai
421613: 6101244977088475029 # Arbitrum Goerli
421614: 3478487238524512106 # Arbitrum Sepolia
534351: 2279865765895943307 # Scroll Sepolia
11155111: 16015286601757825753 # Sepolia
11155420: 5224473277236331295 # Optimism Sepolia
97:
selector: 13264668187771770619
name: "binance_smart_chain-testnet"
280:
selector: 6802309497652714138
name: "ethereum-testnet-goerli-zksync-1"
420:
selector: 2664363617261496610
name: "ethereum-testnet-goerli-optimism-1"
1112:
selector: 9284632837123596123
name: "wemix-testnet"
1337: # Dev network
selector: 3379446385462418246
1442:
selector: 11059667695644972511
name: "ethereum-testnet-goerli-polygon-zkevm-1"
2358:
selector: 5990477251245693094
name: "ethereum-testnet-sepolia-kroma-1"
43113:
selector: 14767482510784806043
name: "avalanche-testnet-fuji"
76578:
selector: 781901677223027175
80001:
selector: 12532609583862916517
name: "polygon-testnet-mumbai"
84531:
selector: 5790810961207155433
name: "ethereum-testnet-goerli-base-1"
84532:
selector: 10344971235874465080
name: "ethereum-testnet-sepolia-base-1"
421613:
selector: 6101244977088475029
name: "ethereum-testnet-goerli-arbitrum-1"
421614:
selector: 3478487238524512106
name: "ethereum-testnet-sepolia-arbitrum-1"
534351:
selector: 2279865765895943307
name: "ethereum-testnet-sepolia-scroll-1"
11155111:
selector: 16015286601757825753
name: "ethereum-testnet-sepolia"
11155420:
selector: 5224473277236331295
name: "ethereum-testnet-sepolia-optimism-1"

# Mainnets
1: 5009297550715157269 # Ethereum
10: 3734403246176062136 # Optimism
56: 11344663589394136015 # BSC
137: 4051577828743386545 # Polygon
255: 3719320017875267166 # Kroma
1101: 4348158687435793198 # Polygon zkEVM
1111: 5142893604156789321 # Wemix
8453: 15971525489660198786 # BASE
42161: 4949039107694359620 # Arbitrum
43114: 6433500567565415381 # Avalanche
534352: 13204309965629103672 # Scroll
1:
selector: 5009297550715157269
name: "ethereum-mainnet"
10:
selector: 3734403246176062136
name: "ethereum-mainnet-optimism-1"
56:
selector: 11344663589394136015
name: "binance_smart_chain-mainnet"
137:
selector: 4051577828743386545
name: "polygon-mainnet"
255:
selector: 3719320017875267166
name: "ethereum-mainnet-kroma-1"
1101:
selector: 4348158687435793198
name: "ethereum-mainnet-polygon-zkevm-1"
1111:
selector: 5142893604156789321
name: "wemix-mainnet"
8453:
selector: 15971525489660198786
name: "ethereum-mainnet-base-1"
42161:
selector: 4949039107694359620
name: "ethereum-mainnet-arbitrum-1"
43114:
selector: 6433500567565415381
name: "avalanche-mainnet"
534352:
selector: 13204309965629103672
name: "ethereum-mainnet-scroll-1"
Loading
Loading