diff --git a/core/chain_indexer.go b/core/chain_indexer.go index e1e7018723..5f39ddddab 100644 --- a/core/chain_indexer.go +++ b/core/chain_indexer.go @@ -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) } diff --git a/core/rawdb/accessors_xdc.go b/core/rawdb/accessors_xdc.go new file mode 100644 index 0000000000..716fd1ec8b --- /dev/null +++ b/core/rawdb/accessors_xdc.go @@ -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 . + +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) + } +} diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index 874b52c15c..b59d5a1b5a 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -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[:]...) +}