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 1 commit
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
53 changes: 36 additions & 17 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,39 @@ type uniqueProperties struct {
RPC []string
}

type chainIDSet map[uint64]bool
type (
chainIDSet map[uint64]bool
chainNameSet map[string]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 chainIDSet
localChainNames chainNameSet
chainIDMutex sync.RWMutex
chainNameMutex sync.RWMutex
}

type chainNameSet map[string]bool
func (c *chainInfo) AddIfUnique(chainId uint64, chainName string) error {
// Check if chainId is unique
c.chainIDMutex.RLock()
if c.localChainIds[chainId] {
c.chainIDMutex.RUnlock()
return fmt.Errorf("Chain with ID %d is duplicated", chainId)
}
c.chainIDMutex.RUnlock()

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
c.chainNameMutex.RLock()
if c.localChainIds[chainId] {
vdamle marked this conversation as resolved.
Show resolved Hide resolved
c.chainNameMutex.RUnlock()
return fmt.Errorf("Chain with name %s is duplicated", chainName)
}
s[name] = true
c.chainNameMutex.RUnlock()

c.chainNameMutex.Lock()
c.localChainNames[chainName] = true
c.chainNameMutex.Unlock()

return nil
}

Expand Down Expand Up @@ -104,9 +121,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 +131,10 @@ func init() {
if err != nil {
panic(err)
}
localChains = &chainInfo{
localChainIds: make(chainIDSet),
localChainNames: make(chainNameSet),
}
}

func testIsGloballyUnique(t *testing.T, chain *ChainConfig) {
Expand All @@ -123,8 +143,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