Skip to content

Commit

Permalink
feat: roller service output should be generalised for da layer (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin authored Jul 4, 2023
1 parent df9ecac commit 9e27d18
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 222 deletions.
44 changes: 32 additions & 12 deletions cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
datalayer "github.com/dymensionxyz/roller/data_layer"
servicemanager "github.com/dymensionxyz/roller/utils/service_manager"
"github.com/spf13/cobra"
)

Expand All @@ -23,24 +24,25 @@ func Cmd() *cobra.Command {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rollappConfig, err := config.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
verifyBalances(rollappConfig)
logger := utils.GetRollerLogger(rollappConfig.Home)

ctx, cancel := context.WithCancel(context.Background())
waitingGroup := sync.WaitGroup{}
waitingGroup.Add(3)
serviceConfig := utils.ServiceConfig{
serviceConfig := &servicemanager.ServiceConfig{
Logger: logger,
Context: ctx,
WaitGroup: &waitingGroup,
}
/* ----------------------------- verify balances ---------------------------- */
verifyBalances(rollappConfig)

/* ------------------------------ run processes ----------------------------- */
runDaWithRestarts(rollappConfig, serviceConfig)
runSequencerWithRestarts(rollappConfig, serviceConfig)
runRelayerWithRestarts(rollappConfig, serviceConfig)

/* ------------------------------ render output ----------------------------- */
RenderUI(rollappConfig)
RenderUI(rollappConfig, serviceConfig)
cancel()
waitingGroup.Wait()
},
Expand All @@ -49,9 +51,15 @@ func Cmd() *cobra.Command {
return cmd
}

func runRelayerWithRestarts(config config.RollappConfig, serviceConfig utils.ServiceConfig) {
startRelayerCmd := getStartRelayerCmd(config)
utils.RunServiceWithRestart(startRelayerCmd, serviceConfig)
func runRelayerWithRestarts(cfg config.RollappConfig, serviceConfig *servicemanager.ServiceConfig) {
startRelayerCmd := getStartRelayerCmd(cfg)
service := servicemanager.ServiceData{
Command: startRelayerCmd,
FetchFn: utils.GetRelayerAddresses,
UIData: servicemanager.UIData{Name: "Relayer"},
}
serviceConfig.AddService("Relayer", service)
serviceConfig.RunServiceWithRestart("Relayer")
}

func getStartRelayerCmd(config config.RollappConfig) *exec.Cmd {
Expand All @@ -62,20 +70,32 @@ func getStartRelayerCmd(config config.RollappConfig) *exec.Cmd {
return exec.Command(ex, "relayer", "start", "--home", config.Home)
}

func runDaWithRestarts(rollappConfig config.RollappConfig, serviceConfig utils.ServiceConfig) {
func runDaWithRestarts(rollappConfig config.RollappConfig, serviceConfig *servicemanager.ServiceConfig) {
damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home)
daLogFilePath := utils.GetDALogFilePath(rollappConfig.Home)
startDALCCmd := damanager.GetStartDACmd(consts.DefaultCelestiaRPC)
if startDALCCmd == nil {
serviceConfig.WaitGroup.Done()
return
}
utils.RunServiceWithRestart(startDALCCmd, serviceConfig, utils.WithLogging(daLogFilePath))

service := servicemanager.ServiceData{
Command: startDALCCmd,
FetchFn: damanager.GetDAAccData,
UIData: servicemanager.UIData{Name: "DA Light Client"},
}
serviceConfig.AddService("DA Light Client", service)
serviceConfig.RunServiceWithRestart("DA Light Client", utils.WithLogging(daLogFilePath))
}

func runSequencerWithRestarts(rollappConfig config.RollappConfig, serviceConfig utils.ServiceConfig) {
func runSequencerWithRestarts(rollappConfig config.RollappConfig, serviceConfig *servicemanager.ServiceConfig) {
startRollappCmd := sequnecer_start.GetStartRollappCmd(rollappConfig, consts.DefaultDALCRPC)
utils.RunServiceWithRestart(startRollappCmd, serviceConfig, utils.WithLogging(utils.GetSequencerLogPath(rollappConfig)))
service := servicemanager.ServiceData{
Command: startRollappCmd,
FetchFn: utils.GetSequencerData,
UIData: servicemanager.UIData{Name: "Sequencer"},
}
serviceConfig.AddService("Sequencer", service)
serviceConfig.RunServiceWithRestart("Sequencer", utils.WithLogging(utils.GetSequencerLogPath(rollappConfig)))
}

func verifyBalances(rollappConfig config.RollappConfig) {
Expand Down
71 changes: 0 additions & 71 deletions cmd/run/services.go

This file was deleted.

87 changes: 11 additions & 76 deletions cmd/run/services_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,18 @@ package run

import (
"log"
"math/big"
"path/filepath"
"time"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
datalayer "github.com/dymensionxyz/roller/data_layer"
servicemanager "github.com/dymensionxyz/roller/utils/service_manager"

ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)

func processDataResults(results chan fetchResult, size int, logger *log.Logger) []*utils.AccountData {
data := make([]*utils.AccountData, size)
for i := 0; i < size; i++ {
res := <-results
if res.err != nil {
logger.Println(res.err)
data[res.id] = &utils.AccountData{
Address: "",
Balance: big.NewInt(0),
}
} else {
data[res.id] = res.data
}
}
return data
}

func activeIfSufficientBalance(currentBalance, threshold *big.Int) string {
if currentBalance.Cmp(threshold) >= 0 {
return "Active"
} else {
return "Stopped"
}
}

func buildServiceData(data []*utils.AccountData, rollappConfig config.RollappConfig) []ServiceData {
damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home)

var servicedata = []ServiceData{
{
Name: "Sequencer",
Balance: data[0].Balance.String() + consts.Denoms.Hub,
Status: activeIfSufficientBalance(data[0].Balance, big.NewInt(1)),
},
{
Name: "Relayer",
Balance: data[1].Balance.String() + consts.Denoms.Hub + ", " +
data[1].Balance.String() + rollappConfig.Denom,
Status: "Starting...",
},
}

if damanager.GetLightNodeEndpoint() != "" {
servicedata = append(servicedata, ServiceData{
Name: "DA Light Client",
Balance: data[3].Balance.String() + consts.Denoms.Celestia,
Status: activeIfSufficientBalance(data[3].Balance, consts.OneDAWritePrice),
})
}
return servicedata
}

func RenderUI(rollappConfig config.RollappConfig) {
func RenderUI(rollappConfig config.RollappConfig, manager *servicemanager.ServiceConfig) {
logger := utils.GetLogger(filepath.Join(rollappConfig.Home, "roller.log"))
initializeUI()
defer ui.Close()
Expand All @@ -78,44 +24,33 @@ func RenderUI(rollappConfig config.RollappConfig) {
servicesStatusTable := NewServiceStatusTable(termWidth)
servicesInfoTable := NewServicesInfoTable(rollappConfig, termWidth)

serviceData, err := fetchServicesData(rollappConfig, logger)
if err != nil {
logger.Printf("Error: failed to fetch service data: %v", err)
serviceData = []ServiceData{}
}
updateUITable(serviceData, servicesStatusTable)
manager.FetchServicesData(rollappConfig)
updateUITable(manager.GetUIData(), servicesStatusTable)
ui.Render(p, servicesStatusTable, servicesInfoTable)

events := ui.PollEvents()
ticker := time.NewTicker(time.Second * 5).C

//TODO: the renderer should be a struct that holds the config and the tables
config := ServiceStatusConfig{
rollappConfig: rollappConfig,
logger: logger,
table: servicesStatusTable,
}
events := ui.PollEvents()
ticker := time.NewTicker(time.Second * 5).C

eventLoop(events, ticker, config)
eventLoop(events, ticker, manager, config)
}

func eventLoop(events <-chan ui.Event, ticker <-chan time.Time, config ServiceStatusConfig) {
func eventLoop(events <-chan ui.Event, ticker <-chan time.Time, manager *servicemanager.ServiceConfig, config ServiceStatusConfig) {
for {
select {
case e := <-events:
if e.ID == "q" || e.ID == "<C-c>" {
return
}
case <-ticker:
config.logger.Println("Fetching service data...")
serviceData, err := fetchServicesData(config.rollappConfig, config.logger)
if err != nil {
config.logger.Printf("Error: failed to fetch service data: %v", err)
serviceData = []ServiceData{}
} else {
config.logger.Printf("Fetched services data successfully %s", serviceData)
}
updateUITable(serviceData, config.table)
manager.Logger.Println("Fetching service data...")
manager.FetchServicesData(config.rollappConfig)
updateUITable(manager.GetUIData(), config.table)
ui.Render(config.table)
}
}
Expand Down
12 changes: 9 additions & 3 deletions cmd/run/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package run
import (
"fmt"
"log"
"strings"

"github.com/dymensionxyz/roller/config"
servicemanager "github.com/dymensionxyz/roller/utils/service_manager"
"github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
Expand All @@ -31,9 +33,13 @@ func NewServiceStatusTable(termWidth int) *widgets.Table {
return table
}

func updateUITable(serviceData []ServiceData, table *widgets.Table) {
func updateUITable(serviceData []servicemanager.UIData, table *widgets.Table) {
table.Rows = [][]string{{"Name", "Balance", "Status"}}
for _, data := range serviceData {
table.Rows = append(table.Rows, []string{data.Name, data.Balance, data.Status})
for _, service := range serviceData {
balances := []string{}
for _, account := range service.Accounts {
balances = append(balances, account.Balance.String())
}
table.Rows = append(table.Rows, []string{service.Name, strings.Join(balances, ","), service.Status})
}
}
22 changes: 12 additions & 10 deletions cmd/utils/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,18 @@ func GetSequencerInsufficientAddrs(cfg config.RollappConfig, requiredBalance big
if err != nil {
return nil, err
}
if sequencerData.Balance.Cmp(&requiredBalance) < 0 {
return []NotFundedAddressData{
{
Address: sequencerData.Address,
Denom: consts.Denoms.Hub,
CurrentBalance: sequencerData.Balance,
RequiredBalance: &requiredBalance,
KeyName: consts.KeysIds.HubSequencer,
},
}, nil
for _, seq := range sequencerData {
if seq.Balance.Cmp(&requiredBalance) < 0 {
return []NotFundedAddressData{
{
Address: seq.Address,
Denom: consts.Denoms.Hub,
CurrentBalance: seq.Balance,
RequiredBalance: &requiredBalance,
KeyName: consts.KeysIds.HubSequencer,
},
}, nil
}
}
return []NotFundedAddressData{}, nil
}
25 changes: 21 additions & 4 deletions cmd/utils/fetch_accounts_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import (
"github.com/dymensionxyz/roller/config"
)

func GetRelayerAddresses(cfg config.RollappConfig) ([]AccountData, error) {
data := []AccountData{}
rollappRlyAcc, err := GetRolRlyAccData(cfg)
if err != nil {
return nil, err
}
data = append(data, *rollappRlyAcc)
hubRlyAcc, err := GetHubRlyAccData(cfg)
if err != nil {
return nil, err
}
data = append(data, *hubRlyAcc)
return data, nil
}

func GetRolRlyAccData(cfg config.RollappConfig) (*AccountData, error) {
RollappRlyAddr, err := GetRelayerAddress(cfg.Home, cfg.RollappID)
if err != nil {
Expand Down Expand Up @@ -45,7 +60,7 @@ func GetHubRlyAccData(cfg config.RollappConfig) (*AccountData, error) {
}, nil
}

func GetSequencerData(cfg config.RollappConfig) (*AccountData, error) {
func GetSequencerData(cfg config.RollappConfig) ([]AccountData, error) {
sequencerAddress, err := GetAddressBinary(GetKeyConfig{
ID: consts.KeysIds.HubSequencer,
Dir: filepath.Join(cfg.Home, consts.ConfigDirName.HubKeys),
Expand All @@ -61,8 +76,10 @@ func GetSequencerData(cfg config.RollappConfig) (*AccountData, error) {
if err != nil {
return nil, err
}
return &AccountData{
Address: sequencerAddress,
Balance: sequencerBalance,
return []AccountData{
{
Address: sequencerAddress,
Balance: sequencerBalance,
},
}, nil
}
Loading

0 comments on commit 9e27d18

Please sign in to comment.