diff --git a/cli/cmd/chains/chain/archway/cmd.go b/cli/cmd/chains/archway/cmd.go similarity index 100% rename from cli/cmd/chains/chain/archway/cmd.go rename to cli/cmd/chains/archway/cmd.go diff --git a/cli/cmd/chains/chain/archway/run.go b/cli/cmd/chains/archway/run.go similarity index 90% rename from cli/cmd/chains/chain/archway/run.go rename to cli/cmd/chains/archway/run.go index 6c11d0aa..222da53d 100644 --- a/cli/cmd/chains/chain/archway/run.go +++ b/cli/cmd/chains/archway/run.go @@ -1,6 +1,9 @@ package archway -import "github.com/hugobyte/dive-core/cli/common" +import ( + "github.com/hugobyte/dive-core/cli/cmd/chains/utils" + "github.com/hugobyte/dive-core/cli/common" +) func RunArchway(cli *common.Cli) (*common.DiveServiceResponse, error) { @@ -10,7 +13,7 @@ func RunArchway(cli *common.Cli) (*common.DiveServiceResponse, error) { return nil, err } - var serviceConfig = &ArchwayServiceConfig{} + var serviceConfig = &utils.CosmosServiceConfig{} err = common.LoadConfig(cli, serviceConfig, configFilePath) if err != nil { diff --git a/cli/cmd/chains/chain/archway/types.go b/cli/cmd/chains/chain/archway/types.go deleted file mode 100644 index bd5b7899..00000000 --- a/cli/cmd/chains/chain/archway/types.go +++ /dev/null @@ -1,47 +0,0 @@ -package archway - -import ( - "encoding/json" - - "github.com/hugobyte/dive-core/cli/common" -) - -type ArchwayServiceConfig struct { - Cid *string `json:"chain_id"` - Key *string `json:"key"` - PublicGrpcPort *int `json:"public_grpc"` - PublicHttpPort *int `json:"public_http"` - PublicTcpPort *int `json:"public_tcp"` - PublicRpcPort *int `json:"public_rpc"` - Password *string `json:"password"` -} - -func (as *ArchwayServiceConfig) LoadDefaultConfig() { - as.Cid = nil - as.Key = nil - as.Password = nil - as.PublicGrpcPort = nil - as.PublicHttpPort = nil - as.PublicRpcPort = nil - as.PublicTcpPort = nil - as.Password = nil -} - -func (as *ArchwayServiceConfig) EncodeToString() (string, error) { - - data, err := json.Marshal(as) - if err != nil { - return "", err - } - - return string(data), nil -} - -func (as *ArchwayServiceConfig) LoadConfigFromFile(cliContext *common.Cli, filePath string) error { - - err := cliContext.FileHandler().ReadJson(filePath, as) - if err != nil { - return err - } - return nil -} diff --git a/cli/cmd/chains/chain/archway/utils.go b/cli/cmd/chains/chain/archway/utils.go deleted file mode 100644 index 1cad3d3b..00000000 --- a/cli/cmd/chains/chain/archway/utils.go +++ /dev/null @@ -1 +0,0 @@ -package archway diff --git a/cli/cmd/chains/chain/eth/types.go b/cli/cmd/chains/chain/eth/types.go deleted file mode 100644 index b11c70e5..00000000 --- a/cli/cmd/chains/chain/eth/types.go +++ /dev/null @@ -1 +0,0 @@ -package eth diff --git a/cli/cmd/chains/chain/eth/utils.go b/cli/cmd/chains/chain/eth/utils.go deleted file mode 100644 index b11c70e5..00000000 --- a/cli/cmd/chains/chain/eth/utils.go +++ /dev/null @@ -1 +0,0 @@ -package eth diff --git a/cli/cmd/chains/chain/hardhat/types.go b/cli/cmd/chains/chain/hardhat/types.go deleted file mode 100644 index 6b2229cc..00000000 --- a/cli/cmd/chains/chain/hardhat/types.go +++ /dev/null @@ -1 +0,0 @@ -package hardhat diff --git a/cli/cmd/chains/chain/hardhat/utils.go b/cli/cmd/chains/chain/hardhat/utils.go deleted file mode 100644 index 6b2229cc..00000000 --- a/cli/cmd/chains/chain/hardhat/utils.go +++ /dev/null @@ -1 +0,0 @@ -package hardhat diff --git a/cli/cmd/chains/chain/icon/types.go b/cli/cmd/chains/chain/icon/types.go deleted file mode 100644 index 638906cf..00000000 --- a/cli/cmd/chains/chain/icon/types.go +++ /dev/null @@ -1,47 +0,0 @@ -package icon - -import ( - "encoding/json" - - "github.com/hugobyte/dive-core/cli/common" -) - -type IconServiceConfig struct { - Port int `json:"private_port"` - PublicPort int `json:"public_port"` - P2PListenAddress string `json:"p2p_listen_address"` - P2PAddress string `json:"p2p_address"` - Cid string `json:"cid"` -} - -func (sc *IconServiceConfig) LoadDefaultConfig() { - sc.Port = 9080 - sc.PublicPort = 8090 - sc.P2PListenAddress = "7080" - sc.P2PAddress = "8080" - sc.Cid = "0xacbc4e" - -} - -func (sc *IconServiceConfig) EncodeToString() (string, error) { - encodedBytes, err := json.Marshal(sc) - if err != nil { - return "", nil - } - - return string(encodedBytes), nil -} - -func (sc *IconServiceConfig) LoadConfigFromFile(cliContext *common.Cli, filePath string) error { - err := cliContext.FileHandler().ReadJson(filePath, sc) - if err != nil { - return err - } - return nil -} - -type genesisHandler struct { - genesisFile string - uploadedFiles string - genesisPath string -} diff --git a/cli/cmd/chains/chain/icon/utils.go b/cli/cmd/chains/chain/icon/utils.go deleted file mode 100644 index bd0d897a..00000000 --- a/cli/cmd/chains/chain/icon/utils.go +++ /dev/null @@ -1,41 +0,0 @@ -package icon - -import ( - "fmt" - "os" - "path/filepath" - - "github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves" -) - -func genesismanager(enclaveContext *enclaves.EnclaveContext) (*genesisHandler, error) { - - gm := genesisHandler{} - - var genesisFilePath = genesis - - if genesisFilePath != "" { - genesisFileName := filepath.Base(genesisFilePath) - if _, err := os.Stat(genesisFilePath); err != nil { - return nil, err - } - - _, d, err := enclaveContext.UploadFiles(genesisFilePath, genesisFileName) - if err != nil { - return nil, err - } - - gm.uploadedFiles = fmt.Sprintf(`{"file_path":"%s","file_name":"%s"}`, d, genesisFileName) - } else { - gm.genesisFile = filepath.Base(DefaultIconGenesisFile) - gm.genesisPath = DefaultIconGenesisFile - gm.uploadedFiles = `{}` - - } - - return &gm, nil -} - -func GetDecentralizeParams(serviceName, nodeEndpoint, keystorePath, keystorepassword, networkID string) string { - return fmt.Sprintf(`{"service_name":"%s","uri":"%s","keystorepath":"%s","keypassword":"%s","nid":"%s"}`, serviceName, nodeEndpoint, keystorePath, keystorepassword, networkID) -} diff --git a/cli/cmd/chains/chain/neutron/cmd.go b/cli/cmd/chains/chain/neutron/cmd.go deleted file mode 100644 index 48030a95..00000000 --- a/cli/cmd/chains/chain/neutron/cmd.go +++ /dev/null @@ -1,15 +0,0 @@ -package neutron - -import ( - "github.com/hugobyte/dive-core/cli/common" - "github.com/spf13/cobra" -) - -var NeutronCmd = common.NewDiveCommandBuilder(). - SetUse("neutron"). - SetShort("Build, initialize and start a neutron node"). - SetLong("The command starts the neutron network and allows node in executing contracts"). - SetRun(neutron). - Build() - -func neutron(cmd *cobra.Command, args []string) {} diff --git a/cli/cmd/chains/chain/neutron/run.go b/cli/cmd/chains/chain/neutron/run.go deleted file mode 100644 index fbba5cef..00000000 --- a/cli/cmd/chains/chain/neutron/run.go +++ /dev/null @@ -1 +0,0 @@ -package neutron diff --git a/cli/cmd/chains/chain/neutron/types.go b/cli/cmd/chains/chain/neutron/types.go deleted file mode 100644 index fbba5cef..00000000 --- a/cli/cmd/chains/chain/neutron/types.go +++ /dev/null @@ -1 +0,0 @@ -package neutron diff --git a/cli/cmd/chains/chain/neutron/utils.go b/cli/cmd/chains/chain/neutron/utils.go deleted file mode 100644 index fbba5cef..00000000 --- a/cli/cmd/chains/chain/neutron/utils.go +++ /dev/null @@ -1 +0,0 @@ -package neutron diff --git a/cli/cmd/chains/chains.go b/cli/cmd/chains/chains.go index f108e47b..73ab4e3a 100644 --- a/cli/cmd/chains/chains.go +++ b/cli/cmd/chains/chains.go @@ -4,11 +4,11 @@ import ( "os" "slices" - "github.com/hugobyte/dive-core/cli/cmd/chains/chain/archway" - "github.com/hugobyte/dive-core/cli/cmd/chains/chain/eth" - "github.com/hugobyte/dive-core/cli/cmd/chains/chain/hardhat" - "github.com/hugobyte/dive-core/cli/cmd/chains/chain/icon" - "github.com/hugobyte/dive-core/cli/cmd/chains/chain/neutron" + "github.com/hugobyte/dive-core/cli/cmd/chains/archway" + "github.com/hugobyte/dive-core/cli/cmd/chains/eth" + "github.com/hugobyte/dive-core/cli/cmd/chains/hardhat" + "github.com/hugobyte/dive-core/cli/cmd/chains/icon" + "github.com/hugobyte/dive-core/cli/cmd/chains/neutron" "github.com/hugobyte/dive-core/cli/common" "github.com/spf13/cobra" ) diff --git a/cli/cmd/chains/chain/eth/cmd.go b/cli/cmd/chains/eth/cmd.go similarity index 100% rename from cli/cmd/chains/chain/eth/cmd.go rename to cli/cmd/chains/eth/cmd.go diff --git a/cli/cmd/chains/chain/eth/run.go b/cli/cmd/chains/eth/run.go similarity index 100% rename from cli/cmd/chains/chain/eth/run.go rename to cli/cmd/chains/eth/run.go diff --git a/cli/cmd/chains/chain/hardhat/cmd.go b/cli/cmd/chains/hardhat/cmd.go similarity index 100% rename from cli/cmd/chains/chain/hardhat/cmd.go rename to cli/cmd/chains/hardhat/cmd.go diff --git a/cli/cmd/chains/chain/hardhat/run.go b/cli/cmd/chains/hardhat/run.go similarity index 100% rename from cli/cmd/chains/chain/hardhat/run.go rename to cli/cmd/chains/hardhat/run.go diff --git a/cli/cmd/chains/chain/icon/cmd.go b/cli/cmd/chains/icon/cmd.go similarity index 100% rename from cli/cmd/chains/chain/icon/cmd.go rename to cli/cmd/chains/icon/cmd.go diff --git a/cli/cmd/chains/chain/icon/run.go b/cli/cmd/chains/icon/run.go similarity index 70% rename from cli/cmd/chains/chain/icon/run.go rename to cli/cmd/chains/icon/run.go index 4e46d75a..f67223b9 100644 --- a/cli/cmd/chains/chain/icon/run.go +++ b/cli/cmd/chains/icon/run.go @@ -2,8 +2,12 @@ package icon import ( "fmt" + "os" + "path/filepath" + "github.com/hugobyte/dive-core/cli/cmd/chains/utils" "github.com/hugobyte/dive-core/cli/common" + "github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves" ) func RunIconNode(cli *common.Cli) (*common.DiveServiceResponse, error) { @@ -13,7 +17,7 @@ func RunIconNode(cli *common.Cli) (*common.DiveServiceResponse, error) { if err != nil { return nil, err } - var serviceConfig = &IconServiceConfig{} + var serviceConfig = &utils.IconServiceConfig{} err = common.LoadConfig(cli, serviceConfig, configFilePath) if err != nil { return nil, err @@ -91,3 +95,41 @@ func RunDecentralization(cli *common.Cli, params string) error { return nil } + +type genesisHandler struct { + genesisFile string + uploadedFiles string + genesisPath string +} + +func genesismanager(enclaveContext *enclaves.EnclaveContext) (*genesisHandler, error) { + + gm := genesisHandler{} + + var genesisFilePath = genesis + + if genesisFilePath != "" { + genesisFileName := filepath.Base(genesisFilePath) + if _, err := os.Stat(genesisFilePath); err != nil { + return nil, err + } + + _, d, err := enclaveContext.UploadFiles(genesisFilePath, genesisFileName) + if err != nil { + return nil, err + } + + gm.uploadedFiles = fmt.Sprintf(`{"file_path":"%s","file_name":"%s"}`, d, genesisFileName) + } else { + gm.genesisFile = filepath.Base(DefaultIconGenesisFile) + gm.genesisPath = DefaultIconGenesisFile + gm.uploadedFiles = `{}` + + } + + return &gm, nil +} + +func GetDecentralizeParams(serviceName, nodeEndpoint, keystorePath, keystorepassword, networkID string) string { + return fmt.Sprintf(`{"service_name":"%s","uri":"%s","keystorepath":"%s","keypassword":"%s","nid":"%s"}`, serviceName, nodeEndpoint, keystorePath, keystorepassword, networkID) +} diff --git a/cli/cmd/chains/neutron/cmd.go b/cli/cmd/chains/neutron/cmd.go new file mode 100644 index 00000000..9934aec9 --- /dev/null +++ b/cli/cmd/chains/neutron/cmd.go @@ -0,0 +1,50 @@ +package neutron + +import ( + "github.com/hugobyte/dive-core/cli/common" + "github.com/spf13/cobra" +) + +var ( + configFilePath string +) + +const ( + runNeutronNodeWithDefaultConfigFunctionName = "start_node_service" +) + +var NeutronCmd = common.NewDiveCommandBuilder(). + SetUse("neutron"). + SetShort("Build, initialize and start a neutron node"). + SetLong("The command starts the neutron network and allows node in executing contracts"). + SetRun(neutron). + AddStringFlagWithShortHand(&configFilePath, "config", "c", "", "path to custom config json file to start archway node "). + Build() + +func neutron(cmd *cobra.Command, args []string) { + + cliContext := common.GetCliWithKurtosisContext() + + err := common.ValidateArgs(args) + if err != nil { + cliContext.Logger().Fatal(common.CodeOf(err), err.Error()) + } + + cliContext.Spinner().StartWithMessage("Starting Neutron Node", "green") + + response, err := RunNeutron(cliContext) + + if err != nil { + cliContext.Logger().Fatal(common.CodeOf(err), err.Error()) + } + + err = common.WriteServiceResponseData(response.ServiceName, *response, cliContext) + if err != nil { + cliContext.Spinner().Stop() + cliContext.Logger().SetErrorToStderr() + cliContext.Logger().Error(common.CodeOf(err), err.Error()) + + } + + cliContext.Spinner().StopWithMessage("Neutron Node Started. Please find service details in current working directory(services.json)") +} diff --git a/cli/cmd/chains/neutron/run.go b/cli/cmd/chains/neutron/run.go new file mode 100644 index 00000000..86a70b9b --- /dev/null +++ b/cli/cmd/chains/neutron/run.go @@ -0,0 +1,63 @@ +package neutron + +import ( + "github.com/hugobyte/dive-core/cli/cmd/chains/utils" + "github.com/hugobyte/dive-core/cli/common" +) + +func RunNeutron(cli *common.Cli) (*common.DiveServiceResponse, error) { + + enclaveContext, err := cli.Context().GetEnclaveContext(common.DiveEnclave) + + if err != nil { + return nil, err + } + + var serviceConfig = &utils.CosmosServiceConfig{} + + err = common.LoadConfig(cli, serviceConfig, configFilePath) + + if err != nil { + return nil, err + } + + encodedServiceConfigDataString, err := serviceConfig.EncodeToString() + + if err != nil { + return nil, common.Errorc(common.InvalidEnclaveConfigError, err.Error()) + } + + runConfig := common.GetStarlarkRunConfig(encodedServiceConfigDataString, common.DiveNeutronDefaultNodeScript, runNeutronNodeWithDefaultConfigFunctionName) + + response, _, err := enclaveContext.RunStarlarkRemotePackage(cli.Context().GetContext(), common.DiveRemotePackagePath, runConfig) + + if err != nil { + return nil, common.Errorc(common.FileError, err.Error()) + } + + responseData, services, skippedInstructions, err := common.GetSerializedData(cli, response) + + if err != nil { + + err = cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave) + if err != nil { + return nil, common.Errorc(common.InvalidEnclaveContextError, err.Error()) + } + + return nil, common.Errorc(common.KurtosisContextError, err.Error()) + } + + if cli.Context().CheckSkippedInstructions(skippedInstructions) { + return nil, common.Errorc(common.KurtosisContextError, "Already Running") + } + + neutronResponseData := &common.DiveServiceResponse{} + result, err := neutronResponseData.Decode([]byte(responseData)) + + if err != nil { + + return nil, common.Errorc(common.KurtosisContextError, err.Error()) + } + + return result, nil +} diff --git a/cli/cmd/chains/utils/types.go b/cli/cmd/chains/utils/types.go new file mode 100644 index 00000000..6fa058e2 --- /dev/null +++ b/cli/cmd/chains/utils/types.go @@ -0,0 +1,81 @@ +package utils + +import ( + "encoding/json" + + "github.com/hugobyte/dive-core/cli/common" +) + +type CosmosServiceConfig struct { + ChainID *string `json:"chain_id"` + Key *string `json:"key"` + Password *string `json:"password"` + PublicGrpc *int `json:"public_grpc"` + PublicHTTP *int `json:"public_http"` + PublicTCP *int `json:"public_tcp"` + PublicRPC *int `json:"public_rpc"` +} + +func (cs *CosmosServiceConfig) LoadDefaultConfig() { + cs.ChainID = nil + cs.Key = nil + cs.Password = nil + cs.PublicGrpc = nil + cs.PublicHTTP = nil + cs.PublicRPC = nil + cs.PublicTCP = nil + cs.Password = nil +} + +func (cs *CosmosServiceConfig) EncodeToString() (string, error) { + + data, err := json.Marshal(cs) + if err != nil { + return "", err + } + + return string(data), nil +} + +func (cs *CosmosServiceConfig) LoadConfigFromFile(cliContext *common.Cli, filePath string) error { + + err := cliContext.FileHandler().ReadJson(filePath, cs) + if err != nil { + return err + } + return nil +} + +type IconServiceConfig struct { + Port int `json:"private_port"` + PublicPort int `json:"public_port"` + P2PListenAddress string `json:"p2p_listen_address"` + P2PAddress string `json:"p2p_address"` + Cid string `json:"cid"` +} + +func (sc *IconServiceConfig) LoadDefaultConfig() { + sc.Port = 9080 + sc.PublicPort = 8090 + sc.P2PListenAddress = "7080" + sc.P2PAddress = "8080" + sc.Cid = "0xacbc4e" + +} + +func (sc *IconServiceConfig) EncodeToString() (string, error) { + encodedBytes, err := json.Marshal(sc) + if err != nil { + return "", nil + } + + return string(encodedBytes), nil +} + +func (sc *IconServiceConfig) LoadConfigFromFile(cliContext *common.Cli, filePath string) error { + err := cliContext.FileHandler().ReadJson(filePath, sc) + if err != nil { + return err + } + return nil +}