Skip to content

Commit

Permalink
feat: add rollapp status command (#812)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Jul 10, 2024
1 parent 3d4e942 commit 0e1d2a1
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 14 deletions.
2 changes: 2 additions & 0 deletions cmd/rollapp/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

initrollapp "github.com/dymensionxyz/roller/cmd/rollapp/init"
"github.com/dymensionxyz/roller/cmd/rollapp/status"
)

func Cmd() *cobra.Command {
Expand All @@ -13,6 +14,7 @@ func Cmd() *cobra.Command {
}

cmd.AddCommand(initrollapp.Cmd)
cmd.AddCommand(status.Cmd())

return cmd
}
30 changes: 30 additions & 0 deletions cmd/rollapp/status/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package status

import (
"fmt"

"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"github.com/dymensionxyz/roller/sequencer"
)

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Show the status of the sequencer on the local machine.",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rollappConfig, err := config.LoadConfigFromTOML(home)
if err != nil {
fmt.Println("failed to load config:", err)
return
}
utils.PrettifyErrorIfExists(err)
seq := sequencer.GetInstance(rollappConfig)
fmt.Println(seq.GetSequencerStatus(rollappConfig))
},
}
return cmd
}
4 changes: 4 additions & 0 deletions sequencer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (seq *Sequencer) GetRPCEndpoint() string {
return "http://localhost:" + seq.RPCPort
}

func (seq *Sequencer) GetLocalEndpoint(port string) string {
return "http://localhost:" + port
}

func getSequencerConfigDir(rollerHome string) string {
return filepath.Join(rollerHome, consts.ConfigDirName.Rollapp, "config")
}
Expand Down
106 changes: 92 additions & 14 deletions sequencer/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sequencer

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -30,6 +31,24 @@ type Response struct {
Result Result `json:"result"`
}

type HubResponse struct {
StateInfo struct {
StartHeight string `json:"startHeight"`
NumBlocks string `json:"numBlocks"`
} `json:"stateInfo"`
}

type HealthResult struct {
IsHealthy bool `json:"isHealthy"`
Error string `json:"error"`
}

type HealthResponse struct {
JsonRPC string `json:"jsonrpc"`
Result HealthResult `json:"result"`
ID int `json:"id"`
}

func (seq *Sequencer) GetRollappHeight() (string, error) {
rollappRPCEndpoint := seq.GetRPCEndpoint()
resp, err := http.Get(fmt.Sprintf("%s/status", rollappRPCEndpoint))
Expand All @@ -54,13 +73,6 @@ func (seq *Sequencer) GetRollappHeight() (string, error) {
}
}

type HubResponse struct {
StateInfo struct {
StartHeight string `json:"startHeight"`
NumBlocks string `json:"numBlocks"`
} `json:"stateInfo"`
}

func (seq *Sequencer) GetHubHeight() (string, error) {
cmd := exec.Command(consts.Executables.Dymension, "q", "rollapp", "state", seq.RlpCfg.RollappID,
"--output", "json", "--node", seq.RlpCfg.HubData.RPC_URL)
Expand All @@ -83,22 +95,88 @@ func (seq *Sequencer) GetHubHeight() (string, error) {
return strconv.Itoa(startHeight + numBlocks - 1), nil
}

func (seq *Sequencer) GetSequencerHealth() error {
var res HealthResponse
url := seq.GetLocalEndpoint(seq.RPCPort)
healthEndpoint := fmt.Sprintf("%s/health", url)

// nolint gosec
resp, err := http.Get(healthEndpoint)
if err != nil {
return err
}

// nolint errcheck
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}

err = json.Unmarshal(body, &res)
if err != nil {
return err
}

if !res.Result.IsHealthy {
return errors.New(res.Result.Error)
}

return nil
}

func (seq *Sequencer) GetSequencerStatus(config.RollappConfig) string {
// TODO: Make sure the sequencer status endpoint is being changed after block production is paused.
rolHeight, err := seq.GetRollappHeight()
if err != nil {
seq.logger.Println(err)
}

// ?
if rolHeight == "-1" {
return "Stopped, Restarting..."
} else if rolHeight == "-2" {
return "Unhealthy"
} else {
hubHeight, err := seq.GetHubHeight()
}

err = seq.GetSequencerHealth()
if err != nil {
return fmt.Sprintf(`
status: Unhealthy
error: %v
`, err,
)
}

hubHeight, err := seq.GetHubHeight()

localAPIEndpoint := seq.GetLocalEndpoint(seq.APIPort)
localRPCEndpoint := seq.GetLocalEndpoint(seq.RPCPort)

if err != nil {
seq.logger.Println(err)

err := seq.ReadPorts()
if err != nil {
seq.logger.Println(err)
return fmt.Sprintf("Active, Height: %s", rolHeight)
fmt.Println("failed to retrieve ports: ", err)
}
return fmt.Sprintf("Active, Height: %s, Hub: %s", rolHeight, hubHeight)

return fmt.Sprintf(`RollApp
status: Healthy
height: %s
Endpoints:
rpc: %s
rest: %s`, rolHeight, localRPCEndpoint, localAPIEndpoint)
}

return fmt.Sprintf(`RollApp:
status: Healthy
height: %s
Endpoints:
rpc: %s
rest: %s
Hub:
height: %s`, rolHeight, localRPCEndpoint, localAPIEndpoint, hubHeight)
}

0 comments on commit 0e1d2a1

Please sign in to comment.