Skip to content

Commit

Permalink
fix: bug fix for nil pointer error and fix bridge (#105)
Browse files Browse the repository at this point in the history
* chore: initial cosmos setup

* feat: update starlark package for cosmos nodes and cosmos relays

* feat: update icon starlark package

* feat: restructure btp setup

* feat: update eth and hardhat btp setup

* fix: bridge btp chainA other icon fails

* fix: start bridge with single chain already running

* fix: run bridge with single chain running

* fix: variable assignment

* fix: typo in command name

* fix: add gas price for eth deployment

* fix: update gas price

* chore: update version and goreleaser
  • Loading branch information
shreyasbhat0 authored Aug 11, 2023
1 parent 3b0342f commit 6aab34a
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 83 deletions.
15 changes: 6 additions & 9 deletions cli/.goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ builds:
- CGO_ENABLED={{ if eq .Os "windows" }}0{{ else }}1{{ end }}
# Only override compilers if we are on CI, otherwise use the locally available C/C++ toolchain
- >-
{{- if index .Env "CI" }}
{{- if eq .Os "darwin" }}CC=o64h-clang{{- end }}
{{- if eq .Os "darwin" }}CC=o64h-clang{{- end }}
{{- if eq .Os "linux" }}
{{- if eq .Arch "amd64"}}CC=x86_64-linux-musl-gcc{{- end }}
{{- if eq .Arch "arm64"}}CC=aarch64-linux-musl-gcc{{- end }}
{{- end }}
{{- end }}
- >-
{{- if index .Env "CI" }}
{{- if eq .Os "darwin" }}CXX=o64h-clang++{{- end }}
{{- if eq .Os "linux" }}
{{- if eq .Arch "amd64"}}CXX=x86_64-linux-musl-g++{{- end }}
{{- if eq .Arch "arm64"}}CXX=aarch64-linux-musl-g++{{- end }}
{{- end }}
{{- if eq .Os "darwin" }}CXX=o64h-clang++{{- end }}
{{- if eq .Os "linux" }}
{{- if eq .Arch "amd64"}}CXX=x86_64-linux-musl-g++{{- end }}
{{- if eq .Arch "arm64"}}CXX=aarch64-linux-musl-g++{{- end }}
{{- end }}
ldflags:
- >-
Expand Down
181 changes: 134 additions & 47 deletions cli/commands/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package bridge

import (
"fmt"
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves"
"strconv"
"strings"

"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves"

"github.com/hugobyte/dive/commands/chain/types"
"github.com/hugobyte/dive/common"
"github.com/kurtosis-tech/kurtosis/api/golang/core/kurtosis_core_rpc_api_bindings"
"github.com/spf13/cobra"
Expand All @@ -19,10 +21,55 @@ const runbridgeicon2icon = "start_btp_for_already_running_icon_nodes"
const runbridgeicon2ethhardhat = "start_btp_icon_to_eth_for_already_running_nodes"

var (
chainA string
chainB string
chainA string
chainB string
serviceA string
serviceB string
)

var runChain = map[string]func(diveContext *common.DiveContext) *common.DiveserviceResponse{
"icon": func(diveContext *common.DiveContext) *common.DiveserviceResponse {
nodeResponse := types.RunIconNode(diveContext)
params := types.GetDecentralizeParms(nodeResponse.ServiceName, nodeResponse.PrivateEndpoint, nodeResponse.KeystorePath, nodeResponse.KeyPassword, nodeResponse.NetworkId)

diveContext.SetSpinnerMessage("Starting Decentralisation")
types.Decentralisation(diveContext, params)

return nodeResponse

},
"eth": func(diveContext *common.DiveContext) *common.DiveserviceResponse {
return types.RunEthNode(diveContext)

},
"hardhat": func(diveContext *common.DiveContext) *common.DiveserviceResponse {

return types.RunHardhatNode(diveContext)
},
}

type Chains struct {
chainA string
chainB string
chainAServiceName string
chainBServiceName string
bridge string
}

func initChains(chainA, chainB, serviceA, serviceB string, bridge bool) *Chains {
return &Chains{
chainA: strings.ToLower(chainA),
chainB: strings.ToLower(chainB),
chainAServiceName: serviceA,
chainBServiceName: serviceB,
bridge: strconv.FormatBool(bridge),
}
}

func (c *Chains) areChainsIcon() bool {
return (c.chainA == "icon" && c.chainB == "icon")
}

func NewBridgeCmd(diveContext *common.DiveContext) *cobra.Command {

var bridgeCmd = &cobra.Command{
Expand Down Expand Up @@ -56,79 +103,96 @@ func btpBridgeCmd(diveContext *common.DiveContext) *cobra.Command {
if err != nil {
diveContext.Error(err.Error())
}
diveContext.StartSpinner(fmt.Sprintf(" Starting BTP Bridge for %s,%s", chainA, chainB))

bridge, _ := cmd.Flags().GetBool("bridge")

if strings.ToLower(chainA) == "icon" && strings.ToLower(chainB) == "icon" {

serviceConfig, err := common.ReadServiceJsonFile()
chains := initChains(chainA, chainB, serviceA, serviceB, bridge)
diveContext.StartSpinner(fmt.Sprintf(" Starting BTP Bridge for %s,%s", chains.chainA, chains.chainB))

if err != nil {
diveContext.FatalError("Failed To read ServiceFile", err.Error())
}
if chains.areChainsIcon() {

if len(serviceConfig) != 0 {
srcChain := "icon"
dstChain := "icon-1"
if chains.chainAServiceName != "" && chains.chainBServiceName != "" {

srcChainServiceResponse, err := serviceConfig["icon-0"].EncodeToString()
if err != nil {
diveContext.FatalError("Failed To read ServiceFile", err.Error())
}
dstChainServiceResponse, err := serviceConfig["icon-1"].EncodeToString()
srcChainServiceResponse, dstChainServiceResponse, err := chains.getServicesResponse()

if err != nil {
diveContext.FatalError("Failed To read ServiceFile", err.Error())
}

srcChainServiceName := serviceConfig["icon-0"].ServiceName
dstChainServiceName := serviceConfig["icon-1"].ServiceName

runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2icon, srcChain, dstChain, srcChainServiceName, dstChainServiceName, bridge, srcChainServiceResponse, dstChainServiceResponse)
runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2icon, chains.chainA, chains.chainB, chains.chainAServiceName, chains.chainBServiceName, bridge, srcChainServiceResponse, dstChainServiceResponse)

} else {

params := getParams(chainA, chainA, strconv.FormatBool(bridge))
params := chains.getParams()

runBtpSetupByRunningNodes(diveContext, enclaveCtx, params)
}

} else if (strings.ToLower(chainA) == "icon") && (strings.ToLower(chainB) == "eth" || strings.ToLower(chainB) == "hardhat") {

serviceConfig, err := common.ReadServiceJsonFile()

if err != nil {
diveContext.FatalError("Failed To read ServiceFile", err.Error())
}

if len(serviceConfig) != 0 {
srcChain := strings.ToLower(chainA)
dstChain := strings.ToLower(chainB)
} else {
if chains.chainAServiceName != "" && chains.chainBServiceName != "" {

srcChainServiceResponse, dstChainServiceResponse, err := chains.getServicesResponse()

srcChainServiceResponse, err := serviceConfig["icon-0"].EncodeToString()
if err != nil {
diveContext.FatalError("Failed To read ServiceFile", err.Error())
}
dstChainServiceResponse, err := serviceConfig[dstChain].EncodeToString()
runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2ethhardhat, chains.chainA, chains.chainB, chains.chainAServiceName, chains.chainBServiceName, bridge, srcChainServiceResponse, dstChainServiceResponse)
} else if (chains.chainAServiceName == "" && chains.chainBServiceName != "") || (chains.chainAServiceName != "" && chains.chainBServiceName == "") {

var chainAServiceResponse string
var chainBServiceResponse string
var chainAServiceName string
var chainBServiceName string

serviceConfig, err := common.ReadServiceJsonFile()
if err != nil {
diveContext.FatalError("Failed To read ServiceFile", err.Error())
diveContext.FatalError("Failed To Get Service Data", err.Error())
}

if chains.chainAServiceName == "" {
response := runChain[chains.chainA](diveContext)
chainAServiceName = response.ServiceName
chainAServiceResponse, err = response.EncodeToString()
if err != nil {
diveContext.FatalError("Failed To Get Service Data", err.Error())
}
chainBServiceName = serviceConfig[chains.chainBServiceName].ServiceName

chainBServiceResponse, err = serviceConfig[chains.chainBServiceName].EncodeToString()

if err != nil {
diveContext.FatalError("Failed To Get Service Data", err.Error())
}

} else if chains.chainBServiceName == "" {
response := runChain[chains.chainB](diveContext)
chainBServiceName = response.ServiceName
chainBServiceResponse, err = response.EncodeToString()
if err != nil {
diveContext.FatalError("Failed To Get Service Data", err.Error())
}
chainAServiceName = serviceConfig[chains.chainAServiceName].ServiceName
chainAServiceResponse, err = serviceConfig[chains.chainAServiceName].EncodeToString()

if err != nil {
diveContext.FatalError("Failed To Get Service Data", err.Error())
}
}
if chains.chainB == "icon" {
runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2ethhardhat, chains.chainB, chains.chainA, chainBServiceName, chainAServiceName, bridge, chainBServiceResponse, chainAServiceResponse)
} else {

srcChainServiceName := serviceConfig["icon-0"].ServiceName
dstChainServiceName := serviceConfig[dstChain].ServiceName
runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2ethhardhat, chains.chainA, chains.chainB, chainAServiceName, chainBServiceName, bridge, chainAServiceResponse, chainBServiceResponse)

runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2ethhardhat, srcChain, dstChain, srcChainServiceName, dstChainServiceName, bridge, srcChainServiceResponse, dstChainServiceResponse)
}

} else {

params := getParams(chainA, chainB, strconv.FormatBool(bridge))
params := chains.getParams()

runBtpSetupByRunningNodes(diveContext, enclaveCtx, params)
}
} else {
diveContext.FatalError("Chains Not Supported", "Supported Chains [icon,eth,hardhat]")

}

diveContext.StopSpinner(fmt.Sprintf("BTP Bridge Setup Completed between %s and %s. Please find service details in current working directory(dive.json)", chainA, chainB))
Expand All @@ -139,6 +203,8 @@ func btpBridgeCmd(diveContext *common.DiveContext) *cobra.Command {
btpbridgeCmd.Flags().StringVar(&chainB, "chainB", "", "Mention Name of Supported Chain")
btpbridgeCmd.Flags().Bool("bridge", false, "Mention Bridge ENV")

btpbridgeCmd.Flags().StringVar(&serviceA, "chainAServiceName", "", "Service Name of Chain A from services.json")
btpbridgeCmd.Flags().StringVar(&serviceB, "chainBServiceName", "", "Service Name of Chain B from services.json")
btpbridgeCmd.MarkFlagRequired("chainA")
btpbridgeCmd.MarkFlagRequired("chainB")

Expand Down Expand Up @@ -166,9 +232,9 @@ func runBtpSetupByRunningNodes(diveContext *common.DiveContext, enclaveCtx *encl

func runBtpSetupForAlreadyRunningNodes(diveContext *common.DiveContext, enclaveCtx *enclaves.EnclaveContext, mainFunctionName string, srcChain string, dstChain string, srcChainServiceName string, dstChainServiceName string, bridge bool, srcChainServiceResponse string, dstChainServiceResponse string) {

configData := fmt.Sprintf(`{"links": {"src":"%s","dst":"%s"},"chains" : { "%s" : %s,"%s" : %s},"contracts" : {"%s" : {},"%s" : {}},"bridge" : "%s"}`, srcChain, dstChain, srcChain, srcChainServiceResponse, dstChain, dstChainServiceResponse, srcChain, dstChain, strconv.FormatBool(bridge))
configData := fmt.Sprintf(`{"links": {"src":"%s","dst":"%s"},"chains" : { "%s" : %s,"%s" : %s},"contracts" : {"%s" : {},"%s" : {}},"bridge" : "%s"}`, srcChain, dstChain, srcChainServiceName, srcChainServiceResponse, dstChainServiceName, dstChainServiceResponse, srcChainServiceName, dstChainServiceName, strconv.FormatBool(bridge))

params := fmt.Sprintf(`{"src_chain":"%s", "dst_chain":"%s", "config_data":%s, "src_service_name":"%s", "dst_src_name":"%s"}`, srcChain, dstChain, configData, srcChainServiceName, dstChainServiceName)
params := fmt.Sprintf(`{"src_chain":"%s", "dst_chain":"%s", "config_data":%s, "src_service_name":"%s", "dst_service_name":"%s"}`, srcChain, dstChain, configData, srcChainServiceName, dstChainServiceName)

data, _, err := enclaveCtx.RunStarlarkRemotePackage(diveContext.Ctx, common.DiveRemotePackagePath, common.DiveBridgeScript, mainFunctionName, params, common.DiveDryRun, common.DiveDefaultParallelism, []kurtosis_core_rpc_api_bindings.KurtosisFeatureFlag{})

Expand All @@ -188,6 +254,27 @@ func runBtpSetupForAlreadyRunningNodes(diveContext *common.DiveContext, enclaveC

}

func getParams(chainSrc, chainDst, bridge string) string {
return fmt.Sprintf(`{"args":{"links": {"src": "%s", "dst": "%s"},"bridge":"%s"}}`, chainSrc, chainDst, bridge)
func (chains *Chains) getParams() string {
return fmt.Sprintf(`{"args":{"links": {"src": "%s", "dst": "%s"},"bridge":"%s"}}`, chains.chainA, chains.chainB, chains.bridge)
}

func (chains *Chains) getServicesResponse() (string, string, error) {

serviceConfig, err := common.ReadServiceJsonFile()

if err != nil {
return "", "", err
}

srcChainServiceResponse, err := serviceConfig[chains.chainAServiceName].EncodeToString()
if err != nil {
return "", "", err
}
dstChainServiceResponse, err := serviceConfig[chains.chainBServiceName].EncodeToString()

if err != nil {
return "", "", err
}

return srcChainServiceResponse, dstChainServiceResponse, nil
}
2 changes: 2 additions & 0 deletions cli/commands/chain/types/cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ It establishes a connection to the Cosmos network and allows the node in executi

return cosmosCmd
}

func RunCosmosNode() {}
3 changes: 2 additions & 1 deletion cli/commands/chain/types/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ It establishes a connection to the Ethereum network and allows the node in execu
data := RunEthNode(diveContext)

diveContext.SetSpinnerMessage("Execution Completed")
err := common.WriteToServiceFile(data.NetworkName, *data)
err := common.WriteToServiceFile(data.ServiceName, *data)
if err != nil {
diveContext.FatalError("Failed To Write To File", err.Error())
}
diveContext.StopSpinner("ETH Node Started. Please find service details in current working directory(services.json)")

},
}

Expand Down
2 changes: 1 addition & 1 deletion cli/commands/chain/types/hardhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ It establishes a connection to the hardhat network and allows the node in execut

diveContext.SetSpinnerMessage("Execution Completed")

err := common.WriteToServiceFile(data.NetworkName, *data)
err := common.WriteToServiceFile(data.ServiceName, *data)
if err != nil {
diveContext.FatalError("Failed To Write To File", err.Error())
}
Expand Down
13 changes: 5 additions & 8 deletions cli/commands/chain/types/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var (
)

type IconServiceConfig struct {
Id string `json:"id" default:"0"`
Port int `json:"private_port"`
PublicPort int `json:"public_port"`
P2PListenAddress string `json:"p2p_listen_address"`
Expand All @@ -34,8 +33,6 @@ type IconServiceConfig struct {
}

func (sc *IconServiceConfig) GetDefaultConfigIconNode0() {

sc.Id = "0"
sc.Port = 9080
sc.PublicPort = 8090
sc.P2PListenAddress = "7080"
Expand Down Expand Up @@ -74,7 +71,7 @@ It establishes a connection to the Icon network and allows the node in executing
diveContext.SetSpinnerMessage("Starting Decentralisation")
Decentralisation(diveContext, params)

err := common.WriteToServiceFile(nodeResponse.NetworkName, *nodeResponse)
err := common.WriteToServiceFile(nodeResponse.ServiceName, *nodeResponse)

if err != nil {
diveContext.FatalError("Failed To Write To File", err.Error())
Expand All @@ -86,7 +83,7 @@ It establishes a connection to the Icon network and allows the node in executing

nodeResponse := RunIconNode(diveContext)

err := common.WriteToServiceFile(nodeResponse.NetworkName, *nodeResponse)
err := common.WriteToServiceFile(nodeResponse.ServiceName, *nodeResponse)

if err != nil {
diveContext.FatalError("Failed To Write To File", err.Error())
Expand All @@ -113,8 +110,8 @@ func IconDecentralisationCmd(diveContext *common.DiveContext) *cobra.Command {

var decentralisationCmd = &cobra.Command{
Use: "decentralize",
Short: "Decentralise already running Icon Node",
Long: `Decentralise Icon Node is necessary if you want to connect your local icon node to BTP network`,
Short: "Decentralize already running Icon Node",
Long: `Decentralize Icon Node is necessary if you want to connect your local icon node to BTP network`,
Run: func(cmd *cobra.Command, args []string) {

params := GetDecentralizeParms(serviceName, nodeEndpoint, keystorePath, keystorepassword, networkID)
Expand Down Expand Up @@ -184,7 +181,7 @@ func RunIconNode(diveContext *common.DiveContext) *common.DiveserviceResponse {

diveContext.CheckInstructionSkipped(skippedInstructions, "Instruction Executed Already")

params := fmt.Sprintf(`{"service_config":%s,"id":"%s","uploaded_genesis":%s,"genesis_file_path":"%s","genesis_file_name":"%s"}`, responseData, serviceConfig.Id, genesisHandler.uploadedFiles, genesisHandler.genesisPath, genesisHandler.genesisFile)
params := fmt.Sprintf(`{"service_config":%s,"uploaded_genesis":%s,"genesis_file_path":"%s","genesis_file_name":"%s"}`, responseData, genesisHandler.uploadedFiles, genesisHandler.genesisPath, genesisHandler.genesisFile)
icon_data, _, err := kurtosisEnclaveContext.RunStarlarkRemotePackage(diveContext.Ctx, common.DiveRemotePackagePath, common.DiveIconNodeScript, "start_icon_node", params, false, 4, []kurtosis_core_rpc_api_bindings.KurtosisFeatureFlag{})

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
var DiveLogs bool

// !!!!!!!!!!! DO NOT UPDATE! WILL BE UPDATED DURING THE RELEASE PROCESS !!!!!!!!!!!!!!!!!!!!!!
var DiveVersion = "v0.0.4-beta"
var DiveVersion = "v0.0.5-beta"

const (
DiveEnclave = "dive"
Expand Down
Loading

0 comments on commit 6aab34a

Please sign in to comment.