github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/types/chain_id.go (about) 1 package types 2 3 import ( 4 "fmt" 5 "math/big" 6 "regexp" 7 "strings" 8 "sync" 9 10 sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors" 11 tendermintTypes "github.com/fibonacci-chain/fbc/libs/tendermint/types" 12 ) 13 14 var ( 15 regexChainID = `[a-z]*` 16 regexSeparator = `-{1}` 17 regexEpoch = `[1-9][0-9]*` 18 ethermintChainID = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, regexChainID, regexSeparator, regexEpoch)) 19 ) 20 21 const mainnetChainId = "fbc-1230" 22 const testnetChainId = "fbc-3021" 23 24 const JupiterBeforeMainNetChainId = 1230 25 const JupiterBeforeTestNetChainId = 3021 26 27 const JupiterMainNetChainId = 12306 28 const JupiterTestNetChainId = 12307 29 30 var ( 31 chainIdSetOnce sync.Once 32 chainIdCache string 33 chainIdEpochCache *big.Int 34 ) 35 36 // IsValidChainID returns false if the given chain identifier is incorrectly formatted. 37 func IsValidChainID(chainID string) bool { 38 if len(chainID) > 48 { 39 return false 40 } 41 42 return ethermintChainID.MatchString(chainID) 43 } 44 func isMainNetChainID(chainID string) bool { 45 return chainID == mainnetChainId 46 } 47 func isTestNetChainID(chainID string) bool { 48 return chainID == testnetChainId 49 } 50 51 func IsMainNetChainID(chainID string) bool { 52 return chainID == mainnetChainId 53 } 54 func IsTestNetChainID(chainID string) bool { 55 return chainID == testnetChainId 56 } 57 58 func SetChainId(chainid string) error { 59 epoch, err := ParseChainID(chainid) 60 if err != nil { 61 return err 62 } 63 chainIdSetOnce.Do(func() { 64 chainIdCache = chainid 65 chainIdEpochCache = epoch 66 }) 67 return nil 68 } 69 70 // ParseChainID parses a string chain identifier's epoch to an Ethereum-compatible 71 // chain-id in *big.Int format. The function returns an error if the chain-id has an invalid format 72 func ParseChainID(chainID string) (*big.Int, error) { 73 //use chainIdEpochCache first. 74 if chainID == chainIdCache && chainIdEpochCache != nil { 75 return chainIdEpochCache, nil 76 } 77 chainID = strings.TrimSpace(chainID) 78 if len(chainID) > 48 { 79 return nil, sdkerrors.Wrapf(ErrInvalidChainID, "chain-id '%s' cannot exceed 48 chars", chainID) 80 } 81 82 matches := ethermintChainID.FindStringSubmatch(chainID) 83 if matches == nil || len(matches) != 3 || matches[1] == "" { 84 return nil, sdkerrors.Wrap(ErrInvalidChainID, chainID) 85 } 86 87 // verify that the chain-id entered is a base 10 integer 88 chainIDInt, ok := new(big.Int).SetString(matches[2], 10) 89 if !ok { 90 return nil, sdkerrors.Wrapf(ErrInvalidChainID, "epoch %s must be base-10 integer format", matches[2]) 91 } 92 93 return chainIDInt, nil 94 } 95 96 func IsValidateChainIdWithGenesisHeight(chainID string) error { 97 if isMainNetChainID(chainID) && !tendermintTypes.IsMainNet() { 98 return fmt.Errorf("Must use <make mainnet> to rebuild if chain-id is <%s>, Current GenesisHeight is <%d>", chainID, tendermintTypes.GetStartBlockHeight()) 99 } 100 //if isTestNetChainID(chainID) && !tendermintTypes.IsTestNet() { 101 // return fmt.Errorf("Must use <make testnet> to rebuild if chain-id is <%s>, Current GenesisHeight is <%d>", chainID, tendermintTypes.GetStartBlockHeight()) 102 //} 103 return nil 104 } 105 106 func IsJupiterBeforeMainNetChainId(chainID *big.Int) bool { 107 return chainID.Int64() == JupiterBeforeMainNetChainId 108 } 109 110 func IsJupiterBeforeTestNetChainId(chainID *big.Int) bool { 111 return chainID.Int64() == JupiterBeforeTestNetChainId 112 }