github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/scrape/scrape_prepare.go (about) 1 package scrapePkg 2 3 // Copyright 2021 The TrueBlocks Authors. All rights reserved. 4 // Use of this source code is governed by a license that can 5 // be found in the LICENSE file. 6 7 import ( 8 "path/filepath" 9 10 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config" 11 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file" 12 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/index" 13 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger" 14 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/prefunds" 15 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/tslib" 16 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types" 17 ) 18 19 // Prepare performs actions that need to be done prior to entering the 20 // forever loop. Returns true if processing should continue, false otherwise. 21 // The routine cleans the temporary folders (if any) and then makes sure the zero 22 // block (reads the allocation file, if present) is processed. 23 func (opts *ScrapeOptions) Prepare() (ok bool, err error) { 24 chain := opts.Globals.Chain 25 26 // We always clean the temporary folders (other than staging) when starting 27 _ = cleanEphemeralIndexFolders(chain) 28 29 // If the file already exists, we're done. 30 bloomPath := filepath.Join(config.PathToIndex(chain), "blooms/000000000-000000000.bloom") 31 if file.FileExists(bloomPath) { 32 return true, nil 33 } 34 35 // If there are no prefunds, we're done. 36 prefundPath := filepath.Join(config.MustGetPathToChainConfig(chain), "allocs.csv") 37 prefunds, err := prefunds.LoadPrefunds(chain, prefundPath, nil) 38 if err != nil { 39 return false, err 40 } 41 42 appMap := make(map[string][]types.AppRecord, len(prefunds)) 43 for i, prefund := range prefunds { 44 addr := prefund.Address.Hex() 45 appMap[addr] = append(appMap[addr], types.AppRecord{ 46 BlockNumber: 0, 47 TransactionIndex: uint32(i), 48 }) 49 } 50 51 array := []tslib.TimestampRecord{} 52 array = append(array, tslib.TimestampRecord{ 53 Bn: uint32(0), 54 Ts: uint32(opts.Conn.GetBlockTimestamp(0)), 55 }) 56 _ = tslib.Append(chain, array) 57 58 logger.Info("Writing block zero allocations for", len(prefunds), "prefunds, nAddresses:", len(appMap)) 59 indexPath := index.ToIndexPath(bloomPath) 60 var chunk index.Chunk 61 if report, err := chunk.Write(chain, opts.PublisherAddr, indexPath, appMap, len(prefunds)); err != nil { 62 return false, err 63 } else if report == nil { 64 logger.Fatal("should not happen ==> write chunk returned empty report") 65 } else { 66 report.Snapped = true // assumes block zero is a snap to grid (which it is in a sense) 67 report.FileSize = file.FileSize(indexPath) 68 report.Report() 69 } 70 if err = opts.NotifyChunkWritten(chunk, indexPath); err != nil { 71 return false, err 72 } 73 74 return true, nil 75 }