mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-24 07:46:45 +00:00

* [NOD-1493] Implement serialization in data stores * [NOD-1493] Remove redundant functions * [NOD-1493] Use bluesAnticoneSizesToDBBluesAnticoneSizes inside BlockGHOSTDAGDataToDBBlockGHOSTDAGData
86 lines
2.7 KiB
Go
86 lines
2.7 KiB
Go
package ghostdagdatastore
|
|
|
|
import (
|
|
"github.com/golang/protobuf/proto"
|
|
"github.com/kaspanet/kaspad/domain/consensus/database/serialization"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/dbkeys"
|
|
)
|
|
|
|
var bucket = dbkeys.MakeBucket([]byte("block-ghostdag-data"))
|
|
|
|
// ghostdagDataStore represents a store of BlockGHOSTDAGData
|
|
type ghostdagDataStore struct {
|
|
staging map[externalapi.DomainHash]*model.BlockGHOSTDAGData
|
|
}
|
|
|
|
// New instantiates a new GHOSTDAGDataStore
|
|
func New() model.GHOSTDAGDataStore {
|
|
return &ghostdagDataStore{
|
|
staging: make(map[externalapi.DomainHash]*model.BlockGHOSTDAGData),
|
|
}
|
|
}
|
|
|
|
// Stage stages the given blockGHOSTDAGData for the given blockHash
|
|
func (gds *ghostdagDataStore) Stage(blockHash *externalapi.DomainHash, blockGHOSTDAGData *model.BlockGHOSTDAGData) {
|
|
gds.staging[*blockHash] = blockGHOSTDAGData
|
|
}
|
|
|
|
func (gds *ghostdagDataStore) IsStaged() bool {
|
|
return len(gds.staging) != 0
|
|
}
|
|
|
|
func (gds *ghostdagDataStore) Discard() {
|
|
gds.staging = make(map[externalapi.DomainHash]*model.BlockGHOSTDAGData)
|
|
}
|
|
|
|
func (gds *ghostdagDataStore) Commit(dbTx model.DBTransaction) error {
|
|
for hash, blockGHOSTDAGData := range gds.staging {
|
|
blockGhostdagDataBytes, err := gds.serializeBlockGHOSTDAGData(blockGHOSTDAGData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = dbTx.Put(gds.hashAsKey(&hash), blockGhostdagDataBytes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
gds.Discard()
|
|
return nil
|
|
}
|
|
|
|
// Get gets the blockGHOSTDAGData associated with the given blockHash
|
|
func (gds *ghostdagDataStore) Get(dbContext model.DBReader, blockHash *externalapi.DomainHash) (*model.BlockGHOSTDAGData, error) {
|
|
if blockGHOSTDAGData, ok := gds.staging[*blockHash]; ok {
|
|
return blockGHOSTDAGData, nil
|
|
}
|
|
|
|
blockGHOSTDAGDataBytes, err := dbContext.Get(gds.hashAsKey(blockHash))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return gds.deserializeBlockGHOSTDAGData(blockGHOSTDAGDataBytes)
|
|
}
|
|
|
|
func (gds *ghostdagDataStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
|
|
return bucket.Key(hash[:])
|
|
}
|
|
|
|
func (gds *ghostdagDataStore) serializeBlockGHOSTDAGData(blockGHOSTDAGData *model.BlockGHOSTDAGData) ([]byte, error) {
|
|
return proto.Marshal(serialization.BlockGHOSTDAGDataToDBBlockGHOSTDAGData(blockGHOSTDAGData))
|
|
}
|
|
|
|
func (gds *ghostdagDataStore) deserializeBlockGHOSTDAGData(blockGHOSTDAGDataBytes []byte) (*model.BlockGHOSTDAGData, error) {
|
|
dbBlockGHOSTDAGData := &serialization.DbBlockGhostdagData{}
|
|
err := proto.Unmarshal(blockGHOSTDAGDataBytes, dbBlockGHOSTDAGData)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return serialization.DBBlockGHOSTDAGDataToBlockGHOSTDAGData(dbBlockGHOSTDAGData)
|
|
}
|