Skip to content

Commit

Permalink
feat: update bridge command
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Dec 8, 2023
1 parent 31290e0 commit 72067d6
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 8 deletions.
44 changes: 41 additions & 3 deletions cli/cmd/bridge/btp/cmd.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package btp

import (
"fmt"

"github.com/hugobyte/dive-core/cli/cmd/bridge/utils"
"github.com/hugobyte/dive-core/cli/common"
"github.com/spf13/cobra"
)

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"
const runBridgeIcon2icon = "start_btp_for_already_running_icon_nodes"
const runBridgeIcon2EthHardhat = "start_btp_icon_to_eth_for_already_running_nodes"

var (
chainA string
Expand All @@ -21,6 +24,41 @@ var BtpRelayCmd = common.NewDiveCommandBuilder().
SetShort("Starts BTP Relay to bridge between ChainA and ChainB").
SetLong("Setup and Starts BTP Relay between ChainA and ChainB a").
SetRun(btpRelay).
AddStringFlag(&chainA, "chainA", "", "Mention Name of Supported Chain").
AddStringFlag(&chainB, "chainB", "", "Mention Name of Supported Chain").
AddBoolFlagP("bmvbridge", "b", false, "To Specify Which Type of BMV to be used in btp setup(if true BMV bridge is used else BMV-BTP Block is used)").
AddStringFlag(&serviceA, "chainAServiceName", "", "Service Name of Chain A from services.json").
AddStringFlag(&serviceB, "chainBServiceName", "", "Service Name of Chain B from services.json").
MarkFlagRequired("chainA").
MarkFlagRequired("chainB").
Build()

func btpRelay(cmd *cobra.Command, args []string) {}
func btpRelay(cmd *cobra.Command, args []string) {

cliContext := common.GetCli()

err := common.ValidateArgs(args)
if err != nil {
cliContext.Fatal(err)
}
cliContext.StartSpinnerIfNotVerbose("Starting BTP Setup", common.DiveLogs)

bridge, err := cmd.Flags().GetBool("bmvbridge")
if err != nil {
cliContext.Fatal(common.WrapMessageToError(common.ErrInvalidCommandArguments, err.Error()))
}

chains := utils.InitChains(chainA, chainB, serviceA, serviceB, bridge)

result, err := RunBtpSetup(cliContext, chains, bridge)

if err != nil {
cliContext.Fatal(err)
}

err = cliContext.FileHandler().WriteFile("dive.json", []byte(result))
if err != nil {
cliContext.Fatal(err)
}
cliContext.StopSpinnerIfNotVerbose(fmt.Sprintf("BTP Setup Completed between %s and %s. Please find service details in current working directory(dive.json)", chainA, chainB), common.DiveLogs)
}
245 changes: 245 additions & 0 deletions cli/cmd/bridge/btp/run.go
Original file line number Diff line number Diff line change
@@ -1 +1,246 @@
package btp

import (
"fmt"
"strconv"

"github.com/hugobyte/dive-core/cli/cmd/bridge/utils"
"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/common"
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves"
)

var runChain = map[string]func(cli *common.Cli) (*common.DiveServiceResponse, error){
"icon": func(cli *common.Cli) (*common.DiveServiceResponse, error) {
nodeResponse, err := icon.RunIconNode(cli)
if err != nil {
return nil, err
}
params := icon.GetDecentralizeParams(nodeResponse.ServiceName, nodeResponse.PrivateEndpoint, nodeResponse.KeystorePath, nodeResponse.KeyPassword, nodeResponse.NetworkId)

cli.Spinner().StartWithMessage("Starting Decentralization", "green")
err = icon.RunDecentralization(cli, params)
if err != nil {
return nil, err
}

return nodeResponse, nil

},
"eth": func(cli *common.Cli) (*common.DiveServiceResponse, error) {
return eth.RunEth(cli)

},
"hardhat": func(cli *common.Cli) (*common.DiveServiceResponse, error) {

return hardhat.RunHardhat(cli)
},
}

func RunBtpSetup(cli *common.Cli, chains *utils.Chains, bridge bool) (string, error) {
var starlarkExecutionResponse string

enclaveContext, err := cli.Context().GetEnclaveContext(common.EnclaveName)

if err != nil {
return "", common.WrapMessageToError(err, "BTP Setup Run Failed")
}
if chains.AreChainsIcon() {
starlarkExecutionResponse, err = runBtpSetupWhenChainsAreIcon(cli, chains, enclaveContext, bridge)
if err != nil {
return "", err
}
} else {
starlarkExecutionResponse, err = runBtpSetupWhenChainsAreNotIcon(cli, enclaveContext, chains, bridge)
if err != nil {
return "", err
}
}

return starlarkExecutionResponse, nil

}

func runBtpSetupWhenChainsAreIcon(cli *common.Cli, chains *utils.Chains, enclaveContext *enclaves.EnclaveContext, bridge bool) (string, error) {

if chains.ChainAServiceName != "" && chains.ChainBServiceName == "" {
srcChainServiceResponse, dstChainServiceResponse, err := chains.GetServicesResponse(cli)
if err != nil {
return "", common.WrapMessageToError(err, "BTP Setup run Failed For Icon Chains")
}
response, err := runBtpSetupForAlreadyRunningNodes(cli, enclaveContext, runBridgeIcon2icon, chains.ChainA, chains.ChainB, chains.ChainAServiceName, chains.ChainBServiceName, bridge, srcChainServiceResponse, dstChainServiceResponse)
if err != nil {
return "", common.WrapMessageToError(err, "BTP Setup run Failed For Icon Chains")
}
return response, nil
} else {
params := chains.GetParams()

response, err := runBtpSetupByRunningNodes(cli, enclaveContext, params)
if err != nil {
return "", common.WrapMessageToError(err, "BTP Setup run Failed For Icon Chains")
}
return response, nil
}

}

func runBtpSetupWhenChainsAreNotIcon(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, chains *utils.Chains, bridge bool) (string, error) {

if chains.ChainAServiceName != "" && chains.ChainBServiceName != "" {
var response string
chainAServiceResponse, chainBServiceResponse, err := chains.GetServicesResponse(cli)
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed For ChainA %s and ChainB %s", chains.ChainA, chains.ChainB))
}
if chains.ChainB == "icon" {
response, err = runBtpSetupForAlreadyRunningNodes(cli, enclaveContext, runBridgeIcon2EthHardhat, chains.ChainB, chains.ChainA, chains.ChainBServiceName, chains.ChainAServiceName, bridge, chainBServiceResponse, chainAServiceResponse)
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed For ChainA %s and ChainB %s", chains.ChainA, chains.ChainB))
}
} else {
response, err = runBtpSetupForAlreadyRunningNodes(cli, enclaveContext, runBridgeIcon2EthHardhat, chains.ChainA, chains.ChainB, chains.ChainAServiceName, chains.ChainBServiceName, bridge, chainAServiceResponse, chainBServiceResponse)
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed For ChainA %s and ChainB %s", chains.ChainA, chains.ChainB))
}
}

