Skip to content
Open
Changes from 1 commit
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
84 changes: 82 additions & 2 deletions cmd/relayer/setup/setup.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package setup

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -268,15 +271,92 @@ func getPreRunInfo(home string) (*consts.RollappData, *consts.HubData, string, e
pterm.Error.Println("failed to retrieve rollapp rpc endpoint: ", err)
return nil, nil, "", err
}
raRpc = strings.TrimSuffix(raRpc, "/")
pterm.Info.Printf("validating rollapp rpc endpoint: %s\n", raRpc)
validatedRpc, err := validateRollappRPCEndpoint(raID, raRpc)
if err != nil {
pterm.Error.Println("rollapp rpc endpoint validation failed:", err)
return nil, nil, "", err
}

raData := consts.RollappData{
ID: raID,
RpcUrl: raRpc,
RpcUrl: validatedRpc,
}
return &raData, hd, kb, nil
}

type tendermintStatusResponse struct {
Result struct {
NodeInfo struct {
Network string `json:"network"`
} `json:"node_info"`
} `json:"result"`
}

func validateRollappRPCEndpoint(rollappID, endpoint string) (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this checks that the on chain rpc endpoint, provided by the sequencer is actually pointing to the correct chain id?

move it to utils/sequencer, other then that - lgtm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea thx @artemijspavlovs . I pushed a fix

trimmed := strings.TrimSpace(endpoint)
if trimmed == "" {
return "", fmt.Errorf("rollapp rpc endpoint is empty")
}

normalized := normalizeRpcScheme(trimmed)
normalized = strings.TrimRight(normalized, "/")

statusURL := normalized + "/status"
client := &http.Client{
Timeout: 10 * time.Second,
}

resp, err := client.Get(statusURL)
if err != nil {
return "", fmt.Errorf("failed to reach rollapp rpc endpoint %s: %w", normalized, err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf(
"rollapp rpc endpoint %s returned %s from /status",
normalized,
resp.Status,
)
}

var status tendermintStatusResponse
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
return "", fmt.Errorf("failed to decode /status from %s: %w", statusURL, err)
}

chainID := strings.TrimSpace(status.Result.NodeInfo.Network)
if chainID == "" {
return "", fmt.Errorf("rollapp rpc endpoint %s returned empty chain-id in /status response", normalized)
}
if chainID != rollappID {
return "", fmt.Errorf(
"rollapp rpc endpoint %s reports chain-id %s, expected %s",
normalized,
chainID,
rollappID,
)
}

return normalized, nil
}

func normalizeRpcScheme(endpoint string) string {
switch {
case strings.HasPrefix(endpoint, "tcp://"):
return "http://" + strings.TrimPrefix(endpoint, "tcp://")
case strings.HasPrefix(endpoint, "ws://"):
return "http://" + strings.TrimPrefix(endpoint, "ws://")
case strings.HasPrefix(endpoint, "wss://"):
return "https://" + strings.TrimPrefix(endpoint, "wss://")
case strings.HasPrefix(endpoint, "http://"), strings.HasPrefix(endpoint, "https://"):
return endpoint
default:
return "http://" + endpoint
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default should be https endpoint, otherwise the http will try to point to port 80, which is not where anything runs.

}
}

func installRelayerDependencies(
home string,
raID string,
Expand Down
Loading