Skip to content

Commit

Permalink
feat: implement neutron-archway ibc realy using dive cli (HugoByte#174)
Browse files Browse the repository at this point in the history
* feat: implement kurtosis package to start neutron node with custom configuration

* feat: implement kurtosis package to run nutron node with custom configuration

* refactor: delete .circleci folder

* feat: implement neutron-neutron IBC relay package

* refactor: add comments in the code

* feat: implement neutron-neutron ibc relay

* refactor: change starlark variable from camel to snake case

* feat: implement neutron- archway ibc relay setup using dive cli

* fix: update typos and remove unused password for archway contract deployment

* fix: remove typo and rename branch

---------

Co-authored-by: abhiyana <[email protected]>
Co-authored-by: shreyasbhat0 <[email protected]>
  • Loading branch information
3 people authored Sep 22, 2023
1 parent 152f5e0 commit 7357580
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 40 deletions.
8 changes: 4 additions & 4 deletions cli/commands/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"os"

"github.com/hugobyte/dive/cli/commands/bridge/relyas"
"github.com/hugobyte/dive/cli/commands/bridge/relays"
"github.com/hugobyte/dive/cli/common"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
Expand All @@ -30,7 +30,7 @@ This will create an relay to connect two different chains and pass any messages

if len(args) == 0 {
cmd.Help()

} else if !slices.Contains(cmd.ValidArgs, args[0]) {

diveContext.Log.SetOutput(os.Stderr)
Expand All @@ -42,9 +42,9 @@ This will create an relay to connect two different chains and pass any messages
},
}

bridgeCmd.AddCommand(relyas.BtpRelayCmd(diveContext))
bridgeCmd.AddCommand(relays.BtpRelayCmd(diveContext))

bridgeCmd.AddCommand(relyas.IbcRelayCmd(diveContext))
bridgeCmd.AddCommand(relays.IbcRelayCmd(diveContext))

return bridgeCmd
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package relyas
package relays

