Skip to content

Commit

Permalink
feat: error handling for kusama
Browse files Browse the repository at this point in the history
  • Loading branch information
riyasng12 committed Dec 20, 2023
1 parent 0194901 commit 6ef793f
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 35 deletions.
11 changes: 5 additions & 6 deletions cli/cmd/chains/kusama/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ var (
configFilePath string
paraChain string
network string
paraNodes string
relayNodes string
noRelay bool
explorer bool
metrics bool
)
Expand All @@ -22,17 +21,17 @@ const (
runKusamaFunctionName = "run_polkadot_setup"
runKusamaRelayLocal = "start_relay_chains_local"
runKusamaRelayTestnetMainet = "start_test_main_net_relay_nodes"
runUploadFiles = "upload_files"
)

var KusamaCmd = common.NewDiveCommandBuilder().
SetUse("kusama").
SetShort("Build, initialize and start a Kusama node").
SetLong("The command starts the kusama relay chain and kusama parachain if -p flag is given").
SetRun(kusama).
AddStringFlagWithShortHand(&paraChain, "parachain", "p", "", "specify the parachain to spwan parachain node").
AddStringFlagWithShortHand(&network, "network", "n", "", "specify the which network to run. local/testnet/mainnet. default will be local.").
AddStringFlag(&paraNodes, "para-nodes", "", "specify the nodes for parachain, default will be '[full, collator]'").
AddStringFlag(&relayNodes, "relay-nodes", "", "specify the nodes for relaychain, default will be '[full, validator]'").
AddStringFlagWithShortHand(&paraChain, "parachain", "p", "", "specify the parachain to spawn parachain node").
AddStringFlagWithShortHand(&network, "network", "n", "", "specify the network to run (local/testnet/mainnet). Default will be local.").
AddBoolFlag(&noRelay, "no-relay", false, "specify the bool flag to run parachain only (only for testnet and mainnet)").
AddStringFlagWithShortHand(&configFilePath, "config", "c", "", "path to custom config json file to start kusama relaychain and parachain nodes.").
AddBoolFlag(&explorer, "explorer", false, "specify the bool flag if you want to start polkadot js explorer service").
AddBoolFlag(&metrics, "metrics", false, "specify the bool flag if you want to start prometheus metrics service").
Expand Down
85 changes: 74 additions & 11 deletions cli/cmd/chains/kusama/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
)

const (
localChain = "local"
configsDirectory = "/home/riya/polakadot-kurtosis-package/parachain/static_files/configs"
localChain = "local"
)

