Skip to content

Commit

Permalink
feat: handle genesis creator archive (#800)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Jul 8, 2024
1 parent 892bbb4 commit e3ea97c
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 542 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
args: [feat, fix, ci, chore, test, docs] # optional: list of Conventional Commits types to allow e.g. [feat, fix, ci, chore, test]
args: [feat, fix, ci, chore, test, docs, refactor] # optional: list of Conventional Commits types to allow e.g. [feat, fix, ci, chore, test]
- repo: local
hooks:
- id: goimports-reviser
Expand Down
3 changes: 1 addition & 2 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/config/export"
configInit "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/config/set"
"github.com/dymensionxyz/roller/cmd/config/show"
)
Expand All @@ -14,7 +13,7 @@ func Cmd() *cobra.Command {
Use: "config",
Short: "Commands for setting up and managing rollapp configuration files.",
}
cmd.AddCommand(configInit.InitCmd())

cmd.AddCommand(show.Cmd())
cmd.AddCommand(set.Cmd())
cmd.AddCommand(export.Cmd())
Expand Down
134 changes: 71 additions & 63 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package initconfig

import (
"crypto/rand"
"fmt"
"math/big"
"regexp"
"path/filepath"
"strings"

"github.com/dymensionxyz/roller/version"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"github.com/spf13/cobra"
global_utils "github.com/dymensionxyz/roller/utils"
"github.com/dymensionxyz/roller/version"
)

const (
defaultTokenSupply = "1000000000"
)

