From 10e5bff0aa5ebfd555e70ae47b367fdbbb89ead6 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 | 39 +++++++++++++++++++++++++++++++++++++-- config/default.go | 9 +++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 572ab379..b33610a0 100644 --- a/config/config.go +++ b/config/config.go @@ -170,12 +170,47 @@ 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. + netChanges, err := cfg.migrateNetworks() + if err != nil { + return false, fmt.Errorf("failed to migrate network configuration: %w", err) + } + changes = changes || netChanges + + // 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, error) { + 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, nil } diff --git a/config/default.go b/config/default.go index f9d7a148..6dccadab 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 = map[string][]string{ + // Testnet. + "testnet": { + "50304f98ddb656620ea817cc1446c401752a05a249b36c9b90dba4616829977a", // 2022-03-03 + }, +}