From 55afb56dca1f3a94ddff8d6e1f584cac11afc3b7 Mon Sep 17 00:00:00 2001 From: artpav <19916123+artemijspavlovs@users.noreply.github.com> Date: Tue, 15 Oct 2024 08:37:31 +0300 Subject: [PATCH] fix: relayer setup flow for new and existing ibc channels (#1072) --- cmd/relayer/setup/setup.go | 69 ++++++++++++------------- cmd/rollapp/init/init.go | 82 ++++++++++++++++++++++++------ cmd/rollapp/setup/setup.go | 2 +- cmd/services/load/load.go | 1 + cmd/services/restart/restart.go | 33 ++++++------ cmd/services/start/start.go | 26 +++++----- relayer/channels.go | 18 ++++++- relayer/connections.go | 3 ++ utils/dependencies/dependencies.go | 2 +- utils/keys/balances.go | 2 +- utils/migrations/migrations.go | 23 ++++++++- utils/rollapp/bechprefix.go | 19 ++++++- utils/rollapp/rollappId.go | 2 +- 13 files changed, 194 insertions(+), 88 deletions(-) diff --git a/cmd/relayer/setup/setup.go b/cmd/relayer/setup/setup.go index 546c9c46..d4c8c098 100644 --- a/cmd/relayer/setup/setup.go +++ b/cmd/relayer/setup/setup.go @@ -151,17 +151,7 @@ func Cmd() *cobra.Command { var shouldOverwrite bool if isRelayerInitialized { - msg := fmt.Sprintf( - "Directory %s is not empty. Do you want to overwrite it?", - relayerHome, - ) - shouldOverwrite, err = pterm.DefaultInteractiveConfirm.WithDefaultText(msg). - WithDefaultValue(false). - Show() - if err != nil { - pterm.Error.Printf("failed to get your input: %v\n", err) - return - } + pterm.Info.Println("relayer already initialized") } else { err = os.MkdirAll(relayerHome, 0o755) if err != nil { @@ -170,30 +160,30 @@ func Cmd() *cobra.Command { } } - if shouldOverwrite { - pterm.Info.Println("overriding the existing relayer configuration") - err = os.RemoveAll(relayerHome) - if err != nil { - pterm.Error.Printf( - "failed to recuresively remove %s: %v\n", - relayerHome, - err, - ) - return - } - - err := filesystem.RemoveServiceFiles(consts.RelayerSystemdServices) - if err != nil { - pterm.Error.Printf("failed to remove relayer systemd services: %v\n", err) - return - } - - err = os.MkdirAll(relayerHome, 0o755) - if err != nil { - pterm.Error.Printf("failed to create %s: %v\n", relayerHome, err) - return - } - } + // if shouldOverwrite { + // pterm.Info.Println("overriding the existing relayer configuration") + // err = os.RemoveAll(relayerHome) + // if err != nil { + // pterm.Error.Printf( + // "failed to recuresively remove %s: %v\n", + // relayerHome, + // err, + // ) + // return + // } + // + // err := filesystem.RemoveServiceFiles(consts.RelayerSystemdServices) + // if err != nil { + // pterm.Error.Printf("failed to remove relayer systemd services: %v\n", err) + // return + // } + // + // err = os.MkdirAll(relayerHome, 0o755) + // if err != nil { + // pterm.Error.Printf("failed to create %s: %v\n", relayerHome, err) + // return + // } + // } srcIbcChannel, dstIbcChannel, err := rly.LoadActiveChannel(raData, hd) if err != nil { @@ -555,6 +545,13 @@ func Cmd() *cobra.Command { return } + if srcIbcChannel != "" && dstIbcChannel != "" { + pterm.Info.Println("existing IBC channels found ") + pterm.Info.Println("Hub: ", srcIbcChannel) + pterm.Info.Println("RollApp: ", dstIbcChannel) + return + } + // TODO: remove code duplication _, err = os.Stat(relayerHome) if err != nil { @@ -564,8 +561,6 @@ func Cmd() *cobra.Command { } } - fmt.Println("rollapp chain data denom: ", rollappChainData.Denom) - pterm.Info.Println("initializing relayer config") err = initconfig.InitializeRelayerConfig( relayer.ChainConfig{ diff --git a/cmd/rollapp/init/init.go b/cmd/rollapp/init/init.go index ff3f1dbd..00104fd4 100644 --- a/cmd/rollapp/init/init.go +++ b/cmd/rollapp/init/init.go @@ -58,9 +58,25 @@ func Cmd() *cobra.Command { return } + var hd consts.HubData + var env string + var raID string + + if shouldUseMockBackend { + env = "mock" + } + + if !isMockFlagSet && !shouldUseMockBackend { + envs := []string{"mock", "playground", "custom"} + env, _ = pterm.DefaultInteractiveSelect. + WithDefaultText("select the environment you want to initialize for"). + WithOptions(envs). + Show() + } + // TODO: move to consts // TODO(v2): move to roller config - if !shouldUseMockBackend { + if !shouldUseMockBackend && env != "custom" { dymdBinaryOptions := types.Dependency{ DependencyName: "dymension", RepositoryOwner: "dymensionxyz", @@ -84,22 +100,55 @@ func Cmd() *cobra.Command { } } - var hd consts.HubData - var env string - var raID string + if env != "custom" { + hd = consts.Hubs[env] + } else { + id, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("provide hub chain id").Show() + rpcUrl, _ := pterm.DefaultInteractiveTextInput.WithDefaultText( + "provide hub rpc endpoint (including port, example: http://dym.dev:26657)", + ).Show() + restUrl, _ := pterm.DefaultInteractiveTextInput.WithDefaultText( + "provide hub rest api endpoint (including port, example: http://dym.dev:1318)", + ).Show() + gasPrice, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("provide gas price"). + WithDefaultValue("2000000000").Show() - if shouldUseMockBackend { - env = "mock" - } + hd = consts.HubData{ + API_URL: restUrl, + ID: id, + RPC_URL: rpcUrl, + ARCHIVE_RPC_URL: rpcUrl, + GAS_PRICE: gasPrice, + } - if !isMockFlagSet && !shouldUseMockBackend { - envs := []string{"mock", "playground"} - env, _ = pterm.DefaultInteractiveSelect. - WithDefaultText("select the environment you want to initialize for"). - WithOptions(envs). - Show() + dymdCommit, _ := pterm.DefaultInteractiveTextInput.WithDefaultText( + "provide dymensionxyz/dymension commit to build (example: 2cd612aaa6c21b473dbbb7dca9fd03b5aaae6583)", + ).Show() + + dymdDep := types.Dependency{ + DependencyName: "dymension", + RepositoryOwner: "dymensionxyz", + RepositoryName: "dymension", + RepositoryUrl: "https://github.com/dymensionxyz/dymension.git", + Release: dymdCommit, + Binaries: []types.BinaryPathPair{ + { + Binary: "./build/dymd", + BinaryDestination: consts.Executables.Dymension, + BuildCommand: exec.Command( + "make", + "build", + ), + }, + }, + PersistFiles: []types.PersistFile{}, + } + + err := dependencies.InstallBinaryFromRepo(dymdDep, dymdDep.DependencyName) + if err != nil { + return + } } - hd = consts.Hubs[env] if len(args) != 0 { raID = args[0] @@ -128,7 +177,8 @@ func Cmd() *cobra.Command { VmType: vmtype, }, } - _, _, err = dependencies.InstallBinaries(home, true, raRespMock) + + _, _, err = dependencies.InstallBinaries(true, raRespMock) if err != nil { pterm.Error.Println("failed to install binaries: ", err) return @@ -173,7 +223,7 @@ func Cmd() *cobra.Command { } start := time.Now() - builtDeps, _, err := dependencies.InstallBinaries(home, false, raResponse) + builtDeps, _, err := dependencies.InstallBinaries(false, raResponse) if err != nil { pterm.Error.Println("failed to install binaries: ", err) return diff --git a/cmd/rollapp/setup/setup.go b/cmd/rollapp/setup/setup.go index c93c4d26..35ae4817 100644 --- a/cmd/rollapp/setup/setup.go +++ b/cmd/rollapp/setup/setup.go @@ -630,7 +630,7 @@ func Cmd() *cobra.Command { err = keys.PrintInsufficientBalancesIfAny(insufficientBalances) if err != nil { - pterm.Error.Println("failed to check insufficien balances: ", err) + pterm.Error.Println("failed to check insufficient balances: ", err) return } diff --git a/cmd/services/load/load.go b/cmd/services/load/load.go index eb4dc704..6fa239a6 100644 --- a/cmd/services/load/load.go +++ b/cmd/services/load/load.go @@ -285,6 +285,7 @@ Description=Roller {{.Name}} service After=network.target [Service] +Environment="PATH=/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ExecStart={{.ExecPath}} {{.Name}} start Restart=on-failure RestartSec=10 diff --git a/cmd/services/restart/restart.go b/cmd/services/restart/restart.go index 4fde6c07..b56c4e4b 100644 --- a/cmd/services/restart/restart.go +++ b/cmd/services/restart/restart.go @@ -7,16 +7,17 @@ import ( "slices" "strings" + "github.com/pterm/pterm" + "github.com/spf13/cobra" + initconfig "github.com/dymensionxyz/roller/cmd/config/init" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/utils/errorhandling" "github.com/dymensionxyz/roller/utils/filesystem" "github.com/dymensionxyz/roller/utils/migrations" "github.com/dymensionxyz/roller/utils/roller" - "github.com/dymensionxyz/roller/utils/upgrades" - "github.com/pterm/pterm" - "github.com/spf13/cobra" - servicemanager "github.com/dymensionxyz/roller/utils/service_manager" + "github.com/dymensionxyz/roller/utils/upgrades" ) func Cmd(services []string) *cobra.Command { @@ -47,18 +48,20 @@ func RestartSystemdServices(services []string, home string) error { rollappConfig, err := roller.LoadConfig(home) errorhandling.PrettifyErrorIfExists(err) - raUpgrade, err := upgrades.NewRollappUpgrade(string(rollappConfig.RollappVMType)) - if err != nil { - pterm.Error.Println("failed to check rollapp version equality: ", err) - } + if rollappConfig.HubData.ID != consts.MockHubID { + raUpgrade, err := upgrades.NewRollappUpgrade(string(rollappConfig.RollappVMType)) + if err != nil { + pterm.Error.Println("failed to check rollapp version equality: ", err) + } - err = migrations.RequireRollappMigrateIfNeeded( - raUpgrade.CurrentVersionCommit, - rollappConfig.RollappBinaryVersion, - string(rollappConfig.RollappVMType), - ) - if err != nil { - return err + err = migrations.RequireRollappMigrateIfNeeded( + raUpgrade.CurrentVersionCommit, + rollappConfig.RollappBinaryVersion, + string(rollappConfig.RollappVMType), + ) + if err != nil { + return err + } } } diff --git a/cmd/services/start/start.go b/cmd/services/start/start.go index a61e7760..87ccbde2 100644 --- a/cmd/services/start/start.go +++ b/cmd/services/start/start.go @@ -34,19 +34,21 @@ func RollappCmd() *cobra.Command { rollappConfig, err := roller.LoadConfig(home) errorhandling.PrettifyErrorIfExists(err) - raUpgrade, err := upgrades.NewRollappUpgrade(string(rollappConfig.RollappVMType)) - if err != nil { - pterm.Error.Println("failed to check rollapp version equality: ", err) - } + if rollappConfig.HubData.ID != consts.MockHubID { + raUpgrade, err := upgrades.NewRollappUpgrade(string(rollappConfig.RollappVMType)) + if err != nil { + pterm.Error.Println("failed to check rollapp version equality: ", err) + } - err = migrations.RequireRollappMigrateIfNeeded( - raUpgrade.CurrentVersionCommit, - rollappConfig.RollappBinaryVersion, - string(rollappConfig.RollappVMType), - ) - if err != nil { - pterm.Error.Println(err) - return + err = migrations.RequireRollappMigrateIfNeeded( + raUpgrade.CurrentVersionCommit, + rollappConfig.RollappBinaryVersion, + string(rollappConfig.RollappVMType), + ) + if err != nil { + pterm.Error.Println(err) + return + } } if runtime.GOOS == "darwin" { diff --git a/relayer/channels.go b/relayer/channels.go index b8fa8085..840563fa 100644 --- a/relayer/channels.go +++ b/relayer/channels.go @@ -37,6 +37,9 @@ func (r *Relayer) LoadActiveChannel( return "", "", nil } + pterm.Info.Println("active connection found on the hub side: ", activeHubConnectionID) + pterm.Info.Println("active connection found on the rollapp side: ", activeRaConnectionID) + var raChannelResponse QueryChannelsResponse rollappChannels, err := bash.ExecCommandWithStdout(r.queryChannelsRollappCmd(raData)) if err != nil { @@ -83,6 +86,9 @@ func (r *Relayer) LoadActiveChannel( ) hubChan := hubChannelResponse.Channels[hubChanIndex] + pterm.Info.Println("active channel found on the hub side: ", hubChan.ChannelID) + pterm.Info.Println("active channel found on the rollapp side: ", raChan.ChannelID) + spinner.Success("IBC channels loaded successfully") r.SrcChannel = hubChan.ChannelID @@ -101,7 +107,17 @@ func (r *Relayer) queryChannelsRollappCmd(raData consts.RollappData) *exec.Cmd { func (r *Relayer) queryChannelsHubCmd(hd consts.HubData) *exec.Cmd { args := []string{"q", "ibc", "channel", "channels"} - args = append(args, "--node", hd.RPC_URL, "--chain-id", hd.ID, "-o", "json") + args = append( + args, + "--node", + hd.RPC_URL, + "--chain-id", + hd.ID, + "-o", + "json", + "--limit", + "100000", + ) cmd := exec.Command(consts.Executables.Dymension, args...) diff --git a/relayer/connections.go b/relayer/connections.go index 2580a758..e0b3e191 100644 --- a/relayer/connections.go +++ b/relayer/connections.go @@ -207,6 +207,7 @@ func (r *Relayer) queryConnectionRollappCmd( return cmd } +// TODO: refactor the limit func (r *Relayer) queryConnectionHubCmd(hd consts.HubData) *exec.Cmd { args := []string{ "q", @@ -219,6 +220,8 @@ func (r *Relayer) queryConnectionHubCmd(hd consts.HubData) *exec.Cmd { hd.RPC_URL, "-o", "json", + "--limit", + "100000", } return exec.Command(consts.Executables.Dymension, args...) diff --git a/utils/dependencies/dependencies.go b/utils/dependencies/dependencies.go index 7ccb7dc0..765f0466 100644 --- a/utils/dependencies/dependencies.go +++ b/utils/dependencies/dependencies.go @@ -24,7 +24,7 @@ import ( "github.com/dymensionxyz/roller/utils/rollapp" ) -func InstallBinaries(home string, withMockDA bool, raResp rollapp.ShowRollappResponse) ( +func InstallBinaries(withMockDA bool, raResp rollapp.ShowRollappResponse) ( map[string]types.Dependency, map[string]types.Dependency, error, diff --git a/utils/keys/balances.go b/utils/keys/balances.go index d4b12d2b..b0fe8e0a 100644 --- a/utils/keys/balances.go +++ b/utils/keys/balances.go @@ -22,7 +22,7 @@ func PrintInsufficientBalancesIfAny( addressesData []NotFundedAddressData, ) error { if len(addressesData) == 0 { - return errors.New("no addresses to print") + return nil } printAddresses := func() { diff --git a/utils/migrations/migrations.go b/utils/migrations/migrations.go index 2f595f08..25f07f1e 100644 --- a/utils/migrations/migrations.go +++ b/utils/migrations/migrations.go @@ -11,6 +11,7 @@ import ( "github.com/pterm/pterm" "github.com/dymensionxyz/roller/cmd/consts" + "github.com/dymensionxyz/roller/utils/rollapp" "github.com/dymensionxyz/roller/utils/roller" "github.com/dymensionxyz/roller/version" ) @@ -155,6 +156,16 @@ func GetCommitFromTag(owner, repo, tag string) (string, error) { // nolint: errcheck defer resp.Body.Close() + if resp.StatusCode == http.StatusNotFound { + pterm.Warning.Println("tag not found, extracting commit from build flags ", tag) + commit, err := rollapp.ExtractCommitFromBinary() + if err != nil { + return "", err + } + + return commit, nil + } + if resp.StatusCode != http.StatusOK { return "", fmt.Errorf("failed to fetch tag data: %s", resp.Status) } @@ -178,11 +189,21 @@ func GetCommitTimestampByTag(owner, repo, tag string) (time.Time, error) { // nolint: errcheck defer resp.Body.Close() + if resp.StatusCode == http.StatusNotFound { + pterm.Warning.Println("tag not found, extracting commit from build flags ", tag) + commit, err := rollapp.ExtractCommitFromBinary() + if err != nil { + return time.Time{}, err + } + + return GetCommitTimestamp(owner, repo, commit) + } + if resp.StatusCode != http.StatusOK { return time.Time{}, fmt.Errorf("failed to fetch tag data: %s", resp.Status) } - var tagData Tag + if err := json.NewDecoder(resp.Body).Decode(&tagData); err != nil { return time.Time{}, err } diff --git a/utils/rollapp/bechprefix.go b/utils/rollapp/bechprefix.go index 0c23be68..4e909459 100644 --- a/utils/rollapp/bechprefix.go +++ b/utils/rollapp/bechprefix.go @@ -2,6 +2,7 @@ package rollapp import ( "errors" + "fmt" "os/exec" "regexp" "strings" @@ -11,7 +12,14 @@ import ( ) func ExtractBech32PrefixFromBinary(vmType string) (string, error) { - c := exec.Command("go", "version", "-m", consts.Executables.RollappEVM) + c := exec.Command( + "go", + "version", + "-m", + consts.Executables.RollappEVM, + ) + fmt.Println(c.String()) + out, err := bash.ExecCommandWithStdout(c) if err != nil { return "", err @@ -48,7 +56,14 @@ func ExtractBech32PrefixFromBinary(vmType string) (string, error) { } func ExtractCommitFromBinary() (string, error) { - c := exec.Command("go", "version", "-m", consts.Executables.RollappEVM) + c := exec.Command( + "go", + "version", + "-m", + consts.Executables.RollappEVM, + ) + fmt.Println(c.String()) + out, err := bash.ExecCommandWithStdout(c) if err != nil { return "", err diff --git a/utils/rollapp/rollappId.go b/utils/rollapp/rollappId.go index c7572749..b6a8e2f4 100644 --- a/utils/rollapp/rollappId.go +++ b/utils/rollapp/rollappId.go @@ -87,7 +87,7 @@ func ValidateChainID(id string) (ChainID, error) { ) } - pterm.Success.Printf("'%s' is a valid RollApp ID", id) + pterm.Success.Printf("'%s' is a valid RollApp ID\n", id) return ChainID{ chainID: chainID, eip155ID: chainIDInt,