import (
"fmt"
Expand Down Expand Up @@ -64,7 +64,7 @@ func BtpRelayCmd(diveContext *common.DiveContext) *cobra.Command {
chains := initChains(chainA, chainB, serviceA, serviceB, bridge)

if err := chains.checkForBtpSupportedChains(); err != nil {
diveContext.FatalError(err.Error(), fmt.Sprintf("Supported Chains for BTP: %v", suppottedChainsForBtp))
diveContext.FatalError(err.Error(), fmt.Sprintf("Supported Chains for BTP: %v", supportedChainsForBtp))
}

diveContext.StartSpinner(fmt.Sprintf(" Starting BTP Bridge for %s,%s", chains.chainA, chains.chainB))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package relyas
package relays

import (
"fmt"
Expand All @@ -10,8 +10,8 @@ import (
"github.com/hugobyte/dive/cli/common"
)

var suppottedChainsForBtp = []string{"icon", "eth", "hardhat"}
var supportedChainsForIbc = []string{"archway", "icon"}
var supportedChainsForBtp = []string{"icon", "eth", "hardhat"}
var supportedChainsForIbc = []string{"archway", "neutron", "icon"}

type Chains struct {
chainA string
Expand Down Expand Up @@ -40,7 +40,7 @@ func (chains *Chains) getParams() string {
}
func (chains *Chains) getIbcRelayParams() string {

return fmt.Sprintf(`{"args":{"links": {"src": "%s", "dst": "%s"}}}`, chains.chainA, chains.chainB)
return fmt.Sprintf(`{"args":{"links": {"src": "%s", "dst": "%s"}, "src_config":{"data":{}}, "dst_config":{"data":{}}}}`, chains.chainA, chains.chainB)
}

func (chains *Chains) getServicesResponse() (string, string, error) {
Expand Down Expand Up @@ -74,10 +74,10 @@ func (chains *Chains) getServicesResponse() (string, string, error) {
}

func (chains *Chains) checkForBtpSupportedChains() error {
if !slices.Contains(suppottedChainsForBtp, chains.chainA) {
if !slices.Contains(supportedChainsForBtp, chains.chainA) {
return fmt.Errorf("invalid Chain: %s", chains.chainA)
}
if !slices.Contains(suppottedChainsForBtp, chains.chainB) {
if !slices.Contains(supportedChainsForBtp, chains.chainB) {
return fmt.Errorf("invalid Chain: %s", chains.chainB)
}
return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package relyas
package relays

import (
"fmt"
Expand Down
105 changes: 78 additions & 27 deletions cli/commands/chain/types/neutron.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,135 @@ package types

import (
"encoding/json"
"fmt"

"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"
)

// Constants for function names
const (
runNeutronNodeWithDefaultConfigFunctionName = "start_node_service"
runNeutronNodeWithCustomServiceFunctionName = "start_neutron_node"
construcNeutrontServiceConfigFunctionName = "get_service_config"
)

// Variable to store the Neutron node configuration file path
var (
neutron_node_config string
)

func NewNeutronCmd(diveContext *common.DiveContext) *cobra.Command {
// NeutronServiceConfig stores configuration parameters for the Neutron service.
type NeutronServiceConfig struct {
ChainID string `json:"chainId"`
Key string `json:"key"`
Password string `json:"password"`
PublicGrpc int `json:"public_grpc"`
PublicTCP int `json:"public_tcp"`
PublicHTTP int `json:"public_http"`
PublicRPC int `json:"public_rpc"`
}

// EncodeToString encodes the NeutronServiceConfig struct to a JSON string.
func (as *NeutronServiceConfig) EncodeToString() (string, error) {
data, err := json.Marshal(as)
if err != nil {
return "", err
}
return string(data), nil
}

// ReadServiceConfig reads the Neutron service configuration from a JSON file.
func (as *NeutronServiceConfig) ReadServiceConfig(path string) error {
configData, err := common.ReadConfigFile(neutron_node_config)
if err != nil {
return err
}
err = json.Unmarshal(configData, as)
if err != nil {
return err
}
return nil
}

// NewNeutronCmd creates a new Cobra command for the Neutron 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 ")

neutronCmd.Flags().StringVarP(&neutron_node_config, "config", "c", "", "path to custom config json file to start neutron node ")
return neutronCmd
}


// RunNeutronNode starts the Neutron node.
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.FatalError("Failed To Retrieve Enclave Context", err.Error())
}

diveContext.StartSpinner(" Starting Neutron Node")
diveContext.StartSpinner("Starting Neutron Node")
var serviceConfig = &NeutronServiceConfig{}
var neutronResponse = &common.DiveserviceResponse{}
var starlarkExecutionData = ""
starlarkExecutionData, err = runNeutronWithDefaultServiceConfig(diveContext, kurtosisEnclaveContext)
if err != nil {
diveContext.FatalError("Starlark Run Failed", err.Error())

if neutron_node_config != "" {
err := serviceConfig.ReadServiceConfig(neutron_node_config)
if err != nil {
diveContext.FatalError("Failed read service config", err.Error())
}

encodedServiceConfigDataString, err := serviceConfig.EncodeToString()
if err != nil {
diveContext.FatalError("Failed to encode service config", err.Error())
}

// Run Neutron Node with custom service config
starlarkExecutionData, err = RunNeutronWithServiceConfig(diveContext, kurtosisEnclaveContext, encodedServiceConfigDataString)
if err != nil {
diveContext.FatalError("Starlark Run Failed", err.Error())
}
} else {
// Run Neutron Node with default service config
starlarkExecutionData, err = RunNeutronWithServiceConfig(diveContext, kurtosisEnclaveContext, "{}")
if err != nil {
diveContext.FatalError("Starlark Run Failed", err.Error())
}
}
err = json.Unmarshal([]byte(starlarkExecutionData), neutronResponse)

err = json.Unmarshal([]byte(starlarkExecutionData), neutronResponse)
if err != nil {
diveContext.FatalError("Failed to Unmarshall Service Response", err.Error())
diveContext.FatalError("Failed to Unmarshal 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{})

// RunNeutronWithServiceConfig runs the Neutron service with the provided configuration data.
func RunNeutronWithServiceConfig(diveContext *common.DiveContext, enclaveContext *enclaves.EnclaveContext, data string) (string, error) {
params := fmt.Sprintf(`{"args":{"data":%s}}`, data)
nodeServiceResponse, _, err := enclaveContext.RunStarlarkPackage(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")

diveContext.CheckInstructionSkipped(skippedInstructions, "Neutron Node Already Running")
return nodeServiceResponseData, nil
}
}

0 comments on commit 7357580

Please sign in to comment.