From 41138403a27093c14ad35c202e9178ae263719d7 Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Mon, 21 Oct 2024 17:13:13 +0700 Subject: [PATCH] e2: port salt assert logic (#12394) --- erigon-lib/downloader/snaptype/type.go | 48 ++++++++++++++++++-------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/erigon-lib/downloader/snaptype/type.go b/erigon-lib/downloader/snaptype/type.go index cd8d76db83c..0603890f800 100644 --- a/erigon-lib/downloader/snaptype/type.go +++ b/erigon-lib/downloader/snaptype/type.go @@ -5,7 +5,7 @@ import ( "encoding/binary" "errors" "fmt" - "math/rand" + "math/rand/v2" "os" "path/filepath" "strconv" @@ -76,19 +76,9 @@ 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") + if !dir.FileExist(fpath) { dir.MustExist(baseDir) @@ -102,12 +92,40 @@ 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() + defer saltLock.Unlock() + salt, err := ReadAndCreateSaltIfNeeded(baseDir) + if err != nil { + return 0, err + } + saltMap[baseDir] = salt - saltLock.Unlock() return salt, nil }