return response, nil
} else if (chains.ChainAServiceName == "" && chains.ChainBServiceName != "") || (chains.ChainAServiceName != "" && chains.ChainBServiceName == "") {
response, err := runBtpSetupWhenSingleChainRunning(cli, enclaveContext, chains, bridge)
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed For ChainA %s and ChainB %s", chains.ChainA, chains.ChainB))
}
return response, nil
} else {
params := chains.GetParams()
response, err := runBtpSetupByRunningNodes(cli, enclaveContext, params)
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed For ChainA %s and ChainB %s", chains.ChainA, chains.ChainB))
}
return response, nil
}

}

func runBtpSetupWhenSingleChainRunning(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, chains *utils.Chains, bridge bool) (string, error) {
var chainAServiceResponse, chainBServiceResponse, chainAServiceName, chainBServiceName, response string
var services = common.Services{}
err := cli.FileHandler().ReadJson("services.json", &services)

if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed For ChainA %s and ChainB %s", chains.ChainA, chains.ChainB))
}

if chains.ChainAServiceName == "" {
serviceResponse, OK := services[chains.ChainBServiceName]
if !OK {
return "", common.WrapMessageToError(common.ErrDataUnMarshall, fmt.Sprint("service name not found:", chains.ChainBServiceName))
}
chainBServiceName = serviceResponse.ServiceName

chainBServiceResponse, err = serviceResponse.EncodeToString()
if err != nil {
return "", common.WrapMessageToError(err, "BTP Setup Failed")
}
responseData, err := runChain[chains.ChainA](cli)
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed Due To ChainA %s Run Failed ", chains.ChainA))
}
chainAServiceName = responseData.ServiceName
chainAServiceResponse, err = responseData.EncodeToString()
if err != nil {
return "", err
}

} else if chains.ChainBServiceName == "" {
serviceResponse, OK := services[chains.ChainAServiceName]
if !OK {
return "", common.WrapMessageToError(common.ErrDataUnMarshall, fmt.Sprint("service name not found:", chains.ChainAServiceName))
}
chainAServiceName = serviceResponse.ServiceName
chainAServiceResponse, err = serviceResponse.EncodeToString()
if err != nil {
return "", common.WrapMessageToError(err, "BTP Setup Failed")
}
responseData, err := runChain[chains.ChainB](cli)
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed Due To ChainB %s Run Failed ", chains.ChainB))
}
chainBServiceName = responseData.ServiceName
chainBServiceResponse, err = responseData.EncodeToString()
if err != nil {
return "", err
}

}

