Skip to content

Commit

Permalink
feat: Add command to register rollapps (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial authored Jun 12, 2023
1 parent 81d00fe commit aee609f
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 34 deletions.
3 changes: 2 additions & 1 deletion cmd/config/init/DALightClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package initconfig
import (
"os/exec"
"path/filepath"
"github.com/dymensionxyz/roller/cmd/consts"
)

func initializeLightNodeConfig(initConfig InitConfig) error {
initLightNodeCmd := exec.Command(celestiaExecutablePath, "light", "init", "--p2p.network", "arabica", "--node.store", filepath.Join(initConfig.Home, ConfigDirName.DALightNode))
initLightNodeCmd := exec.Command(consts.Executables.Celestia, "light", "init", "--p2p.network", "arabica", "--node.store", filepath.Join(initConfig.Home, ConfigDirName.DALightNode))
err := initLightNodeCmd.Run()
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions cmd/config/init/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,4 @@ const defaultRollappRPC = "http://localhost:26657"
const evmCoinType uint32 = 60
const KeysDirName = "keys"
const cosmosDefaultCointype uint32 = 118
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"
const RollerConfigFileName = "config.toml"
9 changes: 6 additions & 3 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package initconfig

import (
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/utils"
)

func addFlags(cmd *cobra.Command) {
cmd.Flags().StringP(FlagNames.HubRPC, "", HubData.RPC_URL, "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")
cmd.Flags().StringP(FlagNames.Home, "", utils.GetRollerRootDir(), "The directory of the roller config files")
}

func getDecimals(cmd *cobra.Command) uint64 {
Expand All @@ -23,12 +26,12 @@ func getDecimals(cmd *cobra.Command) uint64 {
func getRollappBinaryPath(cmd *cobra.Command) string {
rollappBinaryPath := cmd.Flag(FlagNames.RollappBinary).Value.String()
if rollappBinaryPath == "" {
return defaultRollappBinaryPath
return consts.Executables.RollappEVM
}
return rollappBinaryPath
}

func getInitConfig(cmd *cobra.Command, args []string) InitConfig {
func GetInitConfig(cmd *cobra.Command, args []string) InitConfig {
rollappId := args[0]
denom := args[1]
home := cmd.Flag(FlagNames.Home).Value.String()
Expand Down
22 changes: 12 additions & 10 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/dymensionxyz/roller/cmd/utils"
)

type InitConfig struct {
Expand All @@ -13,40 +14,40 @@ type InitConfig struct {
RollappBinary string
CreateDALightNode bool
Denom string
HubID string
Decimals uint64
HubID string
}

func InitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init <chain-id> <denom>",
Short: "Initialize a RollApp configuration on your local machine.",
Run: func(cmd *cobra.Command, args []string) {
initConfig := getInitConfig(cmd, args)
initConfig := GetInitConfig(cmd, args)
isUniqueRollapp, err := isRollappIDUnique(initConfig.RollappID)
OutputCleanError(err)
utils.PrettifyErrorIfExists(err)
if !isUniqueRollapp {
OutputCleanError(fmt.Errorf("Rollapp ID %s already exists on the hub. Please use a unique ID.", initConfig.RollappID))
utils.PrettifyErrorIfExists(fmt.Errorf("Rollapp ID %s already exists on the hub. Please use a unique ID.", initConfig.RollappID))
}
isRootExist, err := dirNotEmpty(initConfig.Home)
OutputCleanError(err)
utils.PrettifyErrorIfExists(err)
if isRootExist {
shouldOverwrite, err := promptOverwriteConfig(initConfig.Home)
OutputCleanError(err)
utils.PrettifyErrorIfExists(err)
if shouldOverwrite {
OutputCleanError(os.RemoveAll(initConfig.Home))
utils.PrettifyErrorIfExists(os.RemoveAll(initConfig.Home))
} else {
os.Exit(0)
}
}

addresses := initializeKeys(initConfig)
if initConfig.CreateDALightNode {
OutputCleanError(initializeLightNodeConfig(initConfig))
utils.PrettifyErrorIfExists(initializeLightNodeConfig(initConfig))
}
initializeRollappConfig(initConfig)
OutputCleanError(initializeRollappGenesis(initConfig))
OutputCleanError(initializeRelayerConfig(ChainConfig{
utils.PrettifyErrorIfExists(initializeRollappGenesis(initConfig))
utils.PrettifyErrorIfExists(initializeRelayerConfig(ChainConfig{
ID: initConfig.RollappID,
RPC: defaultRollappRPC,
Denom: initConfig.Denom,
Expand All @@ -57,6 +58,7 @@ func InitCmd() *cobra.Command {
Denom: "udym",
AddressPrefix: addressPrefixes.Hub,
}, initConfig))
utils.PrettifyErrorIfExists(WriteConfigToTOML(initConfig))
printInitOutput(addresses, initConfig.RollappID)
},
Args: cobra.ExactArgs(2),
Expand Down
4 changes: 2 additions & 2 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func getDefaultKeysConfig(initConfig InitConfig) []KeyConfig {
prefix: addressPrefixes.Rollapp,
},
{
dir: path.Join(ConfigDirName.Relayer, KeysDirName, initConfig.HubID),
dir: path.Join(ConfigDirName.Relayer, KeysDirName, HubData.ID),
keyId: KeyNames.HubRelayer,
coinType: cosmosDefaultCointype,
prefix: addressPrefixes.Hub,
Expand Down Expand Up @@ -115,4 +115,4 @@ func initializeKeys(initConfig InitConfig) map[string]string {
}
return addresses
}
}
}
9 changes: 5 additions & 4 deletions cmd/config/init/output.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package initconfig

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
)

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")
fmt.Printf("💈 RollApp '%s' configuration files have been successfully generated on your local machine. Congratulations!\n\n", rollappId)

fmt.Printf("🔑 Addresses:\n\n")

data := [][]string{
{"Celestia", addresses[KeyNames.DALightNode]},
Expand All @@ -24,5 +25,5 @@ func printInitOutput(addresses map[string]string, rollappId string) {
table.AppendBulk(data)
table.Render()

color.New(color.FgYellow, color.Bold).Printf("\n🔔 Please fund these addresses to register and run the rollapp.\n")
fmt.Printf("\n🔔 Please fund these addresses to register and run the rollapp.\n")
}
35 changes: 35 additions & 0 deletions cmd/config/init/persistence_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package initconfig

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

"github.com/pelletier/go-toml"
)

func WriteConfigToTOML(InitConfig InitConfig) error {
tomlBytes, err := toml.Marshal(InitConfig)
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(InitConfig.Home, RollerConfigFileName), tomlBytes, 0644)
if err != nil {
return err
}

return nil
}

func LoadConfigFromTOML(root string) (InitConfig, error) {
var config InitConfig
tomlBytes, err := ioutil.ReadFile(filepath.Join(root, RollerConfigFileName))
if err != nil {
return config, err
}
err = toml.Unmarshal(tomlBytes, &config)
if err != nil {
return config, err
}

return config, nil
}
10 changes: 6 additions & 4 deletions cmd/config/init/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"os/exec"
"path/filepath"

"github.com/dymensionxyz/roller/cmd/consts"
)

type RelayerFileChainConfig struct {
Expand Down Expand Up @@ -78,15 +80,15 @@ func addChainToRelayer(fileChainConfig RelayerFileChainConfig, relayerHome strin
if err != nil {
return err
}
addChainCmd := exec.Command(relayerExecutablePath, "chains", "add", fileChainConfig.Value.ChainID, "--home", relayerHome, "--file", chainFilePath)
addChainCmd := exec.Command(consts.Executables.Relayer, "chains", "add", fileChainConfig.Value.ChainID, "--home", relayerHome, "--file", chainFilePath)
if err := addChainCmd.Run(); err != nil {
return err
}
return nil
}

func initRelayer(relayerHome string) error {
initRelayerConfigCmd := exec.Command(relayerExecutablePath, "config", "init", "--home", relayerHome)
initRelayerConfigCmd := exec.Command(consts.Executables.Relayer, "config", "init", "--home", relayerHome)
return initRelayerConfigCmd.Run()
}

Expand Down Expand Up @@ -115,12 +117,12 @@ func addChainsConfig(rollappConfig ChainConfig, hubConfig ChainConfig, relayerHo
}

func setupPath(rollappConfig ChainConfig, hubConfig ChainConfig, relayerHome string) error {
setSettlementCmd := exec.Command(relayerExecutablePath, "chains", "set-settlement", hubConfig.ID, "--home", relayerHome)
setSettlementCmd := exec.Command(consts.Executables.Relayer, "chains", "set-settlement", hubConfig.ID, "--home", relayerHome)
if err := setSettlementCmd.Run(); err != nil {
return err
}
relayerPath := "hub-rollapp"
newPathCmd := exec.Command(relayerExecutablePath, "paths", "new", rollappConfig.ID, hubConfig.ID, relayerPath, "--src-port", "transfer", "--dst-port", "transfer", "--version", "ics20-1", "--home", relayerHome)
newPathCmd := exec.Command(consts.Executables.Relayer, "paths", "new", rollappConfig.ID, hubConfig.ID, relayerPath, "--src-port", "transfer", "--dst-port", "transfer", "--version", "ics20-1", "--home", relayerHome)
if err := newPathCmd.Run(); err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/consts/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package consts

var Executables = struct {
Celestia string
RollappEVM string
Relayer string
Dymension string
}{
Celestia: "/usr/local/bin/roller_bins/celestia",
RollappEVM: "/usr/local/bin/rollapp_evm",
Relayer: "/usr/local/bin/roller_bins/rly",
Dymension: "/usr/local/bin/roller_bins/dymd",
}
48 changes: 48 additions & 0 deletions cmd/register/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package register

import (
"bytes"
"os/exec"
"path/filepath"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/spf13/cobra"
)

func RegisterCmd() *cobra.Command {
registerCmd := &cobra.Command{
Use: "register",
Short: "Registers the rollapp and the sequencer to the Dymension hub.",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(initconfig.FlagNames.Home).Value.String()
rollappConfig, err := initconfig.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
utils.PrettifyErrorIfExists(registerRollapp(rollappConfig))
},
}
addFlags(registerCmd)
return registerCmd
}

func addFlags(cmd *cobra.Command) {
cmd.Flags().StringP(initconfig.FlagNames.Home, "", utils.GetRollerRootDir(), "The directory of the roller config files")
}

func registerRollapp(rollappConfig initconfig.InitConfig) error {
cmd := exec.Command(
consts.Executables.Dymension, "tx", "rollapp", "create-rollapp",
"--from", initconfig.KeyNames.HubSequencer,
"--keyring-backend", "test",
"--keyring-dir", filepath.Join(rollappConfig.Home, initconfig.ConfigDirName.Rollapp),
rollappConfig.RollappID, "stamp1", "genesis-path/1", "3", "3", `{"Addresses":[]}`, "--output", "json",
"--node", initconfig.HubData.RPC_URL, "--yes", "--broadcast-mode", "block",
)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
error := cmd.Run()
return error
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

"github.com/dymensionxyz/roller/cmd/config"
"github.com/dymensionxyz/roller/cmd/register"
"github.com/dymensionxyz/roller/cmd/version"
"github.com/spf13/cobra"
)
Expand All @@ -26,4 +27,5 @@ func Execute() {
func init() {
rootCmd.AddCommand(config.ConfigCmd())
rootCmd.AddCommand(version.VersionCmd())
rootCmd.AddCommand(register.RegisterCmd())
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package initconfig
package utils

import (
"os"

"github.com/fatih/color"
)

func OutputCleanError(err error) {
func PrettifyErrorIfExists(err error) {
if err != nil {
defer func() {
if r := recover(); r != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/config/init/utils.go → cmd/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package initconfig
package utils

import (
"os"
"path/filepath"
)

func getRollerRootDir() string {
func GetRollerRootDir() string {
return filepath.Join(os.Getenv("HOME"), ".roller")
}
12 changes: 9 additions & 3 deletions test/config/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
)

func TestInitCmd(t *testing.T) {
DAEndpoint := "http://localhost:26659"
decimals := "6"
testCases := []struct {
name string
goldenDirPath string
Expand All @@ -27,8 +29,8 @@ func TestInitCmd(t *testing.T) {
name: "Roller config init with custom flags",
goldenDirPath: "./goldens/init_with_flags",
optionalFlags: []string{
"--" + initconfig.FlagNames.DAEndpoint, "http://localhost:26659",
"--" + initconfig.FlagNames.Decimals, "6",
"--" + initconfig.FlagNames.DAEndpoint, DAEndpoint,
"--" + initconfig.FlagNames.Decimals, decimals,
},
},
}
Expand All @@ -43,13 +45,17 @@ func TestInitCmd(t *testing.T) {
assert.NoError(err)
}()
cmd := initconfig.InitCmd()
denom := "udym"
rollappID := "mars"
cmd.SetArgs(append([]string{
rollappID,
"udym",
denom,
"--" + initconfig.FlagNames.Home, tempDir,
}, tc.optionalFlags...))
assert.NoError(cmd.Execute())
initConfig := initconfig.GetInitConfig(cmd, []string{rollappID, denom})
assert.NoError(testutils.VerifyRollerConfig(initConfig))
assert.NoError(os.Remove(filepath.Join(tempDir, initconfig.RollerConfigFileName)))
assert.NoError(testutils.VerifyRollappKeys(tempDir))
assert.NoError(testutils.VerifyRelayerKeys(tempDir, rollappID, initconfig.HubData.ID))
if !testutils.Contains(tc.optionalFlags, "--"+initconfig.FlagNames.DAEndpoint) {
Expand Down
Loading

0 comments on commit aee609f

Please sign in to comment.