Skip to content

Commit

Permalink
feat: add support for mock da (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin authored Jul 4, 2023
1 parent 64422fa commit 8b50d34
Show file tree
Hide file tree
Showing 39 changed files with 563 additions and 447 deletions.
6 changes: 4 additions & 2 deletions cmd/config/init/consts.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package initconfig

import "github.com/dymensionxyz/roller/cmd/utils"
import "github.com/dymensionxyz/roller/config"

var FlagNames = struct {
TokenSupply string
RollappBinary string
HubID string
Decimals string
Interactive string
DAType string
}{
TokenSupply: "token-supply",
RollappBinary: "rollapp-binary",
HubID: "hub",
Interactive: "interactive",
Decimals: "decimals",
DAType: "da",
}

const (
Expand All @@ -22,7 +24,7 @@ const (
)

// TODO(#112): The avaialble hub networks should be read from YAML file
var Hubs = map[string]utils.HubData{
var Hubs = map[string]config.HubData{
StagingHubID: {
API_URL: "https://dymension.devnet.api.silknodes.io:443",
ID: "devnet_304-1",
Expand Down
19 changes: 0 additions & 19 deletions cmd/config/init/da_light_client.go

This file was deleted.

24 changes: 8 additions & 16 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package initconfig

import (
"fmt"
"strings"

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

Expand All @@ -22,6 +24,8 @@ func addFlags(cmd *cobra.Command) error {
"It should be an integer ranging between 1 and 18. This is akin to how 1 Ether equates to 10^18 Wei in Ethereum. "+
"Note: EVM RollApps must set this value to 18.")

cmd.Flags().StringP(FlagNames.DAType, "", "Celestia", "The DA layer for the RollApp. Can be one of 'Celestia, Avail, Mock'")

// TODO: Expose when supporting custom sdk rollapps.
err := cmd.Flags().MarkHidden(FlagNames.Decimals)
if err != nil {
Expand All @@ -30,12 +34,8 @@ func addFlags(cmd *cobra.Command) error {
return nil
}

func getTokenSupply(cmd *cobra.Command) string {
return cmd.Flag(FlagNames.TokenSupply).Value.String()
}

func GetInitConfig(initCmd *cobra.Command, args []string) (utils.RollappConfig, error) {
cfg := utils.RollappConfig{}
func GetInitConfig(initCmd *cobra.Command, args []string) (config.RollappConfig, error) {
cfg := config.RollappConfig{}
cfg.Home = initCmd.Flag(utils.FlagNames.Home).Value.String()
cfg.RollappBinary = initCmd.Flag(FlagNames.RollappBinary).Value.String()
// Error is ignored because the flag is validated in the cobra preRun hook
Expand All @@ -51,24 +51,16 @@ func GetInitConfig(initCmd *cobra.Command, args []string) (utils.RollappConfig,
denom := args[1]

hubID := initCmd.Flag(FlagNames.HubID).Value.String()
tokenSupply := getTokenSupply(initCmd)
tokenSupply := initCmd.Flag(FlagNames.TokenSupply).Value.String()
cfg.RollappID = rollappId
cfg.Denom = "u" + denom
cfg.HubData = Hubs[hubID]
cfg.TokenSupply = tokenSupply
cfg.DA = config.DAType(strings.ToLower(initCmd.Flag(FlagNames.DAType).Value.String()))

return cfg, nil
}
func getValidRollappIdMessage() string {
return "A valid RollApp ID should follow the format 'name_EIP155-revision', where 'name' is made up of" +
" lowercase English letters, 'EIP155-revision' is a 1 to 5 digit number representing the EIP155 rollapp ID, and '" +
"revision' is a 1 to 5 digit number representing the revision. For example: 'mars_9721-1'"
}

func getAvailableHubsMessage() string {
return fmt.Sprintf("Acceptable values are '%s' or '%s'", StagingHubID, LocalHubID)
}

func getValidDenomMessage() string {
return "A valid denom should consist of exactly 3 English alphabet letters, for example 'btc', 'eth'"
}
3 changes: 2 additions & 1 deletion cmd/config/init/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"

"os/exec"
"path/filepath"

"github.com/tidwall/sjson"
)

func initializeRollappGenesis(initConfig utils.RollappConfig) error {
func initializeRollappGenesis(initConfig config.RollappConfig) error {
totalTokenSupply, success := new(big.Int).SetString(initConfig.TokenSupply, 10)
if !success {
return fmt.Errorf("invalid token supply")
Expand Down
51 changes: 36 additions & 15 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package initconfig

import (
"fmt"
"github.com/cosmos/cosmos-sdk/types/errors"
"os"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
datalayer "github.com/dymensionxyz/roller/data_layer"
"github.com/spf13/cobra"
"os"
)

func InitCmd() *cobra.Command {
initCmd := &cobra.Command{
Use: "init <rollapp-id> <denom>",
Short: "Initialize a RollApp configuration on your local machine.",
Use: "init <rollapp-id> <denom> | --interactive",
Short: "Initialize a RollApp configuration on your local machine.",
Long: "Initialize a RollApp configuration on your local machine\n" + requiredFlagsUsage(),
Example: `init mars_9721-1 btc`,
PreRunE: func(cmd *cobra.Command, args []string) error {
interactive, _ := cmd.Flags().GetBool(FlagNames.Interactive)
if interactive {
Expand All @@ -37,15 +41,17 @@ func InitCmd() *cobra.Command {
return nil
},
Run: func(cmd *cobra.Command, args []string) {
initConfig, err := GetInitConfig(cmd, args)
utils.PrettifyErrorIfExists(err)

spin := utils.GetLoadingSpinner()
spin.Suffix = consts.SpinnerMsgs.UniqueIdVerification
spin.Start()
initConfig, err := GetInitConfig(cmd, args)
utils.PrettifyErrorIfExists(err)

err = initConfig.Validate()
err = errors.Wrap(err, getValidRollappIdMessage())
utils.PrettifyErrorIfExists(err)
utils.PrettifyErrorIfExists(err, func() {
fmt.Println(requiredFlagsUsage())
})

utils.PrettifyErrorIfExists(VerifyUniqueRollappID(initConfig.RollappID, initConfig))
isRootExist, err := dirNotEmpty(initConfig.Home)
Expand Down Expand Up @@ -84,18 +90,23 @@ func InitCmd() *cobra.Command {
utils.PrettifyErrorIfExists(err)

/* ------------------------ Initialize DA light node ------------------------ */
utils.PrettifyErrorIfExists(initializeLightNodeConfig(initConfig))
daAddress, err := utils.GetCelestiaAddress(initConfig.Home)
damanager := datalayer.NewDAManager(initConfig.DA, initConfig.Home)
err = damanager.InitializeLightNodeConfig()
utils.PrettifyErrorIfExists(err)
addresses = append(addresses, utils.AddressData{
Addr: daAddress,
Name: consts.KeysIds.DALightNode,
})
daAddress, err := damanager.GetDAAccountAddress()
utils.PrettifyErrorIfExists(err)

if daAddress != "" {
addresses = append(addresses, utils.AddressData{
Name: consts.KeysIds.DALightNode,
Addr: daAddress,
})
}

/* --------------------------- Initiailize Rollapp -------------------------- */
utils.PrettifyErrorIfExists(initializeRollappConfig(initConfig))
utils.PrettifyErrorIfExists(initializeRollappGenesis(initConfig))
utils.PrettifyErrorIfExists(utils.WriteConfigToTOML(initConfig))
utils.PrettifyErrorIfExists(config.WriteConfigToTOML(initConfig))

/* ------------------------------ Print output ------------------------------ */
spin.Stop()
Expand All @@ -109,3 +120,13 @@ func InitCmd() *cobra.Command {

return initCmd
}

func requiredFlagsUsage() string {
return `
A valid RollApp ID should follow the format 'name_uniqueID-revision', where
- 'name' is made up of lowercase English letters
- 'uniqueID' is a number up to the length of 5 digits representing the unique ID EIP155 rollapp ID
- 'revision' is a number up to the length of 5 digits representing the revision number for this rollapp
A valid denom should consist of exactly 3 English alphabet letters, for example 'btc', 'eth'`
}
39 changes: 24 additions & 15 deletions cmd/config/init/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package initconfig
import (
"fmt"
"os"
"strings"

"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"github.com/manifoldco/promptui"
)

// TODO: return error output
func RunInteractiveMode(config *utils.RollappConfig) {
func RunInteractiveMode(cfg *config.RollappConfig) {
promptNetwork := promptui.Select{
Label: "Select your network",
Items: []string{"devnet", "local"},
}
_, mode, _ := promptNetwork.Run()
config.HubData = Hubs[mode]
cfg.HubData = Hubs[mode]

promptChainID := promptui.Prompt{
Label: "Enter your RollApp ID",
Expand All @@ -27,8 +28,8 @@ func RunInteractiveMode(config *utils.RollappConfig) {
if err != nil {
break
}
if err := utils.ValidateRollAppID(chainID); err == nil {
config.RollappID = chainID
if err := config.ValidateRollAppID(chainID); err == nil {
cfg.RollappID = chainID
break
}
fmt.Println("Expected format: name_uniqueID-revision (e.g. myrollapp_1234-1)")
Expand All @@ -38,37 +39,45 @@ func RunInteractiveMode(config *utils.RollappConfig) {
Label: "Specify your RollApp denom",
Default: "RAX",
Validate: func(s string) error {
if !utils.IsValidTokenSymbol(s) {
if !config.IsValidTokenSymbol(s) {
return fmt.Errorf("invalid token symbol")
}
return nil
},
}
denom, _ := promptDenom.Run()
config.Denom = "u" + denom
cfg.Denom = "u" + denom

promptTokenSupply := promptui.Prompt{
Label: "How many " + denom + " tokens do you wish to mint for Genesis?",
Default: "1000000000",
Validate: utils.VerifyTokenSupply,
Validate: config.VerifyTokenSupply,
}
supply, _ := promptTokenSupply.Run()
config.TokenSupply = supply
cfg.TokenSupply = supply

availableDAs := []config.DAType{config.Avail, config.Celestia}
if mode == "local" {
availableDAs = append(availableDAs, config.Mock)
}
promptDAType := promptui.Select{
Label: "Choose your data layer",
Items: []string{"Celestia", "Avail"},
Items: availableDAs,
}

//TODO(#76): temporary hack to only support Celestia
for {
_, da, err := promptDAType.Run()
if err != nil || da == "Celestia" {
if err != nil {
break
}
if da != "Celestia" {
fmt.Println("Only Celestia supported for now")
da = strings.ToLower(da)
if da == string(config.Avail) {
fmt.Println("Avail not supported yet")
continue
}
cfg.DA = config.DAType(da)
break
}

promptExecutionEnv := promptui.Select{
Expand All @@ -79,14 +88,14 @@ func RunInteractiveMode(config *utils.RollappConfig) {
if env == "custom" {
promptBinaryPath := promptui.Prompt{
Label: "Set your runtime binary",
Default: config.RollappBinary,
Default: cfg.RollappBinary,
AllowEdit: true,
Validate: func(s string) error {
_, err := os.Stat(s)
return err
},
}
path, _ := promptBinaryPath.Run()
config.RollappBinary = path
cfg.RollappBinary = path
}
}
9 changes: 5 additions & 4 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

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

func generateKeys(rollappConfig utils.RollappConfig) ([]utils.AddressData, error) {
func generateKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, error) {
sequencerAddresses, err := generateSequencersKeys(rollappConfig)
if err != nil {
return nil, err
Expand All @@ -22,7 +23,7 @@ func generateKeys(rollappConfig utils.RollappConfig) ([]utils.AddressData, error
return append(sequencerAddresses, relayerAddresses...), nil
}

func generateSequencersKeys(initConfig utils.RollappConfig) ([]utils.AddressData, error) {
func generateSequencersKeys(initConfig config.RollappConfig) ([]utils.AddressData, error) {
keys := getSequencerKeysConfig()
addresses := make([]utils.AddressData, 0)
for _, key := range keys {
Expand Down Expand Up @@ -63,7 +64,7 @@ func getSequencerKeysConfig() []utils.CreateKeyConfig {
}
}

func getRelayerKeysConfig(rollappConfig utils.RollappConfig) map[string]utils.CreateKeyConfig {
func getRelayerKeysConfig(rollappConfig config.RollappConfig) map[string]utils.CreateKeyConfig {
return map[string]utils.CreateKeyConfig{
consts.KeysIds.RollappRelayer: {
Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer),
Expand Down Expand Up @@ -92,7 +93,7 @@ func createAddressBinary(keyConfig utils.CreateKeyConfig, binaryPath string, hom
return utils.ParseAddressFromOutput(out)
}

func generateRelayerKeys(rollappConfig utils.RollappConfig) ([]utils.AddressData, error) {
func generateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, error) {
relayerAddresses := make([]utils.AddressData, 0)
keys := getRelayerKeysConfig(rollappConfig)
createRollappKeyCmd := getAddRlyKeyCmd(keys[consts.KeysIds.RollappRelayer], rollappConfig.RollappID)
Expand Down
5 changes: 3 additions & 2 deletions cmd/config/init/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (

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

func printInitOutput(rollappConfig utils.RollappConfig, addresses []utils.AddressData, rollappId string) {
func printInitOutput(rollappConfig config.RollappConfig, addresses []utils.AddressData, rollappId string) {
fmt.Printf("💈 RollApp '%s' configuration files have been successfully generated on your local machine. Congratulations!\n\n", rollappId)
fmt.Println(FormatTokenSupplyLine(rollappConfig))
fmt.Println()
Expand All @@ -32,7 +33,7 @@ func formatAddresses(addresses []utils.AddressData) []utils.AddressData {
return filteredAddresses
}

func FormatTokenSupplyLine(rollappConfig utils.RollappConfig) string {
func FormatTokenSupplyLine(rollappConfig config.RollappConfig) string {
displayDenom := strings.ToUpper(rollappConfig.Denom[1:])
return fmt.Sprintf("💰 Total Token Supply: %s %s. Note that 1 %s == 1 * 10^%d %s (like 1 ETH == 1 * 10^18 wei).",
addCommasToNum(rollappConfig.TokenSupply), displayDenom, displayDenom, rollappConfig.Decimals, "u"+displayDenom)
Expand Down
Loading

0 comments on commit 8b50d34

Please sign in to comment.