Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implemented rerun handling logic for roller config init #31

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implemented idempotancy for roller config init
ItayLevyOfficial committed Jun 5, 2023
commit fac8ada6eef7d846971274efea1127ab00909b03
6 changes: 3 additions & 3 deletions cmd/config/init/consts.go
Original file line number Diff line number Diff line change
@@ -56,6 +56,6 @@ const evmCoinType uint32 = 60
const DefaultHubID = "35-C"
const KeysDirName = "keys"
const cosmosDefaultCointype uint32 = 118
const celestiaExecutablePath = "/usr/local/bin/roller/lib/celestia"
const defaultRollappBinaryPath = "/usr/local/bin/roller/lib/rollapp_evm"
const relayerExecutablePath = "/usr/local/bin/roller/lib/rly"
const celestiaExecutablePath = "/usr/local/bin/roller_bins/celestia"
const defaultRollappBinaryPath = "/usr/local/bin/roller_bins/rollapp_evm"
const relayerExecutablePath = "/usr/local/bin/roller_bins/rly"
7 changes: 7 additions & 0 deletions cmd/config/init/error_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package initconfig

func handleError(err error) {
ItayLevyOfficial marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}
}
47 changes: 47 additions & 0 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package initconfig

import (
"github.com/spf13/cobra"
)

func addFlags(cmd *cobra.Command) {
cmd.Flags().StringP(FlagNames.HubRPC, "", defaultHubRPC, "Dymension Hub rpc endpoint")
cmd.Flags().StringP(FlagNames.LightNodeEndpoint, "", "", "The data availability light node endpoint. Runs an Arabica Celestia light node if not provided")
ItayLevyOfficial marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().StringP(FlagNames.RollappBinary, "", "", "The rollapp binary. Should be passed only if you built a custom rollapp")
cmd.Flags().Uint64P(FlagNames.Decimals, "", 18, "The number of decimal places a rollapp token supports")
cmd.Flags().StringP(FlagNames.Home, "", getRollerRootDir(), "The directory of the roller config files")
}

func getDecimals(cmd *cobra.Command) uint64 {
decimals, err := cmd.Flags().GetUint64(FlagNames.Decimals)
if err != nil {
panic(err)
}
return decimals
}

func getRollappBinaryPath(cmd *cobra.Command) string {
rollappBinaryPath := cmd.Flag(FlagNames.RollappBinary).Value.String()
if rollappBinaryPath == "" {
return defaultRollappBinaryPath
}
return rollappBinaryPath
}

func getInitConfig(cmd *cobra.Command, args []string) InitConfig {
rollappId := args[0]
denom := args[1]
home := cmd.Flag(FlagNames.Home).Value.String()
createLightNode := !cmd.Flags().Changed(lightNodeEndpointFlag)
rollappBinaryPath := getRollappBinaryPath(cmd)
decimals := getDecimals(cmd)
return InitConfig{
Home: home,
RollappID: rollappId,
RollappBinary: rollappBinaryPath,
CreateDALightNode: createLightNode,
Denom: denom,
HubID: DefaultHubID,
Decimals: decimals,
}
}
107 changes: 21 additions & 86 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
@@ -19,98 +19,33 @@ func InitCmd() *cobra.Command {
Use: "init <chain-id>",
Short: "Initialize a rollapp configuration on your local machine.",
Run: func(cmd *cobra.Command, args []string) {
rollappId := args[0]
denom := args[1]
home := cmd.Flag(FlagNames.Home).Value.String()
createLightNode := !cmd.Flags().Changed(lightNodeEndpointFlag)
rollappBinaryPath := getRollappBinaryPath(cmd)
decimals := getDecimals(cmd)
initConfig := InitConfig{
Home: home,
RollappID: rollappId,
RollappBinary: rollappBinaryPath,
CreateDALightNode: createLightNode,
Denom: denom,
HubID: DefaultHubID,
Decimals: decimals,
}

addresses := initializeKeys(initConfig)
if createLightNode {
if err := initializeLightNodeConfig(initConfig); err != nil {
panic(err)
initConfig := getInitConfig(cmd, args)
overwrite, err := prepareDirectory(initConfig.Home)
ItayLevyOfficial marked this conversation as resolved.
Show resolved Hide resolved
handleError(err)
if overwrite {
addresses := initializeKeys(initConfig)
if initConfig.CreateDALightNode {
handleError(initializeLightNodeConfig(initConfig))
}
initializeRollappConfig(initConfig)
handleError(initializeRollappGenesis(initConfig))
handleError(initializeRelayerConfig(ChainConfig{
ID: initConfig.RollappID,
RPC: defaultRollappRPC,
Denom: initConfig.Denom,
AddressPrefix: addressPrefixes.Rollapp,
}, ChainConfig{
ID: DefaultHubID,
RPC: cmd.Flag(FlagNames.HubRPC).Value.String(),
Denom: "udym",
AddressPrefix: addressPrefixes.Hub,
}, initConfig))
printInitOutput(addresses, initConfig.RollappID)
}
initializeRollappConfig(initConfig)
if err := initializeRollappGenesis(initConfig); err != nil {
panic(err)
}

if err := initializeRelayerConfig(ChainConfig{
ID: rollappId,
RPC: defaultRollappRPC,
Denom: denom,
AddressPrefix: addressPrefixes.Rollapp,
}, ChainConfig{
ID: DefaultHubID,
RPC: cmd.Flag(FlagNames.HubRPC).Value.String(),
Denom: "udym",
AddressPrefix: addressPrefixes.Hub,
}, initConfig); err != nil {
panic(err)
}
celestiaAddress := addresses[KeyNames.DALightNode]
rollappHubAddress := addresses[KeyNames.HubSequencer]
relayerHubAddress := addresses[KeyNames.HubRelayer]
printInitOutput(AddressesToFund{
DA: celestiaAddress,
HubSequencer: rollappHubAddress,
HubRelayer: relayerHubAddress,
}, rollappId)
},
Args: cobra.ExactArgs(2),
}

addFlags(cmd)
return cmd
}

func addFlags(cmd *cobra.Command) {
cmd.Flags().StringP(FlagNames.HubRPC, "", defaultHubRPC, "Dymension Hub rpc endpoint")
cmd.Flags().StringP(FlagNames.LightNodeEndpoint, "", "", "The data availability light node endpoint. Runs an Arabica Celestia light node if not provided")
cmd.Flags().StringP(FlagNames.RollappBinary, "", "", "The rollapp binary. Should be passed only if you built a custom rollapp")
cmd.Flags().Uint64P(FlagNames.Decimals, "", 18, "The number of decimal places a rollapp token supports")
cmd.Flags().StringP(FlagNames.Home, "", getRollerRootDir(), "The directory of the roller config files")
}

func getDecimals(cmd *cobra.Command) uint64 {
decimals, err := cmd.Flags().GetUint64(FlagNames.Decimals)
if err != nil {
panic(err)
}
return decimals
}

func initializeKeys(initConfig InitConfig) map[string]string {
if initConfig.CreateDALightNode {
addresses, err := generateKeys(initConfig)
if err != nil {
panic(err)
}
return addresses
} else {
addresses, err := generateKeys(initConfig, KeyNames.DALightNode)
if err != nil {
panic(err)
}
return addresses
}
}

func getRollappBinaryPath(cmd *cobra.Command) string {
rollappBinaryPath := cmd.Flag(FlagNames.RollappBinary).Value.String()
if rollappBinaryPath == "" {
return defaultRollappBinaryPath
}
return rollappBinaryPath
}
16 changes: 16 additions & 0 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
@@ -100,3 +100,19 @@ func getDefaultKeysConfig(initConfig InitConfig) []KeyConfig {
},
}
}