if chains.ChainB == "icon" {
response, err = runBtpSetupForAlreadyRunningNodes(cli, enclaveContext, runBridgeIcon2EthHardhat, chains.ChainB, chains.ChainA, chainBServiceName, chainAServiceName, bridge, chainBServiceResponse, chainAServiceResponse)
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed For ChainA %s and ChainB %s", chains.ChainA, chains.ChainB))
}
} else {
response, err = runBtpSetupForAlreadyRunningNodes(cli, enclaveContext, runBridgeIcon2EthHardhat, chains.ChainA, chains.ChainB, chainAServiceName, chainBServiceName, bridge, chainAServiceResponse, chainBServiceResponse)
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed For ChainA %s and ChainB %s", chains.ChainA, chains.ChainB))
}
}

return response, nil
}

func runBtpSetupByRunningNodes(cli *common.Cli, enclaveCtx *enclaves.EnclaveContext, params string) (string, error) {

starlarkConfig := common.GetStarlarkRunConfig(params, common.DiveBridgeBtpScript, bridgeMainFunction)
executionData, _, err := enclaveCtx.RunStarlarkRemotePackage(cli.Context().GetContext(), common.DiveRemotePackagePath, starlarkConfig)

if err != nil {
return "", common.WrapMessageToError(common.ErrStarlarkRunFailed, err.Error())
}
executionSerializedData, services, skippedInstructions, err := common.GetSerializedData(cli, executionData)
if err != nil {
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
if errRemove != nil {
return "", common.WrapMessageToError(errRemove, "BTP Setup Run Failed")
}

return "", common.WrapMessageToError(err, "BTP Setup Run Failed")

}
if cli.Context().CheckSkippedInstructions(skippedInstructions) {
return "", common.WrapMessageToError(common.ErrStarlarkResponse, "Already Running")
}
return executionSerializedData, nil

}

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

params := fmt.Sprintf(`{"src_chain":"%s","dst_chain":"%s", "src_chain_config":%s, "dst_chain_config":%s, "bridge":%s}`, chainA, chainB, srcChainServiceResponse, dstChainServiceResponse, strconv.FormatBool(bridge))
starlarkConfig := common.GetStarlarkRunConfig(params, common.DiveBridgeBtpScript, mainFunctionName)
executionData, _, err := enclaveCtx.RunStarlarkRemotePackage(cli.Context().GetContext(), common.DiveRemotePackagePath, starlarkConfig)