func addFlags(cmd *cobra.Command) error {
func AddFlags(cmd *cobra.Command) error {
cmd.Flags().
StringP(FlagNames.HubID, "", consts.FroopylandHubName, fmt.Sprintf("The ID of the Dymension hub. %s", getAvailableHubsMessage()))
cmd.Flags().
Expand All @@ -28,7 +27,7 @@ func addFlags(cmd *cobra.Command) error {
StringP(FlagNames.VMType, "", string(config.EVM_ROLLAPP), "The rollapp type [evm, sdk]. Defaults to evm")
cmd.Flags().
StringP(FlagNames.TokenSupply, "", defaultTokenSupply, "The total token supply of the RollApp")
cmd.Flags().BoolP(FlagNames.Interactive, "i", false, "Run roller in interactive mode")
// cmd.Flags().BoolP(FlagNames.Interactive, "i", false, "Run roller in interactive mode")
cmd.Flags().BoolP(FlagNames.NoOutput, "", false, "Run init without any output")
cmd.Flags().UintP(FlagNames.Decimals, "", 18,
"The precision level of the RollApp's token defined by the number of decimal places. "+
Expand All @@ -47,36 +46,49 @@ func addFlags(cmd *cobra.Command) error {

func GetInitConfig(initCmd *cobra.Command, args []string) (*config.RollappConfig, error) {
home := initCmd.Flag(utils.FlagNames.Home).Value.String()
interactive, _ := initCmd.Flags().GetBool(FlagNames.Interactive)
rollerConfigFilePath := filepath.Join(home, "roller.toml")
// interactive, _ := initCmd.Flags().GetBool(FlagNames.Interactive)
raType, err := global_utils.GetKeyFromTomlFile(rollerConfigFilePath, "execution")
if err != nil {
return nil, err
}
raID, err := global_utils.GetKeyFromTomlFile(rollerConfigFilePath, "rollapp_id")
if err != nil {
return nil, err
}
raBaseDenom, err := global_utils.GetKeyFromTomlFile(rollerConfigFilePath, "base_denom")
if err != nil {
return nil, err
}
da, err := global_utils.GetKeyFromTomlFile(rollerConfigFilePath, "da")
if err != nil {
return nil, err
}

fmt.Println(home, raType, raID, raBaseDenom, da)

// load initial config if exists
var cfg config.RollappConfig
// load from flags
cfg.Home = home
cfg.RollappBinary = initCmd.Flag(FlagNames.RollappBinary).Value.String()
cfg.VMType = config.VMType(initCmd.Flag(FlagNames.VMType).Value.String())
cfg.TokenSupply = initCmd.Flag(FlagNames.TokenSupply).Value.String()
decimals, _ := initCmd.Flags().GetUint(FlagNames.Decimals)
cfg.Decimals = decimals
cfg.DA = config.DAType(strings.ToLower(initCmd.Flag(FlagNames.DAType).Value.String()))

// TODO: support wasm, make the bainry name generic, like 'rollappd'
// for both RollApp types
cfg.RollappBinary = consts.Executables.RollappEVM
cfg.VMType = config.VMType(raType)
// token supply is provided in the pre-created genesis
// cfg.TokenSupply = initCmd.Flag(FlagNames.TokenSupply).Value.String()
// decimals, _ := initCmd.Flags().GetUint(FlagNames.Decimals)
cfg.Decimals = 18
cfg.DA = config.DAType(strings.ToLower(da))

hubID := initCmd.Flag(FlagNames.HubID).Value.String()
if hub, ok := consts.Hubs[hubID]; ok {
cfg.HubData = hub
}
cfg.RollerVersion = version.TrimVersionStr(version.BuildVersion)

if len(args) > 0 {
cfg.RollappID = args[0]
}
if len(args) > 1 {
cfg.Denom = "a" + args[1]
}

if interactive {
if err := RunInteractiveMode(&cfg); err != nil {
return nil, err
}
}
cfg.RollappID = raID
cfg.Denom = raBaseDenom

return formatBaseCfg(cfg, initCmd)
}
Expand All @@ -86,41 +98,37 @@ func formatBaseCfg(
initCmd *cobra.Command,
) (*config.RollappConfig, error) {
setDecimals(initCmd, &cfg)
formattedRollappId, err := generateRollappId(cfg)
if err != nil {
return nil, err
}
cfg.RollappID = formattedRollappId
return &cfg, nil
}

func generateRollappId(rlpCfg config.RollappConfig) (string, error) {
for {
RandEthId, err := generateRandEthId()
if err != nil {
return "", err
}
if rlpCfg.HubData.ID == consts.LocalHubID {
return fmt.Sprintf("%s_%s-1", rlpCfg.RollappID, RandEthId), nil
}
isUnique, err := isEthIdentifierUnique(RandEthId, rlpCfg)
if err != nil {
return "", err
}
if isUnique {
return fmt.Sprintf("%s_%s-1", rlpCfg.RollappID, RandEthId), nil
}
}
}

func generateRandEthId() (string, error) {
max := big.NewInt(9000000)
n, err := rand.Int(rand.Reader, max)
if err != nil {
return "", err
}
return fmt.Sprintf("%d", n), nil
}
// there's no need for a custom chain id as the rollapp id is set via the genesis-creator
// func generateRollappId(rlpCfg config.RollappConfig) (string, error) {
// for {
// RandEthId, err := generateRandEthId()
// if err != nil {
// return "", err
// }
// if rlpCfg.HubData.ID == consts.LocalHubID {
// return fmt.Sprintf("%s_%s-1", rlpCfg.RollappID, RandEthId), nil
// }
// isUnique, err := isEthIdentifierUnique(RandEthId, rlpCfg)
// if err != nil {
// return "", err
// }
// if isUnique {
// return fmt.Sprintf("%s_%s-1", rlpCfg.RollappID, RandEthId), nil
// }
// }
// }

// func generateRandEthId() (string, error) {
// max := big.NewInt(9000000)
// n, err := rand.Int(rand.Reader, max)
// if err != nil {
// return "", err
// }
// return fmt.Sprintf("%d", n), nil
// }

func setDecimals(initCmd *cobra.Command, cfg *config.RollappConfig) {
decimals, _ := initCmd.Flags().GetUint(FlagNames.Decimals)
Expand All @@ -140,7 +148,7 @@ func getAvailableHubsMessage() string {
)
}

func isLowercaseAlphabetical(s string) bool {
match, _ := regexp.MatchString("^[a-z]+$", s)
return match
}
// func isLowercaseAlphabetical(s string) bool {
// match, _ := regexp.MatchString("^[a-z]+$", s)
// return match
// }
179 changes: 0 additions & 179 deletions cmd/config/init/init.go

This file was deleted.

Loading

0 comments on commit e3ea97c

Please sign in to comment.