From bc5794cdf57d178806938b7c396e631e5eb292ef Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Mon, 8 Dec 2025 17:37:33 +0800 Subject: [PATCH] core: refactor read and write valid sections (#1808) --- core/chain_indexer.go | 12 ++++-------- core/rawdb/accessors_xdc.go | 22 ++++++++++++++++++++++ core/rawdb/schema.go | 1 + 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/core/chain_indexer.go b/core/chain_indexer.go index 5f39ddddab..d24fa4358c 100644 --- a/core/chain_indexer.go +++ b/core/chain_indexer.go @@ -18,7 +18,6 @@ package core import ( "context" - "encoding/binary" "errors" "fmt" "sync" @@ -410,18 +409,15 @@ func (c *ChainIndexer) AddChildIndexer(indexer *ChainIndexer) { // loadValidSections reads the number of valid sections from the index database // and caches is into the local state. func (c *ChainIndexer) loadValidSections() { - data, _ := c.indexDb.Get([]byte("count")) - if len(data) == 8 { - c.storedSections = binary.BigEndian.Uint64(data[:]) + storedSections := rawdb.ReadValidSections(c.indexDb) + if storedSections != nil { + c.storedSections = *storedSections } } // setValidSections writes the number of valid sections to the index database func (c *ChainIndexer) setValidSections(sections uint64) { - // Set the current number of valid sections in the database - var data [8]byte - binary.BigEndian.PutUint64(data[:], sections) - c.indexDb.Put([]byte("count"), data[:]) + rawdb.WriteValidSections(c.indexDb, sections) // Remove any reorged sections, caching the valids in the mean time for c.storedSections > sections { diff --git a/core/rawdb/accessors_xdc.go b/core/rawdb/accessors_xdc.go index 8d212b9375..48a97be54b 100644 --- a/core/rawdb/accessors_xdc.go +++ b/core/rawdb/accessors_xdc.go @@ -17,6 +17,8 @@ package rawdb import ( + "encoding/binary" + "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/ethdb" "github.com/XinFinOrg/XDPoSChain/log" @@ -79,3 +81,23 @@ func DeleteectionHead(db ethdb.KeyValueWriter, section uint64) { log.Crit("Failed to delete section head", "err", err) } } + +// ReadValidSections retrieves the number of valid sections from database. +func ReadValidSections(db ethdb.KeyValueReader) *uint64 { + data, err := db.Get(validSectionsKey) + if err != nil || len(data) != 8 { + return nil + } + storedSections := binary.BigEndian.Uint64(data[:]) + return &storedSections +} + +// WriteValidSections writes the number of valid sections into database +func WriteValidSections(db ethdb.KeyValueWriter, sections uint64) { + // Set the current number of valid sections in the database + var data [8]byte + binary.BigEndian.PutUint64(data[:], sections) + if err := db.Put(validSectionsKey, data[:]); err != nil { + log.Crit("Failed to store valid sections", "err", err) + } +} diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index 1745f4c1c5..010caf4cd6 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -83,6 +83,7 @@ var ( xdposV2Prefix = []byte("XDPoS-V2-") sectionHeadKeyPrefix = []byte("shead") + validSectionsKey = []byte("count") preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil) preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)