Skip to content

Commit

Permalink
fix: check whether rollapp exists during initialization (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Aug 12, 2024
1 parent f121df4 commit ded5e48
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 51 deletions.
13 changes: 9 additions & 4 deletions cmd/rollapp/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,17 @@ func Cmd() *cobra.Command {
WithDefaultText("select the node type you want to run").
WithOptions(envs).
Show()
hd := consts.Hubs[env]

isRollappRegistered, _ := rollapp.IsRollappRegistered(raID, hd)

// TODO: check whether the rollapp exists

if !isRollappRegistered {
pterm.Error.Printf("%s was not found as a registered rollapp", raID)
return
}

err = runInit(cmd, env, raID)
if err != nil {
fmt.Printf("failed to initialize the RollApp: %v\n", err)
Expand Down Expand Up @@ -98,15 +107,11 @@ func Cmd() *cobra.Command {
pterm.Error.Println("failed to calculate hash of genesis file: ", err)
}

hd := consts.Hubs[env]

fmt.Println(genesis.ChainID)
fmt.Println("hash of the downloaded file: ", hash)
// nolint:ineffassign
raHash, _ := getRollappGenesisHash(raID, hd)
fmt.Println("hash of the rollapp: ", raHash)

pterm.Success.Println("finished")
},
}
return cmd
Expand Down
1 change: 1 addition & 0 deletions cmd/rollapp/init/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func runInit(cmd *cobra.Command, env string, raID string) error {

hd := consts.Hubs[env]
rollerTomlData := map[string]string{
"rollapp_id": raID,
"rollapp_binary": strings.ToLower(consts.Executables.RollappEVM),
"home": home,

Expand Down
20 changes: 8 additions & 12 deletions cmd/rollapp/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/dymensionxyz/roller/utils/config"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/dymensionxyz/roller/utils/errorhandling"
rollapputils "github.com/dymensionxyz/roller/utils/rollapp"
sequencerutils "github.com/dymensionxyz/roller/utils/sequencer"
)

Expand All @@ -56,29 +55,26 @@ func Cmd() *cobra.Command {
return
}

var raID string
if len(args) != 0 {
raID = args[0]
} else {
raID, _ = pterm.DefaultInteractiveTextInput.WithDefaultText(
"provide a rollapp ID that you want to run the node for",
).Show()
}

home, err := globalutils.ExpandHomePath(cmd.Flag(utils.FlagNames.Home).Value.String())
if err != nil {
pterm.Error.Println("failed to expand home directory")
return
}

rollerData, err := tomlconfig.LoadRollerConfig(home)
if err != nil {
pterm.Error.Println("failed to load roller config file", err)
return
}

hd, err := tomlconfig.LoadHubData(home)
if err != nil {
pterm.Error.Println("failed to load hub data from roller.toml")
}

rollappConfig, err := tomlconfig.LoadRollappMetadataFromChain(
home,
raID,
rollerData.RollappID,
&hd,
)
errorhandling.PrettifyErrorIfExists(err)
Expand Down Expand Up @@ -137,7 +133,7 @@ func Cmd() *cobra.Command {
rollappConfig.RollappID,
)

seq, err := rollapputils.GetRegisteredSequencers(rollappConfig.RollappID)
seq, err := sequencerutils.GetRegisteredSequencers(rollappConfig.RollappID)
if err != nil {
pterm.Error.Println("failed to retrieve registered sequencers: ", err)
}
Expand Down
60 changes: 25 additions & 35 deletions utils/rollapp/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/dymensionxyz/roller/cmd/consts"
globalutils "github.com/dymensionxyz/roller/utils/bash"
sequencerutils "github.com/dymensionxyz/roller/utils/sequencer"
)

func GetCurrentHeight() (*BlockInformation, error) {
Expand Down Expand Up @@ -39,17 +38,8 @@ func getCurrentBlockCmd() *exec.Cmd {
return cmd
}

func GetInitialSequencerAddress(raID string) (string, error) {
cmd := exec.Command(
consts.Executables.Dymension,
"q",
"rollapp",
"show",
raID,
"-o",
"json",
)

func GetInitialSequencerAddress(raID string, hd consts.HubData) (string, error) {
cmd := GetShowRollappCmd(raID, hd)
out, err := globalutils.ExecCommandWithStdout(cmd)
if err != nil {
fmt.Println(err)
Expand All @@ -61,8 +51,8 @@ func GetInitialSequencerAddress(raID string) (string, error) {
return ra.Rollapp.InitialSequencer, nil
}

func IsInitialSequencer(addr, raID string) (bool, error) {
initSeqAddr, err := GetInitialSequencerAddress(raID)
func IsInitialSequencer(addr, raID string, hd consts.HubData) (bool, error) {
initSeqAddr, err := GetInitialSequencerAddress(raID, hd)
if err != nil {
return false, err
}
Expand All @@ -76,33 +66,33 @@ func IsInitialSequencer(addr, raID string) (bool, error) {
return false, nil
}

func GetRegisteredSequencers(
raID string,
) (*sequencerutils.Sequencers, error) {
var seq sequencerutils.Sequencers
cmd := exec.Command(
consts.Executables.Dymension,
"q",
"sequencer",
"show-sequencers-by-rollapp",
raID,
"--output", "json",
)

out, err := globalutils.ExecCommandWithStdout(cmd)
if err != nil {
return nil, err
}

err = json.Unmarshal(out.Bytes(), &seq)
// TODO: most of rollapp utility functions should be tied to an entity
func IsRollappRegistered(raID string, hd consts.HubData) (bool, error) {
cmd := GetShowRollappCmd(raID, hd)
_, err := globalutils.ExecCommandWithStdout(cmd)
if err != nil {
return nil, err
// tODO: handle NotFound error
return false, err
}

return &seq, nil
return true, nil
}

type BlockInformation struct {
BlockId tmtypes.BlockID `json:"block_id"`
Block tmtypes.Block `json:"block"`
}

func GetShowRollappCmd(raID string, hd consts.HubData) *exec.Cmd {
cmd := exec.Command(
consts.Executables.Dymension,
"q",
"rollapp",
"show",
raID,
"-o", "json",
"--node", hd.RPC_URL,
)

return cmd
}
26 changes: 26 additions & 0 deletions utils/sequencer/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,29 @@ func GetLatestSnapshot(raID string) (*SnapshotInfo, error) {

return latestSnapshot, nil
}

func GetRegisteredSequencers(
raID string,
) (*Sequencers, error) {
var seq Sequencers
cmd := exec.Command(
consts.Executables.Dymension,
"q",
"sequencer",
"show-sequencers-by-rollapp",
raID,
"--output", "json",
)

out, err := bash.ExecCommandWithStdout(cmd)
if err != nil {
return nil, err
}

err = json.Unmarshal(out.Bytes(), &seq)
if err != nil {
return nil, err
}

return &seq, nil
}

0 comments on commit ded5e48

Please sign in to comment.