github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/config/migrate.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors" 9 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/configtypes" 10 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file" 11 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger" 12 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/version" 13 ) 14 15 func pathToChainConfigFile(chain, fileName string) string { 16 return filepath.Join(PathToRootConfig(), "config", chain, fileName) 17 } 18 19 var minVersionStr = "v2.0.0-release" 20 21 // migrate upgrades the config files to the latest versions if necessary 22 func migrate(currentVer version.Version) error { 23 minVersion := version.NewVersion(minVersionStr) 24 if currentVer.Uint64() >= minVersion.Uint64() { 25 return nil 26 } 27 // note that this routine does not return if it gets this far. 28 29 var cfg configtypes.Config 30 configFile := PathToConfigFile() 31 if err := ReadToml(configFile, &cfg); err != nil { 32 return err 33 } 34 35 vers := version.NewVersion("v1.0.0-release") 36 if currentVer.Uint64() < vers.Uint64() { 37 for chain := range cfg.Chains { 38 ch := cfg.Chains[chain] 39 ch.Chain = chain 40 scrape := configtypes.ScrapeSettings{ 41 AppsPerChunk: 2000000, 42 SnapToGrid: 250000, 43 FirstSnap: 2000000, 44 UnripeDist: 28, 45 AllowMissing: false, 46 ChannelCount: 20, 47 } 48 if chain == "mainnet" { 49 scrape.SnapToGrid = 100000 50 scrape.FirstSnap = 2300000 51 } 52 53 fn := pathToChainConfigFile(chain, "blockScrape.toml") 54 if file.FileExists(fn) { 55 _ = MergeScrapeConfig(fn, &scrape) 56 _ = os.Remove(fn) 57 } 58 ch.Scrape = scrape 59 cfg.Chains[chain] = ch 60 61 oldPath := pathToChainConfigFile(chain, "manifest.json") 62 newPath := filepath.Join(PathToIndex(chain), "manifest.json") 63 if file.FileExists(oldPath) { 64 _, _ = file.Copy(newPath, oldPath) 65 _ = os.Remove(oldPath) 66 } 67 } 68 } 69 70 vers = version.NewVersion("v2.0.0-release") 71 if currentVer.Uint64() < vers.Uint64() { 72 pinning := configtypes.PinningGroup{ 73 LocalPinUrl: "http://localhost:5001", 74 RemotePinUrl: "https://api.pinata.cloud/pinning/pinFileToIPFS", 75 } 76 if cfg.Settings.DefaultGateway != "" { 77 pinning.GatewayUrl = cfg.Settings.DefaultGateway 78 } else { 79 pinning.GatewayUrl = defaultIpfsGateway 80 } 81 cfg.Settings.DefaultGateway = "" 82 cfg.Pinning = pinning 83 } 84 85 // Re-write the file (after making a backup) with the new version 86 _, _ = file.Copy(configFile+".bak", configFile) 87 _ = cfg.WriteFile(configFile, minVersion) // updates the version 88 89 msg := "Your configuration files were upgraded to {%s}. Rerun your command." 90 logger.Fatal(colors.Colored(fmt.Sprintf(msg, minVersion.String()))) 91 92 return nil 93 } 94 95 type oldScrapeGroup struct { 96 AppsPerChunk uint64 `toml:"apps_per_chunk"` 97 SnapToGrid uint64 `toml:"snap_to_grid"` 98 FirstSnap uint64 `toml:"first_snap"` 99 UnripeDist uint64 `toml:"unripe_dist"` 100 AllowMissing bool `toml:"allow_missing"` 101 ChannelCount uint64 `toml:"channel_count"` 102 } 103 104 type OldScrape struct { 105 Settings oldScrapeGroup `toml:"settings"` 106 } 107 108 func MergeScrapeConfig(fn string, scrape *configtypes.ScrapeSettings) error { 109 var sCfg OldScrape 110 if err := ReadToml(fn, &sCfg); err != nil { 111 return err 112 } 113 if sCfg.Settings.AppsPerChunk > 0 { 114 scrape.AppsPerChunk = sCfg.Settings.AppsPerChunk 115 } 116 if sCfg.Settings.SnapToGrid > 0 { 117 scrape.SnapToGrid = sCfg.Settings.SnapToGrid 118 } 119 if sCfg.Settings.FirstSnap > 0 { 120 scrape.FirstSnap = sCfg.Settings.FirstSnap 121 } 122 if sCfg.Settings.UnripeDist > 0 { 123 scrape.UnripeDist = sCfg.Settings.UnripeDist 124 } 125 if sCfg.Settings.ChannelCount > 0 { 126 scrape.ChannelCount = sCfg.Settings.ChannelCount 127 } 128 scrape.AllowMissing = sCfg.Settings.AllowMissing 129 return nil 130 }