diff --git a/core/rawdb/table.go b/core/rawdb/table.go index 4df3c66726..c2c9c416ee 100644 --- a/core/rawdb/table.go +++ b/core/rawdb/table.go @@ -17,8 +17,6 @@ package rawdb import ( - "bytes" - "github.com/ethereum/go-ethereum/ethdb" ) @@ -137,7 +135,7 @@ func (t *table) DeleteRange(start, end []byte) error { // The nilness will be lost by adding the prefix, explicitly converting it // to a special flag representing the end of key range. if end == nil { - end = bytes.Repeat([]byte{0xff}, 32) + end = ethdb.MaximumKey } return t.db.DeleteRange(append([]byte(t.prefix), start...), append([]byte(t.prefix), end...)) } @@ -235,7 +233,7 @@ func (b *tableBatch) DeleteRange(start, end []byte) error { // The nilness will be lost by adding the prefix, explicitly converting it // to a special flag representing the end of key range. if end == nil { - end = bytes.Repeat([]byte{0xff}, 32) + end = ethdb.MaximumKey } return b.batch.DeleteRange(append([]byte(b.prefix), start...), append([]byte(b.prefix), end...)) } diff --git a/ethdb/database.go b/ethdb/database.go index 207a07a9a4..e5416a0419 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -18,10 +18,23 @@ package ethdb import ( + "bytes" "errors" "io" ) +var ( + // MaximumKey is a special marker representing the largest possible key + // in the database. + // + // All prefixed database entries will be smaller than this marker. + // For trie nodes in hash mode, we use a 32-byte slice filled with 0xFF + // because there may be shared prefixes starting with multiple 0xFF bytes. + // Using 32 bytes ensures that only a hash collision could potentially + // match or exceed it. + MaximumKey = bytes.Repeat([]byte{0xff}, 32) +) + // KeyValueReader wraps the Has and Get method of a backing data store. type KeyValueReader interface { // Has retrieves if a key is present in the key-value data store. diff --git a/ethdb/pebble/pebble.go b/ethdb/pebble/pebble.go index 09062d26fc..cca99831e6 100644 --- a/ethdb/pebble/pebble.go +++ b/ethdb/pebble/pebble.go @@ -18,7 +18,6 @@ package pebble import ( - "bytes" "fmt" "runtime" "strings" @@ -411,7 +410,7 @@ func (d *Database) DeleteRange(start, end []byte) error { // in pebble(nil in leveldb). Use an ugly hack to construct a // large key to represent it. if end == nil { - end = bytes.Repeat([]byte{0xff}, 32) + end = ethdb.MaximumKey } return d.db.DeleteRange(start, end, d.writeOptions) } @@ -471,7 +470,7 @@ func (d *Database) Compact(start []byte, limit []byte) error { // 0xff-s, so 32 ensures than only a hash collision could touch it. // https://github.com/cockroachdb/pebble/issues/2359#issuecomment-1443995833 if limit == nil { - limit = bytes.Repeat([]byte{0xff}, 32) + limit = ethdb.MaximumKey } return d.db.Compact(start, limit, true) // Parallelization is preferred } @@ -633,7 +632,7 @@ func (b *batch) DeleteRange(start, end []byte) error { // in pebble(nil in leveldb). Use an ugly hack to construct a // large key to represent it. if end == nil { - end = bytes.Repeat([]byte{0xff}, 32) + end = ethdb.MaximumKey } if err := b.b.DeleteRange(start, end, nil); err != nil { return err