func RunKusama(cli *common.Cli) (*common.DiveMultipleServiceResponse, error) {
Expand All @@ -24,18 +23,31 @@ func RunKusama(cli *common.Cli) (*common.DiveMultipleServiceResponse, error) {

var serviceConfig = &utils.PolkadotServiceConfig{}

err = flagCheck()
if err != nil {
return nil, err
}

err = common.LoadConfig(cli, serviceConfig, configFilePath)
if err != nil {
return nil, err
}

configureService(serviceConfig)
err = configureService(serviceConfig)
if err != nil {
return nil, err
}

encodedServiceConfigDataString, err := serviceConfig.EncodeToString()
if err != nil {
return nil, common.WrapMessageToError(common.ErrDataMarshall, err.Error())
}

err = uploadFiles(cli, enclaveContext)
if err != nil {
return nil, err
}

para := fmt.Sprintf(`{"args": %s}`, encodedServiceConfigDataString)
runConfig := getKusamaRunConfig(serviceConfig, enclaveContext, para)

Expand Down Expand Up @@ -76,17 +88,21 @@ func RunKusama(cli *common.Cli) (*common.DiveMultipleServiceResponse, error) {
return result, nil
}

func configureService(serviceConfig *utils.PolkadotServiceConfig) {
func configureService(serviceConfig *utils.PolkadotServiceConfig) error {
if paraChain != "" {
serviceConfig.Para[0].Name = paraChain
serviceConfig.Para = []utils.ParaNodeConfig{}
serviceConfig.Para = append(serviceConfig.Para, utils.ParaNodeConfig{
Name: paraChain,
Nodes: []utils.NodeConfig{
{Name: "alice", NodeType: "full", Prometheus: false},
},
})
}

if network != "" {
serviceConfig.ChainType = network
if network == "testnet" {
serviceConfig.RelayChain.Name = "rococo"
} else if network == "mainnet" {
serviceConfig.RelayChain.Name = "kusama"
if network == "testnet" || network == "mainnet" {
configureFullNodes(serviceConfig)
}
}

Expand All @@ -97,6 +113,29 @@ func configureService(serviceConfig *utils.PolkadotServiceConfig) {
if metrics {
configureMetrics(serviceConfig)
}

if noRelay && serviceConfig.ChainType == "local" {
return common.WrapMessageToError(common.ErrInvalidFlag, "Cannot pass --no-relay flag with local network")
} else if noRelay && serviceConfig.ChainType != "local" {
serviceConfig.RelayChain = utils.RelayChainConfig{}
}

return nil
}

func configureFullNodes(serviceConfig *utils.PolkadotServiceConfig) {
if network == "testnet" {
serviceConfig.RelayChain.Name = "rococo"
} else if network == "mainnet" {
serviceConfig.RelayChain.Name = "kusama"
}

serviceConfig.RelayChain.Nodes = []utils.NodeConfig{}
serviceConfig.RelayChain.Nodes = append(serviceConfig.RelayChain.Nodes, utils.NodeConfig{
Name: "alice",
NodeType: "full",
Prometheus: false,
})
}

func configureMetrics(serviceConfig *utils.PolkadotServiceConfig) {
Expand All @@ -108,16 +147,40 @@ func configureMetrics(serviceConfig *utils.PolkadotServiceConfig) {
}
}

func flagCheck() error {
if configFilePath != "" {
if paraChain != "" || network != "" || explorer || metrics {
return common.WrapMessageToError(common.ErrInvalidFlag, "Additional Flags Found")
}
}

if noRelay && (network == "testnet" || network == "mainnet") {
if paraChain == "" {
return common.WrapMessageToError(common.ErrMissingFlags, "Missing Parachain Flag")
}
}
return nil
}

func getKusamaRunConfig(serviceConfig *utils.PolkadotServiceConfig, enclaveContext *enclaves.EnclaveContext, para string) *starlark_run_config.StarlarkRunConfig {
if serviceConfig.Para[0].Name != "" {
if len(serviceConfig.Para) != 0 && serviceConfig.Para[0].Name != "" {
return common.GetStarlarkRunConfig(para, common.DivePolkadotDefaultNodeSetupScript, runKusamaFunctionName)
} else {
if serviceConfig.ChainType == localChain {
enclaveContext.UploadFiles(configsDirectory, "configs")
return common.GetStarlarkRunConfig(para, common.DivePolkadotRelayNodeSetupScript, runKusamaRelayLocal)
} else {
return common.GetStarlarkRunConfig(para, common.DivePolkadotRelayNodeSetupScript, runKusamaRelayTestnetMainet)
}

}
}

func uploadFiles(cli *common.Cli, enclaveCtx *enclaves.EnclaveContext) error {
runConfig := common.GetStarlarkRunConfig("{}", common.DivePolkaDotUtilsPath, runUploadFiles)
_, _, err := enclaveCtx.RunStarlarkRemotePackage(cli.Context().GetContext(), common.PolkadotRemotePackagePath, runConfig)
if err != nil {
return common.WrapMessageToError(common.ErrStarlarkRunFailed, err.Error())
}

return nil
}
38 changes: 22 additions & 16 deletions cli/cmd/chains/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ type NodeConfig struct {
}

type RelayChainConfig struct {
Name string `json:"name"`
Nodes []NodeConfig `json:"nodes"`
Name string `json:"name,omitempty"`
Nodes []NodeConfig `json:"nodes,omitempty"`
}

type ParaNodeConfig struct {
Expand Down Expand Up @@ -211,22 +211,20 @@ func (sc *PolkadotServiceConfig) LoadDefaultConfig() error {
{Name: "bob", NodeType: "full", Prometheus: false},
{Name: "alice", NodeType: "validator", Prometheus: false},
}
sc.Para = []ParaNodeConfig{
{
Name: "",
Nodes: []NodeConfig{
{Name: "alice", NodeType: "full", Prometheus: false},
{Name: "bob", NodeType: "collator", Prometheus: false},
},
},
}

sc.Para = []ParaNodeConfig{}

return nil
}

func (psc *PolkadotServiceConfig) IsEmpty() error {
if psc == nil || psc.ChainType == "" || psc.Explorer {
return common.WrapMessageToErrorf(common.ErrEmptyFields, "Missing Fields In PolkadotServiceConfig")

if psc == nil || psc.ChainType == "" {
return common.WrapMessageToError(common.ErrEmptyFields, "Missing Fields In PolkadotServiceConfig")
}

if psc.Explorer != true && psc.Explorer != false {
return common.WrapMessageToError(common.ErrEmptyFields, "Missing Fields In PolkadotServiceConfig")
}

if err := psc.RelayChain.IsEmpty(); err != nil {
Expand All @@ -243,8 +241,9 @@ func (psc *PolkadotServiceConfig) IsEmpty() error {
}

func (rcc *RelayChainConfig) IsEmpty() error {

if rcc == nil || rcc.Name == "" || len(rcc.Nodes) == 0 {
return common.WrapMessageToErrorf(common.ErrEmptyFields, "Missing Fields In RelayChainConfig")
return common.WrapMessageToError(common.ErrEmptyFields, "Missing Fields In RelayChainConfig")
}

for _, node := range rcc.Nodes {
Expand All @@ -257,8 +256,9 @@ func (rcc *RelayChainConfig) IsEmpty() error {
}

func (pnc *ParaNodeConfig) IsEmpty() error {

if pnc == nil || pnc.Name == "" || len(pnc.Nodes) == 0 {
return common.WrapMessageToErrorf(common.ErrEmptyFields, "Missing Fields In ParaNodeConfig")
return common.WrapMessageToError(common.ErrEmptyFields, "Missing Fields In ParaNodeConfig")
}

for _, node := range pnc.Nodes {
Expand All @@ -271,8 +271,14 @@ func (pnc *ParaNodeConfig) IsEmpty() error {
}

func (nc *NodeConfig) IsEmpty() error {

if nc == nil || nc.Name == "" || nc.NodeType == "" {
return common.WrapMessageToErrorf(common.ErrEmptyFields, "Missing Fields In NodeConfig")
return common.WrapMessageToError(common.ErrEmptyFields, "Missing Fields In NodeConfig")
}

if nc.Prometheus != true && nc.Prometheus != false {
return common.WrapMessageToError(common.ErrEmptyFields, "Missing Fields In PolkadotServiceConfig")
}

return nil
}
3 changes: 2 additions & 1 deletion cli/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
PolkadotRemotePackagePath = "github.com/hugobyte/polkadot-kurtosis-package/"
DivePolkadotDefaultNodeSetupScript = "main.star"
DivePolkadotRelayNodeSetupScript = "/relaychain/relay-chain.star"
DivePolkaDotUtilsPath = "package_io/utils.star"
DiveDryRun = false
DiveDefaultParallelism = 4
DiveLogDirectory = "/logs/"
Expand Down Expand Up @@ -80,9 +81,9 @@ const (
KurtosisServiceError
InvalidChain
PortError
EmptyFileError
EmptyFieldsError
MissingFlagsError
InvalidFlagError
)

var DiveLogs bool
Expand Down
2 changes: 1 addition & 1 deletion cli/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ var (
ErrKurtosisService = NewBase(KurtosisServiceError, "Kurtosis Service Error")
ErrInvalidChain = NewBase(InvalidChain, "Not A Valid Chain")
ErrPortAllocation = NewBase(PortError, "Failed To Allocate Port")
ErrEmptyFile = NewBase(EmptyFileError, "Cannot Load Config From Empty File")
ErrEmptyFields = NewBase(EmptyFieldsError, "Missing Fields In The Config File")
ErrMissingFlags = NewBase(MissingFlagsError, "Missing Flags")
ErrInvalidFlag = NewBase(InvalidFlagError, "Invalid Flag Usage")
)

func (c ErrorCode) New(msg string) error {
Expand Down

0 comments on commit 6ef793f

Please sign in to comment.