Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artemijspavlovs/relayer path creation #1103

Merged
merged 24 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions cmd/relayer/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func Cmd() *cobra.Command {

if !ok {
pterm.Error.Printf("%s rollapp not registered on %s", raID, hd.ID)
return
}

raRpc, err := sequencerutils.GetRpcEndpointFromChain(raID, *hd)
Expand Down Expand Up @@ -89,6 +90,7 @@ func Cmd() *cobra.Command {
pterm.Error.Println("rollapp data validation error: ", err)
return
}
pterm.Info.Println("rollapp chain data validation passed")

// things to check:
// 1. relayer folder exists
Expand All @@ -98,10 +100,10 @@ func Cmd() *cobra.Command {
return
}

var isRelayerIbcPathValid bool
var ibcPathChains *relayerutils.IbcPathChains

if isRelayerDirPresent {
isRelayerIbcPathValid, err = relayerutils.ValidateIbcPathChains(
ibcPathChains, err = relayerutils.ValidateIbcPathChains(
relayerHome,
raID,
*hd,
Expand All @@ -122,8 +124,35 @@ func Cmd() *cobra.Command {
}
}

if !isRelayerIbcPathValid {
pterm.Warning.Println("relayer config verification failed...")
if ibcPathChains != nil {
if !ibcPathChains.DefaultPathOk || !ibcPathChains.SrcChainOk ||
!ibcPathChains.DstChainOk {
pterm.Warning.Println("relayer config verification failed...")
if ibcPathChains.DefaultPathOk {
pterm.Info.Printfln(
"removing path from config %s",
consts.DefaultRelayerPath,
)
err := relayer.DeletePath(*rollappChainData)
if err != nil {
pterm.Error.Printf("failed to delete relayer IBC path: %v\n", err)
return
}
}

pterm.Info.Println("populating relayer config with correct values...")
err = relayerutils.InitializeRelayer(home, *rollappChainData)
if err != nil {
pterm.Error.Printf("failed to initialize relayer config: %v\n", err)
return
}

if err := relayer.CreatePath(*rollappChainData); err != nil {
pterm.Error.Printf("failed to create relayer IBC path: %v\n", err)
return
}
}
} else {
pterm.Info.Println("populating relayer config with correct values...")
err = relayerutils.InitializeRelayer(home, *rollappChainData)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions relayer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ func CreatePath(rlpCfg roller.RollappConfig) error {
return nil
}

func DeletePath(rlpCfg roller.RollappConfig) error {
relayerHome := filepath.Join(rlpCfg.Home, consts.ConfigDirName.Relayer)
pterm.Info.Printf("removing ibc path from %s to %s\n", rlpCfg.HubData.ID, rlpCfg.RollappID)

newPathCmd := exec.Command(
consts.Executables.Relayer,
"paths",
"delete",
consts.DefaultRelayerPath,
"--home",
relayerHome,
)
if err := newPathCmd.Run(); err != nil {
return err
}

return nil
}

type ChainConfig struct {
ID string
RPC string
Expand Down
2 changes: 2 additions & 0 deletions relayer/create_ibc_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (r *Relayer) CreateIBCChannel(
time.Sleep(15 * time.Second)
// we ran create channel with override, as it not recovarable anyway
createChannelCmd := r.getCreateChannelCmd(true)

fmt.Println("createChannelCmd", createChannelCmd.String())
// TODO: switch to spinned
pterm.Info.Println("💈 Creating channel (this may take a while)...")
if err := r.WriteRelayerStatus(status); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion utils/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func createCustomHubDataFromFile() (*consts.HubData, error) {
{
"id": "<hub-id>",
"rpcUrl": "<hub-rpc-endpoint>",
"restUrl": "<hub-rest-endpoint>",
"apiUrl": "<hub-rest-endpoint>",
}`)
path, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("").Show()
for len(path) == 0 {
Expand Down
40 changes: 23 additions & 17 deletions utils/relayer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,60 @@ func UpdateConfigWithDefaultValues(relayerHome string, rollerData roller.Rollapp
return nil
}

func ValidateIbcPathChains(relayerHome, raID string, hd consts.HubData) (bool, error) {
var srcChainOk bool
var dstChainOk bool
var defaultPathOk bool
var relayerConfigExists bool
type IbcPathChains struct {
SrcChainOk bool
DstChainOk bool
DefaultPathOk bool
RelayerConfigExists bool
}

func ValidateIbcPathChains(relayerHome, raID string, hd consts.HubData) (*IbcPathChains, error) {
var err error
ibcPathChains := IbcPathChains{}

relayerConfigPath := GetConfigFilePath(relayerHome)

pterm.Info.Println("checking configuration")
// 2. config file exists
relayerConfigExists, err = filesystem.DoesFileExist(relayerConfigPath)
relayerConfigExists, err := filesystem.DoesFileExist(relayerConfigPath)
if err != nil {
return false, err
return nil, err
}
ibcPathChains.RelayerConfigExists = relayerConfigExists

if relayerConfigExists {
// 2.1. path exist
defaultPathOk, err = VerifyDefaultPath(relayerHome)
defaultPathOk, err := VerifyDefaultPath(relayerHome)
if err != nil {
pterm.Error.Printf(
"failed to verify relayer path %s: %v\n",
consts.DefaultRelayerPath,
err,
)
}
ibcPathChains.DefaultPathOk = defaultPathOk

if defaultPathOk {
// 2.2. isHubChainPresent
srcChainOk, err = VerifyPathSrcChain(relayerHome, hd)
srcChainOk, err := VerifyPathSrcChain(relayerHome, hd)
if err != nil {
pterm.Error.Printf(
"failed to verify source chain in relayer path: %v\n",
err,
)
}
ibcPathChains.SrcChainOk = srcChainOk

// 2.3. isRollappChainPresent
dstChainOk, err = VerifyPathDstChain(relayerHome, raID)
dstChainOk, err := VerifyPathDstChain(relayerHome, raID)
if err != nil {
return false, err
return &ibcPathChains, err
}
ibcPathChains.DstChainOk = dstChainOk
} else {
pterm.Error.Println("default path not found in relayer config")
}
}

if !relayerConfigExists || !srcChainOk || !dstChainOk {
return false, nil
}

return true, nil
pterm.Info.Println("ibc path validation passed")
return &ibcPathChains, nil
}
17 changes: 8 additions & 9 deletions utils/relayer/connection.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package relayer

import (
"fmt"
"os"

"github.com/pterm/pterm"
"gopkg.in/yaml.v3"

"github.com/dymensionxyz/roller/cmd/consts"
Expand All @@ -14,24 +14,23 @@ func VerifyDefaultPath(relayerHome string) (bool, error) {

data, err := os.ReadFile(cfp)
if err != nil {
pterm.Error.Println("failed to read config file:", err)
return false, err
}

var config map[string]interface{}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
pterm.Error.Println("failed to unmarshal config file:", err)
return false, err
}

// Navigate to paths and check for hub-rollapp
if paths, ok := config["paths"].(map[interface{}]interface{}); ok {
if _, exists := paths[consts.DefaultRelayerPath]; exists {
fmt.Println("hub-rollapp exists in the YAML configuration.")
return true, nil
}
if config.Paths == nil || config.Paths.HubRollapp == nil {
pterm.Error.Println("hub-rollapp not found in the YAML configuration.")
return false, nil
}

return false, nil
return true, nil
}

func VerifyPathSrcChain(relayerHome string, hd consts.HubData) (bool, error) {
Expand Down
20 changes: 14 additions & 6 deletions utils/relayer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ type Channels struct {
// Config struct represents the paths section inside the relayer
// configuration file
type Config struct {
Paths struct {
HubRollapp struct {
Dst struct {
ChainID string `yaml:"chain-id"`
Paths *struct {
HubRollapp *struct {
Dst *struct {
ChainID string `yaml:"chain-id"`
ClientID string `yaml:"client-id"`
ConnectionID string `yaml:"connection-id"`
} `yaml:"dst"`
Src struct {
ChainID string `yaml:"chain-id"`
Src *struct {
ChainID string `yaml:"chain-id"`
ClientID string `yaml:"client-id"`
ConnectionID string `yaml:"connection-id"`
} `yaml:"src"`
SrcChannelFilter *struct {
ChannelList []string `yaml:"channel-list"`
Rule string `yaml:"rule"`
} `yaml:"src-channel-filter"`
} `yaml:"hub-rollapp"`
} `yaml:"paths"`
}
1 change: 1 addition & 0 deletions utils/rollapp/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func PopulateRollerConfigWithRaMetadataFromChain(
)
rollerData, err := roller.LoadConfig(home)
if err != nil {
pterm.Error.Printf("failed to load roller config: %v\n", err)
return nil, err
}
if rollerData.KeyringBackend == "" {
Expand Down
Loading