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 all 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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ ifeq (,$(VERSION))
endif


ldflags = -X github.com/dymensionxyz/roller/cmd/version.BuildVersion=$(VERSION) \
-X github.com/dymensionxyz/roller/cmd/version.BuildCommit=$(COMMIT) \
-X github.com/dymensionxyz/roller/cmd/version.BuildTime=$(TIME)"
ldflags = -X github.com/dymensionxyz/roller/version.BuildVersion=$(VERSION) \
-X github.com/dymensionxyz/roller/version.BuildCommit=$(COMMIT) \
-X github.com/dymensionxyz/roller/version.BuildTime=$(TIME)"

BUILD_FLAGS := -ldflags '$(ldflags)'
# ---------------------------------------------------------------------------- #
Expand Down
5 changes: 4 additions & 1 deletion cmd/config/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package initconfig

import (
"fmt"
"github.com/dymensionxyz/roller/version"
"strings"

"github.com/dymensionxyz/roller/cmd/consts"
Expand Down Expand Up @@ -36,7 +37,9 @@ func addFlags(cmd *cobra.Command) error {
}

func GetInitConfig(initCmd *cobra.Command, args []string) (config.RollappConfig, error) {
cfg := config.RollappConfig{}
cfg := config.RollappConfig{
RollerVersion: version.BuildVersion,
}
cfg.Home = initCmd.Flag(utils.FlagNames.Home).Value.String()
cfg.RollappBinary = initCmd.Flag(FlagNames.RollappBinary).Value.String()
// Error is ignored because the flag is validated in the cobra preRun hook
Expand Down
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
78 changes: 78 additions & 0 deletions cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package migrate

import (
"fmt"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"github.com/dymensionxyz/roller/version"
"github.com/spf13/cobra"
"strings"
)

var migrationsRegistry = []VersionMigrator{
&VersionMigratorV014{},
}

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "migrate",
Short: "Migrates the roller configuration to the newly installed version.",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rlpCfg, err := config.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
prevVersionData, err := GetPrevVersionData(rlpCfg)
utils.PrettifyErrorIfExists(err)
for _, migrator := range migrationsRegistry {
if migrator.ShouldMigrate(*prevVersionData) {
utils.PrettifyErrorIfExists(migrator.PerformMigration(rlpCfg))
}
}
trimmedCurrentVersion := trimVersionStr(version.BuildVersion)
fmt.Printf("💈 Roller has migrated successfully to %s!\n", trimmedCurrentVersion)
},
}
return cmd
}

func trimVersionStr(versionStr string) string {
return strings.Split(versionStr, "-")[0]
}

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 == "" {
return &VersionData{
Major: 0,
Minor: 1,
Patch: 3,
}, nil
}
trimmedVersionStr := trimVersionStr(rollerPrevVersion)
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
}
22 changes: 22 additions & 0 deletions cmd/migrate/v_0_1_4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package migrate

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

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 {
err := sequencer.SetDefaultDymintConfig(rlpCfg)
if err != nil {
return err
}
rlpCfg.RollerVersion = trimVersionStr(version.BuildVersion)
return config.WriteConfigToTOML(rlpCfg)
}
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/dymensionxyz/roller/cmd/migrate"
"github.com/dymensionxyz/roller/cmd/run"
"github.com/dymensionxyz/roller/cmd/services"
"github.com/dymensionxyz/roller/cmd/utils"
Expand Down Expand Up @@ -33,13 +34,14 @@ func Execute() {

func init() {
rootCmd.AddCommand(config.ConfigCmd())
rootCmd.AddCommand(version.VersionCmd())
rootCmd.AddCommand(version.Cmd())
rootCmd.AddCommand(register.Cmd())
rootCmd.AddCommand(da_light_client.DALightClientCmd())
rootCmd.AddCommand(sequencer.SequencerCmd())
rootCmd.AddCommand(relayer.Cmd())
rootCmd.AddCommand(keys.Cmd())
rootCmd.AddCommand(run.Cmd())
rootCmd.AddCommand(services.Cmd())
rootCmd.AddCommand(migrate.Cmd())
utils.AddGlobalFlags(rootCmd)
}
15 changes: 5 additions & 10 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@ package version

import (
"fmt"
"github.com/dymensionxyz/roller/version"

"github.com/spf13/cobra"
)

var (
BuildVersion = "<version>"
BuildTime = "<build-time>"
BuildCommit = "<build-commit>"
)

func VersionCmd() *cobra.Command {
func Cmd() *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the version of roller",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("💈 Roller version", BuildVersion)
fmt.Println("💈 Build time:", BuildTime)
fmt.Println("💈 Git commit:", BuildCommit)
fmt.Println("💈 Roller version", version.BuildVersion)
fmt.Println("💈 Build time:", version.BuildTime)
fmt.Println("💈 Git commit:", version.BuildCommit)
},
}
return versionCmd
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type RollappConfig struct {
Decimals uint
HubData HubData
DA DAType
RollerVersion string
}

type HubData = struct {
Expand Down
7 changes: 1 addition & 6 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,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 Down
13 changes: 5 additions & 8 deletions sequencer/dymint_toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"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 @@ -21,8 +21,6 @@ func SetDefaultDymintConfig(root string, rlpCfg config.RollappConfig) error {
if daConfig != "" {
dymintCfg.Set("da_config", daConfig)
}
dymintCfg.Set("instrumentation.prometheus", true)
dymintCfg.Set("instrumentation.prometheus_listen_addr", ":2112")
hubKeysDir := filepath.Join(rlpCfg.Home, consts.ConfigDirName.HubKeys)
dymintCfg.Set("settlement_layer", "dymension")
dymintCfg.Set("block_batch_size", "500")
Expand All @@ -35,13 +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)
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())
if err != nil {
return err
}
return nil
return err
}
7 changes: 7 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package version

var (
BuildVersion = "<version>"
BuildTime = "<build-time>"
BuildCommit = "<build-commit>"
)