Skip to content

Commit

Permalink
fix: relayer setup flow for new and existing ibc channels (#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Oct 15, 2024
1 parent abf0d7e commit 55afb56
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 88 deletions.
69 changes: 32 additions & 37 deletions cmd/relayer/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
82 changes: 66 additions & 16 deletions cmd/rollapp/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/rollapp/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions cmd/services/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 18 additions & 15 deletions cmd/services/restart/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
}

Expand Down
26 changes: 14 additions & 12 deletions cmd/services/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
18 changes: 17 additions & 1 deletion relayer/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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...)

Expand Down
3 changes: 3 additions & 0 deletions relayer/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion utils/dependencies/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion utils/keys/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func PrintInsufficientBalancesIfAny(
addressesData []NotFundedAddressData,
) error {
if len(addressesData) == 0 {
return errors.New("no addresses to print")
return nil
}

printAddresses := func() {
Expand Down
Loading

0 comments on commit 55afb56

Please sign in to comment.