From 55313bb789f3c4a178c0b3d7dc39e1404d522236 Mon Sep 17 00:00:00 2001 From: Jernej Kos Date: Thu, 19 Oct 2023 09:40:03 +0200 Subject: [PATCH] feat: Automatically migrate old chain contexts for known networks --- config/config.go | 34 ++++++++++++++++++++++++++++++++-- config/default.go | 9 +++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 572ab379..bd9a687a 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { diff --git a/config/default.go b/config/default.go index f9d7a148..7b8d4174 100644 --- a/config/default.go +++ b/config/default.go @@ -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{ + // Testnet. + "testnet": { + "50304f98ddb656620ea817cc1446c401752a05a249b36c9b90dba4616829977a", // 2022-03-03 + }, +}