-
Notifications
You must be signed in to change notification settings - Fork 77
feat: add validate rpc when setup relayer #1409
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
Open
anhductn2001
wants to merge
2
commits into
main
Choose a base branch
from
ducnt/validaterpc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
|
|
@@ -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) { | ||
| 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 | ||
|
||
| } | ||
| } | ||
|
|
||
| func installRelayerDependencies( | ||
| home string, | ||
| raID string, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 - lgtmThere was a problem hiding this comment.
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