mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-03 12:46:43 +00:00

* Add prefix to stores * Add prefix to forgotten stores * Add a special type for prefix * Rename transaction->dbTx * Change error message * Use countKeyName * Rename Temporary Consesnsus to Staging * Add DeleteStagingConsensus to Domain interface * Add lock to staging consensus * Make prefix type-safer * Use ioutil.TempDir instead of t.TempDir
76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
package consensusstatestore
|
|
|
|
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"
|
|
)
|
|
|
|
var tipsKeyName = []byte("tips")
|
|
|
|
func (css *consensusStateStore) Tips(stagingArea *model.StagingArea, dbContext model.DBReader) ([]*externalapi.DomainHash, error) {
|
|
stagingShard := css.stagingShard(stagingArea)
|
|
|
|
if stagingShard.tipsStaging != nil {
|
|
return externalapi.CloneHashes(stagingShard.tipsStaging), nil
|
|
}
|
|
|
|
if css.tipsCache != nil {
|
|
return externalapi.CloneHashes(css.tipsCache), nil
|
|
}
|
|
|
|
tipsBytes, err := dbContext.Get(css.tipsKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tips, err := css.deserializeTips(tipsBytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
css.tipsCache = tips
|
|
return externalapi.CloneHashes(tips), nil
|
|
}
|
|
|
|
func (css *consensusStateStore) StageTips(stagingArea *model.StagingArea, tipHashes []*externalapi.DomainHash) {
|
|
stagingShard := css.stagingShard(stagingArea)
|
|
|
|
stagingShard.tipsStaging = externalapi.CloneHashes(tipHashes)
|
|
}
|
|
|
|
func (css *consensusStateStore) serializeTips(tips []*externalapi.DomainHash) ([]byte, error) {
|
|
dbTips := serialization.TipsToDBTips(tips)
|
|
return proto.Marshal(dbTips)
|
|
}
|
|
|
|
func (css *consensusStateStore) deserializeTips(tipsBytes []byte) ([]*externalapi.DomainHash,
|
|
error) {
|
|
|
|
dbTips := &serialization.DbTips{}
|
|
err := proto.Unmarshal(tipsBytes, dbTips)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return serialization.DBTipsToTips(dbTips)
|
|
}
|
|
|
|
func (csss *consensusStateStagingShard) commitTips(dbTx model.DBTransaction) error {
|
|
if csss.tipsStaging == nil {
|
|
return nil
|
|
}
|
|
|
|
tipsBytes, err := csss.store.serializeTips(csss.tipsStaging)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = dbTx.Put(csss.store.tipsKey, tipsBytes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
csss.store.tipsCache = csss.tipsStaging
|
|
|
|
return nil
|
|
}
|