generated from kurtosis-tech/package-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update ibc command for starting ibc bridge
- Loading branch information
1 parent
2c57d2c
commit 31290e0
Showing
12 changed files
with
333 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package btp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ibc | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hugobyte/dive-core/cli/common" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
chainA string | ||
chainB string | ||
serviceA string | ||
serviceB string | ||
) | ||
|
||
var IbcRelayCmd = common.NewDiveCommandBuilder(). | ||
SetUse("ibc"). | ||
SetShort("Start connection between Cosmos based chainA and ChainB and initiate communication between them"). | ||
SetLong(`This Command deploy , initialize the contracts and make it ready for ibc. | ||
Along with that setup and starts the ibc relayer to establish communication between chains specified`). | ||
SetRun(ibcRelay). | ||
AddStringFlag(&chainA, "chainA", "", "Mention Name of Supported Chain"). | ||
AddStringFlag(&chainB, "chainB", "", "Mention Name of Supported Chain"). | ||
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 ibcRelay(cmd *cobra.Command, args []string) { | ||
|
||
cliContext := common.GetCliWithKurtosisContext() | ||
|
||
err := common.ValidateArgs(args) | ||
|
||
if err != nil { | ||
cliContext.Fatalf("Error %s. %s", err, cmd.UsageString()) | ||
} | ||
cliContext.Spinner().StartWithMessage("Starting IBC Setup", "green") | ||
result, err := RunIbcRelay(cliContext) | ||
if err != nil { | ||
cliContext.Fatal(err) | ||
} | ||
|
||
err = cliContext.FileHandler().WriteFile("dive.json", []byte(result)) | ||
if err != nil { | ||
cliContext.Fatal(err) | ||
} | ||
|
||
cliContext.Spinner().StopWithMessage(fmt.Sprintf("IBC Setup Completed between %s and %s. Please find service details in current working directory(dive.json)", chainA, chainB)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package ibc | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hugobyte/dive-core/cli/cmd/bridge/utils" | ||
"github.com/hugobyte/dive-core/cli/common" | ||
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves" | ||
) | ||
|
||
func RunIbcRelay(cli *common.Cli) (string, error) { | ||
var starlarkExecutionResponse string | ||
chains := utils.InitChains(chainA, chainB, serviceA, serviceB, false) | ||
|
||
err := chains.CheckForIbcSupportedChains() | ||
|
||
if err != nil { | ||
|
||
return "", common.WrapMessageToError(common.ErrInvalidChain, err.Error()) | ||
} | ||
|
||
enclaveContext, err := cli.Context().GetEnclaveContext(common.DiveEnclave) | ||
if err != nil { | ||
return "", common.WrapMessageToError(err, "IBC Setup Failed") | ||
} | ||
|
||
if chains.CheckChainServiceNamesEmpty() { | ||
srcChainServiceResponse, dstChainServiceResponse, err := chains.GetServicesResponse(cli) | ||
if err != nil { | ||
return "", err | ||
} | ||
starlarkExecutionResponse, err = setupIbcRelayforAlreadyRunningCosmosChain(cli, enclaveContext, chains.ChainA, chains.ChainB, srcChainServiceResponse, dstChainServiceResponse) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
} else { | ||
starlarkExecutionResponse, err = startCosmosChainsAndSetupIbcRelay(cli, enclaveContext, chains) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
if chainA == "icon" { | ||
_, err := startIbcRelayIconToCosmos(cli, enclaveContext, common.RelayServiceNameIconToCosmos) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
return starlarkExecutionResponse, nil | ||
} | ||
|
||
func startIbcRelayIconToCosmos(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, serviceName string) (string, error) { | ||
params := fmt.Sprintf(`{"service_name": "%s"}`, serviceName) | ||
starlarkConfig := common.GetStarlarkRunConfig(params, "services/bridges/ibc/src/bridge.star", "start_relay") | ||
executionData, _, err := enclaveContext.RunStarlarkRemotePackage(cli.Context().GetContext(), common.DiveRemotePackagePath, starlarkConfig) | ||
|
||
if err != nil { | ||
return "", common.WrapMessageToError(common.ErrStarlarkRunFailed, err.Error()) | ||
} | ||
|
||
executionSerializedData, services, _, err := common.GetSerializedData(cli, executionData) | ||
|
||
if err != nil { | ||
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave) | ||
if errRemove != nil { | ||
return "", common.WrapMessageToError(errRemove, "IBC Setup Run Failed") | ||
} | ||
|
||
return "", common.WrapMessageToError(err, "IBC Setup Run Failed") | ||
|
||
} | ||
|
||
return executionSerializedData, nil | ||
} | ||
|
||
func startCosmosChainsAndSetupIbcRelay(cli *common.Cli, enclaveCtx *enclaves.EnclaveContext, chains *utils.Chains) (string, error) { | ||
|
||
params := chains.GetIbcRelayParams() | ||
|
||
executionResult, err := runStarlarkPackage(cli, enclaveCtx, params, "run_cosmos_ibc_setup") | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return executionResult, nil | ||
} | ||
|
||
func setupIbcRelayforAlreadyRunningCosmosChain(cli *common.Cli, enclaveCtx *enclaves.EnclaveContext, chainA, chainB, chainAServiceResponse, chainBServiceResponse string) (string, error) { | ||
|
||
params := fmt.Sprintf(`{"src_chain":"%s","dst_chain":"%s", "src_chain_config":%s, "dst_chain_config":%s}`, chainA, chainB, chainAServiceResponse, chainBServiceResponse) | ||
|
||
executionResult, err := runStarlarkPackage(cli, enclaveCtx, params, "run_cosmos_ibc_relay_for_already_running_chains") | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return executionResult, nil | ||
} | ||
|
||
func runStarlarkPackage(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, params, functionName string) (string, error) { | ||
starlarkConfig := common.GetStarlarkRunConfig(params, common.DiveBridgeIbcScript, functionName) | ||
executionData, _, err := enclaveContext.RunStarlarkRemotePackage(cli.Context().GetContext(), common.DiveRemotePackagePath, starlarkConfig) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
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, "IBC Setup Run Failed") | ||
} | ||
|
||
return "", common.WrapMessageToError(err, "IBC Setup Run Failed") | ||
|
||
} | ||
|
||
if cli.Context().CheckSkippedInstructions(skippedInstructions) { | ||
return "", common.WrapMessageToError(common.ErrStarlarkResponse, "Already Running") | ||
} | ||
|
||
return executionSerializedData, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/hugobyte/dive-core/cli/common" | ||
) | ||
|
||
const ( | ||
IconChain = "icon" | ||
EthChain = "eth" | ||
HardhatChain = "hardhat" | ||
ArchwayChain = "archway" | ||
NeutronChain = "neutron" | ||
) | ||
|
||
var supportedChainsForBtp = []string{IconChain, EthChain, HardhatChain} | ||
var supportedChainsForIbc = []string{ArchwayChain, NeutronChain, IconChain} | ||
|
||
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 (chains *Chains) GetParams() string { | ||
return fmt.Sprintf(`{"src_chain": "%s", "dst_chain": "%s", "bridge":"%s"}`, chains.ChainA, chains.ChainB, chains.Bridge) | ||
} | ||
func (chains *Chains) GetIbcRelayParams() string { | ||
|
||
return fmt.Sprintf(`{"src_chain": "%s", "dst_chain": "%s"}`, chains.ChainA, chains.ChainB) | ||
} | ||
|
||
func (chains *Chains) GetServicesResponse(cli *common.Cli) (string, string, error) { | ||
|
||
var serviceConfig = common.Services{} | ||
|
||
err := cli.FileHandler().ReadJson("services.json", &serviceConfig) | ||
|
||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
chainAServiceResponse, OK := serviceConfig[chains.ChainAServiceName] | ||
if !OK { | ||
return "", "", fmt.Errorf("service name not found") | ||
} | ||
chainBServiceResponse, OK := serviceConfig[chains.ChainBServiceName] | ||
if !OK { | ||
return "", "", fmt.Errorf("service name not found") | ||
} | ||
|
||
srcChainServiceResponse, err := chainAServiceResponse.EncodeToString() | ||
if err != nil { | ||
return "", "", err | ||
} | ||
dstChainServiceResponse, err := chainBServiceResponse.EncodeToString() | ||
|
||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
return srcChainServiceResponse, dstChainServiceResponse, nil | ||
} | ||
|
||
func (chains *Chains) CheckForBtpSupportedChains() error { | ||
if !slices.Contains(supportedChainsForBtp, chains.ChainA) { | ||
return fmt.Errorf("invalid Chain: %s", chains.ChainA) | ||
} | ||
if !slices.Contains(supportedChainsForBtp, chains.ChainB) { | ||
return fmt.Errorf("invalid Chain: %s", chains.ChainB) | ||
} | ||
return nil | ||
} | ||
|
||
func (chains *Chains) CheckForIbcSupportedChains() error { | ||
if !slices.Contains(supportedChainsForIbc, chains.ChainA) { | ||
return fmt.Errorf("invalid Chain: %s", chains.ChainA) | ||
} | ||
if !slices.Contains(supportedChainsForIbc, chains.ChainB) { | ||
return fmt.Errorf("invalid Chain: %s", chains.ChainB) | ||
} | ||
return nil | ||
} | ||
|
||
func (chains *Chains) CheckChainServiceNamesEmpty() bool { | ||
return (chains.ChainAServiceName != "" && chains.ChainBServiceName != "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,4 +76,5 @@ const ( | |
InvalidPathError | ||
InvalidFileError | ||
KurtosisServiceError | ||
InvalidChain | ||
) |
Oops, something went wrong.