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: Enhance error handling for 'roller register' common flows #53

Merged
merged 6 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 21 additions & 3 deletions cmd/config/init/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var KeyNames = struct {
HubRelayer: "relayer-hub-key",
}

var addressPrefixes = struct {
var AddressPrefixes = struct {
Hub string
Rollapp string
DA string
Expand Down Expand Up @@ -58,9 +58,27 @@ 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
var CoinTypes = struct {
Cosmos uint32
EVM uint32
}{
Cosmos: 118,
EVM: 60,
}

const KeysDirName = "keys"
const cosmosDefaultCointype uint32 = 118
const RollerConfigFileName = "config.toml"
11 changes: 3 additions & 8 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package initconfig

import (
"fmt"
"os"

"github.com/spf13/cobra"
Expand All @@ -24,11 +23,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 +46,12 @@ func InitCmd() *cobra.Command {
ID: initConfig.RollappID,
RPC: defaultRollappRPC,
Denom: initConfig.Denom,
AddressPrefix: addressPrefixes.Rollapp,
AddressPrefix: AddressPrefixes.Rollapp,
}, ChainConfig{
ID: HubData.ID,
RPC: cmd.Flag(FlagNames.HubRPC).Value.String(),
Denom: "udym",
AddressPrefix: addressPrefixes.Hub,
AddressPrefix: AddressPrefixes.Hub,
}, initConfig))
utils.PrettifyErrorIfExists(WriteConfigToTOML(initConfig))
printInitOutput(addresses, initConfig.RollappID)
Expand Down
86 changes: 55 additions & 31 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (

"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
)

func keyInfoToBech32Address(info keyring.Info, prefix string) (string, error) {
pk := info.GetPubKey()
bech32Address, err := bech32.ConvertAndEncode(prefix, pk.Bytes())
addr := types.AccAddress(pk.Address())
bech32Address, err := bech32.ConvertAndEncode(prefix, addr.Bytes())
if err != nil {
return "", err
}
Expand All @@ -26,40 +28,40 @@ 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 := 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
Dir string
ID string
CoinType uint32
Prefix string
}

func createKey(keyConfig 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
}
Expand All @@ -69,34 +71,34 @@ func createKey(keyConfig KeyConfig, home string) (keyring.Info, error) {
func getDefaultKeysConfig(initConfig InitConfig) []KeyConfig {
return []KeyConfig{
{
dir: ConfigDirName.Rollapp,
keyId: KeyNames.HubSequencer,
coinType: cosmosDefaultCointype,
prefix: addressPrefixes.Hub,
Dir: ConfigDirName.Rollapp,
ID: KeyNames.HubSequencer,
CoinType: CoinTypes.Cosmos,
Prefix: AddressPrefixes.Hub,
},
{
dir: ConfigDirName.Rollapp,
keyId: KeyNames.RollappSequencer,
coinType: evmCoinType,
prefix: addressPrefixes.Rollapp,
Dir: ConfigDirName.Rollapp,
ID: KeyNames.RollappSequencer,
CoinType: CoinTypes.EVM,
Prefix: AddressPrefixes.Rollapp,
},
{
dir: path.Join(ConfigDirName.Relayer, KeysDirName, HubData.ID),
keyId: KeyNames.HubRelayer,
coinType: cosmosDefaultCointype,
prefix: addressPrefixes.Hub,
Dir: path.Join(ConfigDirName.Relayer, KeysDirName, HubData.ID),
ID: KeyNames.HubRelayer,
CoinType: CoinTypes.Cosmos,
Prefix: AddressPrefixes.Hub,
},
{
dir: path.Join(ConfigDirName.Relayer, KeysDirName, initConfig.RollappID),
keyId: KeyNames.RollappRelayer,
coinType: evmCoinType,
prefix: addressPrefixes.Rollapp,
Dir: path.Join(ConfigDirName.Relayer, KeysDirName, initConfig.RollappID),
ID: KeyNames.RollappRelayer,
CoinType: CoinTypes.EVM,
Prefix: AddressPrefixes.Rollapp,
}, {

dir: path.Join(ConfigDirName.DALightNode, KeysDirName),
keyId: KeyNames.DALightNode,
coinType: cosmosDefaultCointype,
prefix: addressPrefixes.DA,
Dir: path.Join(ConfigDirName.DALightNode, KeysDirName),
ID: KeyNames.DALightNode,
CoinType: CoinTypes.Cosmos,
Prefix: AddressPrefixes.DA,
},
}
}
Expand All @@ -116,3 +118,25 @@ func initializeKeys(initConfig InitConfig) map[string]string {
return addresses
}
}

func GetAddress(keyConfig KeyConfig) (string, error) {
kr, err := keyring.New(
"",
keyring.BackendTest,
keyConfig.Dir,
nil,
)
if err != nil {
return "", err
}
keyInfo, err := kr.Key(keyConfig.ID)
if err != nil {
return "", err
}
formattedAddress, err := keyInfoToBech32Address(keyInfo, keyConfig.Prefix)
if err != nil {
return "", err
}

return formattedAddress, nil
}
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
}
53 changes: 46 additions & 7 deletions cmd/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package register

import (
"bytes"
"errors"
"os/exec"
"path/filepath"

"fmt"

"strings"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"

"github.com/dymensionxyz/roller/cmd/utils"
"github.com/spf13/cobra"
)
Expand All @@ -19,6 +25,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(registerRollapp(rollappConfig))
},
}
Expand All @@ -31,18 +38,50 @@ func addFlags(cmd *cobra.Command) {
}

func registerRollapp(rollappConfig initconfig.InitConfig) error {
cmd := exec.Command(
cmd := getRegisterRollappCmd(rollappConfig)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmdExecErr := cmd.Run()
if err := handleStdErr(stderr, rollappConfig); err != nil {
return err
}
if cmdExecErr != nil {
return cmdExecErr
}
return nil
}

func handleStdErr(stderr bytes.Buffer, rollappConfig initconfig.InitConfig) error {
stderrStr := stderr.String()
if len(stderrStr) > 0 {
if strings.Contains(stderrStr, "key not found") {
sequencerAddress, err := initconfig.GetAddress(
ItayLevyOfficial marked this conversation as resolved.
Show resolved Hide resolved
initconfig.KeyConfig{
ID: initconfig.KeyNames.HubSequencer,
ItayLevyOfficial marked this conversation as resolved.
Show resolved Hide resolved
Prefix: initconfig.AddressPrefixes.Hub,
Dir: filepath.Join(rollappConfig.Home, initconfig.ConfigDirName.Rollapp),
CoinType: initconfig.CoinTypes.Cosmos,
},
)
if err != nil {
return err
}
return fmt.Errorf("Insufficient funds in the sequencer's address to register the RollApp. Please deposit DYM to the following address: %s and attempt the registration again", sequencerAddress)
}
return errors.New(stderrStr)
}
return nil
}

func getRegisterRollappCmd(rollappConfig initconfig.InitConfig) *exec.Cmd {
return exec.Command(
consts.Executables.Dymension, "tx", "rollapp", "create-rollapp",
"--from", initconfig.KeyNames.HubSequencer,
"--keyring-backend", "test",
"--keyring-dir", filepath.Join(rollappConfig.Home, initconfig.ConfigDirName.Rollapp),
rollappConfig.RollappID, "stamp1", "genesis-path/1", "3", "3", `{"Addresses":[]}`, "--output", "json",
"--node", initconfig.HubData.RPC_URL, "--yes", "--broadcast-mode", "block",
)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
error := cmd.Run()
return error
}