-
Notifications
You must be signed in to change notification settings - Fork 32
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
Add NetworkByChainID function to return network and blockchain for registered ChainID #460
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,36 @@ | ||
package core | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/iden3/go-iden3-core/v2/w3c" | ||
) | ||
|
||
// ChainID is alias for int32 that represents ChainID | ||
type ChainID int32 | ||
|
||
// ChainIDs Object containing chain IDs for various blockchains and networks. | ||
var chainIDs = map[string]ChainID{ | ||
"eth:main": 1, | ||
"eth:goerli": 5, | ||
"eth:sepolia": 11155111, | ||
"polygon:main": 137, | ||
"polygon:mumbai": 80001, | ||
"zkevm:main": 1101, | ||
"zkevm:test": 1442, | ||
type chainIDKey struct { | ||
blockchain Blockchain | ||
networkID NetworkID | ||
} | ||
|
||
var ErrChainIDNotRegistered = errors.New("chainID is not registered") | ||
|
||
var chainIDsLock sync.RWMutex | ||
|
||
// chainIDs Object containing chain IDs for various blockchains and networks. | ||
// It can be modified using RegisterChainID public function. So it is guarded | ||
// by chainIDsLock mutex. | ||
var chainIDs = map[chainIDKey]ChainID{ | ||
{Ethereum, Main}: 1, | ||
{Ethereum, Goerli}: 5, | ||
{Ethereum, Sepolia}: 11155111, | ||
{Polygon, Main}: 137, | ||
{Polygon, Mumbai}: 80001, | ||
{ZkEVM, Main}: 1101, | ||
{ZkEVM, Test}: 1442, | ||
} | ||
|
||
// ChainIDfromDID returns chain name from w3c.DID | ||
|
@@ -38,25 +51,27 @@ func ChainIDfromDID(did w3c.DID) (ChainID, error) { | |
return 0, err | ||
} | ||
|
||
chainID, ok := chainIDs[fmt.Sprintf("%s:%s", blockchain, networkID)] | ||
if !ok { | ||
return 0, fmt.Errorf("chainID not found for %s:%s", blockchain, networkID) | ||
} | ||
|
||
return chainID, nil | ||
return GetChainID(blockchain, networkID) | ||
} | ||
|
||
// RegisterChainID registers chainID for blockchain and network | ||
func RegisterChainID(blockchain Blockchain, network NetworkID, chainID int) error { | ||
k := fmt.Sprintf("%s:%s", blockchain, network) | ||
chainIDsLock.Lock() | ||
defer chainIDsLock.Unlock() | ||
|
||
k := chainIDKey{ | ||
blockchain: blockchain, | ||
networkID: network, | ||
} | ||
existingChainID, ok := chainIDs[k] | ||
if ok && existingChainID == ChainID(chainID) { | ||
return nil | ||
} | ||
|
||
for _, v := range chainIDs { | ||
if v == ChainID(chainID) { | ||
return fmt.Errorf(`can't register chain id %d for '%s' because it's already registered for another chain id`, chainID, k) | ||
return fmt.Errorf(`can't register chain id %d for '%v:%v' because it's already registered for another chain id`, | ||
chainID, k.blockchain, k.networkID) | ||
} | ||
} | ||
|
||
|
@@ -67,10 +82,31 @@ func RegisterChainID(blockchain Blockchain, network NetworkID, chainID int) erro | |
|
||
// GetChainID returns chainID for blockchain and network | ||
func GetChainID(blockchain Blockchain, network NetworkID) (ChainID, error) { | ||
k := fmt.Sprintf("%s:%s", blockchain, network) | ||
chainIDsLock.RLock() | ||
defer chainIDsLock.RUnlock() | ||
|
||
k := chainIDKey{ | ||
blockchain: blockchain, | ||
networkID: network, | ||
} | ||
if _, ok := chainIDs[k]; !ok { | ||
return 0, fmt.Errorf("chainID not registered for %s:%s", blockchain, network) | ||
return 0, fmt.Errorf("%w for %s:%s", ErrChainIDNotRegistered, blockchain, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Retrun NoChain constant? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NoChain has a Blockchain type, not ChainID. |
||
network) | ||
} | ||
|
||
return chainIDs[k], nil | ||
} | ||
|
||
// NetworkByChainID returns blockchain and networkID for registered chain ID. | ||
// Or ErrChainIDNotRegistered error if chainID is not registered. | ||
func NetworkByChainID(chainID ChainID) (Blockchain, NetworkID, error) { | ||
chainIDsLock.RLock() | ||
defer chainIDsLock.RUnlock() | ||
|
||
for k, v := range chainIDs { | ||
if v == chainID { | ||
return k.blockchain, k.networkID, nil | ||
} | ||
} | ||
return NoChain, NoNetwork, ErrChainIDNotRegistered | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNetworkByChainID(t *testing.T) { | ||
chainID, err := GetChainID(Ethereum, Main) | ||
require.NoError(t, err) | ||
blockchain, networkID, err := NetworkByChainID(chainID) | ||
require.NoError(t, err) | ||
require.Equal(t, Ethereum, blockchain) | ||
require.Equal(t, Main, networkID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@olomix Do we really need sync.RWMutex here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What other options do we have to guard concurrent access to global dict?
We can use sync.Map. But typed map with lock I like more.