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

fix: roller extarcts wrong active channel #498

Merged
merged 3 commits into from
Sep 5, 2023
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
4 changes: 2 additions & 2 deletions cmd/migrate/v_0_1_13.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
12 changes: 0 additions & 12 deletions relayer/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
15 changes: 8 additions & 7 deletions relayer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
55 changes: 45 additions & 10 deletions relayer/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down Expand Up @@ -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() {
Expand All @@ -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
}
Expand All @@ -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...)
}
Loading