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

feat: Add ability to switch between different hub environments #55

Merged
merged 1 commit into from
Jun 14, 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
43 changes: 24 additions & 19 deletions cmd/config/init/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,41 @@ var FlagNames = struct {
DAEndpoint string
Decimals string
RollappBinary string
HubRPC string
HubID string
}{
Home: "home",
DAEndpoint: "data-availability-endpoint",
Decimals: "decimals",
RollappBinary: "rollapp-binary",
HubRPC: "hub-rpc",
HubID: "hub",
}

const TestnetHubID = "35-C"
const StagingHubID = "internal-devnet"
const LocalHubID = "local"

var HubData = struct {
var Hubs = map[string]HubData{
TestnetHubID: {
API_URL: "https://rest-hub-35c.dymension.xyz",
ID: "35-C",
RPC_URL: "https://rpc-hub-35c.dymension.xyz:443",
},
StagingHubID: {
API_URL: "https://rest-hub-devnet.dymension.xyz",
ID: "internal-devnet",
RPC_URL: "https://rpc-hub-devnet.dymension.xyz:443",
},
LocalHubID: {
API_URL: "http://localhost:1317",
ID: "local",
RPC_URL: "http://localhost:36657",
},
}

type HubData = struct {
API_URL string
ID string
RPC_URL string
}{
API_URL: "https://rest-hub-35c.dymension.xyz",
ID: "35-C",
RPC_URL: "https://rpc-hub-35c.dymension.xyz:443",
}

var Executables = struct {
Celestia string
Rollapp string
Relayer string
Dymension string
}{
Celestia: "/usr/local/bin/roller_bins/celestia",
Rollapp: "/usr/local/bin/rollapp_evm",
Relayer: "/usr/local/bin/roller_bins/rly",
Dymension: "/usr/local/bin/roller_bins/dymd",
}

const defaultRollappRPC = "http://localhost:26657"
Expand Down
22 changes: 20 additions & 2 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
package initconfig

import (
"fmt"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/spf13/cobra"

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

func addFlags(cmd *cobra.Command) {
cmd.Flags().StringP(FlagNames.HubRPC, "", HubData.RPC_URL, "Dymension Hub rpc endpoint")
cmd.Flags().StringP(FlagNames.HubID, "", TestnetHubID, fmt.Sprintf("The ID of the Dymension hub. %s", getAvailableHubsMessage()))
cmd.Flags().StringP(FlagNames.DAEndpoint, "", "", "The data availability light node endpoint. Runs an Arabica Celestia light node if not provided")
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, "", utils.GetRollerRootDir(), "The directory of the roller config files")

cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
hubID, err := cmd.Flags().GetString(FlagNames.HubID)
if err != nil {
return err
}
if _, ok := Hubs[hubID]; !ok {
return fmt.Errorf("invalid hub ID: %s. %s", hubID, getAvailableHubsMessage())
}
return nil
}
}

