Skip to content

Commit

Permalink
feat: Automatically migrate old chain contexts for known networks
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Oct 19, 2023
1 parent 1c0acf3 commit 32a2ddc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
34 changes: 32 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,46 @@ func (cfg *Config) Save() error {
return cfg.viper.WriteConfig()
}

// Migrate migrates the given wallet config entry to the latest version and returns true, if any changes were needed.
// Migrate migrates the given wallet config entry to the latest version and returns true, if any
// changes were needed.
func (cfg *Config) Migrate() (bool, error) {
changes, err := cfg.Wallet.Migrate()
var changes bool

// Networks.
changes = changes || cfg.migrateNetworks()

// Wallets.
walletChanges, err := cfg.Wallet.Migrate()
if err != nil {
return false, fmt.Errorf("failed to migrate wallet configuration: %w", err)
}
changes = changes || walletChanges

return changes, nil
}

func (cfg *Config) migrateNetworks() bool {
var changes bool
for name, net := range cfg.Networks.All {
defaultCfg, knownDefault := Default.Networks.All[name]
oldChainContexts, knownOld := OldNetworks[name]
if !knownDefault || !knownOld {
continue
}

for _, oldChainContext := range oldChainContexts {
if net.ChainContext == oldChainContext {
// If old chain context is known, replace with default.
net.ChainContext = defaultCfg.ChainContext
changes = true
break
}
}
}

return changes
}

// Validate performs config validation.
func (cfg *Config) Validate() error {
if err := cfg.Networks.Validate(); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ import (
var Default = Config{
Networks: config.DefaultNetworks,
}

// OldNetworks contains information about old versions (e.g. chain contexts) of known networks so
// they can be automatically migrated.
var OldNetworks map[string][]string = map[string][]string{

Check warning on line 14 in config/default.go

View workflow job for this annotation

GitHub Actions / lint

var-declaration: should omit type map[string][]string from declaration of var OldNetworks; it will be inferred from the right-hand side (revive)
// Testnet.
"testnet": {
"50304f98ddb656620ea817cc1446c401752a05a249b36c9b90dba4616829977a", // 2022-03-03
},
}

0 comments on commit 32a2ddc

Please sign in to comment.