Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add rollapp ID xxxx_num_num enforcement on roller config init #84

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package initconfig

import (
"fmt"
"regexp"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
Expand All @@ -21,10 +22,20 @@ func addFlags(cmd *cobra.Command) {
if _, ok := Hubs[hubID]; !ok {
return fmt.Errorf("invalid hub ID: %s. %s", hubID, getAvailableHubsMessage())
}
rollappID := args[0]
if !validateRollAppID(rollappID) {
return fmt.Errorf("invalid RollApp ID '%s'. %s", rollappID, getValidRollappIdMessage())
}
return nil
}
}

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

func getDecimals(cmd *cobra.Command) uint64 {
decimals, err := cmd.Flags().GetUint64(FlagNames.Decimals)
if err != nil {
Expand Down Expand Up @@ -61,3 +72,9 @@ func GetInitConfig(initCmd *cobra.Command, args []string) InitConfig {
func getAvailableHubsMessage() string {
return fmt.Sprintf("Acceptable values are '%s', '%s' or '%s'", TestnetHubID, StagingHubID, LocalHubID)
}

func validateRollAppID(id string) bool {
pattern := `^[a-z]+_[0-9]{1,5}_[0-9]{1,5}$`
r, _ := regexp.Compile(pattern)
return r.MatchString(id)
}
7 changes: 6 additions & 1 deletion cmd/config/init/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package initconfig

import (
"fmt"
"os"

"github.com/dymensionxyz/roller/cmd/consts"
Expand All @@ -19,8 +20,12 @@ type InitConfig struct {

func InitCmd() *cobra.Command {
initCmd := &cobra.Command{
Use: "init <chain-id> <denom>",
Use: "init <rollapp-id> <denom>",
Short: "Initialize a RollApp configuration on your local machine.",
Long: fmt.Sprintf(`Initialize a RollApp configuration on your local machine.

%s
`, getValidRollappIdMessage()),
Run: func(cmd *cobra.Command, args []string) {
initConfig := GetInitConfig(cmd, args)
utils.PrettifyErrorIfExists(VerifyUniqueRollappID(initConfig.RollappID, initConfig))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ chains:
output-format: json
sign-mode: direct
client-type: 07-tendermint
mars:
mars_1_1:
type: cosmos
value:
key: relayer-rollapp-key
chain-id: mars
chain-id: mars_1_1
rpc-addr: http://localhost:26657
account-prefix: rol
keyring-backend: test
Expand All @@ -37,7 +37,7 @@ chains:
paths:
hub-rollapp:
src:
chain-id: mars
chain-id: mars_1_1
dst:
chain-id: 35-C
src-channel-filter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
###############################################################################

# The network chain ID
chain-id = "mars"
chain-id = "mars_1_1"
# The keyring's backend, where the keys are stored (os|file|kwallet|pass|test|memory)
keyring-backend = "os"
# CLI output format (text|json)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"genesis_time": "PLACEHOLDER_TIMESTAMP",
"chain_id": "mars",
"chain_id": "mars_1_1",
"initial_height": "1",
"consensus_params": {
"block": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ chains:
output-format: json
sign-mode: direct
client-type: 07-tendermint
mars:
mars_1_1:
type: cosmos
value:
key: relayer-rollapp-key
chain-id: mars
chain-id: mars_1_1
rpc-addr: http://localhost:26657
account-prefix: rol
keyring-backend: test
Expand All @@ -37,7 +37,7 @@ chains:
paths:
hub-rollapp:
src:
chain-id: mars
chain-id: mars_1_1
dst:
chain-id: 35-C
src-channel-filter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
###############################################################################

# The network chain ID
chain-id = "mars"
chain-id = "mars_1_1"
# The keyring's backend, where the keys are stored (os|file|kwallet|pass|test|memory)
keyring-backend = "os"
# CLI output format (text|json)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"genesis_time": "PLACEHOLDER_TIMESTAMP",
"chain_id": "mars",
"chain_id": "mars_1_1",
"initial_height": "1",
"consensus_params": {
"block": {
Expand Down
2 changes: 1 addition & 1 deletion test/config/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestInitCmd(t *testing.T) {
}()
initCmd := initconfig.InitCmd()
denom := "udym"
rollappID := "mars"
rollappID := "mars_1_1"
initCmd.SetArgs(append([]string{
rollappID,
denom,
Expand Down