From 4a4035b43a1aecc9d8193061bce5d204264c7da0 Mon Sep 17 00:00:00 2001 From: riyasng12 Date: Fri, 12 Jan 2024 17:09:03 +0530 Subject: [PATCH] fix: bug fixes and validation check for polkadot and kusama --- cli/cmd/chains/kusama/cmd.go | 2 +- cli/cmd/chains/kusama/run.go | 2 +- cli/cmd/chains/polkadot/cmd.go | 2 +- cli/cmd/chains/polkadot/run.go | 2 +- cli/cmd/chains/utils/types.go | 42 ++++++++++++++++++++++------------ cli/common/constants.go | 1 + cli/common/errors.go | 1 + cli/common/utils.go | 20 +++++++++++++++- 8 files changed, 53 insertions(+), 19 deletions(-) diff --git a/cli/cmd/chains/kusama/cmd.go b/cli/cmd/chains/kusama/cmd.go index 9d949d07..6655ee64 100644 --- a/cli/cmd/chains/kusama/cmd.go +++ b/cli/cmd/chains/kusama/cmd.go @@ -30,7 +30,7 @@ const ( ) var ( - kusamaParachains = []string{"altair", "bajun", "bifrost", "calamari", "encointer", "khala", "kintsugi-btc", "litmus", "mangata", "moonriver", "robonomics", "subzero", "turing"} + kusamaParachains = []string{"altair", "bajun", "bifrost", "calamari", "encointer", "karura", "khala", "kintsugi-btc", "litmus", "mangata", "moonriver", "robonomics", "subzero", "turing"} ) var KusamaCmd = common.NewDiveCommandBuilder(). diff --git a/cli/cmd/chains/kusama/run.go b/cli/cmd/chains/kusama/run.go index 5aa0d2c2..830d74a1 100644 --- a/cli/cmd/chains/kusama/run.go +++ b/cli/cmd/chains/kusama/run.go @@ -267,7 +267,7 @@ func configureService(serviceConfig *utils.PolkadotServiceConfig) error { func flagCheck() error { if configFilePath != "" { - if len(paraChain) != 0 || network != "" || explorer || metrics { + if len(paraChain) != 0 || network != "" || explorer || metrics || noRelay{ return common.WrapMessageToError(common.ErrInvalidFlag, "The '-c' flag does not allow additional flags.") } } diff --git a/cli/cmd/chains/polkadot/cmd.go b/cli/cmd/chains/polkadot/cmd.go index 5311721e..effb7754 100644 --- a/cli/cmd/chains/polkadot/cmd.go +++ b/cli/cmd/chains/polkadot/cmd.go @@ -30,7 +30,7 @@ const ( ) var ( - polkadotParachains = []string{"acala", "ajuna", "bifrost", "centrifuge", "clover", "frequency", "integritee", "interlay", "karura", "kilt", "kylin", "litentry", "manta", "moonbeam", "moonsama", "nodle", "parallel", "pendulum", "phala", "polkadex", "subsocial", "zeitgeist"} + polkadotParachains = []string{"acala", "ajuna", "bifrost", "centrifuge", "clover", "frequency", "integritee", "interlay", "kilt", "kylin", "litentry", "manta", "moonbeam", "moonsama", "nodle", "parallel", "pendulum", "phala", "polkadex", "subsocial", "zeitgeist"} ) var PolkadotCmd = common.NewDiveCommandBuilder(). diff --git a/cli/cmd/chains/polkadot/run.go b/cli/cmd/chains/polkadot/run.go index 4a3a8593..2bb6ef5c 100644 --- a/cli/cmd/chains/polkadot/run.go +++ b/cli/cmd/chains/polkadot/run.go @@ -267,7 +267,7 @@ func configureService(serviceConfig *utils.PolkadotServiceConfig) error { func flagCheck() error { if configFilePath != "" { - if len(paraChain) != 0 || network != "" || explorer || metrics { + if len(paraChain) != 0 || network != "" || explorer || metrics || noRelay{ return common.WrapMessageToError(common.ErrInvalidFlag, "The '-c' flag does not allow additional flags.") } } diff --git a/cli/cmd/chains/utils/types.go b/cli/cmd/chains/utils/types.go index 97f97fad..b2121699 100644 --- a/cli/cmd/chains/utils/types.go +++ b/cli/cmd/chains/utils/types.go @@ -234,7 +234,7 @@ func (sc *PolkadotServiceConfig) LoadConfigFromFile(cliContext *common.Cli, file for _, parachain := range sc.Para { for i := range parachain.Nodes { - sc.RelayChain.Nodes[i].AssignPorts(sc.RelayChain.Nodes[i].Prometheus) + parachain.Nodes[i].AssignPorts(parachain.Nodes[i].Prometheus) } } @@ -290,6 +290,10 @@ func (psc *PolkadotServiceConfig) IsEmpty() error { return err } + if psc.RelayChain.Name == "" && len(psc.RelayChain.Nodes) == 0 && len(psc.Para) == 0 { + return common.WrapMessageToError(common.ErrEmptyFields, "Missing Fields In RelayChainConfig") + } + if len(psc.Para) == 0 { return nil } else { @@ -372,26 +376,19 @@ func (sc *PolkadotServiceConfig) ValidateConfig() error { var validChainTypes = []string{"local", "testnet", "mainnet"} var validRelayNodeType = []string{"validator", "full"} var validParaNodeType = []string{"collator", "full"} + var invalidTestNetParaChains = []string{"parallel", "subzero"} if !slices.Contains(validChainTypes, sc.ChainType) { return fmt.Errorf("invalid Chain Type: %s", sc.ChainType) } - if sc.ChainType == "local" && sc.RelayChain.Name != "rococo-local" { - return fmt.Errorf("invalid Chain Name for local: %s", sc.RelayChain.Name) - } - - if sc.RelayChain.Name != "" { - if sc.ChainType == "testnet" && !(sc.RelayChain.Name == "rococo" || sc.RelayChain.Name == "westend") { - return fmt.Errorf("invalid Chain Name for testnet: %s", sc.RelayChain.Name) + if sc.ChainType == "local" { + if sc.RelayChain.Name != "rococo-local" { + return fmt.Errorf("invalid Chain Name for local: %s", sc.RelayChain.Name) } - - if sc.ChainType == "mainnet" && !(sc.RelayChain.Name == "kusama" || sc.RelayChain.Name == "polkadot") { - return fmt.Errorf("invalid Chain Name for mainnet: %s", sc.RelayChain.Name) + if len(sc.RelayChain.Nodes) < 2 { + return fmt.Errorf("atleast two nodes required for Relay Chain Local") } - } - - if sc.ChainType == "local" { for _, node := range sc.RelayChain.Nodes { if node.NodeType != "validator" { return fmt.Errorf("invalid Node Type for Relay Chain Local: %s", node.NodeType) @@ -405,6 +402,23 @@ func (sc *PolkadotServiceConfig) ValidateConfig() error { } } + if sc.RelayChain.Name != "" { + if sc.ChainType == "testnet" && !(sc.RelayChain.Name == "rococo" || sc.RelayChain.Name == "westend") { + return fmt.Errorf("invalid Chain Name for testnet: %s", sc.RelayChain.Name) + } + if sc.ChainType == "mainnet" && !(sc.RelayChain.Name == "kusama" || sc.RelayChain.Name == "polkadot") { + return fmt.Errorf("invalid Chain Name for mainnet: %s", sc.RelayChain.Name) + } + } + + if sc.ChainType == "testnet" { + for _, paraChain := range sc.Para { + if slices.Contains(invalidTestNetParaChains, paraChain.Name) { + return fmt.Errorf("no Testnet for Para Chain: %s", paraChain.Name) + } + } + } + for _, paraChain := range sc.Para { for _, node := range paraChain.Nodes { if !slices.Contains(validParaNodeType, node.NodeType) { diff --git a/cli/common/constants.go b/cli/common/constants.go index c971e726..f9a211e7 100644 --- a/cli/common/constants.go +++ b/cli/common/constants.go @@ -90,6 +90,7 @@ const ( MissingFlagsError InvalidFlagError InvalidConfigError + PathConversionError ) var DiveLogs bool diff --git a/cli/common/errors.go b/cli/common/errors.go index a0279b25..0e51dc3e 100644 --- a/cli/common/errors.go +++ b/cli/common/errors.go @@ -39,6 +39,7 @@ var ( ErrMissingFlags = NewBase(MissingFlagsError, "Missing Flags") ErrInvalidFlag = NewBase(InvalidFlagError, "Invalid Flag Usage") ErrInvalidConfig = NewBase(InvalidConfigError, "Invalid Config") + ErrFailedPathConversion = NewBase(PathConversionError, "Failed To Convert Path to Absolute Path") ) func (c ErrorCode) New(msg string) error { diff --git a/cli/common/utils.go b/cli/common/utils.go index b30e8687..0f7db32d 100644 --- a/cli/common/utils.go +++ b/cli/common/utils.go @@ -5,6 +5,7 @@ import ( "math/rand" "net" "os/exec" + "path/filepath" "runtime" "strings" @@ -102,7 +103,11 @@ func LoadConfig(cliContext *Cli, config ConfigLoader, filePath string) error { return err } } else { - err := config.LoadConfigFromFile(cliContext, filePath) + absoluePath, err := GetAbsolutePath(filePath) + if err != nil { + return err + } + err = config.LoadConfigFromFile(cliContext, absoluePath) if err != nil { return err } @@ -205,3 +210,16 @@ func GetAvailablePort() (int, error) { return 0, ErrPortAllocation } + +// The function returns the absolute path for a given relative path +func GetAbsolutePath(filePath string) (string, error) { + if filepath.IsAbs(filePath) { + return filePath, nil + } else { + absoluePath, err := filepath.Abs(filePath) + if err != nil { + return "", ErrFailedPathConversion + } + return absoluePath, nil + } +}