diff --git a/cmd/config/init/DALightClient.go b/cmd/config/init/DALightClient.go index 7598423e..e59820f5 100644 --- a/cmd/config/init/DALightClient.go +++ b/cmd/config/init/DALightClient.go @@ -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 diff --git a/cmd/config/init/consts.go b/cmd/config/init/consts.go index 58e9f87d..aedc0dce 100644 --- a/cmd/config/init/consts.go +++ b/cmd/config/init/consts.go @@ -1,6 +1,7 @@ package init var flagNames = struct { + Home string LightNodeEndpoint string Denom string KeyPrefix string @@ -8,6 +9,7 @@ var flagNames = struct { RollappBinary string HubRPC string }{ + Home: "home", LightNodeEndpoint: "light-node-endpoint", Denom: "denom", KeyPrefix: "key-prefix", @@ -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 { diff --git a/cmd/config/init/genesis.go b/cmd/config/init/genesis.go index 30c27cde..ad0f326d 100644 --- a/cmd/config/init/genesis.go +++ b/cmd/config/init/genesis.go @@ -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 } diff --git a/cmd/config/init/init.go b/cmd/config/init/init.go index 04199e02..2ea5e770 100644 --- a/cmd/config/init/init.go +++ b/cmd/config/init/init.go @@ -4,6 +4,16 @@ 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 ", @@ -11,33 +21,29 @@ func InitCmd() *cobra.Command { 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) } @@ -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] @@ -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 } diff --git a/cmd/config/init/keys.go b/cmd/config/init/keys.go index 3f54d868..8ba787ab 100644 --- a/cmd/config/init/keys.go +++ b/cmd/config/init/keys.go @@ -18,8 +18,8 @@ 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{}{} @@ -27,7 +27,7 @@ func generateKeys(rollappId string, hubId string, rollappKeyPrefix string, exclu 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 } @@ -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, @@ -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), diff --git a/cmd/config/init/relayer.go b/cmd/config/init/relayer.go index 106cad1f..0352f88e 100644 --- a/cmd/config/init/relayer.go +++ b/cmd/config/init/relayer.go @@ -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 } diff --git a/cmd/config/init/rollapp.go b/cmd/config/init/rollapp.go index b4088759..04f01f86 100644 --- a/cmd/config/init/rollapp.go +++ b/cmd/config/init/rollapp.go @@ -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) {