Skip to content

Commit d86fe26

Browse files
s7v7nislandsseven
andauthored
core/rawdb: refactor db key prefix (#26000)
Co-authored-by: seven <[email protected]>
1 parent 6069d82 commit d86fe26

File tree

6 files changed

+34
-30
lines changed

6 files changed

+34
-30
lines changed

consensus/clique/snapshot.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/core/rawdb"
2627
"github.com/ethereum/go-ethereum/core/types"
2728
"github.com/ethereum/go-ethereum/ethdb"
2829
"github.com/ethereum/go-ethereum/log"
@@ -87,7 +88,7 @@ func newSnapshot(config *params.CliqueConfig, sigcache *lru.ARCCache, number uin
8788

8889
// loadSnapshot loads an existing snapshot from the database.
8990
func loadSnapshot(config *params.CliqueConfig, sigcache *lru.ARCCache, db ethdb.Database, hash common.Hash) (*Snapshot, error) {
90-
blob, err := db.Get(append([]byte("clique-"), hash[:]...))
91+
blob, err := db.Get(append(rawdb.CliqueSnapshotPrefix, hash[:]...))
9192
if err != nil {
9293
return nil, err
9394
}
@@ -107,7 +108,7 @@ func (s *Snapshot) store(db ethdb.Database) error {
107108
if err != nil {
108109
return err
109110
}
110-
return db.Put(append([]byte("clique-"), s.Hash[:]...), blob)
111+
return db.Put(append(rawdb.CliqueSnapshotPrefix, s.Hash[:]...), blob)
111112
}
112113

113114
// copy creates a deep copy of the snapshot, though not the individual votes.

core/rawdb/database.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,15 @@ func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error {
439439
bloomBits.Add(size)
440440
case bytes.HasPrefix(key, skeletonHeaderPrefix) && len(key) == (len(skeletonHeaderPrefix)+8):
441441
beaconHeaders.Add(size)
442-
case bytes.HasPrefix(key, []byte("clique-")) && len(key) == 7+common.HashLength:
442+
case bytes.HasPrefix(key, CliqueSnapshotPrefix) && len(key) == 7+common.HashLength:
443443
cliqueSnaps.Add(size)
444-
case bytes.HasPrefix(key, []byte("cht-")) ||
445-
bytes.HasPrefix(key, []byte("chtIndexV2-")) ||
446-
bytes.HasPrefix(key, []byte("chtRootV2-")): // Canonical hash trie
444+
case bytes.HasPrefix(key, ChtTablePrefix) ||
445+
bytes.HasPrefix(key, ChtIndexTablePrefix) ||
446+
bytes.HasPrefix(key, ChtPrefix): // Canonical hash trie
447447
chtTrieNodes.Add(size)
448-
case bytes.HasPrefix(key, []byte("blt-")) ||
449-
bytes.HasPrefix(key, []byte("bltIndex-")) ||
450-
bytes.HasPrefix(key, []byte("bltRoot-")): // Bloomtrie sub
448+
case bytes.HasPrefix(key, BloomTrieTablePrefix) ||
449+
bytes.HasPrefix(key, BloomTrieIndexPrefix) ||
450+
bytes.HasPrefix(key, BloomTriePrefix): // Bloomtrie sub
451451
bloomTrieNodes.Add(size)
452452
default:
453453
var accounted bool

core/rawdb/schema.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ var (
109109
// BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
110110
BloomBitsIndexPrefix = []byte("iB")
111111

112+
ChtPrefix = []byte("chtRootV2-") // ChtPrefix + chtNum (uint64 big endian) -> trie root hash
113+
ChtTablePrefix = []byte("cht-")
114+
ChtIndexTablePrefix = []byte("chtIndexV2-")
115+
116+
BloomTriePrefix = []byte("bltRoot-") // BloomTriePrefix + bloomTrieNum (uint64 big endian) -> trie root hash
117+
BloomTrieTablePrefix = []byte("blt-")
118+
BloomTrieIndexPrefix = []byte("bltIndex-")
119+
120+
CliqueSnapshotPrefix = []byte("clique-")
121+
112122
preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
113123
preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
114124
)

les/handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ func testGetCHTProofs(t *testing.T, protocol int) {
512512
AuxData: [][]byte{rlp},
513513
}
514514
root := light.GetChtRoot(server.db, 0, bc.GetHeaderByNumber(config.ChtSize-1).Hash())
515-
trie, _ := trie.New(trie.TrieID(root), trie.NewDatabase(rawdb.NewTable(server.db, light.ChtTablePrefix)))
515+
trie, _ := trie.New(trie.TrieID(root), trie.NewDatabase(rawdb.NewTable(server.db, string(rawdb.ChtTablePrefix))))
516516
trie.Prove(key, 0, &proofsV2.Proofs)
517517
// Assemble the requests for the different protocols
518518
requestsV2 := []HelperTrieReq{{
@@ -577,7 +577,7 @@ func testGetBloombitsProofs(t *testing.T, protocol int) {
577577
var proofs HelperTrieResps
578578

579579
root := light.GetBloomTrieRoot(server.db, 0, bc.GetHeaderByNumber(config.BloomTrieSize-1).Hash())
580-
trie, _ := trie.New(trie.TrieID(root), trie.NewDatabase(rawdb.NewTable(server.db, light.BloomTrieTablePrefix)))
580+
trie, _ := trie.New(trie.TrieID(root), trie.NewDatabase(rawdb.NewTable(server.db, string(rawdb.BloomTrieTablePrefix))))
581581
trie.Prove(key, 0, &proofs.Proofs)
582582

583583
// Send the proof request and verify the response

les/server_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,10 @@ func (h *serverHandler) GetHelperTrie(typ uint, index uint64) *trie.Trie {
383383
switch typ {
384384
case htCanonical:
385385
sectionHead := rawdb.ReadCanonicalHash(h.chainDb, (index+1)*h.server.iConfig.ChtSize-1)
386-
root, prefix = light.GetChtRoot(h.chainDb, index, sectionHead), light.ChtTablePrefix
386+
root, prefix = light.GetChtRoot(h.chainDb, index, sectionHead), string(rawdb.ChtTablePrefix)
387387
case htBloomBits:
388388
sectionHead := rawdb.ReadCanonicalHash(h.chainDb, (index+1)*h.server.iConfig.BloomTrieSize-1)
389-
root, prefix = light.GetBloomTrieRoot(h.chainDb, index, sectionHead), light.BloomTrieTablePrefix
389+
root, prefix = light.GetBloomTrieRoot(h.chainDb, index, sectionHead), string(rawdb.BloomTrieTablePrefix)
390390
}
391391
if root == (common.Hash{}) {
392392
return nil

light/postprocess.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ var (
102102
errNoTrustedCht = errors.New("no trusted canonical hash trie")
103103
errNoTrustedBloomTrie = errors.New("no trusted bloom trie")
104104
errNoHeader = errors.New("header not found")
105-
chtPrefix = []byte("chtRootV2-") // chtPrefix + chtNum (uint64 big endian) -> trie root hash
106-
ChtTablePrefix = "cht-"
107105
)
108106

109107
// ChtNode structures are stored in the Canonical Hash Trie in an RLP encoded format
@@ -116,15 +114,15 @@ type ChtNode struct {
116114
func GetChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash {
117115
var encNumber [8]byte
118116
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
119-
data, _ := db.Get(append(append(chtPrefix, encNumber[:]...), sectionHead.Bytes()...))
117+
data, _ := db.Get(append(append(rawdb.ChtPrefix, encNumber[:]...), sectionHead.Bytes()...))
120118
return common.BytesToHash(data)
121119
}
122120

123121
// StoreChtRoot writes the CHT root associated to the given section into the database
124122
func StoreChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash) {
125123
var encNumber [8]byte
126124
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
127-
db.Put(append(append(chtPrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes())
125+
db.Put(append(append(rawdb.ChtPrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes())
128126
}
129127

130128
// ChtIndexerBackend implements core.ChainIndexerBackend.
@@ -140,7 +138,7 @@ type ChtIndexerBackend struct {
140138

141139
// NewChtIndexer creates a Cht chain indexer
142140
func NewChtIndexer(db ethdb.Database, odr OdrBackend, size, confirms uint64, disablePruning bool) *core.ChainIndexer {
143-
trieTable := rawdb.NewTable(db, ChtTablePrefix)
141+
trieTable := rawdb.NewTable(db, string(rawdb.ChtTablePrefix))
144142
backend := &ChtIndexerBackend{
145143
diskdb: db,
146144
odr: odr,
@@ -149,7 +147,7 @@ func NewChtIndexer(db ethdb.Database, odr OdrBackend, size, confirms uint64, dis
149147
sectionSize: size,
150148
disablePruning: disablePruning,
151149
}
152-
return core.NewChainIndexer(db, rawdb.NewTable(db, "chtIndexV2-"), backend, size, confirms, time.Millisecond*100, "cht")
150+
return core.NewChainIndexer(db, rawdb.NewTable(db, string(rawdb.ChtIndexTablePrefix)), backend, size, confirms, time.Millisecond*100, "cht")
153151
}
154152

155153
// fetchMissingNodes tries to retrieve the last entry of the latest trusted CHT from the
@@ -249,7 +247,7 @@ func (c *ChtIndexerBackend) Commit() error {
249247
}
250248
}
251249
for it.Next() {
252-
trimmed := bytes.TrimPrefix(it.Key(), []byte(ChtTablePrefix))
250+
trimmed := bytes.TrimPrefix(it.Key(), rawdb.ChtTablePrefix)
253251
if len(trimmed) == common.HashLength {
254252
if _, ok := hashes[common.BytesToHash(trimmed)]; !ok {
255253
batch.Delete(trimmed)
@@ -311,24 +309,19 @@ func (c *ChtIndexerBackend) Prune(threshold uint64) error {
311309
return nil
312310
}
313311

314-
var (
315-
bloomTriePrefix = []byte("bltRoot-") // bloomTriePrefix + bloomTrieNum (uint64 big endian) -> trie root hash
316-
BloomTrieTablePrefix = "blt-"
317-
)
318-
319312
// GetBloomTrieRoot reads the BloomTrie root associated to the given section from the database
320313
func GetBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash {
321314
var encNumber [8]byte
322315
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
323-
data, _ := db.Get(append(append(bloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...))
316+
data, _ := db.Get(append(append(rawdb.BloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...))
324317
return common.BytesToHash(data)
325318
}
326319

327320
// StoreBloomTrieRoot writes the BloomTrie root associated to the given section into the database
328321
func StoreBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash) {
329322
var encNumber [8]byte
330323
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
331-
db.Put(append(append(bloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes())
324+
db.Put(append(append(rawdb.BloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes())
332325
}
333326

334327
// BloomTrieIndexerBackend implements core.ChainIndexerBackend
@@ -347,7 +340,7 @@ type BloomTrieIndexerBackend struct {
347340

348341
// NewBloomTrieIndexer creates a BloomTrie chain indexer
349342
func NewBloomTrieIndexer(db ethdb.Database, odr OdrBackend, parentSize, size uint64, disablePruning bool) *core.ChainIndexer {
350-
trieTable := rawdb.NewTable(db, BloomTrieTablePrefix)
343+
trieTable := rawdb.NewTable(db, string(rawdb.BloomTrieTablePrefix))
351344
backend := &BloomTrieIndexerBackend{
352345
diskdb: db,
353346
odr: odr,
@@ -359,7 +352,7 @@ func NewBloomTrieIndexer(db ethdb.Database, odr OdrBackend, parentSize, size uin
359352
}
360353
backend.bloomTrieRatio = size / parentSize
361354
backend.sectionHeads = make([]common.Hash, backend.bloomTrieRatio)
362-
return core.NewChainIndexer(db, rawdb.NewTable(db, "bltIndex-"), backend, size, 0, time.Millisecond*100, "bloomtrie")
355+
return core.NewChainIndexer(db, rawdb.NewTable(db, string(rawdb.BloomTrieIndexPrefix)), backend, size, 0, time.Millisecond*100, "bloomtrie")
363356
}
364357

365358
// fetchMissingNodes tries to retrieve the last entries of the latest trusted bloom trie from the
@@ -500,7 +493,7 @@ func (b *BloomTrieIndexerBackend) Commit() error {
500493
}
501494
}
502495
for it.Next() {
503-
trimmed := bytes.TrimPrefix(it.Key(), []byte(BloomTrieTablePrefix))
496+
trimmed := bytes.TrimPrefix(it.Key(), rawdb.BloomTrieTablePrefix)
504497
if len(trimmed) == common.HashLength {
505498
if _, ok := hashes[common.BytesToHash(trimmed)]; !ok {
506499
batch.Delete(trimmed)

0 commit comments

Comments
 (0)