Skip to content

Commit

Permalink
feat: implement cli to start neutron node (HugoByte#158)
Browse files Browse the repository at this point in the history
* feat: implement cli to start neutron node

* refactor: remove commented code

---------

Co-authored-by: abhiyana <[email protected]>
  • Loading branch information
abhiyana and abhiyana authored Sep 5, 2023
1 parent 54e8d23 commit 9650125
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cli/commands/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 5 additions & 1 deletion cli/commands/chain/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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

Expand Down
85 changes: 85 additions & 0 deletions cli/commands/chain/types/neutron.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion cli/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,4 +43,4 @@ const (
openFileWindowsCommandName = "rundll32"

openFileWindowsCommandFirstArgumentDefault = "url.dll,FileProtocolHandler"
)
)

0 comments on commit 9650125

Please sign in to comment.