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

Validation: wrap local chain info maps with mutexes #669

Merged
merged 2 commits into from
Nov 4, 2024
Merged
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
35 changes: 17 additions & 18 deletions validation/uniqueness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"testing"

. "github.com/ethereum-optimism/superchain-registry/superchain"
Expand All @@ -20,23 +21,22 @@ type uniqueProperties struct {
RPC []string
}

type chainIDSet map[uint64]bool

func (s chainIDSet) AddIfUnique(id uint64) error {
if s[id] {
return fmt.Errorf("Chain with ID %d is duplicated", id)
}
s[id] = true
return nil
type chainInfo struct {
localChainIds sync.Map
localChainNames sync.Map
}

type chainNameSet map[string]bool
func (c *chainInfo) AddIfUnique(chainId uint64, chainName string) error {
// Check if chainId is unique
if _, exists := c.localChainIds.LoadOrStore(chainId, true); exists {
return fmt.Errorf("Chain with ID %d is duplicated", chainId)
}

func (s chainNameSet) AddIfUnique(name string) error {
if s[name] {
return fmt.Errorf("Chain with name %s is duplicated", name)
// Check if chainName is unique
if _, exists := c.localChainNames.LoadOrStore(chainName, true); exists {
return fmt.Errorf("Chain with name %s is duplicated", chainName)
}
s[name] = true

return nil
}

Expand Down Expand Up @@ -104,9 +104,8 @@ func getGlobalChains() (map[uint]*uniqueProperties, error) {
}

var (
globalChainIds map[uint]*uniqueProperties
localChainIds = make(chainIDSet)
localChainNames = make(chainNameSet)
globalChainIds map[uint]*uniqueProperties
localChains *chainInfo
)

func init() {
Expand All @@ -115,6 +114,7 @@ func init() {
if err != nil {
panic(err)
}
localChains = &chainInfo{}
}

func testIsGloballyUnique(t *testing.T, chain *ChainConfig) {
Expand All @@ -123,8 +123,7 @@ func testIsGloballyUnique(t *testing.T, chain *ChainConfig) {
globalChainName := props.Name
assert.Equal(t, globalChainName, chain.Name,
"Local chain name for %d does not match name from chainid.network", chain.ChainID)
assert.NoError(t, localChainIds.AddIfUnique(chain.ChainID))
assert.NoError(t, localChainNames.AddIfUnique(chain.Name))
assert.NoError(t, localChains.AddIfUnique(chain.ChainID, chain.Name))
normalizedURL, err := normalizeURL(chain.PublicRPC)
require.NoError(t, err)
assert.Contains(t, props.RPC, normalizedURL, "Specified RPC not specified in chainid.network")
Expand Down
Loading