From dcbe2d1406f8e3bcaef622dbe51c19b007edd703 Mon Sep 17 00:00:00 2001 From: artpav <19916123+artemijspavlovs@users.noreply.github.com> Date: Fri, 30 Aug 2024 09:06:06 +0300 Subject: [PATCH] fix: improve relayer flow and address handling (#869) --- cmd/config/init/keys.go | 50 +++++---- cmd/config/init/relayer.go | 4 - cmd/relayer/run/run.go | 213 ++++++++++++++++--------------------- cmd/relayer/start/start.go | 67 +----------- cmd/rollapp/start/start.go | 6 +- cmd/run/run.go | 155 --------------------------- cmd/run/services_info.go | 45 -------- cmd/run/services_status.go | 68 ------------ cmd/run/ui.go | 63 ----------- cmd/utils/key_info.go | 32 +++++- cmd/utils/keys.go | 4 +- relayer/query.go | 68 ++++++++---- utils/dymint/dymint.go | 16 ++- 13 files changed, 222 insertions(+), 569 deletions(-) delete mode 100644 cmd/run/run.go delete mode 100644 cmd/run/services_info.go delete mode 100644 cmd/run/services_status.go delete mode 100644 cmd/run/ui.go diff --git a/cmd/config/init/keys.go b/cmd/config/init/keys.go index c41fbda2..61292074 100644 --- a/cmd/config/init/keys.go +++ b/cmd/config/init/keys.go @@ -64,7 +64,7 @@ func getSequencerKeysConfig(rollappConfig config.RollappConfig) []utils.KeyConfi } } -func getRelayerKeysConfig(rollappConfig config.RollappConfig) map[string]utils.KeyConfig { +func GetRelayerKeysConfig(rollappConfig config.RollappConfig) map[string]utils.KeyConfig { return map[string]utils.KeyConfig{ consts.KeysIds.RollappRelayer: { Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer), @@ -101,7 +101,7 @@ func CreateAddressBinary( func GetRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, error) { pterm.Info.Println("getting relayer keys") relayerAddresses := make([]utils.KeyInfo, 0) - keys := getRelayerKeysConfig(rollappConfig) + keys := GetRelayerKeysConfig(rollappConfig) showRollappKeyCmd := getShowRlyKeyCmd( keys[consts.KeysIds.RollappRelayer], @@ -142,38 +142,48 @@ func GetRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, error) return relayerAddresses, nil } +func AddRlyKey(kc utils.KeyConfig, chainID string) (*utils.KeyInfo, error) { + addKeyCmd := getAddRlyKeyCmd( + kc, + chainID, + ) + + fmt.Println(addKeyCmd.String()) + + out, err := bash.ExecCommandWithStdout(addKeyCmd) + if err != nil { + return nil, err + } + + ki, err := utils.ParseAddressFromOutput(out) + if err != nil { + return nil, err + } + ki.Name = kc.ID + + return ki, nil +} + func GenerateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, error) { pterm.Info.Println("creating relayer keys") - relayerAddresses := make([]utils.KeyInfo, 0) - keys := getRelayerKeysConfig(rollappConfig) + var relayerAddresses []utils.KeyInfo + keys := GetRelayerKeysConfig(rollappConfig) - createRollappKeyCmd := getAddRlyKeyCmd( + pterm.Info.Println("creating relayer rollapp key") + relayerRollappAddress, err := AddRlyKey( keys[consts.KeysIds.RollappRelayer], rollappConfig.RollappID, ) - createHubKeyCmd := getAddRlyKeyCmd(keys[consts.KeysIds.HubRelayer], rollappConfig.HubData.ID) - - pterm.Info.Println("creating relayer rollapp key") - out, err := bash.ExecCommandWithStdout(createRollappKeyCmd) - if err != nil { - return nil, err - } - relayerRollappAddress, err := utils.ParseAddressFromOutput(out) - relayerRollappAddress.Name = consts.KeysIds.RollappRelayer if err != nil { return nil, err } + relayerAddresses = append( relayerAddresses, *relayerRollappAddress, ) pterm.Info.Println("creating relayer hub key") - out, err = bash.ExecCommandWithStdout(createHubKeyCmd) - if err != nil { - return nil, err - } - relayerHubAddress, err := utils.ParseAddressFromOutput(out) - relayerHubAddress.Name = consts.KeysIds.HubRelayer + relayerHubAddress, err := AddRlyKey(keys[consts.KeysIds.HubRelayer], rollappConfig.HubData.ID) if err != nil { return nil, err } diff --git a/cmd/config/init/relayer.go b/cmd/config/init/relayer.go index a86fe8e6..f6ec417f 100644 --- a/cmd/config/init/relayer.go +++ b/cmd/config/init/relayer.go @@ -6,8 +6,6 @@ import ( "os/exec" "path/filepath" - "github.com/pterm/pterm" - "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/relayer" "github.com/dymensionxyz/roller/utils/config" @@ -139,8 +137,6 @@ func InitializeRelayerConfig( hubConfig relayer.ChainConfig, initConfig config.RollappConfig, ) error { - pterm.Info.Println("initializing relayer config") - relayerHome := filepath.Join(initConfig.Home, consts.ConfigDirName.Relayer) if err := initRelayer(relayerHome); err != nil { diff --git a/cmd/relayer/run/run.go b/cmd/relayer/run/run.go index 61e3c4ba..575a4ea0 100644 --- a/cmd/relayer/run/run.go +++ b/cmd/relayer/run/run.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "math/big" "os" "path/filepath" "strconv" @@ -12,7 +11,7 @@ import ( comettypes "github.com/cometbft/cometbft/types" "github.com/pterm/pterm" "github.com/spf13/cobra" - yaml "gopkg.in/yaml.v3" + "gopkg.in/yaml.v3" initconfig "github.com/dymensionxyz/roller/cmd/config/init" "github.com/dymensionxyz/roller/cmd/consts" @@ -21,7 +20,7 @@ import ( "github.com/dymensionxyz/roller/sequencer" globalutils "github.com/dymensionxyz/roller/utils" "github.com/dymensionxyz/roller/utils/bash" - config2 "github.com/dymensionxyz/roller/utils/config" + configutils "github.com/dymensionxyz/roller/utils/config" "github.com/dymensionxyz/roller/utils/config/tomlconfig" "github.com/dymensionxyz/roller/utils/config/yamlconfig" dymintutils "github.com/dymensionxyz/roller/utils/dymint" @@ -31,11 +30,6 @@ import ( ) // TODO: Test relaying on 35-C and update the prices -var ( - oneDayRelayPriceHub = big.NewInt(1) - oneDayRelayPriceRollapp = big.NewInt(1) -) - const ( flagOverride = "override" ) @@ -118,30 +112,26 @@ func Cmd() *cobra.Command { if !isRelayerInitialized || shouldOverwrite { // preflight checks - for { - blockInformation, err := rollapputils.GetCurrentHeight() - if err != nil { - pterm.Error.Printf("failed to get current block height: %v\n", err) - return - } - currentHeight, err := strconv.Atoi( - blockInformation.Block.Header.Height, - ) + blockInformation, err := rollapputils.GetCurrentHeight() + if err != nil { + pterm.Error.Printf("failed to get current block height: %v\n", err) + return + } + currentHeight, err := strconv.Atoi( + blockInformation.Block.Header.Height, + ) + if err != nil { + pterm.Error.Printf("failed to get current block height: %v\n", err) + return + } + + if currentHeight <= 2 { + pterm.Warning.Println("current height is too low, updating dymint config") + err = dymintutils.UpdateDymintConfigForIBC(home, "5s", false) if err != nil { - pterm.Error.Printf("failed to get current block height: %v\n", err) + pterm.Error.Println("failed to update dymint config") return } - - if currentHeight <= 2 { - pterm.Warning.Println("current height is too low, updating dymint config") - err = dymintutils.UpdateDymintConfigForIBC(home, "5s", false) - if err != nil { - pterm.Error.Println("failed to update dymint config") - return - } - } else { - break - } } rollappPrefix := rollappChainData.Bech32Prefix @@ -174,6 +164,27 @@ func Cmd() *cobra.Command { return } + keys, err := initconfig.GenerateRelayerKeys(rollappConfig) + if err != nil { + pterm.Error.Printf("failed to create relayer keys: %v\n", err) + return + } + + for _, key := range keys { + key.Print(utils.WithMnemonic(), utils.WithName()) + } + + pterm.Info.Println("please fund the keys below with 20 respectively: ") + for _, k := range keys { + k.Print(utils.WithName()) + } + interactiveContinue, _ := pterm.DefaultInteractiveConfirm.WithDefaultText( + "Press enter when the keys are funded: ", + ).WithDefaultValue(true).Show() + if !interactiveContinue { + return + } + pterm.Info.Println("updating application relayer config") path := filepath.Join(relayerHome, "config") data, err := os.ReadFile(filepath.Join(path, "config.yaml")) @@ -235,6 +246,56 @@ func Cmd() *cobra.Command { } } + if isRelayerInitialized && !shouldOverwrite { + pterm.Info.Println("ensuring relayer keys are present") + kc := initconfig.GetRelayerKeysConfig(rollappConfig) + + for k, v := range kc { + pterm.Info.Printf("checking %s\n", k) + + switch v.ID { + case consts.KeysIds.RollappRelayer: + chainId := rollappConfig.RollappID + isPresent, err := utils.IsRlyAddressWithNameInKeyring(v, chainId) + if err != nil { + pterm.Error.Printf("failed to check address: %v\n", err) + return + } + + if !isPresent { + key, err := initconfig.AddRlyKey(v, rollappConfig.RollappID) + if err != nil { + pterm.Error.Printf("failed to add key: %v\n", err) + } + + key.Print(utils.WithMnemonic(), utils.WithName()) + } + case consts.KeysIds.HubRelayer: + chainId := rollappConfig.HubData.ID + isPresent, err := utils.IsRlyAddressWithNameInKeyring(v, chainId) + if err != nil { + pterm.Error.Printf("failed to check address: %v\n", err) + return + } + if !isPresent { + key, err := initconfig.AddRlyKey(v, rollappConfig.HubData.ID) + if err != nil { + pterm.Error.Printf("failed to add key: %v\n", err) + } + + key.Print(utils.WithMnemonic(), utils.WithName()) + } + default: + pterm.Error.Println("invalid key name", err) + return + } + } + } + + err = verifyRelayerBalances(rollappConfig) + if err != nil { + return + } rly := relayer.NewRelayer( rollappConfig.Home, rollappConfig.RollappID, @@ -285,30 +346,8 @@ func Cmd() *cobra.Command { // TODO: look up relayer keys if createIbcChannels || shouldOverwrite { - if shouldOverwrite { - keys, err := initconfig.GenerateRelayerKeys(rollappConfig) - if err != nil { - pterm.Error.Printf("failed to create relayer keys: %v\n", err) - return - } - - for _, key := range keys { - key.Print(utils.WithMnemonic(), utils.WithName()) - } - - pterm.Info.Println("please fund the keys below with X respectively: ") - for _, k := range keys { - k.Print(utils.WithName()) - } - interactiveContinue, _ := pterm.DefaultInteractiveConfirm.WithDefaultText( - "Press enter when the keys are funded: ", - ).WithDefaultValue(true).Show() - if !interactiveContinue { - return - } - } - err = VerifyRelayerBalances(rollappConfig) + err = verifyRelayerBalances(rollappConfig) if err != nil { pterm.Error.Printf("failed to verify relayer balances: %v\n", err) return @@ -361,8 +400,8 @@ func Cmd() *cobra.Command { return relayerStartCmd } -func VerifyRelayerBalances(rolCfg config2.RollappConfig) error { - insufficientBalances, err := GetRelayerInsufficientBalances(rolCfg) +func verifyRelayerBalances(rolCfg configutils.RollappConfig) error { + insufficientBalances, err := relayer.GetRelayerInsufficientBalances(rolCfg) if err != nil { return err } @@ -370,69 +409,3 @@ func VerifyRelayerBalances(rolCfg config2.RollappConfig) error { return nil } - -func GetRlyHubInsufficientBalances( - config config2.RollappConfig, -) ([]utils.NotFundedAddressData, error) { - HubRlyAddr, err := utils.GetRelayerAddress(config.Home, config.HubData.ID) - if err != nil { - pterm.Error.Printf("failed to get relayer address: %v", err) - return nil, err - } - - HubRlyBalance, err := utils.QueryBalance( - utils.ChainQueryConfig{ - RPC: config.HubData.RPC_URL, - Denom: consts.Denoms.Hub, - Binary: consts.Executables.Dymension, - }, HubRlyAddr, - ) - if err != nil { - pterm.Error.Printf("failed to query %s balances: %v", HubRlyAddr, err) - return nil, err - } - - insufficientBalances := make([]utils.NotFundedAddressData, 0) - if HubRlyBalance.Amount.Cmp(oneDayRelayPriceHub) < 0 { - insufficientBalances = append( - insufficientBalances, utils.NotFundedAddressData{ - KeyName: consts.KeysIds.HubRelayer, - Address: HubRlyAddr, - CurrentBalance: HubRlyBalance.Amount, - RequiredBalance: oneDayRelayPriceHub, - Denom: consts.Denoms.Hub, - Network: config.HubData.ID, - }, - ) - } - return insufficientBalances, nil -} - -func GetRelayerInsufficientBalances( - config config2.RollappConfig, -) ([]utils.NotFundedAddressData, error) { - insufficientBalances, err := GetRlyHubInsufficientBalances(config) - if err != nil { - return insufficientBalances, err - } - - rolRlyData, err := relayer.GetRolRlyAccData(config) - if err != nil { - return insufficientBalances, err - } - - if rolRlyData.Balance.Amount.Cmp(oneDayRelayPriceRollapp) < 0 { - insufficientBalances = append( - insufficientBalances, utils.NotFundedAddressData{ - KeyName: consts.KeysIds.RollappRelayer, - Address: rolRlyData.Address, - CurrentBalance: rolRlyData.Balance.Amount, - RequiredBalance: oneDayRelayPriceRollapp, - Denom: config.Denom, - Network: config.RollappID, - }, - ) - } - - return insufficientBalances, nil -} diff --git a/cmd/relayer/start/start.go b/cmd/relayer/start/start.go index 243dfb3d..afc1bed0 100644 --- a/cmd/relayer/start/start.go +++ b/cmd/relayer/start/start.go @@ -3,12 +3,10 @@ package start import ( "context" "fmt" - "math/big" "github.com/pterm/pterm" "github.com/spf13/cobra" - "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/relayer" "github.com/dymensionxyz/roller/utils/bash" @@ -18,10 +16,6 @@ import ( ) // TODO: Test relaying on 35-C and update the prices -var ( - oneDayRelayPriceHub = big.NewInt(1) - oneDayRelayPriceRollapp = big.NewInt(1) -) const ( flagOverride = "override" @@ -101,66 +95,7 @@ Consider using 'services' if you want to run a 'systemd' service instead. } func VerifyRelayerBalances(rolCfg config.RollappConfig) { - insufficientBalances, err := GetRelayerInsufficientBalances(rolCfg) + insufficientBalances, err := relayer.GetRelayerInsufficientBalances(rolCfg) errorhandling.PrettifyErrorIfExists(err) utils.PrintInsufficientBalancesIfAny(insufficientBalances) } - -func GetRlyHubInsufficientBalances( - config config.RollappConfig, -) ([]utils.NotFundedAddressData, error) { - HubRlyAddr, err := utils.GetRelayerAddress(config.Home, config.HubData.ID) - if err != nil { - return nil, err - } - HubRlyBalance, err := utils.QueryBalance( - utils.ChainQueryConfig{ - RPC: config.HubData.RPC_URL, - Denom: consts.Denoms.Hub, - Binary: consts.Executables.Dymension, - }, HubRlyAddr, - ) - if err != nil { - return nil, err - } - insufficientBalances := make([]utils.NotFundedAddressData, 0) - if HubRlyBalance.Amount.Cmp(oneDayRelayPriceHub) < 0 { - insufficientBalances = append( - insufficientBalances, utils.NotFundedAddressData{ - KeyName: consts.KeysIds.HubRelayer, - Address: HubRlyAddr, - CurrentBalance: HubRlyBalance.Amount, - RequiredBalance: oneDayRelayPriceHub, - Denom: consts.Denoms.Hub, - Network: config.HubData.ID, - }, - ) - } - return insufficientBalances, nil -} - -func GetRelayerInsufficientBalances( - config config.RollappConfig, -) ([]utils.NotFundedAddressData, error) { - insufficientBalances, err := GetRlyHubInsufficientBalances(config) - if err != nil { - return insufficientBalances, err - } - rolRlyData, err := relayer.GetRolRlyAccData(config) - if err != nil { - return insufficientBalances, err - } - if rolRlyData.Balance.Amount.Cmp(oneDayRelayPriceRollapp) < 0 { - insufficientBalances = append( - insufficientBalances, utils.NotFundedAddressData{ - KeyName: consts.KeysIds.RollappRelayer, - Address: rolRlyData.Address, - CurrentBalance: rolRlyData.Balance.Amount, - RequiredBalance: oneDayRelayPriceRollapp, - Denom: config.Denom, - Network: config.RollappID, - }, - ) - } - return insufficientBalances, nil -} diff --git a/cmd/rollapp/start/start.go b/cmd/rollapp/start/start.go index 622ba551..9d8843b1 100644 --- a/cmd/rollapp/start/start.go +++ b/cmd/rollapp/start/start.go @@ -10,9 +10,6 @@ import ( "runtime" "strings" - "github.com/pterm/pterm" - "github.com/spf13/cobra" - initconfig "github.com/dymensionxyz/roller/cmd/config/init" "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" @@ -23,6 +20,8 @@ import ( "github.com/dymensionxyz/roller/utils/config" "github.com/dymensionxyz/roller/utils/config/tomlconfig" "github.com/dymensionxyz/roller/utils/errorhandling" + "github.com/pterm/pterm" + "github.com/spf13/cobra" ) // var OneDaySequencePrice = big.NewInt(1) @@ -63,6 +62,7 @@ Consider using 'services' if you want to run a 'systemd' service instead. LogPath = filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp, "rollapp.log") RollappDirPath = filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp) + fmt.Println(startRollappCmd.String()) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go bash.RunCmdAsync( diff --git a/cmd/run/run.go b/cmd/run/run.go deleted file mode 100644 index 5d678a75..00000000 --- a/cmd/run/run.go +++ /dev/null @@ -1,155 +0,0 @@ -package run - -import ( - "context" - "os" - "os/exec" - "sync" - - "github.com/spf13/cobra" - - relayerrun "github.com/dymensionxyz/roller/cmd/relayer/run" - rollapprun "github.com/dymensionxyz/roller/cmd/rollapp/run" - "github.com/dymensionxyz/roller/cmd/utils" - datalayer "github.com/dymensionxyz/roller/data_layer" - "github.com/dymensionxyz/roller/relayer" - "github.com/dymensionxyz/roller/sequencer" - "github.com/dymensionxyz/roller/utils/config" - "github.com/dymensionxyz/roller/utils/config/tomlconfig" - "github.com/dymensionxyz/roller/utils/errorhandling" - servicemanager "github.com/dymensionxyz/roller/utils/service_manager" -) - -var flagNames = struct { - NoOutput string -}{ - NoOutput: "no-output", -} - -func Cmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "run", - Short: "Runs the rollapp on the local machine.", - Run: func(cmd *cobra.Command, args []string) { - home := cmd.Flag(utils.FlagNames.Home).Value.String() - rollappConfig, err := tomlconfig.LoadRollerConfig(home) - errorhandling.PrettifyErrorIfExists(err) - errorhandling.RequireMigrateIfNeeded(rollappConfig) - logger := utils.GetRollerLogger(rollappConfig.Home) - - ctx, cancel := context.WithCancel(context.Background()) - waitingGroup := sync.WaitGroup{} - serviceConfig := &servicemanager.ServiceConfig{ - Logger: logger, - Context: ctx, - WaitGroup: &waitingGroup, - } - /* ----------------------------- verify balances ---------------------------- */ - verifyBalances(rollappConfig) - - /* ------------------------------ run processes ----------------------------- */ - seq := sequencer.GetInstance(rollappConfig) - errorhandling.PrettifyErrorIfExists(err) - runSequencerWithRestarts(seq, serviceConfig) - runDaWithRestarts(rollappConfig, serviceConfig) - runRelayerWithRestarts(rollappConfig, serviceConfig) - - /* ------------------------------ render output ----------------------------- */ - noOutput, err := cmd.Flags().GetBool(flagNames.NoOutput) - errorhandling.PrettifyErrorIfExists(err) - if noOutput { - select {} - } else { - RenderUI(rollappConfig, serviceConfig) - } - cancel() - spin := utils.GetLoadingSpinner() - spin.Suffix = " Stopping rollapp services, please wait..." - spin.Start() - errorhandling.RunOnInterrupt(spin.Stop) - waitingGroup.Wait() - spin.Stop() - }, - } - cmd.Flags().BoolP(flagNames.NoOutput, "", false, "Run the rollapp without rendering the UI.") - return cmd -} - -func runRelayerWithRestarts( - cfg config.RollappConfig, - serviceConfig *servicemanager.ServiceConfig, -) { - startRelayerCmd := getStartRelayerCmd(cfg) - - rly := relayer.NewRelayer(cfg.Home, cfg.RollappID, cfg.HubData.ID) - - service := servicemanager.Service{ - Command: startRelayerCmd, - FetchFn: relayer.GetRelayerAccountsData, - UIData: servicemanager.UIData{Name: "Relayer"}, - StatusFn: rly.GetRelayerStatus, - } - serviceConfig.AddService("Relayer", service) - serviceConfig.RunServiceWithRestart("Relayer") -} - -func getStartRelayerCmd(config config.RollappConfig) *exec.Cmd { - ex, err := os.Executable() - if err != nil { - panic(err) - } - return exec.Command(ex, "relayer", "start", "--home", config.Home) -} - -func runDaWithRestarts( - rollappConfig config.RollappConfig, - serviceConfig *servicemanager.ServiceConfig, -) { - damanager := datalayer.NewDAManager(rollappConfig.DA.Backend, rollappConfig.Home) - damanager.SetRPCEndpoint(rollappConfig.DA.RpcUrl) - daLogFilePath := utils.GetDALogFilePath(rollappConfig.Home) - service := servicemanager.Service{ - Command: damanager.GetStartDACmd(), - FetchFn: damanager.GetDAAccData, - StatusFn: damanager.GetStatus, - UIData: servicemanager.UIData{Name: "DA Light Client"}, - } - serviceConfig.AddService("DA Light Client", service) - serviceConfig.RunServiceWithRestart("DA Light Client", utils.WithLogging(daLogFilePath)) -} - -func runSequencerWithRestarts( - seq *sequencer.Sequencer, - serviceConfig *servicemanager.ServiceConfig, -) { - startRollappCmd := seq.GetStartCmd() - - service := servicemanager.Service{ - Command: startRollappCmd, - FetchFn: utils.GetSequencerData, - StatusFn: seq.GetSequencerStatus, - UIData: servicemanager.UIData{Name: "Sequencer"}, - } - serviceConfig.AddService("Sequencer", service) - serviceConfig.RunServiceWithRestart( - "Sequencer", - utils.WithLogging(utils.GetSequencerLogPath(seq.RlpCfg)), - ) -} - -func verifyBalances(rollappConfig config.RollappConfig) { - damanager := datalayer.NewDAManager(rollappConfig.DA.Backend, rollappConfig.Home) - insufficientBalances, err := damanager.CheckDABalance() - errorhandling.PrettifyErrorIfExists(err) - - sequencerInsufficientBalances, err := utils.GetSequencerInsufficientAddrs( - rollappConfig, rollapprun.OneDaySequencePrice, - ) - errorhandling.PrettifyErrorIfExists(err) - insufficientBalances = append(insufficientBalances, sequencerInsufficientBalances...) - - rlyAddrs, err := relayerrun.GetRlyHubInsufficientBalances(rollappConfig) - errorhandling.PrettifyErrorIfExists(err) - insufficientBalances = append(insufficientBalances, rlyAddrs...) - utils.PrintInsufficientBalancesIfAny(insufficientBalances) -} diff --git a/cmd/run/services_info.go b/cmd/run/services_info.go deleted file mode 100644 index d59d8589..00000000 --- a/cmd/run/services_info.go +++ /dev/null @@ -1,45 +0,0 @@ -package run - -import ( - "fmt" - "strings" - - "github.com/gizak/termui/v3" - "github.com/gizak/termui/v3/widgets" - - "github.com/dymensionxyz/roller/cmd/utils" - datalayer "github.com/dymensionxyz/roller/data_layer" - "github.com/dymensionxyz/roller/sequencer" - "github.com/dymensionxyz/roller/utils/config" -) - -func NewServicesInfoTable(rollappConfig config.RollappConfig, termWidth int) *widgets.Table { - table := widgets.NewTable() - table.RowStyles[0] = termui.NewStyle(termui.ColorWhite, termui.ColorClear, termui.ModifierBold) - table.SetRect(0, 13, termWidth, 22) - table.Title = "Services Info" - table.FillRow = true - table.ColumnWidths = []int{termWidth / 6, termWidth / 2, termWidth / 3} - seq := sequencer.GetInstance(rollappConfig) - table.Rows = [][]string{ - {"Name", "Log File", "Ports"}, - { - "Sequencer", - utils.GetSequencerLogPath(rollappConfig), - fmt.Sprintf("%v, %v, %v", seq.RPCPort, seq.JsonRPCPort, seq.APIPort), - }, - {"Relayer", utils.GetRelayerLogPath(rollappConfig), ""}, - } - - damanager := datalayer.NewDAManager(rollappConfig.DA.Backend, rollappConfig.Home) - lcEndPoint := damanager.GetLightNodeEndpoint() - if lcEndPoint != "" { - parts := strings.Split(lcEndPoint, ":") - port := parts[len(parts)-1] - table.Rows = append( - table.Rows, - []string{"DA Light Client", utils.GetDALogFilePath(rollappConfig.Home), port}, - ) - } - return table -} diff --git a/cmd/run/services_status.go b/cmd/run/services_status.go deleted file mode 100644 index 2579fe11..00000000 --- a/cmd/run/services_status.go +++ /dev/null @@ -1,68 +0,0 @@ -package run - -import ( - "log" - "path/filepath" - "time" - - ui "github.com/gizak/termui/v3" - "github.com/gizak/termui/v3/widgets" - - "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/utils/config" - servicemanager "github.com/dymensionxyz/roller/utils/service_manager" -) - -func RenderUI(rollappConfig config.RollappConfig, manager *servicemanager.ServiceConfig) { - logger := utils.GetLogger(filepath.Join(rollappConfig.Home, "roller.log")) - initializeUI() - defer ui.Close() - - termWidth, _ := ui.TerminalDimensions() - - p := buildTitleParagraph(rollappConfig, termWidth) - servicesStatusTable := NewServiceStatusTable(termWidth) - servicesInfoTable := NewServicesInfoTable(rollappConfig, termWidth) - - manager.InitServicesData(rollappConfig) - updateUITable(manager.GetUIData(), servicesStatusTable, rollappConfig) - ui.Render(p, servicesStatusTable, servicesInfoTable) - - // 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, manager, config) -} - -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 == "" { - return - } - case <-ticker: - manager.Logger.Println("Fetching service data...") - manager.FetchServicesData(config.rollappConfig) - updateUITable(manager.GetUIData(), config.table, config.rollappConfig) - ui.Render(config.table) - } - } -} - -type ServiceStatusConfig struct { - rollappConfig config.RollappConfig - logger *log.Logger - table *widgets.Table -} diff --git a/cmd/run/ui.go b/cmd/run/ui.go deleted file mode 100644 index 03e1c364..00000000 --- a/cmd/run/ui.go +++ /dev/null @@ -1,63 +0,0 @@ -package run - -import ( - "fmt" - "log" - "sort" - "strings" - - termui "github.com/gizak/termui/v3" - "github.com/gizak/termui/v3/widgets" - - "github.com/dymensionxyz/roller/utils/config" - servicemanager "github.com/dymensionxyz/roller/utils/service_manager" -) - -func initializeUI() { - if err := termui.Init(); err != nil { - log.Fatalf("failed to initialize termui: %v", err) - } -} - -func buildTitleParagraph(rollappConfig config.RollappConfig, termWidth int) *widgets.Paragraph { - p := widgets.NewParagraph() - p.Text = fmt.Sprintf( - "💈 Rollapp '%s' is successfully running on your local machine, connected to Dymension hub '%s'.", - rollappConfig.RollappID, - rollappConfig.HubData.ID, - ) - p.SetRect(0, 0, termWidth, 3) - return p -} - -func NewServiceStatusTable(termWidth int) *widgets.Table { - table := widgets.NewTable() - table.RowStyles[0] = termui.NewStyle(termui.ColorWhite, termui.ColorClear, termui.ModifierBold) - table.SetRect(0, 3, termWidth, 12) - table.Title = "Services Status" - return table -} - -func updateUITable( - serviceData []servicemanager.UIData, - table *widgets.Table, - cfg config.RollappConfig, -) { - table.Rows = [][]string{{"Name", "Balance", "Status"}} - sort.Slice( - serviceData, func(i, j int) bool { - return serviceData[i].Name < serviceData[j].Name - }, - ) - for _, service := range serviceData { - balances := []string{} - for _, account := range service.Accounts { - balances = append(balances, account.Balance.BiggerDenomStr(cfg)) - } - - table.Rows = append( - table.Rows, - []string{service.Name, strings.Join(balances, ","), service.Status}, - ) - } -} diff --git a/cmd/utils/key_info.go b/cmd/utils/key_info.go index b71f1a41..260e3513 100644 --- a/cmd/utils/key_info.go +++ b/cmd/utils/key_info.go @@ -11,6 +11,7 @@ import ( "github.com/pterm/pterm" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/utils/bash" ) @@ -115,11 +116,17 @@ func PrintAddressesWithTitle(addresses []KeyInfo) { } } -func IsAddressWithNameInKeyring(info KeyConfig, home string) (bool, error) { +// TODO: refactor customkeyringdir into options? +func IsAddressWithNameInKeyring( + info KeyConfig, + home string, +) (bool, error) { + keyringDir := filepath.Join(home, info.Dir) + cmd := exec.Command( info.ChainBinary, "keys", "list", "--output", "json", - "--keyring-backend", "test", "--keyring-dir", filepath.Join(home, info.Dir), + "--keyring-backend", "test", "--keyring-dir", keyringDir, ) var ki []KeyInfo @@ -128,6 +135,8 @@ func IsAddressWithNameInKeyring(info KeyConfig, home string) (bool, error) { return false, err } + fmt.Println(out.String()) + err = json.Unmarshal(out.Bytes(), &ki) if err != nil { return false, err @@ -144,6 +153,25 @@ func IsAddressWithNameInKeyring(info KeyConfig, home string) (bool, error) { ), nil } +func IsRlyAddressWithNameInKeyring( + info KeyConfig, + chainId string, +) (bool, error) { + cmd := exec.Command( + consts.Executables.Relayer, + "keys", "list", chainId, "--home", info.Dir, + ) + fmt.Println(cmd.String()) + + out, err := bash.ExecCommandWithStdout(cmd) + if err != nil { + return false, err + } + lookFor := fmt.Sprintf("no keys found for chain %s", chainId) + + return !strings.Contains(out.String(), lookFor), nil +} + // TODO: remove this struct as it's redundant to KeyInfo type SecretAddressData struct { AddressData diff --git a/cmd/utils/keys.go b/cmd/utils/keys.go index 45f18067..bb2c3479 100644 --- a/cmd/utils/keys.go +++ b/cmd/utils/keys.go @@ -6,13 +6,13 @@ import ( "path/filepath" "strings" - "github.com/pterm/pterm" - "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/utils/bash" "github.com/dymensionxyz/roller/utils/config" + "github.com/pterm/pterm" ) +// TODO: add the keyring to use for the key // KeyConfig struct store information about a wallet // Dir refers to the keyringDir where the key is created type KeyConfig struct { diff --git a/relayer/query.go b/relayer/query.go index 94d045c7..09a558ab 100644 --- a/relayer/query.go +++ b/relayer/query.go @@ -1,13 +1,17 @@ package relayer import ( + "math/big" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/sequencer" "github.com/dymensionxyz/roller/utils/config" ) -func GetRolRlyAccData(cfg config.RollappConfig) (*utils.AccountData, error) { +var oneDayRelayPrice = big.NewInt(1) + +func getRolRlyAccData(cfg config.RollappConfig) (*utils.AccountData, error) { RollappRlyAddr, err := utils.GetRelayerAddress(cfg.Home, cfg.RollappID) seq := sequencer.GetInstance(cfg) if err != nil { @@ -31,16 +35,39 @@ func GetRolRlyAccData(cfg config.RollappConfig) (*utils.AccountData, error) { }, nil } +func getHubRlyAccData(cfg config.RollappConfig) (*utils.AccountData, error) { + HubRlyAddr, err := utils.GetRelayerAddress(cfg.Home, cfg.HubData.ID) + if err != nil { + return nil, err + } + + HubRlyBalance, err := utils.QueryBalance( + utils.ChainQueryConfig{ + RPC: cfg.HubData.RPC_URL, + Denom: consts.Denoms.Hub, + Binary: consts.Executables.Dymension, + }, HubRlyAddr, + ) + if err != nil { + return nil, err + } + + return &utils.AccountData{ + Address: HubRlyAddr, + Balance: HubRlyBalance, + }, nil +} + func GetRelayerAccountsData(cfg config.RollappConfig) ([]utils.AccountData, error) { data := []utils.AccountData{} - rollappRlyAcc, err := GetRolRlyAccData(cfg) + rollappRlyAcc, err := getRolRlyAccData(cfg) if err != nil { return nil, err } data = append(data, *rollappRlyAcc) - hubRlyAcc, err := GetHubRlyAccData(cfg) + hubRlyAcc, err := getHubRlyAccData(cfg) if err != nil { return nil, err } @@ -49,25 +76,30 @@ func GetRelayerAccountsData(cfg config.RollappConfig) ([]utils.AccountData, erro return data, nil } -func GetHubRlyAccData(cfg config.RollappConfig) (*utils.AccountData, error) { - HubRlyAddr, err := utils.GetRelayerAddress(cfg.Home, cfg.HubData.ID) +func GetRelayerInsufficientBalances( + config config.RollappConfig, +) ([]utils.NotFundedAddressData, error) { + var insufficientBalances []utils.NotFundedAddressData + + accData, err := GetRelayerAccountsData(config) if err != nil { return nil, err } - HubRlyBalance, err := utils.QueryBalance( - utils.ChainQueryConfig{ - RPC: cfg.HubData.RPC_URL, - Denom: consts.Denoms.Hub, - Binary: consts.Executables.Dymension, - }, HubRlyAddr, - ) - if err != nil { - return nil, err + for _, acc := range accData { + if acc.Balance.Amount.Cmp(oneDayRelayPrice) < 0 { + insufficientBalances = append( + insufficientBalances, utils.NotFundedAddressData{ + KeyName: consts.KeysIds.RollappRelayer, + Address: acc.Address, + CurrentBalance: acc.Balance.Amount, + RequiredBalance: oneDayRelayPrice, + Denom: config.Denom, + Network: config.RollappID, + }, + ) + } } - return &utils.AccountData{ - Address: HubRlyAddr, - Balance: HubRlyBalance, - }, nil + return insufficientBalances, nil } diff --git a/utils/dymint/dymint.go b/utils/dymint/dymint.go index 6605801e..fa44c47e 100644 --- a/utils/dymint/dymint.go +++ b/utils/dymint/dymint.go @@ -15,6 +15,7 @@ import ( "github.com/dymensionxyz/roller/utils" "github.com/dymensionxyz/roller/utils/bash" "github.com/dymensionxyz/roller/utils/config/tomlconfig" + servicemanager "github.com/dymensionxyz/roller/utils/service_manager" ) // TODO: use dymint instead @@ -133,13 +134,19 @@ func UpdateDymintConfigForIBC(home string, t string, forceUpdate bool) error { ) time.Sleep(time.Second * 2) - waitForHealthyRollApp("http://localhost:26657") + waitForHealthyRollApp("http://localhost:26657/health") _, err = bash.ExecCommandWithStdout(cmd) if err != nil { return err } } else { pterm.Info.Println("block time settings already up to date") + pterm.Info.Println("restarting rollapp process to ensure correct block time is applied") + err = servicemanager.RestartSystemdService("rollapp") + if err != nil { + return err + } + waitForHealthyRollApp("http://localhost:26657/health") } return nil @@ -164,17 +171,20 @@ func waitForHealthyRollApp(url string) { fmt.Printf("Error making request: %v\n", err) continue } - // nolint:errcheck - defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { fmt.Printf("Error reading response body: %v\n", err) continue } + // nolint:errcheck + resp.Body.Close() var response RollappHealthResponse + err = json.Unmarshal(body, &response) + fmt.Println("response: ", string(body)) + if err != nil { fmt.Printf("Error unmarshaling JSON: %v\n", err) continue