consensus/XDPoS, core/rawdb: stop node if fail to store snapshot (#1803)

This commit is contained in:
Daniel Liu 2025-12-08 17:37:22 +08:00 committed by GitHub
parent d6309612fc
commit 348b7fa68f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 54 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
"github.com/XinFinOrg/XDPoSChain/consensus/clique"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/params"
@ -62,7 +63,7 @@ func newSnapshot(config *params.XDPoSConfig, sigcache *utils.SigLRU, number uint
// loadSnapshot loads an existing snapshot from the database.
func loadSnapshot(config *params.XDPoSConfig, sigcache *utils.SigLRU, db ethdb.Database, hash common.Hash) (*SnapshotV1, error) {
blob, err := db.Get(append([]byte("XDPoS-"), hash[:]...))
blob, err := rawdb.ReadXdposV1Snapshot(db, hash)
if err != nil {
return nil, err
}
@ -82,7 +83,7 @@ func (s *SnapshotV1) store(db ethdb.Database) error {
if err != nil {
return err
}
return db.Put(append([]byte("XDPoS-"), s.Hash[:]...), blob)
return rawdb.WriteXdposV1Snapshot(db, s.Hash, blob)
}
// copy creates a deep copy of the SnapshotV1, though not the individual votes.

View file

@ -6,6 +6,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/consensus"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/log"
)
@ -34,7 +35,7 @@ func newSnapshot(number uint64, hash common.Hash, candidates []common.Address) *
// loadSnapshot loads an existing snapshot from the database.
func loadSnapshot(db ethdb.Database, hash common.Hash) (*SnapshotV2, error) {
blob, err := db.Get(append([]byte("XDPoS-V2-"), hash[:]...))
blob, err := rawdb.ReadXdposV2Snapshot(db, hash)
if err != nil {
return nil, err
}
@ -52,7 +53,7 @@ func storeSnapshot(s *SnapshotV2, db ethdb.Database) error {
if err != nil {
return err
}
return db.Put(append([]byte("XDPoS-V2-"), s.Hash[:]...), blob)
return rawdb.WriteXdposV2Snapshot(db, s.Hash, blob)
}
// retrieves candidates nodes list in map type

View file

@ -22,6 +22,40 @@ import (
"github.com/XinFinOrg/XDPoSChain/log"
)
// ReadXdposV1Snapshot retrieves an existing snapshot from the database.
func ReadXdposV1Snapshot(db ethdb.KeyValueReader, hash common.Hash) ([]byte, error) {
data, err := db.Get(xdposV1Key(hash))
if err != nil {
return nil, err
}
return data, nil
}
// WriteXdposV1Snapshot writes the SnapshotV2 into the database.
func WriteXdposV1Snapshot(db ethdb.KeyValueWriter, hash common.Hash, blob []byte) error {
if err := db.Put(xdposV1Key(hash), blob); err != nil {
log.Crit("Failed to store SnapshotV2", "err", err)
}
return nil
}
// ReadXdposV2Snapshot retrieves an existing snapshot from the database.
func ReadXdposV2Snapshot(db ethdb.KeyValueReader, hash common.Hash) ([]byte, error) {
data, err := db.Get(xdposV2Key(hash))
if err != nil {
return nil, err
}
return data, nil
}
// WriteXdposV2Snapshot writes the SnapshotV2 into the database.
func WriteXdposV2Snapshot(db ethdb.KeyValueWriter, hash common.Hash, blob []byte) error {
if err := db.Put(xdposV2Key(hash), blob); err != nil {
log.Crit("Failed to store SnapshotV2", "err", err)
}
return nil
}
// ReadSectionHead retrieves the last block hash of a processed section
// from the database.
func ReadSectionHead(db ethdb.KeyValueReader, section uint64) common.Hash {

View file

@ -78,6 +78,10 @@ var (
// used by old db, now only used for conversion
oldTxMetaSuffix = []byte{0x01}
// XDPoS snapshot prefix
xdposV1Prefix = []byte("XDPoS-")
xdposV2Prefix = []byte("XDPoS-V2-")
sectionHeadKeyPrefix = []byte("shead")
preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
@ -205,6 +209,16 @@ func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte {
return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...)
}
// xdposV1Key = xdposV1Prefix + hash
func xdposV1Key(hash common.Hash) []byte {
return append(xdposV1Prefix, hash.Bytes()...)
}
// xdposV2Key = xdposV2Prefix + hash
func xdposV2Key(hash common.Hash) []byte {
return append(xdposV2Prefix, hash.Bytes()...)
}
// sectionHeadKey = sectionHeadKeyPrefix + section (uint64 big endian)
func sectionHeadKey(section uint64) []byte {
var data [8]byte