Skip to content

Commit

Permalink
fix: fix bridge issue between same chains
Browse files Browse the repository at this point in the history
  • Loading branch information
riyasng12 committed Dec 18, 2023
1 parent 46a89a7 commit 39e67cc
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 30 deletions.
78 changes: 62 additions & 16 deletions cli/cmd/bridge/btp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strconv"

chainutil "github.com/hugobyte/dive-core/cli/cmd/chains/utils"

"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"
Expand Down Expand Up @@ -194,42 +196,86 @@ func runBtpSetupWhenSingleChainRunning(cli *common.Cli, enclaveContext *enclaves
}

func runBtpSetupByRunningNodes(cli *common.Cli, enclaveCtx *enclaves.EnclaveContext, chains *utils.Chains, bridge bool) (string, error) {
var chainAServiceResponse, chainBServiceResponse string
var err error

var runFunction string
cidChainA := "0xacbc4e"
cidChainB := "0x42f1f3"

srcResponseData, err := runChain[chains.ChainA](cli)
chainAServiceResponse, err = getServiceConfigForIconChain(cidChainA)
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed Due To ChainA %s Run Failed ", chains.ChainA))
return "", err
}

chainAServiceResponse, err := srcResponseData.EncodeToString()
if err != nil {
return "", err
if chains.AreChainsIcon() {
chainBServiceResponse, err = getServiceConfigForIconChain(cidChainB)
if err != nil {
return "", err
}
} else if chains.ChainA == "hardhat" || chains.ChainB == "hardhat" {
chainBServiceResponse, err = getServiceConfigForHardhatChain()
if err != nil {
return "", err
}
} else if chains.ChainA == "eth" || chains.ChainB == "eth" {
chainBServiceResponse = "{}"
}

dstResponseData, err := runChain[chains.ChainB](cli)
params := chains.GetParams(chainAServiceResponse, chainBServiceResponse)
starlarkConfig := common.GetStarlarkRunConfig(params, common.DiveBridgeBtpScript, bridgeMainFunction)
executionData, _, err := enclaveCtx.RunStarlarkRemotePackage(cli.Context().GetContext(), common.DiveRemotePackagePath, starlarkConfig)

if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed Due To ChainB %s Run Failed ", chains.ChainB))
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")

chainBServiceResponse, err := dstResponseData.EncodeToString()
}
if cli.Context().CheckSkippedInstructions(skippedInstructions) {
return "", common.WrapMessageToError(common.ErrStarlarkResponse, "Already Running")
}
return executionSerializedData, nil
}

func getServiceConfigForIconChain(cid string) (string, error) {
var serviceConfigChain = &chainutil.IconServiceConfig{}

err := serviceConfigChain.LoadDefaultConfig()
if err != nil {
return "", err
}

if chains.AreChainsIcon() {
runFunction = runBridgeIcon2icon
} else {
runFunction = runBridgeIcon2EthHardhat
serviceConfigChain.Cid = cid

chainServiceResponse, err := serviceConfigChain.EncodeToString()
if err != nil {
return "", common.WrapMessageToError(common.ErrDataMarshall, err.Error())
}

starlarkExecutionResponse, err := runBtpSetupForAlreadyRunningNodes(cli, enclaveCtx, runFunction, chains.ChainA, chains.ChainB, bridge, chainAServiceResponse, chainBServiceResponse)
return chainServiceResponse, nil
}

func getServiceConfigForHardhatChain() (string, error) {
var serviceConfigChain = &chainutil.HardhatServiceConfig{}

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

return starlarkExecutionResponse, nil
chainServiceResponse, err := serviceConfigChain.EncodeToString()
if err != nil {
return "", common.WrapMessageToError(common.ErrDataMarshall, err.Error())
}

return chainServiceResponse, nil
}

func runBtpSetupForAlreadyRunningNodes(cli *common.Cli, enclaveCtx *enclaves.EnclaveContext, mainFunctionName string, srcChain string, dstChain string, bridge bool, srcChainServiceResponse string, dstChainServiceResponse string) (string, error) {
Expand Down
59 changes: 49 additions & 10 deletions cli/cmd/bridge/ibc/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ibc
import (
"fmt"

chainutil "github.com/hugobyte/dive-core/cli/cmd/chains/utils"

"github.com/hugobyte/dive-core/cli/cmd/bridge/utils"
"github.com/hugobyte/dive-core/cli/cmd/chains/archway"
"github.com/hugobyte/dive-core/cli/cmd/chains/icon"
Expand All @@ -20,7 +22,6 @@ var runChain = map[string]func(cli *common.Cli) (*common.DiveServiceResponse, er

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

return neutron.RunNeutron(cli)
},
}
Expand Down Expand Up @@ -94,33 +95,71 @@ func startIbcRelayIconToCosmos(cli *common.Cli, enclaveContext *enclaves.Enclave
}

func startCosmosChainsAndSetupIbcRelay(cli *common.Cli, enclaveCtx *enclaves.EnclaveContext, chains *utils.Chains) (string, error) {
var chainAServiceResponse, chainBServiceResponse string
var err error
if chains.AreChainsCosmos() {
chainAServiceResponse, err = getServiceConfigForCosmosChains(chains.ChainA)
if err != nil {
return "", err
}

srcChainServiceResponse, err := runChain[chains.ChainA](cli)
chainBServiceResponse, err = getServiceConfigForCosmosChains(chains.ChainB)
if err != nil {
return "", err
}

} else {
chainAServiceResponse, err = getServiceConfigForIconChain()
if err != nil {
return "", err
}

chainBServiceResponse, err = getServiceConfigForCosmosChains(chains.ChainB)
if err != nil {
return "", err
}

}

params := chains.GetIbcRelayParams(chainAServiceResponse, chainBServiceResponse)
executionResult, err := runStarlarkPackage(cli, enclaveCtx, params, "run_cosmos_ibc_setup")
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("IBC Setup Failed Due To ChainA %s Run Failed ", chains.ChainA))
return "", common.WrapMessageToErrorf(common.ErrStarlarkRunFailed, "%s. %s", err, "IBC Run Failed")
}

srcChainServiceResponseString, err := srcChainServiceResponse.EncodeToString()
return executionResult, nil
}

