Skip to content

Commit

Permalink
feat: add sequencer address and balance to rollapp output commands (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Sep 15, 2024
1 parent e5e4ac6 commit 7668320
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 40 deletions.
77 changes: 63 additions & 14 deletions cmd/rollapp/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
Expand All @@ -20,8 +25,7 @@ import (
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/dymensionxyz/roller/utils/errorhandling"
"github.com/dymensionxyz/roller/utils/filesystem"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
sequencerutils "github.com/dymensionxyz/roller/utils/sequencer"
)

// var OneDaySequencePrice = big.NewInt(1)
Expand All @@ -42,6 +46,8 @@ func Cmd() *cobra.Command {
Consider using 'services' if you want to run a 'systemd' service instead.
`,
Run: func(cmd *cobra.Command, args []string) {
showSequencerBalance, _ := cmd.Flags().GetBool("show-sequencer-balance")

err := initconfig.AddFlags(cmd)
if err != nil {
pterm.Error.Println("failed to add flags")
Expand All @@ -66,13 +72,20 @@ Consider using 'services' if you want to run a 'systemd' service instead.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go bash.RunCmdAsync(
ctx, startRollappCmd, func() {
printOutput(rollappConfig, startRollappCmd)
ctx,
startRollappCmd,
func() {
PrintOutput(
rollappConfig,
strconv.Itoa(startRollappCmd.Process.Pid),
showSequencerBalance,
)
err := createPidFile(RollappDirPath, startRollappCmd)
if err != nil {
pterm.Warning.Println("failed to create pid file")
}
}, parseError,
},
parseError,
utils.WithLogging(utils.GetSequencerLogPath(rollappConfig)),
)

Expand Down Expand Up @@ -104,25 +117,61 @@ Consider using 'services' if you want to run a 'systemd' service instead.
select {}
},
}
cmd.Flags().Bool("show-sequencer-balance", false, "initialize the rollapp with mock backend")

return cmd
}

func printOutput(rlpCfg config.RollappConfig, cmd *exec.Cmd) {
func PrintOutput(rlpCfg config.RollappConfig, pid string, withBalance bool) {
seq := sequencer.GetInstance(rlpCfg)
seqAddrData, err := sequencerutils.GetSequencerData(rlpCfg)
if err != nil {
return
}

fmt.Println("💈 The Rollapp sequencer is running on your local machine!")
fmt.Printf(
"💈 RollApp ID: %s\n", pterm.DefaultBasicText.WithStyle(pterm.FgYellow.ToStyle()).
Sprintf(rlpCfg.RollappID),
)
fmt.Println("💈 Endpoints:")

fmt.Printf("💈 EVM RPC: http://0.0.0.0:%v\n", seq.JsonRPCPort)
fmt.Printf("💈 Node RPC: http://0.0.0.0:%v\n", seq.RPCPort)
fmt.Printf("💈 Rest API: http://0.0.0.0:%v\n", seq.APIPort)

fmt.Println("💈 Log file path: ", LogPath)
fmt.Println("💈 Rollapp root dir: ", RollappDirPath)
fmt.Println("💈 PID: ", cmd.Process.Pid)
pterm.DefaultSection.WithIndentCharacter("💈").
Println("Endpoints:")
fmt.Printf("EVM RPC: http://0.0.0.0:%v\n", seq.JsonRPCPort)
fmt.Printf("Node RPC: http://0.0.0.0:%v\n", seq.RPCPort)
fmt.Printf("Rest API: http://0.0.0.0:%v\n", seq.APIPort)

pterm.DefaultSection.WithIndentCharacter("💈").
Println("Filesystem Paths:")
fmt.Println("Rollapp root dir: ", RollappDirPath)
fmt.Println("Log file path: ", LogPath)

pterm.DefaultSection.WithIndentCharacter("💈").
Println("Process Info:")
fmt.Println("PID: ", pid)

pterm.DefaultSection.WithIndentCharacter("💈").
Println("Wallet Info:")
fmt.Println("Sequencer Address:", seqAddrData[0].Address)

if withBalance {
fmt.Println("Sequencer Balance:", seqAddrData[0].Balance.String())
go func() {
for {
// nolint: gosimple
select {
default:
seqAddrData, err := sequencerutils.GetSequencerData(rlpCfg)
if err == nil {
// Clear the previous line and print the updated balance
fmt.Print("\033[1A\033[K") // Move cursor up one line and clear it
fmt.Println("Sequencer Balance:", seqAddrData[0].Balance.String())
}
time.Sleep(5 * time.Second)
}
}
}()
}
}

func printDaOutput() {
Expand Down
15 changes: 14 additions & 1 deletion cmd/rollapp/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package status

import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/rollapp/start"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/sequencer"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
Expand All @@ -24,7 +28,16 @@ func Cmd() *cobra.Command {
}
errorhandling.PrettifyErrorIfExists(err)
seq := sequencer.GetInstance(rollappConfig)
fmt.Println(seq.GetSequencerStatus(rollappConfig))
fmt.Println(seq.GetSequencerStatus())

pidFilePath := filepath.Join(home, consts.ConfigDirName.Rollapp, "rollapp.pid")
pid, err := os.ReadFile(pidFilePath)
if err != nil {
fmt.Println("failed to read pid file:", err)
return
}

start.PrintOutput(rollappConfig, string(pid), true)
},
}
return cmd
Expand Down
15 changes: 9 additions & 6 deletions cmd/services/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"runtime"
"strings"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
servicemanager "github.com/dymensionxyz/roller/utils/service_manager"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

func RollappCmd() *cobra.Command {
Expand All @@ -30,9 +29,9 @@ func RollappCmd() *cobra.Command {
Sprintf("roller relayer setup"),
)
pterm.Info.Printf(
"run %s to view the current status of the rollapp\n",
"run %s to view the logs of the relayer\n",
pterm.DefaultBasicText.WithStyle(pterm.FgYellow.ToStyle()).
Sprintf("journalctl -fu rollapp"),
Sprintf("roller relayer services load"),
)
},
}
Expand Down Expand Up @@ -93,7 +92,11 @@ func startSystemdServices(services []string) error {
)
}
for _, service := range services {
err := servicemanager.StartSystemdService(fmt.Sprintf("%s.service", service))
err := servicemanager.StartSystemdService(
fmt.Sprintf("%s.service", service),
"--show-sequencer-balance",
"false",
)
if err != nil {
return fmt.Errorf("failed to start %s systemd service: %v", service, err)
}
Expand Down
17 changes: 3 additions & 14 deletions sequencer/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/utils/bash"
"github.com/dymensionxyz/roller/utils/config"
)

type NodeInfo struct {
Expand Down Expand Up @@ -139,7 +138,7 @@ func (seq *Sequencer) GetSequencerHealth() error {
return nil
}

func (seq *Sequencer) GetSequencerStatus(config.RollappConfig) string {
func (seq *Sequencer) GetSequencerStatus() string {
// TODO: Make sure the sequencer status endpoint is being changed after block production is paused.
rolHeight, err := seq.GetRollappHeight()
if err != nil {
Expand All @@ -163,9 +162,6 @@ error: %v

hubHeight, err := seq.GetHubHeight()

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

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

Expand All @@ -178,10 +174,7 @@ error: %v
`RollApp
status: Healthy
height: %s
Endpoints:
rpc: %s
rest: %s`, rolHeight, localRPCEndpoint, localAPIEndpoint,
`, rolHeight,
)
}

