-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add init flag support (#816)
- Loading branch information
1 parent
a64cd37
commit 34a8182
Showing
8 changed files
with
433 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,273 +1,83 @@ | ||
package initrollapp | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
"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" | ||
"github.com/dymensionxyz/roller/config" | ||
datalayer "github.com/dymensionxyz/roller/data_layer" | ||
global_utils "github.com/dymensionxyz/roller/utils" | ||
"github.com/dymensionxyz/roller/utils/archives" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "init", | ||
Use: "init [path-to-config-archive]", | ||
Short: "Inititlize RollApp locally", | ||
Long: ``, | ||
Args: cobra.MaximumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
pterm.DefaultHeader.WithFullWidth().Printfln("welcome to roller") | ||
err := initconfig.AddFlags(cmd) | ||
if err != nil { | ||
fmt.Println("failed to add flags") | ||
return | ||
} | ||
reader := bufio.NewReader(os.Stdin) | ||
|
||
fmt.Println("Do you already have rollapp config? (y/n)") | ||
resp, err := reader.ReadString('\n') | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
resp = strings.TrimSpace(resp) | ||
resp = strings.ToLower(resp) | ||
|
||
if resp == "n" || resp == "no" { | ||
fmt.Println( | ||
`To generate a RollApp configuration file go to <website> | ||
or run 'rollapp config' to expose the UI on localhost:11133. | ||
after configuration files are generated, rerun the 'init' command`, | ||
) | ||
return | ||
} | ||
|
||
if resp == "y" || resp == "yes" { | ||
fmt.Println( | ||
"provide a path to the configuration archive file downloaded from <website>", | ||
) | ||
fp, err := reader.ReadString('\n') | ||
if len(args) != 0 { | ||
archivePath, err := checkConfigArchive(args[0]) | ||
if err != nil { | ||
fmt.Printf("failed to get archive: %v\n", err) | ||
return | ||
} | ||
|
||
fp = strings.TrimSpace(fp) | ||
if fp == "" { | ||
fmt.Println("no path was provided") | ||
return | ||
} | ||
|
||
archivePath, err := expandHomePath(fp) | ||
err = runInit(cmd, WithConfig(strings.TrimSpace(archivePath))) | ||
if err != nil { | ||
fmt.Printf("failed to initialize the RollApp: %v\n", err) | ||
return | ||
} | ||
|
||
if _, err := os.Stat(archivePath); os.IsNotExist(err) { | ||
fmt.Printf("the file %s does not exist. \n", fp) | ||
return | ||
} | ||
return | ||
} | ||
|
||
err = runInit(cmd, args, archivePath) | ||
options := []string{"mock", "dymension"} | ||
backend, _ := pterm.DefaultInteractiveSelect.WithOptions(options).Show() | ||
isMockBackend := backend == "mock" | ||
|
||
if isMockBackend { | ||
err := runInit(cmd) | ||
if err != nil { | ||
fmt.Printf("failed to initialize the RollApp: %v\n", err) | ||
fmt.Println("failed to run init: ", err) | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
fmt.Println("invalid or no input") | ||
}, | ||
} | ||
hasConfig, _ := pterm.DefaultInteractiveConfirm.WithDefaultText( | ||
"do you have an existing configuration archive?", | ||
).Show() | ||
|
||
func expandHomePath(path string) (string, error) { | ||
if path[:2] == "~/" { | ||
usr, err := user.Current() | ||
if err != nil { | ||
return "", err | ||
if !hasConfig { | ||
fmt.Println( | ||
`To generate a RollApp configuration file go to <website> | ||
or run 'rollapp config' to expose the UI on localhost:11133. | ||
after configuration files are generated, rerun the 'init' command`, | ||
) | ||
return | ||
} | ||
path = filepath.Join(usr.HomeDir, path[2:]) | ||
} | ||
return path, nil | ||
} | ||
|
||
// in runInit I parse the entire genesis creator zip file twice to extract | ||
// the file this looks awful but since the archive has only 2 files it's | ||
// kinda fine | ||
func runInit(cmd *cobra.Command, args []string, configArchivePath string) error { | ||
home := utils.GetRollerRootDir() | ||
outputHandler := initconfig.NewOutputHandler(false) | ||
|
||
defer outputHandler.StopSpinner() | ||
fp, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("provide the configuration archive path"). | ||
Show() | ||
|
||
isRootExist, err := global_utils.DirNotEmpty(home) | ||
if err != nil { | ||
utils.PrettifyErrorIfExists(err) | ||
return err | ||
} | ||
|
||
if isRootExist { | ||
outputHandler.StopSpinner() | ||
shouldOverwrite, err := outputHandler.PromptOverwriteConfig(home) | ||
archivePath, err := checkConfigArchive(fp) | ||
if err != nil { | ||
utils.PrettifyErrorIfExists(err) | ||
return err | ||
} | ||
if shouldOverwrite { | ||
err = os.RemoveAll(home) | ||
if err != nil { | ||
utils.PrettifyErrorIfExists(err) | ||
return err | ||
} | ||
} else { | ||
os.Exit(0) | ||
fmt.Printf("failed to get archive: %v\n", err) | ||
return | ||
} | ||
} | ||
|
||
// nolint:gofumpt | ||
err = os.MkdirAll(home, 0755) | ||
if err != nil { | ||
utils.PrettifyErrorIfExists(err) | ||
return err | ||
} | ||
|
||
err = archives.ExtractFileFromNestedTar( | ||
configArchivePath, | ||
config.RollerConfigFileName, | ||
home, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
initConfigPtr, err := initconfig.GetInitConfig(cmd, args) | ||
if err != nil { | ||
utils.PrettifyErrorIfExists(err) | ||
return err | ||
} | ||
|
||
initConfig := *initConfigPtr | ||
|
||
utils.RunOnInterrupt(outputHandler.StopSpinner) | ||
outputHandler.StartSpinner(consts.SpinnerMsgs.UniqueIdVerification) | ||
err = initConfig.Validate() | ||
if err != nil { | ||
utils.PrettifyErrorIfExists(err) | ||
return err | ||
} | ||
|
||
// TODO: create all dirs here | ||
outputHandler.StartSpinner(" Initializing RollApp configuration files...") | ||
/* ---------------------------- Initialize relayer --------------------------- */ | ||
// 20240607 relayer will be handled using a separate, relayer command | ||
// rollerConfigFilePath := filepath.Join(utils.GetRollerRootDir(), "roller.toml") | ||
// rollappPrefix, err := global_utils.GetKeyFromTomlFile(rollerConfigFilePath, "bech32_prefix") | ||
// utils.PrettifyErrorIfExists(err) | ||
|
||
// err = initializeRelayerConfig(relayer.ChainConfig{ | ||
// ID: initConfig.RollappID, | ||
// RPC: consts.DefaultRollappRPC, | ||
// Denom: initConfig.Denom, | ||
// AddressPrefix: rollappPrefix, | ||
// GasPrices: "0", | ||
// }, relayer.ChainConfig{ | ||
// ID: initConfig.HubData.ID, | ||
// RPC: initConfig.HubData.ARCHIVE_RPC_URL, | ||
// Denom: consts.Denoms.Hub, | ||
// AddressPrefix: consts.AddressPrefixes.Hub, | ||
// GasPrices: initConfig.HubData.GAS_PRICE, | ||
// }, initConfig) | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
/* ------------------------------ Generate keys ----------------------------- */ | ||
addresses, err := initconfig.GenerateKeys(initConfig) | ||
if err != nil { | ||
utils.PrettifyErrorIfExists(err) | ||
return err | ||
} | ||
|
||
/* ------------------------ Initialize DA light node ------------------------ */ | ||
damanager := datalayer.NewDAManager(initConfig.DA, initConfig.Home) | ||
err = damanager.InitializeLightNodeConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
daAddress, err := damanager.GetDAAccountAddress() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if daAddress != "" { | ||
addresses = append(addresses, utils.AddressData{ | ||
Name: damanager.GetKeyName(), | ||
Addr: daAddress, | ||
}) | ||
} | ||
|
||
/* --------------------------- Initialize Rollapp -------------------------- */ | ||
err = initconfig.InitializeRollappConfig(initConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// genesis creator archive | ||
err = archives.ExtractFileFromNestedTar( | ||
configArchivePath, | ||
"genesis.json", | ||
filepath.Join(home, consts.ConfigDirName.Rollapp, "config"), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// adds the sequencer address to the whitelists | ||
err = initconfig.UpdateGenesisParams(home) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rollerConfigFilePath := filepath.Join(utils.GetRollerRootDir(), "roller.toml") | ||
err = global_utils.UpdateFieldInToml(rollerConfigFilePath, "home", utils.GetRollerRootDir()) | ||
if err != nil { | ||
fmt.Println("failed to add home to roller.toml: ", err) | ||
return err | ||
} | ||
|
||
// 20240607 genesis is generated using the genesis-creator | ||
// err = initializeRollappGenesis(initConfig) | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
// TODO: review, roller config is generated using genesis-creator | ||
// some of the config values should be moved there | ||
// err = config.WriteConfigToTOML(initConfig) | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
/* ------------------------------ Initialize Local Hub ---------------------------- */ | ||
// TODO: local hub is out of scope, implement as the last step | ||
// hub := cmd.Flag(FlagNames.HubID).Value.String() | ||
// if hub == consts.LocalHubName { | ||
// err := initLocalHub(initConfig) | ||
// utils.PrettifyErrorIfExists(err) | ||
// } | ||
|
||
/* ------------------------------ Print output ------------------------------ */ | ||
outputHandler.StopSpinner() | ||
outputHandler.PrintInitOutput(initConfig, addresses, initConfig.RollappID) | ||
|
||
return nil | ||
err = runInit(cmd, WithConfig(archivePath)) | ||
if err != nil { | ||
fmt.Printf("failed to initialize the RollApp: %v\n", err) | ||
return | ||
} | ||
}, | ||
} |
Oops, something went wrong.