github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/index/checkVersion.go (about) 1 package index 2 3 import ( 4 "errors" 5 "fmt" 6 "io" 7 "os" 8 "path/filepath" 9 "strings" 10 11 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors" 12 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config" 13 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file" 14 ) 15 16 var ErrNotInitialized = errors.New("index not initialized") 17 var ErrIncorrectMagic = errors.New("incorrect magic number") 18 var ErrIncorrectHash = errors.New("incorrect header hash") 19 20 // IsInitialized returns an error if the version in the header is not as requested 21 func IsInitialized(chain, required string) error { 22 fileName := filepath.Join(config.PathToIndex(chain), "blooms/000000000-000000000.bloom") 23 if !file.FileExists(fileName) { 24 const indexNotInitialized string = ` 25 26 The Unchained Index does not appear to be initialized. You must run 'chifra init' 27 (and allow it to complete) or 'chifra scrape' before using this command. 28 29 Error: %w 30 31 ` 32 return fmt.Errorf(indexNotInitialized, ErrNotInitialized) 33 } 34 35 var err error 36 var bl Bloom 37 bl.File, err = os.OpenFile(fileName, os.O_RDONLY, 0644) 38 if err != nil { 39 return err 40 } 41 defer func() { 42 bl.File.Close() 43 bl.File = nil 44 }() 45 46 _, _ = bl.File.Seek(0, io.SeekStart) // already true, but can't hurt 47 if err = bl.readHeader(true /* check */); err != nil { 48 if errors.Is(err, ErrIncorrectHash) { 49 msg := ` 50 Outdated file: {WHICH}. 51 File version: {FOUND} 52 Manifest version: {WANT} 53 Error: {%w} 54 55 See https://github.com/TrueBlocks/trueblocks-core/blob/develop/src/other/migrations/README-v2.0.0.md. 56 ` 57 msg = colors.ColoredWith(msg, colors.Yellow) 58 msg = strings.Replace(msg, "WHICH", fileName, -1) 59 msg = strings.Replace(msg, "FOUND", config.VersionTags[bl.Header.Hash.Hex()], -1) 60 msg = strings.Replace(msg, "WANT", required, -1) 61 return fmt.Errorf(msg, ErrIncorrectHash) 62 } 63 return err 64 } 65 66 return nil 67 }