if err != nil {
return "", common.WrapMessageToError(common.ErrStarlarkRunFailed, err.Error())
}
executionSerializedData, services, skippedInstructions, err := common.GetSerializedData(cli, executionData)
if err != nil {
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
if errRemove != nil {
return "", common.WrapMessageToError(errRemove, "BTP Setup Run Failed")
}

return "", common.WrapMessageToError(err, "BTP Setup Run Failed")

}

if cli.Context().CheckSkippedInstructions(skippedInstructions) {
return "", common.WrapMessageToError(common.ErrStarlarkResponse, "Already Running")
}

return executionSerializedData, nil

}
10 changes: 6 additions & 4 deletions cli/cmd/utility/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import (
"github.com/spf13/cobra"
)

var enclaveName = ""

var CleanCmd = common.NewDiveCommandBuilder().
SetUse("clean").
SetShort("Cleans up Kurtosis leftover artifacts").
SetLong("Destroys and removes any running encalves. If no enclaves running to remove it will throw an error").
AddBoolFlagP("all", "a", false, "To Clean All the Service in Enclave").
AddStringFlagWithShortHand(&enclaveName, "enclaveName", "e", common.DiveEnclave, "Please Provide enclave name to clean").
AddStringFlagWithShortHand(&common.EnclaveName, "enclaveName", "e", common.DiveEnclave, "Please Provide enclave name to clean").
SetRun(clean).Build()

func clean(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -57,6 +55,7 @@ func clean(cmd *cobra.Command, args []string) {
}

if cleanAll {
cliContext.StartSpinnerIfNotVerbose("Cleaning All Dive Enclaves", common.DiveLogs)
enclavesInfo, err := cliContext.Context().CleanEnclaves()
if err != nil {
cliContext.Logger().SetErrorToStderr()
Expand All @@ -66,10 +65,13 @@ func clean(cmd *cobra.Command, args []string) {
cliContext.Logger().Info(fmt.Sprintf("Enclaves Cleaned %v", enclavesInfo))

} else {
err = cliContext.Context().CleanEnclaveByName(enclaveName)
cliContext.StartSpinnerIfNotVerbose(fmt.Sprintf("Cleaning Dive By Enclave %s", common.EnclaveName), common.DiveLogs)
err = cliContext.Context().CleanEnclaveByName(common.EnclaveName)
if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}
}

cliContext.StopSpinnerIfNotVerbose("Clean Completed", common.DiveLogs)
}
22 changes: 21 additions & 1 deletion cli/common/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func (c *Cli) Error(err error) {
func (c *Cli) Fatal(err error) {
c.spinner.Stop()
c.log.SetErrorToStderr()
c.log.Fatal(CodeOf(err), err.Error())
actualError, _ := CoderOf(err)
c.log.Fatal(actualError.ErrorCode(), fmt.Sprintf("%s. message: %s", actualError.Error(), err.Error()))
}

func (c *Cli) Info(message string) {
Expand All @@ -128,3 +129,22 @@ func (c *Cli) Debug(message string) {
func (c *Cli) Debugf(format string, args ...interface{}) {
c.log.Debugf(format, args...)
}

func (c *Cli) StartSpinnerIfNotVerbose(message string, verbose bool) {
if verbose {
c.log.SetOutputToStdout()
c.log.Info(message)

} else {
c.spinner.StartWithMessage(message, "green")
}
}

func (c *Cli) StopSpinnerIfNotVerbose(message string, verbose bool) {
if verbose {
c.log.SetOutputToStdout()
c.log.Info(message)
} else {
c.spinner.StopWithMessage(message)
}
}
1 change: 1 addition & 0 deletions cli/common/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

var DiveLogs bool
var EnclaveName string

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

0 comments on commit 72067d6

Please sign in to comment.