From 234c73a7b7bed751accd3eb83adc2f581d381c17 Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Sat, 11 Oct 2025 19:31:25 +0800 Subject: [PATCH] all: make increaseKey common --- common/bytes.go | 12 ++++++++++++ core/rawdb/accessors_history.go | 14 +------------- core/state/snapshot/generate.go | 16 ++-------------- triedb/pathdb/generate.go | 16 ++-------------- 4 files changed, 17 insertions(+), 41 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index d1f5c6c995..ebf3649458 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -149,3 +149,15 @@ func TrimRightZeroes(s []byte) []byte { } return s[:idx] } + +// IncreaseKey increase the input key by one bit. Return nil if the entire +// addition operation overflows. +func IncreaseKey(key []byte) []byte { + for i := len(key) - 1; i >= 0; i-- { + key[i]++ + if key[i] != 0x0 { + return key + } + } + return nil +} diff --git a/core/rawdb/accessors_history.go b/core/rawdb/accessors_history.go index 95a8907edc..3a78d22c1f 100644 --- a/core/rawdb/accessors_history.go +++ b/core/rawdb/accessors_history.go @@ -212,18 +212,6 @@ func DeleteTrienodeHistoryIndexBlock(db ethdb.KeyValueWriter, addressHash common } } -// increaseKey increase the input key by one bit. Return nil if the entire -// addition operation overflows. -func increaseKey(key []byte) []byte { - for i := len(key) - 1; i >= 0; i-- { - key[i]++ - if key[i] != 0x0 { - return key - } - } - return nil -} - // DeleteStateHistoryIndexes completely removes all history indexing data, including // indexes for accounts and storages. func DeleteStateHistoryIndexes(db ethdb.KeyValueRangeDeleter) { @@ -243,7 +231,7 @@ func DeleteTrienodeHistoryIndexes(db ethdb.KeyValueRangeDeleter) { // Note, this method assumes the space with the given prefix is exclusively occupied! func DeleteHistoryByRange(db ethdb.KeyValueRangeDeleter, prefix []byte) { start := prefix - limit := increaseKey(bytes.Clone(prefix)) + limit := common.IncreaseKey(bytes.Clone(prefix)) // Try to remove the data in the range by a loop, as the leveldb // doesn't support the native range deletion. diff --git a/core/state/snapshot/generate.go b/core/state/snapshot/generate.go index 01fb55ea4c..e462c3c043 100644 --- a/core/state/snapshot/generate.go +++ b/core/state/snapshot/generate.go @@ -545,7 +545,7 @@ func generateStorages(ctx *generatorContext, dl *diskLayer, stateRoot common.Has if exhausted { break } - if origin = increaseKey(last); origin == nil { + if origin = common.IncreaseKey(last); origin == nil { break // special case, the last is 0xffffffff...fff } } @@ -631,7 +631,7 @@ func generateAccounts(ctx *generatorContext, dl *diskLayer, accMarker []byte) er if err != nil { return err // The procedure it aborted, either by external signal or internal error. } - origin = increaseKey(last) + origin = common.IncreaseKey(last) // Last step, cleanup the storages after the last account. // All the left storages should be treated as dangling. @@ -706,18 +706,6 @@ func (dl *diskLayer) generate(stats *generatorStats) { abort <- nil } -// increaseKey increase the input key by one bit. Return nil if the entire -// addition operation overflows. -func increaseKey(key []byte) []byte { - for i := len(key) - 1; i >= 0; i-- { - key[i]++ - if key[i] != 0x0 { - return key - } - } - return nil -} - // abortErr wraps an interruption signal received to represent the // generation is aborted by external processes. type abortErr struct { diff --git a/triedb/pathdb/generate.go b/triedb/pathdb/generate.go index 2efbbbb4e1..4980444036 100644 --- a/triedb/pathdb/generate.go +++ b/triedb/pathdb/generate.go @@ -663,7 +663,7 @@ func (g *generator) generateStorages(ctx *generatorContext, account common.Hash, if exhausted { break } - if origin = increaseKey(last); origin == nil { + if origin = common.IncreaseKey(last); origin == nil { break // special case, the last is 0xffffffff...fff } } @@ -749,7 +749,7 @@ func (g *generator) generateAccounts(ctx *generatorContext, accMarker []byte) er if err != nil { return err // The procedure it aborted, either by external signal or internal error. } - origin = increaseKey(last) + origin = common.IncreaseKey(last) // Last step, cleanup the storages after the last account. // All the left storages should be treated as dangling. @@ -829,18 +829,6 @@ func (g *generator) generate(ctx *generatorContext) { close(abort) } -// increaseKey increase the input key by one bit. Return nil if the entire -// addition operation overflows. -func increaseKey(key []byte) []byte { - for i := len(key) - 1; i >= 0; i-- { - key[i]++ - if key[i] != 0x0 { - return key - } - } - return nil -} - // abortErr wraps an interruption signal received to represent the // generation is aborted by external processes. type abortErr struct {