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
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 13 additions & 14 deletions cmd/config/init/consts.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package initconfig

var FlagNames = struct {
Home string
LightNodeEndpoint string
Decimals string
RollappBinary string
HubRPC string
Home string
DAEndpoint string
Decimals string
RollappBinary string
HubRPC string
}{
Home: "home",
LightNodeEndpoint: "light-node-endpoint",
Decimals: "decimals",
RollappBinary: "rollapp-binary",
HubRPC: "hub-rpc",
Home: "home",
DAEndpoint: "data-availability-endpoint",
Decimals: "decimals",
RollappBinary: "rollapp-binary",
HubRPC: "hub-rpc",
}

var KeyNames = struct {
Expand Down Expand Up @@ -50,12 +50,11 @@ var ConfigDirName = struct {

const defaultHubRPC = "https://rpc-hub-35c.dymension.xyz:443"
const defaultRollappRPC = "http://localhost:26657"
const lightNodeEndpointFlag = "light-node-endpoint"

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"
19 changes: 19 additions & 0 deletions cmd/config/init/error_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package initconfig

import (
"os"

"github.com/fatih/color"
)

func OutputCleanError(err error) {
if err != nil {
defer func() {
if r := recover(); r != nil {
os.Exit(1)
}
}()
color.Red(err.Error())
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.DAEndpoint, "", "", "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 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(FlagNames.DAEndpoint)
rollappBinaryPath := getRollappBinaryPath(cmd)
decimals := getDecimals(cmd)
return InitConfig{
Home: home,
RollappID: rollappId,
RollappBinary: rollappBinaryPath,
CreateDALightNode: createLightNode,
Denom: denom,
HubID: DefaultHubID,
Decimals: decimals,
}
}
98 changes: 21 additions & 77 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package initconfig

import (
"os"

"github.com/spf13/cobra"
)

Expand All @@ -19,98 +21,40 @@ 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,
initConfig := getInitConfig(cmd, args)
isRootExist, err := dirNotEmpty(initConfig.Home)
OutputCleanError(err)
if isRootExist {
shouldOverwrite, err := promptOverwriteConfig(initConfig.Home)
OutputCleanError(err)
if shouldOverwrite {
OutputCleanError(os.RemoveAll(initConfig.Home))
} else {
os.Exit(0)
}
}

addresses := initializeKeys(initConfig)
if createLightNode {
if err := initializeLightNodeConfig(initConfig); err != nil {
panic(err)
}
if initConfig.CreateDALightNode {
OutputCleanError(initializeLightNodeConfig(initConfig))
}
initializeRollappConfig(initConfig)
if err := initializeRollappGenesis(initConfig); err != nil {
panic(err)
}

if err := initializeRelayerConfig(ChainConfig{
ID: rollappId,
OutputCleanError(initializeRollappGenesis(initConfig))
OutputCleanError(initializeRelayerConfig(ChainConfig{
ID: initConfig.RollappID,
RPC: defaultRollappRPC,
Denom: denom,
Denom: initConfig.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)
}, initConfig))
printInitOutput(addresses, initConfig.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
Expand Up @@ -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
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions cmd/config/init/overrideHomeDir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package initconfig

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

"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 promptOverwriteConfig(home string) (bool, error) {
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
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions test/config/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestInitCmd(t *testing.T) {
name: "Roller config init with custom flags",
goldenDirPath: "./goldens/init_with_flags",
optionalFlags: []string{
"--" + initconfig.FlagNames.LightNodeEndpoint, "http://localhost:26659",
"--" + initconfig.FlagNames.DAEndpoint, "http://localhost:26659",
"--" + initconfig.FlagNames.Decimals, "6",
},
},
Expand All @@ -50,7 +50,7 @@ func TestInitCmd(t *testing.T) {
assert.NoError(cmd.Execute())
assert.NoError(testutils.VerifyRollappKeys(tempDir))
assert.NoError(testutils.VerifyRelayerKeys(tempDir, rollappID, initconfig.DefaultHubID))
if !testutils.Contains(tc.optionalFlags, "--"+initconfig.FlagNames.LightNodeEndpoint) {
if !testutils.Contains(tc.optionalFlags, "--"+initconfig.FlagNames.DAEndpoint) {
assert.NoError(testutils.VerifyLightNodeKeys(tempDir))
}
assert.NoError(testutils.ClearKeys(tempDir))
Expand Down