diff --git a/erigon-lib/downloader/snaptype/type.go b/erigon-lib/downloader/snaptype/type.go index 22037faab43..682251c3b18 100644 --- a/erigon-lib/downloader/snaptype/type.go +++ b/erigon-lib/downloader/snaptype/type.go @@ -92,18 +92,7 @@ func (f IndexBuilderFunc) Build(ctx context.Context, info FileInfo, salt uint32, var saltMap = map[string]uint32{} var saltLock sync.RWMutex -// GetIndicesSalt - try read salt for all indices from DB. Or fall-back to new salt creation. -// if db is Read-Only (for example remote RPCDaemon or utilities) - we will not create new indices - -// and existing indices have salt in metadata. -func GetIndexSalt(baseDir string) (uint32, error) { - saltLock.RLock() - salt, ok := saltMap[baseDir] - saltLock.RUnlock() - - if ok { - return salt, nil - } - +func ReadAndCreateSaltIfNeeded(baseDir string) (uint32, error) { fpath := filepath.Join(baseDir, "salt-blocks.txt") exists, err := dir.FileExist(fpath) if err != nil { @@ -123,10 +112,37 @@ func GetIndexSalt(baseDir string) (uint32, error) { if err != nil { return 0, err } + if len(saltBytes) != 4 { + dir.MustExist(baseDir) - salt = binary.BigEndian.Uint32(saltBytes) + saltBytes := make([]byte, 4) + binary.BigEndian.PutUint32(saltBytes, rand.Uint32()) + if err := dir.WriteFileWithFsync(fpath, saltBytes, os.ModePerm); err != nil { + return 0, err + } + } + + return binary.BigEndian.Uint32(saltBytes), nil + +} + +// GetIndicesSalt - try read salt for all indices from DB. Or fall-back to new salt creation. +// if db is Read-Only (for example remote RPCDaemon or utilities) - we will not create new indices - +// and existing indices have salt in metadata. +func GetIndexSalt(baseDir string) (uint32, error) { + saltLock.RLock() + salt, ok := saltMap[baseDir] + saltLock.RUnlock() + if ok { + return salt, nil + } saltLock.Lock() + salt, err := ReadAndCreateSaltIfNeeded(baseDir) + if err != nil { + return 0, err + } + saltMap[baseDir] = salt saltLock.Unlock() diff --git a/turbo/snapshotsync/freezeblocks/block_snapshots.go b/turbo/snapshotsync/freezeblocks/block_snapshots.go index adf0e44bc48..c77f79885f3 100644 --- a/turbo/snapshotsync/freezeblocks/block_snapshots.go +++ b/turbo/snapshotsync/freezeblocks/block_snapshots.go @@ -820,6 +820,10 @@ func (s *RoSnapshots) buildMissedIndices(logPrefix string, ctx context.Context, return nil } + if _, err := snaptype.ReadAndCreateSaltIfNeeded(dirs.Snap); err != nil { + return err + } + dir, tmpDir := dirs.Snap, dirs.Tmp //log.Log(lvl, "[snapshots] Build indices", "from", min)