Skip to content

Commit ad34d98

Browse files
committed
core, ethdb: define MaximumKey
1 parent 9bc6bf6 commit ad34d98

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

core/rawdb/table.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package rawdb
1818

1919
import (
20-
"bytes"
21-
2220
"github.com/ethereum/go-ethereum/ethdb"
2321
)
2422

@@ -137,7 +135,7 @@ func (t *table) DeleteRange(start, end []byte) error {
137135
// The nilness will be lost by adding the prefix, explicitly converting it
138136
// to a special flag representing the end of key range.
139137
if end == nil {
140-
end = bytes.Repeat([]byte{0xff}, 32)
138+
end = ethdb.MaximumKey
141139
}
142140
return t.db.DeleteRange(append([]byte(t.prefix), start...), append([]byte(t.prefix), end...))
143141
}
@@ -235,7 +233,7 @@ func (b *tableBatch) DeleteRange(start, end []byte) error {
235233
// The nilness will be lost by adding the prefix, explicitly converting it
236234
// to a special flag representing the end of key range.
237235
if end == nil {
238-
end = bytes.Repeat([]byte{0xff}, 32)
236+
end = ethdb.MaximumKey
239237
}
240238
return b.batch.DeleteRange(append([]byte(b.prefix), start...), append([]byte(b.prefix), end...))
241239
}

ethdb/database.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,23 @@
1818
package ethdb
1919

2020
import (
21+
"bytes"
2122
"errors"
2223
"io"
2324
)
2425

26+
var (
27+
// MaximumKey is a special marker representing the largest possible key
28+
// in the database.
29+
//
30+
// All prefixed database entries will be smaller than this marker.
31+
// For trie nodes in hash mode, we use a 32-byte slice filled with 0xFF
32+
// because there may be shared prefixes starting with multiple 0xFF bytes.
33+
// Using 32 bytes ensures that only a hash collision could potentially
34+
// match or exceed it.
35+
MaximumKey = bytes.Repeat([]byte{0xff}, 32)
36+
)
37+
2538
// KeyValueReader wraps the Has and Get method of a backing data store.
2639
type KeyValueReader interface {
2740
// Has retrieves if a key is present in the key-value data store.

ethdb/pebble/pebble.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package pebble
1919

2020
import (
21-
"bytes"
2221
"fmt"
2322
"runtime"
2423
"strings"
@@ -411,7 +410,7 @@ func (d *Database) DeleteRange(start, end []byte) error {
411410
// in pebble(nil in leveldb). Use an ugly hack to construct a
412411
// large key to represent it.
413412
if end == nil {
414-
end = bytes.Repeat([]byte{0xff}, 32)
413+
end = ethdb.MaximumKey
415414
}
416415
return d.db.DeleteRange(start, end, d.writeOptions)
417416
}
@@ -471,7 +470,7 @@ func (d *Database) Compact(start []byte, limit []byte) error {
471470
// 0xff-s, so 32 ensures than only a hash collision could touch it.
472471
// https://github.com/cockroachdb/pebble/issues/2359#issuecomment-1443995833
473472
if limit == nil {
474-
limit = bytes.Repeat([]byte{0xff}, 32)
473+
limit = ethdb.MaximumKey
475474
}
476475
return d.db.Compact(start, limit, true) // Parallelization is preferred
477476
}
@@ -633,7 +632,7 @@ func (b *batch) DeleteRange(start, end []byte) error {
633632
// in pebble(nil in leveldb). Use an ugly hack to construct a
634633
// large key to represent it.
635634
if end == nil {
636-
end = bytes.Repeat([]byte{0xff}, 32)
635+
end = ethdb.MaximumKey
637636
}
638637
if err := b.b.DeleteRange(start, end, nil); err != nil {
639638
return err

0 commit comments

Comments
 (0)