func getServiceConfigForCosmosChains(chain string) (string, error) {
var serviceConfigChain = &chainutil.CosmosServiceConfig{}
serviceConfigChain.ChainName = &chain
err := serviceConfigChain.LoadDefaultConfig()
if err != nil {
return "", err
}

dstChainServiceResponse, err := runChain[chains.ChainB](cli)
chainServiceResponse, err := serviceConfigChain.EncodeToString()
if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("IBC Setup Failed Due To ChainB %s Run Failed ", chains.ChainB))
return "", common.WrapMessageToError(common.ErrDataMarshall, err.Error())
}

dstChainServiceResponseString, err := dstChainServiceResponse.EncodeToString()
return chainServiceResponse, nil
}

func getServiceConfigForIconChain() (string, error) {
var serviceConfigChain = &chainutil.IconServiceConfig{}

err := serviceConfigChain.LoadDefaultConfig()
if err != nil {
return "", err
}

starlarkExecutionResponse, err := setupIbcRelayforAlreadyRunningCosmosChain(cli, enclaveCtx, chains.ChainA, chains.ChainB, srcChainServiceResponseString, dstChainServiceResponseString)
chainServiceResponse, err := serviceConfigChain.EncodeToString()
if err != nil {
return "", err
return "", common.WrapMessageToError(common.ErrDataMarshall, err.Error())
}

return starlarkExecutionResponse, nil
return chainServiceResponse, nil
}

func setupIbcRelayforAlreadyRunningCosmosChain(cli *common.Cli, enclaveCtx *enclaves.EnclaveContext, chainA, chainB, chainAServiceResponse, chainBServiceResponse string) (string, error) {
Expand Down
12 changes: 8 additions & 4 deletions cli/cmd/bridge/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ func (c *Chains) AreChainsIcon() bool {
return (c.ChainA == "icon" && c.ChainB == "icon")
}

func (chains *Chains) GetParams() string {
return fmt.Sprintf(`{"src_chain": "%s", "dst_chain": "%s", "bridge":"%s"}`, chains.ChainA, chains.ChainB, chains.Bridge)
func (c *Chains) AreChainsCosmos() bool {
return (c.ChainA == "archway" || c.ChainA == "neutron") && (c.ChainB == "archway" || c.ChainB == "neutron")
}
func (chains *Chains) GetIbcRelayParams() string {

return fmt.Sprintf(`{"src_chain": "%s", "dst_chain": "%s"}`, chains.ChainA, chains.ChainB)
func (chains *Chains) GetParams(src_service_config string, dst_service_config string) string {
return fmt.Sprintf(`{"src_chain": "%s", "dst_chain": "%s", "bridge":"%s", "src_service_config": %s, "dst_service_config": %s}`, chains.ChainA, chains.ChainB, chains.Bridge, src_service_config, dst_service_config)
}

func (chains *Chains) GetIbcRelayParams(src_service_config string, dst_service_config string) string {
return fmt.Sprintf(`{"src_chain": "%s", "dst_chain": "%s", "src_service_config": %s, "dst_service_config": %s}`, chains.ChainA, chains.ChainB, src_service_config, dst_service_config)
}

func (chains *Chains) GetServicesResponse(cli *common.Cli) (string, string, error) {
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/chains/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (cs *CosmosServiceConfig) LoadDefaultConfig() error {
cs.ChainID = nil
cs.Key = nil
cs.Password = nil
cs.ChainName = nil
publicGrpc, err := common.GetAvailablePort()
if err != nil {
return common.WrapMessageToError(err, "error getting available gRPC port")
Expand Down

0 comments on commit 39e67cc

Please sign in to comment.