From 1363859958944f4ba582b8200140533066484913 Mon Sep 17 00:00:00 2001 From: ledgerwatch Date: Sun, 13 Feb 2022 18:56:36 +0000 Subject: [PATCH] [erigon2] Fix history bug, optimise history search (#326) * Another fix for DeleteAccount * Optimise bitmap search * Optimise bitmap search Co-authored-by: Alexey Sharp --- aggregator/aggregator.go | 3 +++ aggregator/history.go | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/aggregator/aggregator.go b/aggregator/aggregator.go index cad8bd030..e5eb20f02 100644 --- a/aggregator/aggregator.go +++ b/aggregator/aggregator.go @@ -2436,6 +2436,9 @@ func (w *Writer) DeleteAccount(addr []byte, trace bool) { key, _ := g.Next(nil) if bytes.HasPrefix(key, addr) { val, _ := g.Next(nil) + if len(val) > 0 { + val = val[1:] + } heap.Push(&cp, &CursorItem{t: FILE_CURSOR, key: key, val: val, dg: g, endBlock: item.endBlock}) } } diff --git a/aggregator/history.go b/aggregator/history.go index 47d3c9b10..ff5b2e797 100644 --- a/aggregator/history.go +++ b/aggregator/history.go @@ -224,11 +224,17 @@ func (hr *HistoryReader) searchInHistory(bitmapType, historyType FileType, key [ if _, err = bm.ReadFrom(bytes.NewReader(bitmapVal)); err != nil { return false } - bm.RemoveRange(0, searchTx) - if bm.IsEmpty() { + if searchTx == 0 { + foundTxNum = bm.Minimum() + foundEndBlock = item.endBlock + found = true + return false + } + searchRank := bm.Rank(searchTx - 1) + if searchRank >= bm.GetCardinality() { continue } - foundTxNum = bm.Minimum() + foundTxNum, _ = bm.Select(searchRank) foundEndBlock = item.endBlock found = true return false