diff --git a/consensus/XDPoS/engines/engine_v1/snapshot.go b/consensus/XDPoS/engines/engine_v1/snapshot.go index 012b29aabe..bf3d4615e7 100644 --- a/consensus/XDPoS/engines/engine_v1/snapshot.go +++ b/consensus/XDPoS/engines/engine_v1/snapshot.go @@ -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. diff --git a/consensus/XDPoS/engines/engine_v2/snapshot.go b/consensus/XDPoS/engines/engine_v2/snapshot.go index b98e935b26..7c45a6a107 100644 --- a/consensus/XDPoS/engines/engine_v2/snapshot.go +++ b/consensus/XDPoS/engines/engine_v2/snapshot.go @@ -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 diff --git a/core/rawdb/accessors_xdc.go b/core/rawdb/accessors_xdc.go index 716fd1ec8b..8d212b9375 100644 --- a/core/rawdb/accessors_xdc.go +++ b/core/rawdb/accessors_xdc.go @@ -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 { diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index b59d5a1b5a..1745f4c1c5 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -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