Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement bridge logic to handle sequential node run and setup btp #85

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 103 additions & 24 deletions cli/commands/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package bridge

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

Expand All @@ -14,6 +15,8 @@ import (
)

const bridgeMainFunction = "run_btp_setup"
const runbridgeicon2icon = "start_btp_for_already_running_icon_nodes"
const runbridgeicon2ethhardhat = "start_btp_icon_to_eth_for_already_running_nodes"

var (
chainA string
Expand Down Expand Up @@ -57,44 +60,73 @@ func btpBridgeCmd(diveContext *common.DiveContext) *cobra.Command {

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

params := fmt.Sprintf(`{"args":{"links": {"src": "%s", "dst": "%s"},"bridge":"%s"}}`, chainA, chainB, strconv.FormatBool(bridge))

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

diveContext.SetSpinnerMessage(" Executing BTP Starlark Package")

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

if err != nil {
diveContext.FatalError("Starlark Run Failed", err.Error())
diveContext.FatalError("Failed To read ServiceFile", err.Error())
}
response, services, skippedInstructions, err := diveContext.GetSerializedData(data)
if err != nil {
diveContext.StopServices(services)
diveContext.FatalError("Starlark Run Failed", err.Error())

}
if len(serviceConfig) != 0 {
srcChain := "icon"
dstChain := "icon-1"

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

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

} else if strings.ToLower(chainA) == "icon" && (strings.ToLower(chainB) == "eth" || strings.ToLower(chainB) == "hardhat") {
diveContext.SetSpinnerMessage(" Executing BTP Starlark Package")
data, _, err := enclaveCtx.RunStarlarkRemotePackage(diveContext.Ctx, common.DiveRemotePackagePath, common.DiveBridgeScript, bridgeMainFunction, params, common.DiveDryRun, common.DiveDefaultParallelism, []kurtosis_core_rpc_api_bindings.KurtosisFeatureFlag{})
srcChainServiceName := serviceConfig["icon-0"].ServiceName
dstChainServiceName := serviceConfig["icon-1"].ServiceName

if err != nil {
diveContext.FatalError("Starlark Run Failed", err.Error())
runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2icon, srcChain, dstChain, srcChainServiceName, dstChainServiceName, bridge, srcChainServiceResponse, dstChainServiceResponse)

} else {

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

runBtpSetupByRunningNodes(diveContext, enclaveCtx, params)
}
response, services, skippedInstructions, err := diveContext.GetSerializedData(data)
if err != nil {
diveContext.StopServices(services)
diveContext.FatalError("Starlark Run Failed", err.Error())

} 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())
}
diveContext.CheckInstructionSkipped(skippedInstructions, "Bridge Already Running")
common.WriteToFile(response)

if len(serviceConfig) != 0 {
srcChain := strings.ToLower(chainA)
dstChain := strings.ToLower(chainB)

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

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

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

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

} else {

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

runBtpSetupByRunningNodes(diveContext, enclaveCtx, params)
}
} else {
diveContext.FatalError("Chains Not Supported", "Supported Chains [icon,eth,hardhat]")
}
Expand All @@ -112,3 +144,50 @@ func btpBridgeCmd(diveContext *common.DiveContext) *cobra.Command {

return btpbridgeCmd
}

func runBtpSetupByRunningNodes(diveContext *common.DiveContext, enclaveCtx *enclaves.EnclaveContext, params string) {
diveContext.SetSpinnerMessage(" Executing BTP Starlark Package")

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

if err != nil {
diveContext.FatalError("Starlark Run Failed", err.Error())
}
response, services, skippedInstructions, err := diveContext.GetSerializedData(data)
if err != nil {
diveContext.StopServices(services)
diveContext.FatalError("Starlark Run Failed", err.Error())

}
diveContext.CheckInstructionSkipped(skippedInstructions, "Bridge Already Running")
common.WriteToFile(response)

}

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

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)

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

if err != nil {
diveContext.FatalError("Starlark Run Failed", err.Error())
}
response, services, skippedInstructions, err := diveContext.GetSerializedData(data)
if err != nil {
diveContext.StopServices(services)
diveContext.FatalError("Starlark Run Failed", err.Error())

}

diveContext.CheckInstructionSkipped(skippedInstructions, "Bridge Already Running")

common.WriteToFile(response)

}

func getParams(chainSrc, chainDst, bridge string) string {
return fmt.Sprintf(`{"args":{"links": {"src": "%s", "dst": "%s"},"bridge":"%s"}}`, chainSrc, chainDst, bridge)
}
2 changes: 1 addition & 1 deletion cli/commands/chain/types/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ It establishes a connection to the Ethereum network and allows the node in execu
data := RunEthNode(diveContext)

diveContext.SetSpinnerMessage("Execution Completed")
err := data.WriteDiveResponse()
err := common.WriteToServiceFile(data.NetworkName, *data)
if err != nil {
diveContext.FatalError("Failed To Write To File", err.Error())
}
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 := data.WriteDiveResponse()
err := common.WriteToServiceFile(data.NetworkName, *data)
if err != nil {
diveContext.FatalError("Failed To Write To File", err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions cli/commands/chain/types/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ It establishes a connection to the Icon network and allows the node in executing
diveContext.SetSpinnerMessage("Starting Decentralisation")
Decentralisation(diveContext, params)

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

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

nodeResponse := RunIconNode(diveContext)

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

if err != nil {
diveContext.FatalError("Failed To Write To File", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/hugobyte/dive/styles"
"github.com/spf13/cobra"
)
)

func RootCmd() *cobra.Command {
diveContext := common.NewDiveContext()
Expand Down
93 changes: 93 additions & 0 deletions cli/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,96 @@ func setupLogger() *logrus.Logger {

return log
}

type Services map[string]*DiveserviceResponse

func WriteToServiceFile(serviceName string, data DiveserviceResponse) error {

pwd, err := getPwd()
if err != nil {
return err
}

file, err := os.OpenFile(pwd+"/services.json", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}

if err != nil {
return err
}
defer file.Close()

jsonDataFromFile, err := ReadServiceJsonFile()

if err != nil {
return err
}

if len(jsonDataFromFile) != 0 {
_, ok := jsonDataFromFile[serviceName]
if !ok {
jsonDataFromFile[serviceName] = &data
dataTowrite, err := json.Marshal(jsonDataFromFile)
if err != nil {
return err
}

_, err = file.WriteAt(dataTowrite, 0)

if err != nil {
return err
}

return nil
}

}
newServices := Services{}

newServices[serviceName] = &data

dataTowrite, err := json.Marshal(newServices)
if err != nil {
return err
}

_, err = file.Write(dataTowrite)

if err != nil {
return err
}

return nil

}

func ReadServiceJsonFile() (Services, error) {

services := Services{}

pwd, err := getPwd()
if err != nil {
return nil, err
}

jsonFile, _ := os.ReadFile(pwd + "/services.json")

if len(jsonFile) == 0 {
return nil, nil
}
json.Unmarshal(jsonFile, &services)

return services, nil

}

func getPwd() (string, error) {
pwd, err := os.Getwd()

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

return pwd, nil
}
Loading