From 96501253953d03a0274bdcf069e8108e3d2d1f00 Mon Sep 17 00:00:00 2001 From: Abhishek Harde <47945971+abhiyana@users.noreply.github.com> Date: Tue, 5 Sep 2023 18:00:11 +0530 Subject: [PATCH] feat: implement cli to start neutron node (#158) * feat: implement cli to start neutron node * refactor: remove commented code --------- Co-authored-by: abhiyana --- cli/commands/bridge/bridge.go | 5 +- cli/commands/chain/chains.go | 6 +- cli/commands/chain/types/neutron.go | 85 +++++++++++++++++++++++++++++ cli/common/constants.go | 4 +- 4 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 cli/commands/chain/types/neutron.go diff --git a/cli/commands/bridge/bridge.go b/cli/commands/bridge/bridge.go index 5b25a0eb..1167a7b3 100644 --- a/cli/commands/bridge/bridge.go +++ b/cli/commands/bridge/bridge.go @@ -28,7 +28,10 @@ This will create an relay to connect two different chains and pass any messages } cmd.ValidArgs = validArgs - if !slices.Contains(cmd.ValidArgs, args[0]) { + if len(args) == 0 { + cmd.Help() + + } else if !slices.Contains(cmd.ValidArgs, args[0]) { diveContext.Log.SetOutput(os.Stderr) diveContext.Error(fmt.Sprintf("Invalid Subcommand: %v", args)) diff --git a/cli/commands/chain/chains.go b/cli/commands/chain/chains.go index 015c70d9..8f017bef 100644 --- a/cli/commands/chain/chains.go +++ b/cli/commands/chain/chains.go @@ -31,7 +31,10 @@ maintenance within the specified blockchain ecosystem.`, } cmd.ValidArgs = validArgs - if !slices.Contains(cmd.ValidArgs, args[0]) { + if len(args) == 0 { + cmd.Help() + + } else if !slices.Contains(cmd.ValidArgs, args[0]) { diveContext.Log.SetOutput(os.Stderr) diveContext.Error(fmt.Sprintf("Invalid Subcommand: %v", args)) @@ -47,6 +50,7 @@ maintenance within the specified blockchain ecosystem.`, chainCmd.AddCommand(types.NewEthCmd(diveContext)) chainCmd.AddCommand(types.NewHardhatCmd(diveContext)) chainCmd.AddCommand(types.NewArchwayCmd(diveContext)) + chainCmd.AddCommand(types.NewNeutronCmd(diveContext)) return chainCmd diff --git a/cli/commands/chain/types/neutron.go b/cli/commands/chain/types/neutron.go new file mode 100644 index 00000000..2ee95d97 --- /dev/null +++ b/cli/commands/chain/types/neutron.go @@ -0,0 +1,85 @@ +package types + +import ( + "encoding/json" + + "github.com/hugobyte/dive/cli/common" + "github.com/kurtosis-tech/kurtosis/api/golang/core/kurtosis_core_rpc_api_bindings" + "github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves" + "github.com/spf13/cobra" +) + +const ( + runNeutronNodeWithDefaultConfigFunctionName = "start_node_service" +) + + +func NewNeutronCmd(diveContext *common.DiveContext) *cobra.Command { + + neutronCmd := &cobra.Command{ + Use: "neutron", + Short: "Build, initialize and start a neutron node", + Long: "The command starts the neutron network and allows node in executing contracts", + Run: func(cmd *cobra.Command, args []string) { + common.ValidateCmdArgs(diveContext, args, cmd.UsageString()) + runResponse := RunNeutronNode(diveContext) + + common.WriteToServiceFile(runResponse.ServiceName, *runResponse) + + diveContext.StopSpinner("Neutron Node Started. Please find service details in current working directory(services.json)") + }, + } + neutronCmd.Flags().StringVarP(&config, "config", "c", "", "path to custom config json file to start neutron node ") + + return neutronCmd +} + + +func RunNeutronNode(diveContext *common.DiveContext) *common.DiveserviceResponse { + diveContext.InitKurtosisContext() + kurtosisEnclaveContext, err := diveContext.GetEnclaveContext() + + if err != nil { + diveContext.FatalError("Failed To Retrive Enclave Context", err.Error()) + } + + diveContext.StartSpinner(" Starting Neutron Node") + var neutronResponse = &common.DiveserviceResponse{} + var starlarkExecutionData = "" + starlarkExecutionData, err = runNeutronWithDefaultServiceConfig(diveContext, kurtosisEnclaveContext) + if err != nil { + diveContext.FatalError("Starlark Run Failed", err.Error()) + } + err = json.Unmarshal([]byte(starlarkExecutionData), neutronResponse) + + if err != nil { + diveContext.FatalError("Failed to Unmarshall Service Response", err.Error()) + } + + return neutronResponse + +} + + +func runNeutronWithDefaultServiceConfig(diveContext *common.DiveContext, enclaveContext *enclaves.EnclaveContext) (string, error) { + + params := `{"args":{"data":{}}}` + nodeServiceResponse, _, err := enclaveContext.RunStarlarkRemotePackage(diveContext.Ctx, common.DiveRemotePackagePath, common.DiveNeutronDefaultNodeScript, runNeutronNodeWithDefaultConfigFunctionName, params, common.DiveDryRun, common.DiveDefaultParallelism, []kurtosis_core_rpc_api_bindings.KurtosisFeatureFlag{}) + + if err != nil { + + return "", err + + } + + nodeServiceResponseData, services, skippedInstructions, err := diveContext.GetSerializedData(nodeServiceResponse) + if err != nil { + + diveContext.StopServices(services) + diveContext.FatalError("Starlark Run Failed", err.Error()) + + } + diveContext.CheckInstructionSkipped(skippedInstructions, "Nueutron Node Already Running") + + return nodeServiceResponseData, nil +} \ No newline at end of file diff --git a/cli/common/constants.go b/cli/common/constants.go index 790bdb48..5cdcc6ea 100644 --- a/cli/common/constants.go +++ b/cli/common/constants.go @@ -13,6 +13,8 @@ const ( DiveEthHardhatNodeScript = "services/evm/eth/src/node-setup/start-eth-node.star" DiveArchwayNodeScript = "services/cosmvm/archway/src/node-setup/start_node.star" DiveArchwayDefaultNodeScript = "services/cosmvm/archway/archway.star" + DiveNeutronNodeScript = "services/cosmvm/neutron/src/node-setup/start_node.star" + DiveNeutronDefaultNodeScript = "services/cosmvm/neutron/neutron.star" DiveBridgeScript = "main.star" DiveDryRun = false DiveDefaultParallelism = 4 @@ -41,4 +43,4 @@ const ( openFileWindowsCommandName = "rundll32" openFileWindowsCommandFirstArgumentDefault = "url.dll,FileProtocolHandler" -) +) \ No newline at end of file