core/filtermaps: rename hashDbSafe to hashScheme

This commit is contained in:
Zsolt Felfoldi 2025-03-30 01:11:55 +01:00
parent e41033b188
commit 1a29cdb818
4 changed files with 30 additions and 27 deletions

View file

@ -58,7 +58,7 @@ type FilterMaps struct {
closeCh chan struct{} closeCh chan struct{}
closeWg sync.WaitGroup closeWg sync.WaitGroup
history uint64 history uint64
hashDbSafe bool // use hashdb-safe delete range method hashScheme bool // use hashdb-safe delete range method
exportFileName string exportFileName string
Params Params
@ -181,7 +181,10 @@ type Config struct {
// This option enables the checkpoint JSON file generator. // This option enables the checkpoint JSON file generator.
// If set, the given file will be updated with checkpoint information. // If set, the given file will be updated with checkpoint information.
ExportFileName string ExportFileName string
HashDbSafe bool
// expect trie nodes of hash based state scheme in the filtermaps key range;
// use safe iterator based implementation of DeleteRange that skips them
HashScheme bool
} }
// NewFilterMaps creates a new FilterMaps and starts the indexer. // NewFilterMaps creates a new FilterMaps and starts the indexer.
@ -199,7 +202,7 @@ func NewFilterMaps(db ethdb.KeyValueStore, initView *ChainView, historyCutoff, f
blockProcessingCh: make(chan bool, 1), blockProcessingCh: make(chan bool, 1),
history: config.History, history: config.History,
disabled: config.Disabled, disabled: config.Disabled,
hashDbSafe: config.HashDbSafe, hashScheme: config.HashScheme,
disabledCh: make(chan struct{}), disabledCh: make(chan struct{}),
exportFileName: config.ExportFileName, exportFileName: config.ExportFileName,
Params: params, Params: params,
@ -380,13 +383,13 @@ func (f *FilterMaps) removeBloomBits() {
// safeDeleteWithLogs is a wrapper for a function that performs a safe range // safeDeleteWithLogs is a wrapper for a function that performs a safe range
// delete operation using rawdb.SafeDeleteRange. It emits log messages if the // delete operation using rawdb.SafeDeleteRange. It emits log messages if the
// process takes long enough to call the stop callback. // process takes long enough to call the stop callback.
func (f *FilterMaps) safeDeleteWithLogs(deleteFn func(db ethdb.KeyValueStore, hashDbSafe bool, stopCb func() bool) error, action string, stopCb func() bool) error { func (f *FilterMaps) safeDeleteWithLogs(deleteFn func(db ethdb.KeyValueStore, hashScheme bool, stopCb func() bool) error, action string, stopCb func() bool) error {
var ( var (
start = time.Now() start = time.Now()
logPrinted bool logPrinted bool
lastLogPrinted = start lastLogPrinted = start
) )
switch err := deleteFn(f.db, f.hashDbSafe, func() bool { switch err := deleteFn(f.db, f.hashScheme, func() bool {
if !logPrinted || time.Since(lastLogPrinted) > time.Second*10 { if !logPrinted || time.Since(lastLogPrinted) > time.Second*10 {
log.Info(action+" in progress...", "elapsed", time.Since(start)) log.Info(action+" in progress...", "elapsed", time.Since(start))
logPrinted, lastLogPrinted = true, time.Now() logPrinted, lastLogPrinted = true, time.Now()
@ -720,22 +723,22 @@ func (f *FilterMaps) deleteTailEpoch(epoch uint32) (bool, error) {
return false, errors.New("invalid tail epoch number") return false, errors.New("invalid tail epoch number")
} }
// remove index data // remove index data
if err := f.safeDeleteWithLogs(func(db ethdb.KeyValueStore, hashDbSafe bool, stopCb func() bool) error { if err := f.safeDeleteWithLogs(func(db ethdb.KeyValueStore, hashScheme bool, stopCb func() bool) error {
first := f.mapRowIndex(firstMap, 0) first := f.mapRowIndex(firstMap, 0)
count := f.mapRowIndex(firstMap+f.mapsPerEpoch, 0) - first count := f.mapRowIndex(firstMap+f.mapsPerEpoch, 0) - first
if err := rawdb.DeleteFilterMapRows(f.db, common.NewRange(first, count), hashDbSafe, stopCb); err != nil { if err := rawdb.DeleteFilterMapRows(f.db, common.NewRange(first, count), hashScheme, stopCb); err != nil {
return err return err
} }
for mapIndex := firstMap; mapIndex < firstMap+f.mapsPerEpoch; mapIndex++ { for mapIndex := firstMap; mapIndex < firstMap+f.mapsPerEpoch; mapIndex++ {
f.filterMapCache.Remove(mapIndex) f.filterMapCache.Remove(mapIndex)
} }
if err := rawdb.DeleteFilterMapLastBlocks(f.db, common.NewRange(firstMap, f.mapsPerEpoch-1), hashDbSafe, stopCb); err != nil { // keep last enrty if err := rawdb.DeleteFilterMapLastBlocks(f.db, common.NewRange(firstMap, f.mapsPerEpoch-1), hashScheme, stopCb); err != nil { // keep last enrty
return err return err
} }
for mapIndex := firstMap; mapIndex < firstMap+f.mapsPerEpoch-1; mapIndex++ { for mapIndex := firstMap; mapIndex < firstMap+f.mapsPerEpoch-1; mapIndex++ {
f.lastBlockCache.Remove(mapIndex) f.lastBlockCache.Remove(mapIndex)
} }
if err := rawdb.DeleteBlockLvPointers(f.db, common.NewRange(firstBlock, lastBlock-firstBlock), hashDbSafe, stopCb); err != nil { // keep last enrty if err := rawdb.DeleteBlockLvPointers(f.db, common.NewRange(firstBlock, lastBlock-firstBlock), hashScheme, stopCb); err != nil { // keep last enrty
return err return err
} }
for blockNumber := firstBlock; blockNumber < lastBlock; blockNumber++ { for blockNumber := firstBlock; blockNumber < lastBlock; blockNumber++ {

View file

@ -354,8 +354,8 @@ func WriteFilterMapBaseRows(db ethdb.KeyValueWriter, mapRowIndex uint64, rows []
} }
} }
func DeleteFilterMapRows(db ethdb.KeyValueStore, mapRows common.Range[uint64], hashDbSafe bool, stopCallback func() bool) error { func DeleteFilterMapRows(db ethdb.KeyValueStore, mapRows common.Range[uint64], hashScheme bool, stopCallback func() bool) error {
return SafeDeleteRange(db, filterMapRowKey(mapRows.First(), false), filterMapRowKey(mapRows.AfterLast(), false), hashDbSafe, stopCallback) return SafeDeleteRange(db, filterMapRowKey(mapRows.First(), false), filterMapRowKey(mapRows.AfterLast(), false), hashScheme, stopCallback)
} }
// ReadFilterMapLastBlock retrieves the number of the block that generated the // ReadFilterMapLastBlock retrieves the number of the block that generated the
@ -392,8 +392,8 @@ func DeleteFilterMapLastBlock(db ethdb.KeyValueWriter, mapIndex uint32) {
} }
} }
func DeleteFilterMapLastBlocks(db ethdb.KeyValueStore, maps common.Range[uint32], hashDbSafe bool, stopCallback func() bool) error { func DeleteFilterMapLastBlocks(db ethdb.KeyValueStore, maps common.Range[uint32], hashScheme bool, stopCallback func() bool) error {
return SafeDeleteRange(db, filterMapLastBlockKey(maps.First()), filterMapLastBlockKey(maps.AfterLast()), hashDbSafe, stopCallback) return SafeDeleteRange(db, filterMapLastBlockKey(maps.First()), filterMapLastBlockKey(maps.AfterLast()), hashScheme, stopCallback)
} }
// ReadBlockLvPointer retrieves the starting log value index where the log values // ReadBlockLvPointer retrieves the starting log value index where the log values
@ -427,8 +427,8 @@ func DeleteBlockLvPointer(db ethdb.KeyValueWriter, blockNumber uint64) {
} }
} }
func DeleteBlockLvPointers(db ethdb.KeyValueStore, blocks common.Range[uint64], hashDbSafe bool, stopCallback func() bool) error { func DeleteBlockLvPointers(db ethdb.KeyValueStore, blocks common.Range[uint64], hashScheme bool, stopCallback func() bool) error {
return SafeDeleteRange(db, filterMapBlockLVKey(blocks.First()), filterMapBlockLVKey(blocks.AfterLast()), hashDbSafe, stopCallback) return SafeDeleteRange(db, filterMapBlockLVKey(blocks.First()), filterMapBlockLVKey(blocks.AfterLast()), hashScheme, stopCallback)
} }
// FilterMapsRange is a storage representation of the block range covered by the // FilterMapsRange is a storage representation of the block range covered by the
@ -479,22 +479,22 @@ func DeleteFilterMapsRange(db ethdb.KeyValueWriter) {
} }
// deletePrefixRange deletes everything with the given prefix from the database. // deletePrefixRange deletes everything with the given prefix from the database.
func deletePrefixRange(db ethdb.KeyValueStore, prefix []byte, hashDbSafe bool, stopCallback func() bool) error { func deletePrefixRange(db ethdb.KeyValueStore, prefix []byte, hashScheme bool, stopCallback func() bool) error {
end := bytes.Clone(prefix) end := bytes.Clone(prefix)
end[len(end)-1]++ end[len(end)-1]++
return SafeDeleteRange(db, prefix, end, hashDbSafe, stopCallback) return SafeDeleteRange(db, prefix, end, hashScheme, stopCallback)
} }
// DeleteFilterMapsDb removes the entire filter maps database // DeleteFilterMapsDb removes the entire filter maps database
func DeleteFilterMapsDb(db ethdb.KeyValueStore, hashDbSafe bool, stopCallback func() bool) error { func DeleteFilterMapsDb(db ethdb.KeyValueStore, hashScheme bool, stopCallback func() bool) error {
return deletePrefixRange(db, []byte(filterMapsPrefix), hashDbSafe, stopCallback) return deletePrefixRange(db, []byte(filterMapsPrefix), hashScheme, stopCallback)
} }
// DeleteFilterMapsDb removes the old bloombits database and the associated // DeleteFilterMapsDb removes the old bloombits database and the associated
// chain indexer database. // chain indexer database.
func DeleteBloomBitsDb(db ethdb.KeyValueStore, hashDbSafe bool, stopCallback func() bool) error { func DeleteBloomBitsDb(db ethdb.KeyValueStore, hashScheme bool, stopCallback func() bool) error {
if err := deletePrefixRange(db, bloomBitsPrefix, hashDbSafe, stopCallback); err != nil { if err := deletePrefixRange(db, bloomBitsPrefix, hashScheme, stopCallback); err != nil {
return err return err
} }
return deletePrefixRange(db, bloomBitsMetaPrefix, hashDbSafe, stopCallback) return deletePrefixRange(db, bloomBitsMetaPrefix, hashScheme, stopCallback)
} }

View file

@ -614,7 +614,7 @@ func ReadChainMetadata(db ethdb.KeyValueStore) [][]string {
// SafeDeleteRange deletes all of the keys (and values) in the range // SafeDeleteRange deletes all of the keys (and values) in the range
// [start,end) (inclusive on start, exclusive on end). // [start,end) (inclusive on start, exclusive on end).
// If hashDbSafe is true then it always uses an iterator and skips hashdb trie // If hashScheme is true then it always uses an iterator and skips hashdb trie
// node entries. If it is false and the backing db is pebble db then it uses // node entries. If it is false and the backing db is pebble db then it uses
// the fast native range delete. // the fast native range delete.
// In case of fallback mode (hashdb or leveldb) the range deletion might be // In case of fallback mode (hashdb or leveldb) the range deletion might be
@ -622,8 +622,8 @@ func ReadChainMetadata(db ethdb.KeyValueStore) [][]string {
// is periodically called and if it returns an error then SafeDeleteRange // is periodically called and if it returns an error then SafeDeleteRange
// stops and also returns that error. The callback is not called if native // stops and also returns that error. The callback is not called if native
// range delete is used or there are a small number of keys only. // range delete is used or there are a small number of keys only.
func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashDbSafe bool, stopCallback func() bool) error { func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashScheme bool, stopCallback func() bool) error {
if !hashDbSafe { if !hashScheme {
// delete entire range; use fast native range delete on pebble db // delete entire range; use fast native range delete on pebble db
for { for {
switch err := db.DeleteRange(start, end); err { switch err := db.DeleteRange(start, end); err {
@ -670,7 +670,7 @@ func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashDbSafe bool,
if stopCallback() { if stopCallback() {
return ErrDeleteRangeInterrupted return ErrDeleteRangeInterrupted
} }
start = append(bytes.Clone(it.Key()), 0) start = append(bytes.Clone(it.Key()), 0) // appending a zero gives us the next possible key
it.Release() it.Release()
batch = db.NewBatch() batch = db.NewBatch()
it = db.NewIterator(nil, start) it = db.NewIterator(nil, start)

View file

@ -243,7 +243,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
History: config.LogHistory, History: config.LogHistory,
Disabled: config.LogNoHistory, Disabled: config.LogNoHistory,
ExportFileName: config.LogExportCheckpoints, ExportFileName: config.LogExportCheckpoints,
HashDbSafe: scheme == rawdb.HashScheme, HashScheme: scheme == rawdb.HashScheme,
} }
chainView := eth.newChainView(eth.blockchain.CurrentBlock()) chainView := eth.newChainView(eth.blockchain.CurrentBlock())
historyCutoff := eth.blockchain.HistoryPruningCutoff() historyCutoff := eth.blockchain.HistoryPruningCutoff()