Skip to content

Commit

Permalink
feat: fetch rollapp info from chain (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs committed Aug 12, 2024
1 parent 95a9bff commit 130eb89
Show file tree
Hide file tree
Showing 22 changed files with 393 additions and 162 deletions.
4 changes: 2 additions & 2 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"strings"

"github.com/BurntSushi/toml"
cmdutils "github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/utils/config"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
cmdutils "github.com/dymensionxyz/roller/cmd/utils"
globalutils "github.com/dymensionxyz/roller/utils"
"github.com/dymensionxyz/roller/utils/config"
)

func AddFlags(cmd *cobra.Command) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/init/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"
"strings"

"github.com/dymensionxyz/roller/utils/config"
"github.com/pterm/pterm"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
datalayer "github.com/dymensionxyz/roller/data_layer"
"github.com/dymensionxyz/roller/utils/config"
)

func formatAddresses(
Expand Down
20 changes: 4 additions & 16 deletions cmd/config/init/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/tidwall/sjson"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/utils/bash"
"github.com/dymensionxyz/roller/utils/config"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
)

// const (
Expand Down Expand Up @@ -80,10 +80,6 @@ func UpdateGenesisParams(home string, raCfg *config.RollappConfig) error {
if err != nil {
return err
}
cfg, err := tomlconfig.LoadRollerConfig(home)
if err != nil {
return err
}

sa, err := GetRollappSequencerAddress(home)
if err != nil {
Expand All @@ -92,16 +88,8 @@ func UpdateGenesisParams(home string, raCfg *config.RollappConfig) error {
params := getDefaultGenesisParams(sa, oa, raCfg)

// TODO: move to generalized helper
addGenAccountCmd := exec.Command(
consts.Executables.RollappEVM,
"add-genesis-account",
consts.KeysIds.RollappSequencer,
fmt.Sprintf("%s%s", consts.DefaultTokenSupply, cfg.BaseDenom),
"--home",
fmt.Sprintf("%s/%s", home, consts.ConfigDirName.Rollapp),
"--keyring-backend",
"test",
)
amount := fmt.Sprintf("%s%s", consts.DefaultTokenSupply, raCfg.BaseDenom)
addGenAccountCmd := GetAddGenesisAccountCmd(consts.KeysIds.RollappSequencer, amount, raCfg)

_, err = bash.ExecCommandWithStdout(addGenAccountCmd)
if err != nil {
Expand Down Expand Up @@ -150,7 +138,7 @@ func getGenesisOperatorAddress(home string) (string, error) {
return "", err
}

a := addr.String()
a := strings.TrimSpace(addr.String())
return a, nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/config/init/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"os/exec"
"path/filepath"

"github.com/dymensionxyz/roller/utils/config"
"github.com/pterm/pterm"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/relayer"
"github.com/dymensionxyz/roller/utils/config"
)

type RelayerFileChainConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"os"
"path/filepath"

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

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/utils/errorhandling"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/eibc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"os/exec"
"path/filepath"

"github.com/dymensionxyz/roller/utils/bash"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
global_utils "github.com/dymensionxyz/roller/utils"
"github.com/dymensionxyz/roller/utils/bash"
)

func initCmd() *cobra.Command {
Expand Down
2 changes: 1 addition & 1 deletion cmd/eibc/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package eibc
import (
"os/exec"

"github.com/dymensionxyz/roller/utils/bash"
"github.com/spf13/cobra"

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

func scaleCmd() *cobra.Command {
Expand Down
2 changes: 1 addition & 1 deletion cmd/eibc/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"os/exec"
"path/filepath"

"github.com/dymensionxyz/roller/utils/bash"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
global_utils "github.com/dymensionxyz/roller/utils"
"github.com/dymensionxyz/roller/utils/bash"
)

func startCmd() *cobra.Command {
Expand Down
173 changes: 173 additions & 0 deletions cmd/rollapp/init/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package initrollapp

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"

comettypes "github.com/cometbft/cometbft/types"
"github.com/pterm/pterm"
"github.com/spf13/cobra"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/utils/bash"
"github.com/dymensionxyz/roller/utils/rollapp"
)

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init [rollapp-id]",
Short: "Inititlize a RollApp",
Long: ``,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := initconfig.AddFlags(cmd)
if err != nil {
pterm.Error.Println("failed to add flags")
return
}
home := cmd.Flag(utils.FlagNames.Home).Value.String()
genesisPath := initconfig.GetGenesisFilePath(home)

options := []string{"mock", "dymension"}
backend, _ := pterm.DefaultInteractiveSelect.
WithDefaultText("select the settlement layer backend").
WithOptions(options).
Show()

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()
}

if backend == "mock" {
err := runInit(cmd, backend, raID)
if err != nil {
fmt.Println("failed to run init: ", err)
return
}
return
}

envs := []string{"devnet", "testnet", "mainnet"}
env, _ := pterm.DefaultInteractiveSelect.
WithDefaultText("select the node type you want to run").
WithOptions(envs).
Show()

// TODO: check whether the rollapp exists
err = runInit(cmd, env, raID)
if err != nil {
fmt.Printf("failed to initialize the RollApp: %v\n", err)
return
}

genesisUrl, err := pterm.DefaultInteractiveTextInput.WithDefaultText(
"provide a genesis file url",
).Show()
if err != nil {
return
}

err = downloadFile(genesisUrl, genesisPath)
if err != nil {
pterm.Error.Println("failed to retrieve genesis file: ", err)
return
}

// move to helper function with a spinner?
genesis, err := comettypes.GenesisDocFromFile(genesisPath)
if err != nil {
pterm.Error.Println("failed to read genesis file: ", err)
return
}

hash, err := calculateSHA256(genesisPath)
if err != nil {
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
}

// TODO: download the file in chunks if possible
func downloadFile(url, filepath string) error {
spinner, _ := pterm.DefaultSpinner.
Start("Downloading genesis file from ", url)

// nolint:gosec
resp, err := http.Get(url)
if err != nil {
spinner.Fail("failed to download file: ", err)
return err
}
defer resp.Body.Close()

Check failure on line 126 in cmd/rollapp/init/init.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `resp.Body.Close` is not checked (errcheck)

out, err := os.Create(filepath)
if err != nil {
spinner.Fail("failed to download file: ", err)
return err
}
defer out.Close()

Check failure on line 133 in cmd/rollapp/init/init.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `out.Close` is not checked (errcheck)

spinner.Success("Successfully downloaded the genesis file")
_, err = io.Copy(out, resp.Body)
return err
}

func calculateSHA256(filepath string) (string, error) {
file, err := os.Open(filepath)
if err != nil {
return "", fmt.Errorf("error opening file: %v", err)
}
defer file.Close()

Check failure on line 145 in cmd/rollapp/init/init.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `file.Close` is not checked (errcheck)

hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
return "", fmt.Errorf("error calculating hash: %v", err)
}

return hex.EncodeToString(hash.Sum(nil)), nil
}

func getRollappGenesisHash(raID string, hd consts.HubData) (string, error) {
var raResponse rollapp.ShowRollappResponse
getRollappCmd := exec.Command(
consts.Executables.Dymension,
"q", "rollapp", "show",
raID, "-o", "json", "--node", hd.RPC_URL,
)

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

err = json.Unmarshal(out.Bytes(), &raResponse)
if err != nil {
return "", err
}
return raResponse.Rollapp.GenesisChecksum, nil
}
63 changes: 0 additions & 63 deletions cmd/rollapp/init/main.go

This file was deleted.

Loading

0 comments on commit 130eb89

Please sign in to comment.