Skip to content

Commit

Permalink
fix: Add a migration to update the relayer path (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial authored Sep 3, 2023
1 parent 44d0af7 commit 92e36c3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 52 deletions.
5 changes: 3 additions & 2 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package initconfig

import (
"fmt"
"github.com/dymensionxyz/roller/relayer"
global_utils "github.com/dymensionxyz/roller/utils"
"os"

Expand Down Expand Up @@ -104,13 +105,13 @@ func runInit(cmd *cobra.Command, args []string) error {
/* ---------------------------- Initialize relayer --------------------------- */
rollappPrefix, err := utils.GetAddressPrefix(initConfig.RollappBinary)
utils.PrettifyErrorIfExists(err)
err = initializeRelayerConfig(ChainConfig{
err = initializeRelayerConfig(relayer.ChainConfig{
ID: initConfig.RollappID,
RPC: consts.DefaultRollappRPC,
Denom: initConfig.Denom,
AddressPrefix: rollappPrefix,
GasPrices: "0",
}, ChainConfig{
}, relayer.ChainConfig{
ID: initConfig.HubData.ID,
RPC: initConfig.HubData.RPC_URL,
Denom: consts.Denoms.Hub,
Expand Down
32 changes: 5 additions & 27 deletions cmd/config/init/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package initconfig

import (
"encoding/json"
"github.com/dymensionxyz/roller/relayer"
"os"
"os/exec"
"path/filepath"

"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"

"github.com/dymensionxyz/roller/cmd/consts"
Expand All @@ -31,16 +31,8 @@ type RelayerFileChainConfigValue struct {
ClientType string `json:"client-type"`
}

type ChainConfig struct {
ID string
RPC string
Denom string
AddressPrefix string
GasPrices string
}

type RelayerChainConfig struct {
ChainConfig ChainConfig
ChainConfig relayer.ChainConfig
GasPrices string
ClientType string
KeyName string
Expand Down Expand Up @@ -95,7 +87,7 @@ func initRelayer(relayerHome string) error {
return initRelayerConfigCmd.Run()
}

func addChainsConfig(rollappConfig ChainConfig, hubConfig ChainConfig, relayerHome string) error {
func addChainsConfig(rollappConfig relayer.ChainConfig, hubConfig relayer.ChainConfig, relayerHome string) error {
relayerRollappConfig := getRelayerFileChainConfig(RelayerChainConfig{
ChainConfig: rollappConfig,
GasPrices: rollappConfig.GasPrices + rollappConfig.Denom,
Expand All @@ -119,29 +111,15 @@ func addChainsConfig(rollappConfig ChainConfig, hubConfig ChainConfig, relayerHo
return nil
}

func setupPath(rollappConfig ChainConfig, hubConfig ChainConfig, relayerHome string) error {
setSettlementCmd := exec.Command(consts.Executables.Relayer, "chains", "set-settlement", hubConfig.ID, "--home", relayerHome)
if err := setSettlementCmd.Run(); err != nil {
return err
}
args := []string{"paths", "new", hubConfig.ID, rollappConfig.ID, consts.DefaultRelayerPath}
args = append(args, utils.GetRelayerDefaultFlags(relayerHome)...)
newPathCmd := exec.Command(consts.Executables.Relayer, args...)
if err := newPathCmd.Run(); err != nil {
return err
}
return nil
}

func initializeRelayerConfig(rollappConfig ChainConfig, hubConfig ChainConfig, initConfig config.RollappConfig) error {
func initializeRelayerConfig(rollappConfig relayer.ChainConfig, hubConfig relayer.ChainConfig, initConfig config.RollappConfig) error {
relayerHome := filepath.Join(initConfig.Home, consts.ConfigDirName.Relayer)
if err := initRelayer(relayerHome); err != nil {
return err
}
if err := addChainsConfig(rollappConfig, hubConfig, relayerHome); err != nil {
return err
}
if err := setupPath(rollappConfig, hubConfig, relayerHome); err != nil {
if err := relayer.CreatePath(initConfig); err != nil {
return err
}
return nil
Expand Down
1 change: 1 addition & 0 deletions cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var migrationsRegistry = []VersionMigrator{
&VersionMigratorV018{},
&VersionMigratorV0111{},
&VersionMigratorV0112{},
&VersionMigratorV0113{},
}

func Cmd() *cobra.Command {
Expand Down
19 changes: 19 additions & 0 deletions cmd/migrate/v_0_1_13.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package migrate

import (
"github.com/dymensionxyz/roller/config"
"github.com/dymensionxyz/roller/relayer"
)

type VersionMigratorV0113 struct{}

func (v *VersionMigratorV0113) ShouldMigrate(prevVersion VersionData) bool {
return prevVersion.Major < 1 && prevVersion.Minor < 2 && prevVersion.Patch < 13
}

func (v *VersionMigratorV0113) PerformMigration(rlpCfg config.RollappConfig) error {
if err := relayer.DeletePath(rlpCfg, "hub-rollapp"); err != nil {
return err
}
return relayer.CreatePath(rlpCfg)
}
23 changes: 0 additions & 23 deletions cmd/utils/bash_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"time"

"github.com/dymensionxyz/roller/config"
)

func GetRelayerDefaultFlags(root string) []string {
return []string{
"--src-port", "transfer", "--dst-port", "transfer", "--version", "ics20-1", "--home", root,
}
}

// TODO: should accept a context and cancel the command if the context is cancelled
func RunCommandEvery(ctx context.Context, command string, args []string, intervalSec int, options ...CommandOption) {
go func() {
Expand Down Expand Up @@ -103,22 +96,6 @@ func ExecBashCommandWithStdErr(cmd *exec.Cmd) (bytes.Buffer, error) {
return stderr, nil
}

func ExecBashCmdWithOSOutput(cmd *exec.Cmd, options ...CommandOption) error {
for _, option := range options {
option(cmd)
}
var stderr bytes.Buffer
outmw := io.MultiWriter(cmd.Stdout, os.Stdout)
cmd.Stdout = outmw
errmw := io.MultiWriter(os.Stderr, &stderr, cmd.Stderr)
cmd.Stderr = errmw
err := cmd.Run()
if err != nil {
return fmt.Errorf("command execution failed: %w, stderr: %s", err, stderr.String())
}
return nil
}

func ExecBashCmd(cmd *exec.Cmd, options ...CommandOption) error {
for _, option := range options {
option(cmd)
Expand Down
42 changes: 42 additions & 0 deletions relayer/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package relayer

import (
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"os/exec"
"path/filepath"
)

func CreatePath(rlpCfg config.RollappConfig) error {
relayerHome := filepath.Join(rlpCfg.Home, consts.ConfigDirName.Relayer)
setSettlementCmd := exec.Command(consts.Executables.Relayer, "chains", "set-settlement",
rlpCfg.HubData.ID, "--home", relayerHome)
if err := setSettlementCmd.Run(); err != nil {
return err
}
newPathCmd := exec.Command(consts.Executables.Relayer, "paths", "new", rlpCfg.HubData.ID, rlpCfg.RollappID,
consts.DefaultRelayerPath, "--home", relayerHome)
if err := newPathCmd.Run(); err != nil {
return err
}
return nil
}

func DeletePath(rlpCfg config.RollappConfig, pathName string) error {
deletePathCmd := exec.Command(consts.Executables.Relayer, "paths", "delete", pathName, "--home",
filepath.Join(rlpCfg.Home, consts.ConfigDirName.Relayer))
_, err := utils.ExecBashCommandWithStdout(deletePathCmd)
if err != nil {
return err
}
return nil
}

type ChainConfig struct {
ID string
RPC string
Denom string
AddressPrefix string
GasPrices string
}

0 comments on commit 92e36c3

Please sign in to comment.