core: refactor read and write section head (#1809)

This commit is contained in:
Daniel Liu 2025-12-08 15:29:23 +08:00 committed by GitHub
parent 8c42c02bde
commit e3efe5e0bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 59 additions and 16 deletions

View file

@ -434,30 +434,17 @@ func (c *ChainIndexer) setValidSections(sections uint64) {
// SectionHead retrieves the last block hash of a processed section from the
// index database.
func (c *ChainIndexer) SectionHead(section uint64) common.Hash {
var data [8]byte
binary.BigEndian.PutUint64(data[:], section)
hash, _ := c.indexDb.Get(append([]byte("shead"), data[:]...))
if len(hash) == len(common.Hash{}) {
return common.BytesToHash(hash)
}
return common.Hash{}
return rawdb.ReadSectionHead(c.indexDb, section)
}
// setSectionHead writes the last block hash of a processed section to the index
// database.
func (c *ChainIndexer) setSectionHead(section uint64, hash common.Hash) {
var data [8]byte
binary.BigEndian.PutUint64(data[:], section)
c.indexDb.Put(append([]byte("shead"), data[:]...), hash.Bytes())
rawdb.WriteSectionHead(c.indexDb, section, hash)
}
// removeSectionHead removes the reference to a processed section from the index
// database.
func (c *ChainIndexer) removeSectionHead(section uint64) {
var data [8]byte
binary.BigEndian.PutUint64(data[:], section)
c.indexDb.Delete(append([]byte("shead"), data[:]...))
rawdb.DeleteectionHead(c.indexDb, section)
}

View file

@ -0,0 +1,47 @@
// Copyright 2025 The XDPoSChain Authors
// This file is part of the XDPoSChain library.
//
// The XDPoSChain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The XDPoSChain library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the XDPoSChain library. If not, see <http://www.gnu.org/licenses/>.
package rawdb
import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/log"
)
// ReadSectionHead retrieves the last block hash of a processed section
// from the database.
func ReadSectionHead(db ethdb.KeyValueReader, section uint64) common.Hash {
hash, err := db.Get(sectionHeadKey(section))
if err != nil || len(hash) != len(common.Hash{}) {
return common.Hash{}
}
return common.BytesToHash(hash)
}
// WriteSectionHead writes the last block hash of a processed section into database.
func WriteSectionHead(db ethdb.KeyValueWriter, section uint64, hash common.Hash) {
if err := db.Put(sectionHeadKey(section), hash.Bytes()); err != nil {
log.Crit("Failed to write section head", "err", err)
}
}
// DeleteectionHead removes the reference to a processed section from the database.
func DeleteectionHead(db ethdb.KeyValueWriter, section uint64) {
if err := db.Delete(sectionHeadKey(section)); err != nil {
log.Crit("Failed to delete section head", "err", err)
}
}

View file

@ -78,6 +78,8 @@ var (
// used by old db, now only used for conversion
oldTxMetaSuffix = []byte{0x01}
sectionHeadKeyPrefix = []byte("shead")
preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
)
@ -202,3 +204,10 @@ func accountTrieNodeKey(path []byte) []byte {
func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte {
return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...)
}
// sectionHeadKey = sectionHeadKeyPrefix + section (uint64 big endian)
func sectionHeadKey(section uint64) []byte {
var data [8]byte
binary.BigEndian.PutUint64(data[:], section)
return append(sectionHeadKeyPrefix, data[:]...)
}