Skip to content

Commit

Permalink
fix: bug fixes and validation check for polkadot and kusama
Browse files Browse the repository at this point in the history
  • Loading branch information
riyasng12 committed Jan 12, 2024
1 parent 681a69f commit 4a4035b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/chains/kusama/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/chains/kusama/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/chains/polkadot/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/chains/polkadot/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
Expand Down
42 changes: 28 additions & 14 deletions cli/cmd/chains/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions cli/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const (
MissingFlagsError
InvalidFlagError
InvalidConfigError
PathConversionError
)

var DiveLogs bool
Expand Down
1 change: 1 addition & 0 deletions cli/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion cli/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/rand"
"net"
"os/exec"
"path/filepath"
"runtime"
"strings"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}

0 comments on commit 4a4035b

Please sign in to comment.