func initializeKeys(initConfig InitConfig) map[string]string {
if initConfig.CreateDALightNode {
addresses, err := generateKeys(initConfig)
if err != nil {
panic(err)
}
return addresses
} else {
addresses, err := generateKeys(initConfig, KeyNames.DALightNode)
if err != nil {
panic(err)
}
return addresses
}
}
15 changes: 4 additions & 11 deletions cmd/config/init/output.go
Original file line number Diff line number Diff line change
@@ -7,24 +7,17 @@ import (
"github.com/olekukonko/tablewriter"
)

type AddressesToFund struct {
DA string
HubSequencer string
HubRelayer string
}

func printInitOutput(addresses AddressesToFund, rollappId string) {
func printInitOutput(addresses map[string]string, rollappId string) {
color.New(color.FgCyan, color.Bold).Printf("💈 RollApp '%s' configuration files have been successfully generated on your local machine. Congratulations!\n\n", rollappId)
color.New(color.FgGreen, color.Bold).Printf("🔑 Key Details:\n\n")

data := [][]string{
{"Celestia Light Node", addresses.DA},
{"Rollapp Hub Sequencer", addresses.HubSequencer},
{"Rollapp Relayer", addresses.HubRelayer},
{"Celestia", addresses[KeyNames.DALightNode]},
{"Sequencer", addresses[KeyNames.HubSequencer]},
{"Relayer", addresses[KeyNames.HubRelayer]},
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Item", "Address"})
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetBorder(false)
57 changes: 57 additions & 0 deletions cmd/config/init/overrideHomeDir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package initconfig

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

"github.com/manifoldco/promptui"
)

func dirNotEmpty(path string) (bool, error) {
info, err := os.Stat(path)
if err != nil || !info.IsDir() {
return false, err
}
files, err := ioutil.ReadDir(path)
return len(files) > 0, err
}

func prepareDirectory(path string) (bool, error) {
isNotEmpty, err := dirNotEmpty(path)
if err != nil {
return false, err
}
if !isNotEmpty {
return true, nil
}
prompt := promptui.Prompt{
Label: fmt.Sprintf("Directory %s is not empty. Do you want to overwrite", path),
IsConfirm: true,
}
_, err = prompt.Run()
if err != nil {
if err == promptui.ErrAbort {
return false, nil
}
return false, err
}
tempDir, err := ioutil.TempDir("", "config_backup")
if err != nil {
return false, err
}
files, err := ioutil.ReadDir(path)
if err != nil {
return false, err
}
for _, file := range files {
src := filepath.Join(path, file.Name())
dst := filepath.Join(tempDir, file.Name())
err = os.Rename(src, dst)
if err != nil {
return false, err
}
}
return true, nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ require (
github.com/btcsuite/btcd v0.22.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/confio/ics23/go v0.7.0 // indirect
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
@@ -57,6 +58,7 @@ require (
github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
@@ -311,6 +312,8 @@ github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QT
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
@@ -604,6 +607,7 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=