diff --git a/core/filtermaps/filtermaps.go b/core/filtermaps/filtermaps.go index ecf93054d8..6860ee90de 100644 --- a/core/filtermaps/filtermaps.go +++ b/core/filtermaps/filtermaps.go @@ -58,7 +58,7 @@ type FilterMaps struct { closeCh chan struct{} closeWg sync.WaitGroup history uint64 - hashDbSafe bool // use hashdb-safe delete range method + hashScheme bool // use hashdb-safe delete range method exportFileName string Params @@ -181,7 +181,10 @@ type Config struct { // This option enables the checkpoint JSON file generator. // If set, the given file will be updated with checkpoint information. 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. @@ -199,7 +202,7 @@ func NewFilterMaps(db ethdb.KeyValueStore, initView *ChainView, historyCutoff, f blockProcessingCh: make(chan bool, 1), history: config.History, disabled: config.Disabled, - hashDbSafe: config.HashDbSafe, + hashScheme: config.HashScheme, disabledCh: make(chan struct{}), exportFileName: config.ExportFileName, Params: params, @@ -380,13 +383,13 @@ func (f *FilterMaps) removeBloomBits() { // safeDeleteWithLogs is a wrapper for a function that performs a safe range // delete operation using rawdb.SafeDeleteRange. It emits log messages if the // 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 ( start = time.Now() logPrinted bool 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 { log.Info(action+" in progress...", "elapsed", time.Since(start)) 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") } // 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) 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 } for mapIndex := firstMap; mapIndex < firstMap+f.mapsPerEpoch; 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 } for mapIndex := firstMap; mapIndex < firstMap+f.mapsPerEpoch-1; 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 } for blockNumber := firstBlock; blockNumber < lastBlock; blockNumber++ { diff --git a/core/rawdb/accessors_indexes.go b/core/rawdb/accessors_indexes.go index f058614b77..b9ee1148d0 100644 --- a/core/rawdb/accessors_indexes.go +++ b/core/rawdb/accessors_indexes.go @@ -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 { - return SafeDeleteRange(db, filterMapRowKey(mapRows.First(), false), filterMapRowKey(mapRows.AfterLast(), false), hashDbSafe, stopCallback) +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), hashScheme, stopCallback) } // 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 { - return SafeDeleteRange(db, filterMapLastBlockKey(maps.First()), filterMapLastBlockKey(maps.AfterLast()), hashDbSafe, stopCallback) +func DeleteFilterMapLastBlocks(db ethdb.KeyValueStore, maps common.Range[uint32], hashScheme bool, stopCallback func() bool) error { + return SafeDeleteRange(db, filterMapLastBlockKey(maps.First()), filterMapLastBlockKey(maps.AfterLast()), hashScheme, stopCallback) } // 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 { - return SafeDeleteRange(db, filterMapBlockLVKey(blocks.First()), filterMapBlockLVKey(blocks.AfterLast()), hashDbSafe, stopCallback) +func DeleteBlockLvPointers(db ethdb.KeyValueStore, blocks common.Range[uint64], hashScheme bool, stopCallback func() bool) error { + return SafeDeleteRange(db, filterMapBlockLVKey(blocks.First()), filterMapBlockLVKey(blocks.AfterLast()), hashScheme, stopCallback) } // 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. -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[len(end)-1]++ - return SafeDeleteRange(db, prefix, end, hashDbSafe, stopCallback) + return SafeDeleteRange(db, prefix, end, hashScheme, stopCallback) } // DeleteFilterMapsDb removes the entire filter maps database -func DeleteFilterMapsDb(db ethdb.KeyValueStore, hashDbSafe bool, stopCallback func() bool) error { - return deletePrefixRange(db, []byte(filterMapsPrefix), hashDbSafe, stopCallback) +func DeleteFilterMapsDb(db ethdb.KeyValueStore, hashScheme bool, stopCallback func() bool) error { + return deletePrefixRange(db, []byte(filterMapsPrefix), hashScheme, stopCallback) } // DeleteFilterMapsDb removes the old bloombits database and the associated // chain indexer database. -func DeleteBloomBitsDb(db ethdb.KeyValueStore, hashDbSafe bool, stopCallback func() bool) error { - if err := deletePrefixRange(db, bloomBitsPrefix, hashDbSafe, stopCallback); err != nil { +func DeleteBloomBitsDb(db ethdb.KeyValueStore, hashScheme bool, stopCallback func() bool) error { + if err := deletePrefixRange(db, bloomBitsPrefix, hashScheme, stopCallback); err != nil { return err } - return deletePrefixRange(db, bloomBitsMetaPrefix, hashDbSafe, stopCallback) + return deletePrefixRange(db, bloomBitsMetaPrefix, hashScheme, stopCallback) } diff --git a/core/rawdb/database.go b/core/rawdb/database.go index cc215e6dbe..0985b06426 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -614,7 +614,7 @@ func ReadChainMetadata(db ethdb.KeyValueStore) [][]string { // SafeDeleteRange deletes all of the keys (and values) in the range // [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 // the fast native range delete. // 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 // 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. -func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashDbSafe bool, stopCallback func() bool) error { - if !hashDbSafe { +func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashScheme bool, stopCallback func() bool) error { + if !hashScheme { // delete entire range; use fast native range delete on pebble db for { switch err := db.DeleteRange(start, end); err { @@ -670,7 +670,7 @@ func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashDbSafe bool, if stopCallback() { 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() batch = db.NewBatch() it = db.NewIterator(nil, start) diff --git a/eth/backend.go b/eth/backend.go index 54c74f1ea4..ab612b1de7 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -243,7 +243,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { History: config.LogHistory, Disabled: config.LogNoHistory, ExportFileName: config.LogExportCheckpoints, - HashDbSafe: scheme == rawdb.HashScheme, + HashScheme: scheme == rawdb.HashScheme, } chainView := eth.newChainView(eth.blockchain.CurrentBlock()) historyCutoff := eth.blockchain.HistoryPruningCutoff()