Skip to content

Commit

Permalink
inverted_index: sanity error (#12730)
Browse files Browse the repository at this point in the history
my node had old and broken .ef file - i didn't know about it - until
found that GetAsOf used one `.ef` file and totally different `.v`
(because `.ef` returned some out-of-bound txNum).

addad post-validation to `ii.seekInFiles` method
  • Loading branch information
AskAlexSharov authored Nov 18, 2024
1 parent ef01e88 commit 4f70b79
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
5 changes: 4 additions & 1 deletion erigon-lib/state/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,10 @@ func (ht *HistoryRoTx) getFile(txNum uint64) (it visibleFile, ok bool) {
func (ht *HistoryRoTx) historySeekInFiles(key []byte, txNum uint64) ([]byte, bool, error) {
// Files list of II and History is different
// it means II can't return index of file, but can return TxNum which History will use to find own file
ok, histTxNum := ht.iit.seekInFiles(key, txNum)
ok, histTxNum, err := ht.iit.seekInFiles(key, txNum)
if err != nil {
return nil, false, err
}
if !ok {
return nil, false, nil
}
Expand Down
17 changes: 10 additions & 7 deletions erigon-lib/state/inverted_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,12 @@ func (iit *InvertedIndexRoTx) statelessIdxReader(i int) *recsplit.IndexReader {
return r
}

func (iit *InvertedIndexRoTx) seekInFiles(key []byte, txNum uint64) (found bool, equalOrHigherTxNum uint64) {
func (iit *InvertedIndexRoTx) seekInFiles(key []byte, txNum uint64) (found bool, equalOrHigherTxNum uint64, err error) {
if len(iit.files) == 0 {
return false, 0
return false, 0, nil
}
if iit.files[len(iit.files)-1].endTxNum <= txNum {
return false, 0
return false, 0, nil
}

hi, lo := iit.hashKey(key)
Expand All @@ -576,10 +576,10 @@ func (iit *InvertedIndexRoTx) seekInFiles(key []byte, txNum uint64) (found bool,
if ok && fromCache.requested <= txNum {
if txNum <= fromCache.found {
iit.seekInFilesCache.hit++
return true, fromCache.found
return true, fromCache.found, nil
} else if fromCache.found == 0 {
iit.seekInFilesCache.hit++
return false, 0
return false, 0, nil
}
}
}
Expand All @@ -603,17 +603,20 @@ func (iit *InvertedIndexRoTx) seekInFiles(key []byte, txNum uint64) (found bool,
equalOrHigherTxNum, found = eliasfano32.Seek(eliasVal, txNum)

if found {
if equalOrHigherTxNum < iit.files[i].startTxNum || equalOrHigherTxNum >= iit.files[i].endTxNum {
return false, equalOrHigherTxNum, fmt.Errorf("inverted_index(%s) at (%x, %d) returned value %d, but it out-of-bounds %d-%d. it may signal that .ef file is broke - can detect by `erigon seg integrity --check=InvertedIndex`, or re-download files", g.FileName(), key, txNum, iit.files[i].startTxNum, iit.files[i].endTxNum, equalOrHigherTxNum)
}
if iit.seekInFilesCache != nil {
iit.seekInFilesCache.Add(hi, iiSeekInFilesCacheItem{requested: txNum, found: equalOrHigherTxNum})
}
return true, equalOrHigherTxNum
return true, equalOrHigherTxNum, nil
}
}

if iit.seekInFilesCache != nil {
iit.seekInFilesCache.Add(hi, iiSeekInFilesCacheItem{requested: txNum, found: 0})
}
return false, 0
return false, 0, nil
}

// IdxRange - return range of txNums for given `key`
Expand Down

0 comments on commit 4f70b79

Please sign in to comment.