Skip to content

Commit

Permalink
chore: Add prompt and output when changing DA layer (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial authored Aug 29, 2023
1 parent 98504f4 commit 5e5b21c
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 54 deletions.
23 changes: 0 additions & 23 deletions cmd/config/init/dir_not_empty.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package initconfig

import (
"fmt"
global_utils "github.com/dymensionxyz/roller/utils"
"os"

"github.com/dymensionxyz/roller/cmd/consts"
Expand Down Expand Up @@ -75,7 +76,7 @@ func runInit(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
isRootExist, err := dirNotEmpty(initConfig.Home)
isRootExist, err := global_utils.DirNotEmpty(initConfig.Home)
if err != nil {
return err
}
Expand Down
16 changes: 2 additions & 14 deletions cmd/config/init/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"github.com/manifoldco/promptui"
)

type OutputHandler struct {
Expand All @@ -24,24 +23,13 @@ func (o *OutputHandler) printInitOutput(rollappConfig config.RollappConfig, addr
fmt.Printf("💈 RollApp '%s' configuration files have been successfully generated on your local machine. Congratulations!\n\n", rollappId)
fmt.Println(FormatTokenSupplyLine(rollappConfig))
fmt.Println()
utils.PrintAddresses(formatAddresses(rollappConfig, addresses))
utils.PrintAddressesWithTitle(formatAddresses(rollappConfig, addresses))
fmt.Printf("\n🔔 Please fund these addresses to register and run the rollapp.\n")
}

func (o *OutputHandler) PromptOverwriteConfig(home string) (bool, error) {
if o.NoOutput {
return true, nil
}
prompt := promptui.Prompt{
Label: fmt.Sprintf("Directory %s is not empty. Do you want to overwrite", home),
IsConfirm: true,
}
_, err := prompt.Run()
if err != nil {
if err == promptui.ErrAbort {
return false, nil
}
return false, err
}
return true, nil
return utils.PromptBool(fmt.Sprintf("Directory %s is not empty. Do you want to overwrite", home))
}
41 changes: 35 additions & 6 deletions cmd/config/set/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package set
import (
"fmt"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
datalayer "github.com/dymensionxyz/roller/data_layer"
"github.com/dymensionxyz/roller/sequencer"
global_utils "github.com/dymensionxyz/roller/utils"
"os"
"path/filepath"
)
Expand All @@ -23,7 +25,19 @@ func setDA(rlpCfg config.RollappConfig, value string) error {
}

func updateDaConfig(rlpCfg config.RollappConfig, newDa config.DAType) error {
if err := cleanDADir(rlpCfg); err != nil {
daCfgDirPath := filepath.Join(rlpCfg.Home, consts.ConfigDirName.DALightNode)
dirExist, err := global_utils.DirNotEmpty(daCfgDirPath)
if err != nil {
return err
}
if dirExist {
if yes, err := utils.PromptBool("Changing DA will remove the old DA keys permanently. Are you sure you want to proceed"); err != nil {
return err
} else if !yes {
return nil
}
}
if err := os.RemoveAll(daCfgDirPath); err != nil {
return err
}
daManager := datalayer.NewDAManager(newDa, rlpCfg.Home)
Expand All @@ -34,9 +48,24 @@ func updateDaConfig(rlpCfg config.RollappConfig, newDa config.DAType) error {
if err := sequencer.UpdateDymintDAConfig(rlpCfg); err != nil {
return err
}
return config.WriteConfigToTOML(rlpCfg)
}

func cleanDADir(cfg config.RollappConfig) error {
return os.RemoveAll(filepath.Join(cfg.Home, consts.ConfigDirName.DALightNode))
if err := config.WriteConfigToTOML(rlpCfg); err != nil {
return err
}
fmt.Printf("💈 RollApp DA has been successfully set to '%s'\n\n", newDa)
if newDa != config.Local {
addresses := make([]utils.AddressData, 0)
damanager := datalayer.NewDAManager(newDa, rlpCfg.Home)
daAddress, err := damanager.GetDAAccountAddress()
if err != nil {
return err
}
addresses = append(addresses, utils.AddressData{
Name: damanager.GetKeyName(),
Addr: daAddress,
})
fmt.Printf("🔑 Address:\n\n")
utils.PrintAddresses(addresses)
fmt.Printf("\n🔔 Please fund this address to run the DA light client.\n")
}
return nil
}
2 changes: 1 addition & 1 deletion cmd/keys/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Cmd() *cobra.Command {
if outputType == "json" {
utils.PrettifyErrorIfExists(printAsJSON(addresses))
} else if outputType == "text" {
utils.PrintAddresses(addresses)
utils.PrintAddressesWithTitle(addresses)
}
},
}
Expand Down
13 changes: 12 additions & 1 deletion cmd/migrate/v_0_1_12.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package migrate
import (
"github.com/dymensionxyz/roller/config"
datalayer "github.com/dymensionxyz/roller/data_layer"
"github.com/dymensionxyz/roller/data_layer/avail"
"github.com/dymensionxyz/roller/sequencer"
"github.com/dymensionxyz/roller/utils"
"path/filepath"
)

type VersionMigratorV0112 struct{}
Expand All @@ -20,5 +22,14 @@ func (v *VersionMigratorV0112) PerformMigration(rlpCfg config.RollappConfig) err
if sequencerDaConfig == "" {
return nil
}
return utils.UpdateFieldInToml(dymintTomlPath, "da_config", sequencerDaConfig)
if err := utils.UpdateFieldInToml(dymintTomlPath, "da_config", sequencerDaConfig); err != nil {
return err
}
if rlpCfg.DA == config.Avail {
availNewCfgPath := avail.GetCfgFilePath(rlpCfg.Home)
if err := utils.MoveFile(filepath.Join(rlpCfg.Home, avail.ConfigFileName), availNewCfgPath); err != nil {
return err
}
}
return nil
}
5 changes: 4 additions & 1 deletion cmd/utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ type AddressData struct {
Addr string
}

func PrintAddresses(addresses []AddressData) {
func PrintAddressesWithTitle(addresses []AddressData) {
fmt.Printf("🔑 Addresses:\n\n")
PrintAddresses(addresses)
}
func PrintAddresses(addresses []AddressData) {
data := make([][]string, 0, len(addresses))
for _, address := range addresses {
data = append(data, []string{address.Name, address.Addr})
Expand Down
18 changes: 17 additions & 1 deletion cmd/utils/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"errors"
"fmt"
"github.com/manifoldco/promptui"
"math/big"
"os"
"time"
Expand Down Expand Up @@ -39,7 +40,7 @@ func PrintInsufficientBalancesIfAny(addressesData []NotFundedAddressData, config
fmt.Println()
fmt.Println("💈 Please fund these addresses and try again.")
}
PrettifyErrorIfExists(errors.New("The following addresses have insufficient balance to perform this operation"),
PrettifyErrorIfExists(errors.New("the following addresses have insufficient balance to perform this operation"),
printAddresses)
}

Expand All @@ -61,6 +62,21 @@ type OutputHandler struct {
spinner *spinner.Spinner
}

func PromptBool(msg string) (bool, error) {
prompt := promptui.Prompt{
Label: msg,
IsConfirm: true,
}
_, err := prompt.Run()
if err != nil {
if err == promptui.ErrAbort {
return false, nil
}
return false, err
}
return true, nil
}

func NewOutputHandler(noOutput bool) *OutputHandler {
if noOutput {
return &OutputHandler{
Expand Down
10 changes: 4 additions & 6 deletions data_layer/avail/avail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package avail

import (
"fmt"
"math/big"
"os/exec"
"path/filepath"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"math/big"
"os/exec"

gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
Expand All @@ -18,7 +16,7 @@ import (
)

const (
availConfigFileName = "avail.toml"
ConfigFileName = "avail.toml"
mnemonicEntropySize = 256
keyringNetworkID uint8 = 42
DeafultRPCEndpoint = "wss://dymension-devnet.avail.tools/ws"
Expand All @@ -42,7 +40,7 @@ func (a *Avail) SetMetricsEndpoint(endpoint string) {
}

func NewAvail(root string) *Avail {
cfgPath := filepath.Join(root, availConfigFileName)
cfgPath := GetCfgFilePath(root)
availConfig, err := loadConfigFromTOML(cfgPath)
if err != nil {
entropySeed, err := bip39.NewEntropy(mnemonicEntropySize)
Expand Down
10 changes: 10 additions & 0 deletions data_layer/avail/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package avail

import (
"github.com/dymensionxyz/roller/cmd/consts"
"os"
"path/filepath"

"github.com/pelletier/go-toml"
)
Expand All @@ -11,6 +13,10 @@ func writeConfigToTOML(path string, c Avail) error {
if err != nil {
return err
}
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
err = os.WriteFile(path, tomlBytes, 0644)
if err != nil {
return err
Expand All @@ -32,3 +38,7 @@ func loadConfigFromTOML(path string) (Avail, error) {

return config, nil
}

func GetCfgFilePath(rollerHome string) string {
return filepath.Join(rollerHome, consts.ConfigDirName.DALightNode, ConfigFileName)
}
51 changes: 51 additions & 0 deletions utils/filesystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package utils

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

func DirNotEmpty(path string) (bool, error) {
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

if !info.IsDir() {
return false, fmt.Errorf("%s is not a directory", path)
}

files, err := os.ReadDir(path)
return len(files) > 0, err
}

func MoveFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open source file: %w", err)
}
defer func() { _ = srcFile.Close() }()
err = os.MkdirAll(filepath.Dir(dst), 0755)
if err != nil {
return fmt.Errorf("failed to create parent directories: %w", err)
}
dstFile, err := os.Create(dst)
if err != nil {
return fmt.Errorf("failed to create destination file: %w", err)
}
defer func() { _ = dstFile.Close() }()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return fmt.Errorf("failed to copy file: %w", err)
}
err = os.Remove(src)
if err != nil {
return fmt.Errorf("failed to delete source file: %w", err)
}
return nil
}

0 comments on commit 5e5b21c

Please sign in to comment.