func getDecimals(cmd *cobra.Command) uint64 {
Expand All @@ -38,13 +51,18 @@ func GetInitConfig(cmd *cobra.Command, args []string) InitConfig {
createLightNode := !cmd.Flags().Changed(FlagNames.DAEndpoint)
rollappBinaryPath := getRollappBinaryPath(cmd)
decimals := getDecimals(cmd)
hubID := cmd.Flag(FlagNames.HubID).Value.String()
return InitConfig{
Home: home,
RollappID: rollappId,
RollappBinary: rollappBinaryPath,
CreateDALightNode: createLightNode,
Denom: denom,
HubID: HubData.ID,
Decimals: decimals,
HubData: Hubs[hubID],
}
}

func getAvailableHubsMessage() string {
return fmt.Sprintf("Acceptable values are '%s', '%s' or '%s'", TestnetHubID, StagingHubID, LocalHubID)
}
12 changes: 6 additions & 6 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package initconfig
import (
"os"

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

type InitConfig struct {
Expand All @@ -15,7 +15,7 @@ type InitConfig struct {
CreateDALightNode bool
Denom string
Decimals uint64
HubID string
HubData HubData
}

func InitCmd() *cobra.Command {
Expand All @@ -24,7 +24,7 @@ func InitCmd() *cobra.Command {
Short: "Initialize a RollApp configuration on your local machine.",
Run: func(cmd *cobra.Command, args []string) {
initConfig := GetInitConfig(cmd, args)
utils.PrettifyErrorIfExists(VerifyUniqueRollappID(initConfig.RollappID))
utils.PrettifyErrorIfExists(VerifyUniqueRollappID(initConfig.RollappID, initConfig))
isRootExist, err := dirNotEmpty(initConfig.Home)
utils.PrettifyErrorIfExists(err)
if isRootExist {
Expand All @@ -49,8 +49,8 @@ func InitCmd() *cobra.Command {
Denom: initConfig.Denom,
AddressPrefix: consts.AddressPrefixes.Rollapp,
}, ChainConfig{
ID: HubData.ID,
RPC: cmd.Flag(FlagNames.HubRPC).Value.String(),
ID: initConfig.HubData.ID,
RPC: initConfig.HubData.RPC_URL,
Denom: "udym",
AddressPrefix: consts.AddressPrefixes.Hub,
}, initConfig))
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func getDefaultKeysConfig(initConfig InitConfig) []utils.KeyConfig {
Prefix: consts.AddressPrefixes.Rollapp,
},
{
Dir: path.Join(consts.ConfigDirName.Relayer, KeysDirName, HubData.ID),
Dir: path.Join(consts.ConfigDirName.Relayer, KeysDirName, initConfig.HubData.ID),
ID: consts.KeyNames.HubRelayer,
CoinType: consts.CoinTypes.Cosmos,
Prefix: consts.AddressPrefixes.Hub,
Expand Down
8 changes: 4 additions & 4 deletions cmd/config/init/unique_rollapp_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"net/http"
)

func IsRollappIDUnique(rollappID string) (bool, error) {
url := HubData.API_URL + "/dymensionxyz/dymension/rollapp/rollapp/" + rollappID
func IsRollappIDUnique(rollappID string, initConfig InitConfig) (bool, error) {
url := initConfig.HubData.API_URL + "/dymensionxyz/dymension/rollapp/rollapp/" + rollappID

req, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand All @@ -31,8 +31,8 @@ func IsRollappIDUnique(rollappID string) (bool, error) {
}
}

func VerifyUniqueRollappID(rollappID string) error {
isUniqueRollapp, err := IsRollappIDUnique(rollappID)
func VerifyUniqueRollappID(rollappID string, initConfig InitConfig) error {
isUniqueRollapp, err := IsRollappIDUnique(rollappID, initConfig)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func RegisterCmd() *cobra.Command {
home := cmd.Flag(initconfig.FlagNames.Home).Value.String()
rollappConfig, err := initconfig.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
utils.PrettifyErrorIfExists(initconfig.VerifyUniqueRollappID(rollappConfig.RollappID))
utils.PrettifyErrorIfExists(initconfig.VerifyUniqueRollappID(rollappConfig.RollappID, rollappConfig))
utils.PrettifyErrorIfExists(registerRollapp(rollappConfig))
},
}
Expand Down Expand Up @@ -81,6 +81,6 @@ func getRegisterRollappCmd(rollappConfig initconfig.InitConfig) *exec.Cmd {
"--keyring-backend", "test",
"--keyring-dir", filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp),
rollappConfig.RollappID, "stamp1", "genesis-path/1", "3", "3", `{"Addresses":[]}`, "--output", "json",
"--node", initconfig.HubData.RPC_URL, "--yes", "--broadcast-mode", "block",
"--node", rollappConfig.HubData.RPC_URL, "--yes", "--broadcast-mode", "block",
)
}
2 changes: 1 addition & 1 deletion test/config/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestInitCmd(t *testing.T) {
assert.NoError(testutils.VerifyRollerConfig(initConfig))
assert.NoError(os.Remove(filepath.Join(tempDir, initconfig.RollerConfigFileName)))
assert.NoError(testutils.VerifyRollappKeys(tempDir))
assert.NoError(testutils.VerifyRelayerKeys(tempDir, rollappID, initconfig.HubData.ID))
assert.NoError(testutils.VerifyRelayerKeys(tempDir, rollappID, initConfig.HubData.ID))
if !testutils.Contains(tc.optionalFlags, "--"+initconfig.FlagNames.DAEndpoint) {
assert.NoError(testutils.VerifyLightNodeKeys(tempDir))
}
Expand Down