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 roller migrate for Configuration Upgrades #317

Merged
merged 14 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion cmd/config/init/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func initializeRollappConfig(initConfig config.RollappConfig) error {
if err != nil {
return err
}
if err = sequencer.SetDefaultDymintConfig(initConfig.Home, initConfig); err != nil {
if err = sequencer.SetDefaultDymintConfig(initConfig); err != nil {
return err
}
return nil
Expand Down
96 changes: 35 additions & 61 deletions cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,13 @@ import (
"fmt"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"github.com/dymensionxyz/roller/sequencer"
"github.com/dymensionxyz/roller/version"
"github.com/pelletier/go-toml"
"github.com/spf13/cobra"
"strings"
)

type VersionMigrator interface {
PerformMigration(rlpCfg config.RollappConfig) error
}

func NewVersionMigrator(version string) VersionMigrator {
switch version {
case "v0.1.4":
return &VersionMigratorV014{}
default:
return nil
}
}

type VersionData struct {
Major int
Minor int
Patch int
}

type VersionMigratorV014 struct{}

func (v *VersionMigratorV014) PerformMigration(rlpCfg config.RollappConfig) error {
dymintTomlPath := sequencer.GetDymintFilePath(rlpCfg.Home)
dymintCfg, err := toml.LoadFile(dymintTomlPath)
if err != nil {
return err
}
sequencer.EnableDymintMetrics(dymintCfg)
return config.WriteTomlToFile(dymintTomlPath, dymintCfg)
var migrationsRegistry = []VersionMigrator{
&VersionMigratorV014{},
}

func Cmd() *cobra.Command {
Expand All @@ -49,24 +20,12 @@ func Cmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rlpCfg, err := config.LoadConfigFromTOML(home)
if err != nil {
utils.PrettifyErrorIfExists(err)
return
}
prevVersionData, err := GetVersionData(rlpCfg)
if err != nil {
utils.PrettifyErrorIfExists(err)
return
}
if prevVersionData.Major < 1 {
if prevVersionData.Minor < 2 {
if prevVersionData.Patch < 4 {
migrator := NewVersionMigrator("v0.1.4")
if migrator != nil {
err := migrator.PerformMigration(rlpCfg)
utils.PrettifyErrorIfExists(err)
}
}
utils.PrettifyErrorIfExists(err)
prevVersionData, err := GetPrevVersionData(rlpCfg)
utils.PrettifyErrorIfExists(err)
for _, migrator := range migrationsRegistry {
if migrator.ShouldMigrate(*prevVersionData) {
utils.PrettifyErrorIfExists(migrator.PerformMigration(rlpCfg))
}
}
trimmedCurrentVersion := strings.Split(version.BuildVersion, "-")[0]
Expand All @@ -76,25 +35,40 @@ func Cmd() *cobra.Command {
return cmd
}

func GetVersionData(rlpCfg config.RollappConfig) (*VersionData, error) {
func GetPrevVersionData(rlpCfg config.RollappConfig) (*VersionData, error) {
rollerPrevVersion := rlpCfg.RollerVersion
var major, minor, patch int
// Special case for the first version of roller, that didn't have a version field.
if rollerPrevVersion == "" {
major, minor, patch = 0, 1, 3
} else {
trimmedVersionStr := strings.Split(rollerPrevVersion, "-")[0]
n, err := fmt.Sscanf(trimmedVersionStr, "v%d.%d.%d", &major, &minor, &patch)
if err != nil {
return nil, err
}
if n != 3 {
return nil, fmt.Errorf("failed to extract all version components from version %s",
rollerPrevVersion)
}
return &VersionData{
Major: 0,
Minor: 1,
Patch: 3,
}, nil
}
trimmedVersionStr := strings.Split(rollerPrevVersion, "-")[0]
n, err := fmt.Sscanf(trimmedVersionStr, "v%d.%d.%d", &major, &minor, &patch)
if err != nil {
return nil, err
}
if n != 3 {
return nil, fmt.Errorf("failed to extract all version components from version %s",
rollerPrevVersion)
}
return &VersionData{
Major: major,
Minor: minor,
Patch: patch,
}, nil
}

type VersionMigrator interface {
PerformMigration(rlpCfg config.RollappConfig) error
ShouldMigrate(prevVersion VersionData) bool
}

type VersionData struct {
Major int
Minor int
Patch int
}
16 changes: 16 additions & 0 deletions cmd/migrate/v_0_1_4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package migrate

import (
"github.com/dymensionxyz/roller/config"
"github.com/dymensionxyz/roller/sequencer"
)

type VersionMigratorV014 struct{}

func (v *VersionMigratorV014) ShouldMigrate(prevVersion VersionData) bool {
return prevVersion.Major < 1 && prevVersion.Minor < 2 && prevVersion.Patch < 4
}

func (v *VersionMigratorV014) PerformMigration(rlpCfg config.RollappConfig) error {
return sequencer.SetDefaultDymintConfig(rlpCfg)
ItayLevyOfficial marked this conversation as resolved.
Show resolved Hide resolved
}
20 changes: 1 addition & 19 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"io/ioutil"
"os"
"path/filepath"

"github.com/pelletier/go-toml"
Expand All @@ -13,12 +12,7 @@ func WriteConfigToTOML(InitConfig RollappConfig) error {
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(InitConfig.Home, RollerConfigFileName), tomlBytes, 0644)
if err != nil {
return err
}

return nil
return ioutil.WriteFile(filepath.Join(InitConfig.Home, RollerConfigFileName), tomlBytes, 0644)
}

// TODO: should be called from root command
Expand All @@ -35,15 +29,3 @@ func LoadConfigFromTOML(root string) (RollappConfig, error) {

return config, nil
}

func WriteTomlToFile(path string, data *toml.Tree) error {
file, err := os.Create(path)
if err != nil {
return err
}
_, err = file.WriteString(data.String())
if err != nil {
return err
}
return nil
}
16 changes: 9 additions & 7 deletions sequencer/dymint_toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"github.com/dymensionxyz/roller/config"
datalayer "github.com/dymensionxyz/roller/data_layer"
"github.com/pelletier/go-toml"
"os"
"path/filepath"
)

func SetDefaultDymintConfig(root string, rlpCfg config.RollappConfig) error {
dymintTomlPath := GetDymintFilePath(root)
func SetDefaultDymintConfig(rlpCfg config.RollappConfig) error {
dymintTomlPath := GetDymintFilePath(rlpCfg.Home)
dymintCfg, err := toml.LoadFile(dymintTomlPath)
if err != nil {
return err
Expand All @@ -20,7 +21,6 @@ func SetDefaultDymintConfig(root string, rlpCfg config.RollappConfig) error {
if daConfig != "" {
dymintCfg.Set("da_config", daConfig)
}
EnableDymintMetrics(dymintCfg)
hubKeysDir := filepath.Join(rlpCfg.Home, consts.ConfigDirName.HubKeys)
dymintCfg.Set("settlement_layer", "dymension")
dymintCfg.Set("block_batch_size", "500")
Expand All @@ -33,10 +33,12 @@ func SetDefaultDymintConfig(root string, rlpCfg config.RollappConfig) error {
dymintCfg.Set("dym_account_name", consts.KeysIds.HubSequencer)
dymintCfg.Set("keyring_home_dir", hubKeysDir)
dymintCfg.Set("gas_prices", rlpCfg.HubData.GAS_PRICE+consts.Denoms.Hub)
return config.WriteTomlToFile(dymintTomlPath, dymintCfg)
}

func EnableDymintMetrics(dymintCfg *toml.Tree) {
dymintCfg.Set("instrumentation.prometheus", true)
dymintCfg.Set("instrumentation.prometheus_listen_addr", ":2112")
file, err := os.Create(dymintTomlPath)
if err != nil {
return err
}
_, err = file.WriteString(dymintCfg.String())
return err
}