Skip to content

Commit

Permalink
fix: Enhance error handling for 'roller register' common flows (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial authored Jun 13, 2023
1 parent aee609f commit b0c360d
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 138 deletions.
2 changes: 1 addition & 1 deletion cmd/config/init/DALightClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func initializeLightNodeConfig(initConfig InitConfig) error {
initLightNodeCmd := exec.Command(consts.Executables.Celestia, "light", "init", "--p2p.network", "arabica", "--node.store", filepath.Join(initConfig.Home, ConfigDirName.DALightNode))
initLightNodeCmd := exec.Command(consts.Executables.Celestia, "light", "init", "--p2p.network", "arabica", "--node.store", filepath.Join(initConfig.Home, consts.ConfigDirName.DALightNode))
err := initLightNodeCmd.Run()
if err != nil {
return err
Expand Down
47 changes: 12 additions & 35 deletions cmd/config/init/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,6 @@ var FlagNames = struct {
HubRPC: "hub-rpc",
}

var KeyNames = struct {
HubSequencer string
RollappSequencer string
RollappRelayer string
DALightNode string
HubRelayer string
}{
HubSequencer: "hub_sequencer",
RollappSequencer: "rollapp_sequencer",
RollappRelayer: "relayer-rollapp-key",
DALightNode: "my-celes-key",
HubRelayer: "relayer-hub-key",
}

var addressPrefixes = struct {
Hub string
Rollapp string
DA string
}{
Rollapp: "rol",
Hub: "dym",
DA: "celestia",
}

var ConfigDirName = struct {
Rollapp string
Relayer string
DALightNode string
}{
Rollapp: "rollapp",
Relayer: "relayer",
DALightNode: "light-node",
}

var HubData = struct {
API_URL string
Expand All @@ -58,9 +25,19 @@ var HubData = struct {
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"

const evmCoinType uint32 = 60
const KeysDirName = "keys"
const cosmosDefaultCointype uint32 = 118
const RollerConfigFileName = "config.toml"
5 changes: 3 additions & 2 deletions cmd/config/init/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"path/filepath"

"github.com/tidwall/sjson"
"github.com/dymensionxyz/roller/cmd/consts"
)

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)
rollappConfigDirPath := filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp)
genesisSequencerAccountCmd := exec.Command(initConfig.RollappBinary, "add-genesis-account", consts.KeyNames.RollappSequencer, tokenAmount, "--keyring-backend", "test", "--home", rollappConfigDirPath)
err := genesisSequencerAccountCmd.Run()
if err != nil {
return err
Expand Down
12 changes: 4 additions & 8 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package initconfig

import (
"fmt"
"os"

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

type InitConfig struct {
Expand All @@ -24,11 +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)
isUniqueRollapp, err := isRollappIDUnique(initConfig.RollappID)
utils.PrettifyErrorIfExists(err)
if !isUniqueRollapp {
utils.PrettifyErrorIfExists(fmt.Errorf("Rollapp ID %s already exists on the hub. Please use a unique ID.", initConfig.RollappID))
}
utils.PrettifyErrorIfExists(VerifyUniqueRollappID(initConfig.RollappID))
isRootExist, err := dirNotEmpty(initConfig.Home)
utils.PrettifyErrorIfExists(err)
if isRootExist {
Expand All @@ -51,12 +47,12 @@ func InitCmd() *cobra.Command {
ID: initConfig.RollappID,
RPC: defaultRollappRPC,
Denom: initConfig.Denom,
AddressPrefix: addressPrefixes.Rollapp,
AddressPrefix: consts.AddressPrefixes.Rollapp,
}, ChainConfig{
ID: HubData.ID,
RPC: cmd.Flag(FlagNames.HubRPC).Value.String(),
Denom: "udym",
AddressPrefix: addressPrefixes.Hub,
AddressPrefix: consts.AddressPrefixes.Hub,
}, initConfig))
utils.PrettifyErrorIfExists(WriteConfigToTOML(initConfig))
printInitOutput(addresses, initConfig.RollappID)
Expand Down
79 changes: 32 additions & 47 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@ import (

"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
)

func keyInfoToBech32Address(info keyring.Info, prefix string) (string, error) {
pk := info.GetPubKey()
bech32Address, err := bech32.ConvertAndEncode(prefix, pk.Bytes())
if err != nil {
return "", err
}
return bech32Address, nil
}

func generateKeys(initConfig InitConfig, excludeKeys ...string) (map[string]string, error) {
keys := getDefaultKeysConfig(initConfig)
excludeKeysMap := make(map[string]struct{})
Expand All @@ -26,77 +18,70 @@ func generateKeys(initConfig InitConfig, excludeKeys ...string) (map[string]stri
}
addresses := make(map[string]string)
for _, key := range keys {
if _, exists := excludeKeysMap[key.keyId]; !exists {
if _, exists := excludeKeysMap[key.ID]; !exists {
keyInfo, err := createKey(key, initConfig.Home)
if err != nil {
return nil, err
}
formattedAddress, err := keyInfoToBech32Address(keyInfo, key.prefix)
formattedAddress, err := utils.KeyInfoToBech32Address(keyInfo, key.Prefix)
if err != nil {
return nil, err
}
addresses[key.keyId] = formattedAddress
addresses[key.ID] = formattedAddress
}
}
return addresses, nil
}

type KeyConfig struct {
dir string
keyId string
coinType uint32
prefix string
}

func createKey(keyConfig KeyConfig, home string) (keyring.Info, error) {
func createKey(keyConfig utils.KeyConfig, home string) (keyring.Info, error) {
kr, err := keyring.New(
"",
keyring.BackendTest,
filepath.Join(home, keyConfig.dir),
filepath.Join(home, keyConfig.Dir),
nil,
)
if err != nil {
return nil, err
}
bip44Params := hd.NewFundraiserParams(0, keyConfig.coinType, 0)
info, _, err := kr.NewMnemonic(keyConfig.keyId, keyring.English, bip44Params.String(), "", hd.Secp256k1)
bip44Params := hd.NewFundraiserParams(0, keyConfig.CoinType, 0)
info, _, err := kr.NewMnemonic(keyConfig.ID, keyring.English, bip44Params.String(), "", hd.Secp256k1)
if err != nil {
return nil, err
}
return info, nil
}

func getDefaultKeysConfig(initConfig InitConfig) []KeyConfig {
return []KeyConfig{
func getDefaultKeysConfig(initConfig InitConfig) []utils.KeyConfig {
return []utils.KeyConfig{
{
dir: ConfigDirName.Rollapp,
keyId: KeyNames.HubSequencer,
coinType: cosmosDefaultCointype,
prefix: addressPrefixes.Hub,
Dir: consts.ConfigDirName.Rollapp,
ID: consts.KeyNames.HubSequencer,
CoinType: consts.CoinTypes.Cosmos,
Prefix: consts.AddressPrefixes.Hub,
},
{
dir: ConfigDirName.Rollapp,
keyId: KeyNames.RollappSequencer,
coinType: evmCoinType,
prefix: addressPrefixes.Rollapp,
Dir: consts.ConfigDirName.Rollapp,
ID: consts.KeyNames.RollappSequencer,
CoinType: consts.CoinTypes.EVM,
Prefix: consts.AddressPrefixes.Rollapp,
},
{
dir: path.Join(ConfigDirName.Relayer, KeysDirName, HubData.ID),
keyId: KeyNames.HubRelayer,
coinType: cosmosDefaultCointype,
prefix: addressPrefixes.Hub,
Dir: path.Join(consts.ConfigDirName.Relayer, KeysDirName, HubData.ID),
ID: consts.KeyNames.HubRelayer,
CoinType: consts.CoinTypes.Cosmos,
Prefix: consts.AddressPrefixes.Hub,
},
{
dir: path.Join(ConfigDirName.Relayer, KeysDirName, initConfig.RollappID),
keyId: KeyNames.RollappRelayer,
coinType: evmCoinType,
prefix: addressPrefixes.Rollapp,
Dir: path.Join(consts.ConfigDirName.Relayer, KeysDirName, initConfig.RollappID),
ID: consts.KeyNames.RollappRelayer,
CoinType: consts.CoinTypes.EVM,
Prefix: consts.AddressPrefixes.Rollapp,
}, {

dir: path.Join(ConfigDirName.DALightNode, KeysDirName),
keyId: KeyNames.DALightNode,
coinType: cosmosDefaultCointype,
prefix: addressPrefixes.DA,
Dir: path.Join(consts.ConfigDirName.DALightNode, KeysDirName),
ID: consts.KeyNames.DALightNode,
CoinType: consts.CoinTypes.Cosmos,
Prefix: consts.AddressPrefixes.DA,
},
}
}
Expand All @@ -109,7 +94,7 @@ func initializeKeys(initConfig InitConfig) map[string]string {
}
return addresses
} else {
addresses, err := generateKeys(initConfig, KeyNames.DALightNode)
addresses, err := generateKeys(initConfig, consts.KeyNames.DALightNode)
if err != nil {
panic(err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/config/init/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/olekukonko/tablewriter"
"github.com/dymensionxyz/roller/cmd/consts"
)

func printInitOutput(addresses map[string]string, rollappId string) {
Expand All @@ -13,9 +14,9 @@ func printInitOutput(addresses map[string]string, rollappId string) {
fmt.Printf("🔑 Addresses:\n\n")

data := [][]string{
{"Celestia", addresses[KeyNames.DALightNode]},
{"Sequencer", addresses[KeyNames.HubSequencer]},
{"Relayer", addresses[KeyNames.HubRelayer]},
{"Celestia", addresses[consts.KeyNames.DALightNode]},
{"Sequencer", addresses[consts.KeyNames.HubSequencer]},
{"Relayer", addresses[consts.KeyNames.HubRelayer]},
}

table := tablewriter.NewWriter(os.Stdout)
Expand Down
6 changes: 3 additions & 3 deletions cmd/config/init/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ func addChainsConfig(rollappConfig ChainConfig, hubConfig ChainConfig, relayerHo
ChainConfig: rollappConfig,
GasPrices: "0.0" + rollappConfig.Denom,
ClientType: "01-dymint",
KeyName: KeyNames.RollappRelayer,
KeyName: consts.KeyNames.RollappRelayer,
})

relayerHubConfig := getRelayerFileChainConfig(RelayerChainConfig{
ChainConfig: hubConfig,
GasPrices: "0.25" + hubConfig.Denom,
ClientType: "07-tendermint",
KeyName: KeyNames.HubRelayer,
KeyName: consts.KeyNames.HubRelayer,
})

if err := addChainToRelayer(relayerRollappConfig, relayerHome); err != nil {
Expand All @@ -130,7 +130,7 @@ func setupPath(rollappConfig ChainConfig, hubConfig ChainConfig, relayerHome str
}

func initializeRelayerConfig(rollappConfig ChainConfig, hubConfig ChainConfig, initConfig InitConfig) error {
relayerHome := filepath.Join(initConfig.Home, ConfigDirName.Relayer)
relayerHome := filepath.Join(initConfig.Home, consts.ConfigDirName.Relayer)
if err := initRelayer(relayerHome); err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/config/init/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"path/filepath"

toml "github.com/pelletier/go-toml"
"github.com/dymensionxyz/roller/cmd/consts"
)

func initializeRollappConfig(initConfig InitConfig) {
initRollappCmd := exec.Command(initConfig.RollappBinary, "init", KeyNames.HubSequencer, "--chain-id", initConfig.RollappID, "--home", filepath.Join(initConfig.Home, ConfigDirName.Rollapp))
initRollappCmd := exec.Command(initConfig.RollappBinary, "init", consts.KeyNames.HubSequencer, "--chain-id", initConfig.RollappID, "--home", filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp))
err := initRollappCmd.Run()
if err != nil {
panic(err)
}
setRollappAppConfig(filepath.Join(initConfig.Home, ConfigDirName.Rollapp, "config/app.toml"), initConfig.Denom)
setRollappAppConfig(filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp, "config/app.toml"), initConfig.Denom)
}

func setRollappAppConfig(appConfigFilePath string, denom string) {
Expand All @@ -30,5 +31,5 @@ func setRollappAppConfig(appConfigFilePath string, denom string) {
}

func RollappConfigDir(root string) string {
return filepath.Join(root, ConfigDirName.Rollapp, "config")
return filepath.Join(root, consts.ConfigDirName.Rollapp, "config")
}
13 changes: 12 additions & 1 deletion cmd/config/init/unique_rollapp_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
)

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

req, err := http.NewRequest("GET", url, nil)
Expand All @@ -30,3 +30,14 @@ func isRollappIDUnique(rollappID string) (bool, error) {
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
}

func VerifyUniqueRollappID(rollappID string) error {
isUniqueRollapp, err := IsRollappIDUnique(rollappID)
if err != nil {
return err
}
if !isUniqueRollapp {
return fmt.Errorf("Rollapp ID \"%s\" already exists on the hub. Please use a unique ID.", rollappID)
}
return nil
}
Loading

0 comments on commit b0c360d

Please sign in to comment.