diff --git a/cmd/migrate/v_0_1_13.go b/cmd/migrate/v_0_1_13.go index 242106b3..09ed50bc 100644 --- a/cmd/migrate/v_0_1_13.go +++ b/cmd/migrate/v_0_1_13.go @@ -23,7 +23,7 @@ func (v *VersionMigratorV0113) PerformMigration(rlpCfg config.RollappConfig) err } func flipRlyPath(rlpCfg config.RollappConfig) error { - rlyCfg, err := relayer.ReadRlyConfig(rlpCfg) + rlyCfg, err := relayer.ReadRlyConfig(rlpCfg.Home) if err != nil { return err } @@ -52,5 +52,5 @@ func flipRlyPath(rlpCfg config.RollappConfig) error { if err := utils.SetNestedValue(rlyCfg, []string{"paths", "hub-rollapp"}, nil); err != nil { return err } - return relayer.WriteRlyConfig(rlpCfg, rlyCfg) + return relayer.WriteRlyConfig(rlpCfg.Home, rlyCfg) } diff --git a/relayer/channels.go b/relayer/channels.go index ed1ba456..d472ac67 100644 --- a/relayer/channels.go +++ b/relayer/channels.go @@ -74,18 +74,6 @@ func (r *Relayer) LoadChannels() (string, string, error) { return r.SrcChannel, r.DstChannel, nil } -func (r *Relayer) queryConnectionsRollappCmd() *exec.Cmd { - args := []string{"q", "connections", r.RollappID} - args = append(args, "--home", filepath.Join(r.Home, consts.ConfigDirName.Relayer)) - return exec.Command(consts.Executables.Relayer, args...) -} - -func (r *Relayer) queryConnectionsHubCmd(connectionID string) *exec.Cmd { - args := []string{"q", "connection", r.HubID, connectionID} - args = append(args, "--home", filepath.Join(r.Home, consts.ConfigDirName.Relayer)) - return exec.Command(consts.Executables.Relayer, args...) -} - func (r *Relayer) queryChannelsRollappCmd() *exec.Cmd { args := []string{"q", "channels", r.RollappID} args = append(args, "--home", filepath.Join(r.Home, consts.ConfigDirName.Relayer)) diff --git a/relayer/config.go b/relayer/config.go index 78c152f1..cbe2ebd1 100644 --- a/relayer/config.go +++ b/relayer/config.go @@ -2,13 +2,14 @@ package relayer import ( "fmt" + "os" + "os/exec" + "path/filepath" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/utils" "gopkg.in/yaml.v2" - "os" - "os/exec" - "path/filepath" ) func CreatePath(rlpCfg config.RollappConfig) error { @@ -55,8 +56,8 @@ func UpdateRlyConfigValue(rlpCfg config.RollappConfig, keyPath []string, newValu return os.WriteFile(rlyConfigPath, newData, 0644) } -func ReadRlyConfig(rlpCfg config.RollappConfig) (map[interface{}]interface{}, error) { - rlyConfigPath := filepath.Join(rlpCfg.Home, consts.ConfigDirName.Relayer, "config", "config.yaml") +func ReadRlyConfig(homeDir string) (map[interface{}]interface{}, error) { + rlyConfigPath := filepath.Join(homeDir, consts.ConfigDirName.Relayer, "config", "config.yaml") data, err := os.ReadFile(rlyConfigPath) if err != nil { return nil, fmt.Errorf("failed to load %s: %v", rlyConfigPath, err) @@ -69,8 +70,8 @@ func ReadRlyConfig(rlpCfg config.RollappConfig) (map[interface{}]interface{}, er return rlyCfg, nil } -func WriteRlyConfig(rlpCfg config.RollappConfig, rlyCfg map[interface{}]interface{}) error { - rlyConfigPath := filepath.Join(rlpCfg.Home, consts.ConfigDirName.Relayer, "config", "config.yaml") +func WriteRlyConfig(homeDir string, rlyCfg map[interface{}]interface{}) error { + rlyConfigPath := filepath.Join(homeDir, consts.ConfigDirName.Relayer, "config", "config.yaml") data, err := yaml.Marshal(rlyCfg) if err != nil { return fmt.Errorf("failed to marshal config: %v", err) diff --git a/relayer/connections.go b/relayer/connections.go index 961c87b1..d5058cf2 100644 --- a/relayer/connections.go +++ b/relayer/connections.go @@ -3,11 +3,15 @@ package relayer import ( "encoding/json" "fmt" + "os/exec" + "path/filepath" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" + roller_utils "github.com/dymensionxyz/roller/utils" ) -type RollappConnectionQueryResult struct { +type ConnectionsQueryResult struct { ID string `json:"id"` ClientID string `json:"client_id"` Versions []VersionInfo `json:"versions"` @@ -16,7 +20,7 @@ type RollappConnectionQueryResult struct { DelayPeriod string `json:"delay_period"` } -type HubConnectionQueryResult struct { +type ConnectionQueryResult struct { Connection ConnectionInfo `json:"connection"` Proof string `json:"proof"` ProofHeight ProofHeightInfo `json:"proof_height"` @@ -51,17 +55,36 @@ type PrefixInfo struct { } func (r *Relayer) IsLatestConnectionOpen() (bool, string, error) { - output, err := utils.ExecBashCommandWithStdout(r.queryConnectionsRollappCmd()) + rlyCfg, err := ReadRlyConfig(r.Home) if err != nil { return false, "", err + } + connectionIDRollapp_raw, err := roller_utils.GetNestedValue(rlyCfg, []string{"paths", consts.DefaultRelayerPath, "dst", "connection-id"}) + if err != nil { + return false, "", err + } + + connectionIDHub_raw, err := roller_utils.GetNestedValue(rlyCfg, []string{"paths", consts.DefaultRelayerPath, "src", "connection-id"}) + if err != nil { + return false, "", err + } + + connectionIDRollapp := connectionIDRollapp_raw.(string) + connectionIDHub := connectionIDHub_raw.(string) - if output.Len() == 0 { + if connectionIDRollapp == "" || connectionIDHub == "" { + r.logger.Printf("can't find active connection in the config") return false, "", nil } + output, err := utils.ExecBashCommandWithStdout(r.queryConnectionRollappCmd(connectionIDRollapp)) + if err != nil { + return false, "", err + } + // While there are JSON objects in the stream... - var outputStruct RollappConnectionQueryResult + var outputStruct ConnectionQueryResult dec := json.NewDecoder(&output) for dec.More() { @@ -71,13 +94,13 @@ func (r *Relayer) IsLatestConnectionOpen() (bool, string, error) { } } - if outputStruct.State != "STATE_OPEN" { + if outputStruct.Connection.State != "STATE_OPEN" { return false, "", nil } // Check if the connection is open on the hub - var res HubConnectionQueryResult - outputHub, err := utils.ExecBashCommandWithStdout(r.queryConnectionsHubCmd(outputStruct.Counterparty.ConnectionID)) + var res ConnectionQueryResult + outputHub, err := utils.ExecBashCommandWithStdout(r.queryConnectionsHubCmd(connectionIDHub)) if err != nil { return false, "", err } @@ -88,10 +111,22 @@ func (r *Relayer) IsLatestConnectionOpen() (bool, string, error) { if res.Connection.State != "STATE_OPEN" { r.logger.Printf("connection %s is STATE_OPEN on the rollapp, but connection %s is %s on the hub", - outputStruct.ID, outputStruct.Counterparty.ConnectionID, res.Connection.State, + connectionIDRollapp, connectionIDHub, res.Connection.State, ) return false, "", nil } - return true, outputStruct.ID, nil + return true, connectionIDRollapp, nil +} + +func (r *Relayer) queryConnectionRollappCmd(connectionID string) *exec.Cmd { + args := []string{"q", "connection", r.RollappID, connectionID} + args = append(args, "--home", filepath.Join(r.Home, consts.ConfigDirName.Relayer)) + return exec.Command(consts.Executables.Relayer, args...) +} + +func (r *Relayer) queryConnectionsHubCmd(connectionID string) *exec.Cmd { + args := []string{"q", "connection", r.HubID, connectionID} + args = append(args, "--home", filepath.Join(r.Home, consts.ConfigDirName.Relayer)) + return exec.Command(consts.Executables.Relayer, args...) }