Expand All @@ -190,11 +183,7 @@ rest: %s`, rolHeight, localRPCEndpoint, localAPIEndpoint,
status: Healthy
height: %s
Endpoints:
rpc: %s
rest: %s
Hub:
height: %s`, rolHeight, localRPCEndpoint, localAPIEndpoint, hubHeight,
height: %s`, rolHeight, hubHeight,
)
}
2 changes: 1 addition & 1 deletion utils/sequencer/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func isValidSequencerMetadata(path string) (bool, error) {
return true, err
}

func GetSequencerAccountData(cfg config.RollappConfig) (string, error) {
func GetSequencerAccountAddress(cfg config.RollappConfig) (string, error) {
seqAddr, err := utils.GetAddressBinary(
utils.KeyConfig{
ChainBinary: consts.Executables.Dymension,
Expand Down
16 changes: 12 additions & 4 deletions utils/service_manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,18 @@ func (s *ServiceConfig) RunServiceWithRestart(name string, options ...bash.Comma
}()
}

func StartSystemdService(serviceName string) error {
// not ideal, shouldn't run sudo commands from within roller
cmd := exec.Command("sudo", "systemctl", "start", serviceName)
err := bash.ExecCmd(cmd)
func StartSystemdService(serviceName string, options ...string) error {
// Create the base command
cmd := []string{"sudo", "systemctl", "start", serviceName}

// Append any additional options
cmd = append(cmd, options...)

// Create the exec.Command
execCmd := exec.Command(cmd[0], cmd[1:]...)

// Execute the command
err := bash.ExecCmd(execCmd)
if err != nil {
return err
}
Expand Down

0 comments on commit 7668320

Please sign in to comment.