Skip to content

Commit

Permalink
feat: Configurable root directory in roller config init (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial authored Jun 4, 2023
1 parent 9b638a6 commit c89328b
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 66 deletions.
4 changes: 2 additions & 2 deletions cmd/config/init/DALightClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"path/filepath"
)

func initializeLightNodeConfig() error {
initLightNodeCmd := exec.Command(celestiaExecutablePath, "light", "init", "--p2p.network", "arabica", "--node.store", filepath.Join(getRollerRootDir(), configDirName.DALightNode))
func initializeLightNodeConfig(initConfig InitConfig) error {
initLightNodeCmd := exec.Command(celestiaExecutablePath, "light", "init", "--p2p.network", "arabica", "--node.store", filepath.Join(initConfig.Home, configDirName.DALightNode))
err := initLightNodeCmd.Run()
if err != nil {
return err
Expand Down
12 changes: 8 additions & 4 deletions cmd/config/init/consts.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package init

var flagNames = struct {
Home string
LightNodeEndpoint string
Denom string
KeyPrefix string
Decimals string
RollappBinary string
HubRPC string
}{
Home: "home",
LightNodeEndpoint: "light-node-endpoint",
Denom: "denom",
KeyPrefix: "key-prefix",
Expand All @@ -31,11 +33,13 @@ var keyNames = struct {
}

var keyPrefixes = struct {
Hub string
DA string
Hub string
Rollapp string
DA string
}{
Hub: "dym",
DA: "celestia",
Rollapp: "rol",
Hub: "dym",
DA: "celestia",
}

var configDirName = struct {
Expand Down
12 changes: 6 additions & 6 deletions cmd/config/init/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"github.com/tidwall/sjson"
)

func initializeRollappGenesis(rollappExecutablePath string, decimals uint64, denom string) error {
zeros := decimals + 9
tokenAmount := "1" + fmt.Sprintf("%0*d", zeros, 0) + denom
rollappConfigDirPath := filepath.Join(getRollerRootDir(), configDirName.Rollapp)
genesisSequencerAccountCmd := exec.Command(rollappExecutablePath, "add-genesis-account", keyNames.RollappSequencer, tokenAmount, "--keyring-backend", "test", "--home", rollappConfigDirPath)
func initializeRollappGenesis(initConfig InitConfig) error {
zeros := initConfig.Decimals + 9
tokenAmount := "1" + fmt.Sprintf("%0*d", zeros, 0) + initConfig.Denom
rollappConfigDirPath := filepath.Join(initConfig.Home, configDirName.Rollapp)
genesisSequencerAccountCmd := exec.Command(initConfig.RollappBinary, "add-genesis-account", keyNames.RollappSequencer, tokenAmount, "--keyring-backend", "test", "--home", rollappConfigDirPath)
err := genesisSequencerAccountCmd.Run()
if err != nil {
return err
}
err = updateGenesisParams(filepath.Join(rollappConfigDirPath, "config", "genesis.json"), denom)
err = updateGenesisParams(filepath.Join(rollappConfigDirPath, "config", "genesis.json"), initConfig.Denom)
if err != nil {
return err
}
Expand Down
98 changes: 68 additions & 30 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,46 @@ import (
"github.com/spf13/cobra"
)

type InitConfig struct {
Home string
RollappID string
RollappBinary string
CreateDALightNode bool
Denom string
HubID string
Decimals uint64
}

func InitCmd() *cobra.Command {
cmd := &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]
rollappKeyPrefix := getKeyPrefix(cmd.Flag(flagNames.KeyPrefix).Value.String(), rollappId)
home := cmd.Flag(flagNames.Home).Value.String()
rollappKeyPrefix := getKeyPrefix(cmd, rollappId)
createLightNode := !cmd.Flags().Changed(lightNodeEndpointFlag)
var addresses map[string]string
var err error
if createLightNode {
addresses, err = generateKeys(rollappId, defaultHubId, rollappKeyPrefix)
if err != nil {
panic(err)
}
} else {
addresses, err = generateKeys(rollappId, defaultHubId, rollappKeyPrefix, keyNames.DALightNode)
if err != nil {
panic(err)
}
}
rollappBinaryPath := getRollappBinaryPath(cmd.Flag(flagNames.RollappBinary).Value.String())
decimals, err := cmd.Flags().GetUint64(flagNames.Decimals)
if err != nil {
panic(err)
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(); err != nil {
if err := initializeLightNodeConfig(initConfig); err != nil {
panic(err)
}
}
initializeRollappConfig(rollappBinaryPath, rollappId, denom)
if err = initializeRollappGenesis(rollappBinaryPath, decimals, denom); err != nil {
initializeRollappConfig(initConfig)
if err := initializeRollappGenesis(initConfig); err != nil {
panic(err)
}

Expand All @@ -51,7 +57,7 @@ func InitCmd() *cobra.Command {
RPC: cmd.Flag(flagNames.HubRPC).Value.String(),
Denom: "udym",
KeyPrefix: keyPrefixes.Hub,
}); err != nil {
}, initConfig); err != nil {
panic(err)
}
celestiaAddress := addresses[keyNames.DALightNode]
Expand All @@ -65,24 +71,56 @@ func InitCmd() *cobra.Command {
},
Args: cobra.ExactArgs(2),
}
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.KeyPrefix, "", "", "The `bech32` prefix of the rollapp keys. Defaults to the first three characters of the chain-id.")
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.")

addFlags(cmd)
return cmd
}

func getRollappBinaryPath(rollappBinaryPath string) string {
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.KeyPrefix, "", "", "The `bech32` prefix of the rollapp keys. Defaults to the first three characters of the chain-id")
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
}

func getKeyPrefix(keyPrefix string, chainId string) string {
func getKeyPrefix(cmd *cobra.Command, rollappID string) string {
keyPrefix := cmd.Flag(flagNames.KeyPrefix).Value.String()
if keyPrefix == "" {
return chainId[:3]
return rollappID[:3]
}
return keyPrefix
}
34 changes: 15 additions & 19 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ func keyInfoToBech32Address(info keyring.Info, prefix string) (string, error) {
return bech32Address, nil
}

func generateKeys(rollappId string, hubId string, rollappKeyPrefix string, excludeKeys ...string) (map[string]string, error) {
keys := getDefaultKeysConfig(rollappId, hubId, rollappKeyPrefix)
func generateKeys(initConfig InitConfig, excludeKeys ...string) (map[string]string, error) {
keys := getDefaultKeysConfig(initConfig)
excludeKeysMap := make(map[string]struct{})
for _, key := range excludeKeys {
excludeKeysMap[key] = struct{}{}
}
addresses := make(map[string]string)
for _, key := range keys {
if _, exists := excludeKeysMap[key.keyId]; !exists {
keyInfo, err := createKey(rollappId, key.dir, key.keyId, key.coinType)
keyInfo, err := createKey(key, initConfig.Home)
if err != nil {
return nil, err
}
Expand All @@ -41,37 +41,33 @@ func generateKeys(rollappId string, hubId string, rollappKeyPrefix string, exclu
return addresses, nil
}

type keyConfig struct {
type KeyConfig struct {
dir string
keyId string
coinType uint32
prefix string
}

func createKey(rollappId string, relativePath string, keyId string, coinType ...uint32) (keyring.Info, error) {
var coinTypeVal = cosmosDefaultCointype
if len(coinType) != 0 {
coinTypeVal = coinType[0]
}
func createKey(keyConfig KeyConfig, home string) (keyring.Info, error) {
kr, err := keyring.New(
rollappId,
"",
keyring.BackendTest,
filepath.Join(getRollerRootDir(), relativePath),
filepath.Join(home, keyConfig.dir),
nil,
)
if err != nil {
return nil, err
}
bip44Params := hd.NewFundraiserParams(0, coinTypeVal, 0)
info, _, err := kr.NewMnemonic(keyId, keyring.English, bip44Params.String(), "", hd.Secp256k1)
bip44Params := hd.NewFundraiserParams(0, keyConfig.coinType, 0)
info, _, err := kr.NewMnemonic(keyConfig.keyId, keyring.English, bip44Params.String(), "", hd.Secp256k1)
if err != nil {
return nil, err
}
return info, nil
}

func getDefaultKeysConfig(rollappId string, hubId string, rollappKeyPrefix string) []keyConfig {
return []keyConfig{
func getDefaultKeysConfig(initConfig InitConfig) []KeyConfig {
return []KeyConfig{
{
dir: configDirName.Rollapp,
keyId: keyNames.HubSequencer,
Expand All @@ -82,19 +78,19 @@ func getDefaultKeysConfig(rollappId string, hubId string, rollappKeyPrefix strin
dir: configDirName.Rollapp,
keyId: keyNames.RollappSequencer,
coinType: evmCoinType,
prefix: rollappKeyPrefix,
prefix: keyPrefixes.Rollapp,
},
{
dir: path.Join(configDirName.Relayer, relayerKeysDirName, rollappId),
dir: path.Join(configDirName.Relayer, relayerKeysDirName, initConfig.RollappID),
keyId: keyNames.HubRelayer,
coinType: cosmosDefaultCointype,
prefix: keyPrefixes.Hub,
},
{
dir: path.Join(configDirName.Relayer, relayerKeysDirName, hubId),
dir: path.Join(configDirName.Relayer, relayerKeysDirName, initConfig.HubID),
keyId: keyNames.RollappRelayer,
coinType: evmCoinType,
prefix: rollappKeyPrefix,
prefix: keyPrefixes.Rollapp,
}, {

dir: path.Join(configDirName.DALightNode, relayerKeysDirName),
Expand Down
4 changes: 2 additions & 2 deletions cmd/config/init/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func setupPath(rollappConfig ChainConfig, hubConfig ChainConfig, relayerHome str
return nil
}

func initializeRelayerConfig(rollappConfig ChainConfig, hubConfig ChainConfig) error {
relayerHome := filepath.Join(getRollerRootDir(), configDirName.Relayer)
func initializeRelayerConfig(rollappConfig ChainConfig, hubConfig ChainConfig, initConfig InitConfig) error {
relayerHome := filepath.Join(initConfig.Home, configDirName.Relayer)
if err := initRelayer(relayerHome); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/config/init/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
toml "github.com/pelletier/go-toml"
)

func initializeRollappConfig(rollappExecutablePath string, chainId string, denom string) {
initRollappCmd := exec.Command(rollappExecutablePath, "init", keyNames.HubSequencer, "--chain-id", chainId, "--home", filepath.Join(getRollerRootDir(), configDirName.Rollapp))
func initializeRollappConfig(initConfig InitConfig) {
initRollappCmd := exec.Command(initConfig.RollappBinary, "init", keyNames.HubSequencer, "--chain-id", initConfig.RollappID, "--home", filepath.Join(initConfig.Home, configDirName.Rollapp))
err := initRollappCmd.Run()
if err != nil {
panic(err)
}
setRollappAppConfig(filepath.Join(getRollerRootDir(), configDirName.Rollapp, "config/app.toml"), denom)
setRollappAppConfig(filepath.Join(initConfig.Home, configDirName.Rollapp, "config/app.toml"), initConfig.Denom)
}

func setRollappAppConfig(appConfigFilePath string, denom string) {
Expand Down

0 comments on commit c89328b

Please sign in to comment.