stasatdaglabs 0fa13357c3
[NOD-1566] Add caching to all stores (#1152)
* [NOD-1566] Add a dependency to golang-lru.

* [NOD-1566] Add caching to blockstore.go.

* [NOD-1566] Add LRUCache to all store objects and initialize them.

* [NOD-1566] Add caching to acceptanceDataStore.

* [NOD-1566] Add caching to blockHeaderStore.

* [NOD-1566] Implement a simpler LRU cache.

* [NOD-1566] Use the simpler cache implementation everywhere.

* [NOD-1566] Remove dependency in golang-lru.

* [NOD-1566] Fix object reuse issues in store Get functions.

* [NOD-1566] Add caching to blockRelationStore.

* [NOD-1566] Add caching to blockStatusStore.

* [NOD-1566] Add caching to ghostdagDataStore.

* [NOD-1566] Add caching to multisetStore.

* [NOD-1566] Add caching to reachabilityDataStore.

* [NOD-1566] Add caching to utxoDiffStore.

* [NOD-1566] Add caching to reachabilityReindexRoot.

* [NOD-1566] Add caching to pruningStore.

* [NOD-1566] Add caching to headerTipsStore.

* [NOD-1566] Add caching to consensusStateStore.

* [NOD-1566] Add comments explaining why we don't discard staging at the normal location in consensusStateStore.

* [NOD-1566] Make go vet happy.

* [NOD-1566] Fix merge errors.

* [NOD-1566] Add a missing break statement.

* [NOD-1566] Run go mod tidy.

* [NOD-1566] Remove serializedUTXOSetCache.
2020-11-25 13:41:13 +02:00

116 lines
3.2 KiB
Go

package blockstatusstore
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"
"github.com/kaspanet/kaspad/domain/consensus/utils/lrucache"
)
var bucket = dbkeys.MakeBucket([]byte("block-statuses"))
// blockStatusStore represents a store of BlockStatuses
type blockStatusStore struct {
staging map[externalapi.DomainHash]externalapi.BlockStatus
cache *lrucache.LRUCache
}
// New instantiates a new BlockStatusStore
func New(cacheSize int) model.BlockStatusStore {
return &blockStatusStore{
staging: make(map[externalapi.DomainHash]externalapi.BlockStatus),
cache: lrucache.New(cacheSize),
}
}
// Stage stages the given blockStatus for the given blockHash
func (bss *blockStatusStore) Stage(blockHash *externalapi.DomainHash, blockStatus externalapi.BlockStatus) {
bss.staging[*blockHash] = blockStatus.Clone()
}
func (bss *blockStatusStore) IsStaged() bool {
return len(bss.staging) != 0
}
func (bss *blockStatusStore) Discard() {
bss.staging = make(map[externalapi.DomainHash]externalapi.BlockStatus)
}
func (bss *blockStatusStore) Commit(dbTx model.DBTransaction) error {
for hash, status := range bss.staging {
blockStatusBytes, err := bss.serializeBlockStatus(status)
if err != nil {
return err
}
err = dbTx.Put(bss.hashAsKey(&hash), blockStatusBytes)
if err != nil {
return err
}
bss.cache.Add(&hash, status)
}
bss.Discard()
return nil
}
// Get gets the blockStatus associated with the given blockHash
func (bss *blockStatusStore) Get(dbContext model.DBReader, blockHash *externalapi.DomainHash) (externalapi.BlockStatus, error) {
if status, ok := bss.staging[*blockHash]; ok {
return status, nil
}
if status, ok := bss.cache.Get(blockHash); ok {
return status.(externalapi.BlockStatus), nil
}
statusBytes, err := dbContext.Get(bss.hashAsKey(blockHash))
if err != nil {
return 0, err
}
status, err := bss.deserializeBlockStatus(statusBytes)
if err != nil {
return 0, err
}
bss.cache.Add(blockHash, status)
return status, nil
}
// Exists returns true if the blockStatus for the given blockHash exists
func (bss *blockStatusStore) Exists(dbContext model.DBReader, blockHash *externalapi.DomainHash) (bool, error) {
if _, ok := bss.staging[*blockHash]; ok {
return true, nil
}
if bss.cache.Has(blockHash) {
return true, nil
}
exists, err := dbContext.Has(bss.hashAsKey(blockHash))
if err != nil {
return false, err
}
return exists, nil
}
func (bss *blockStatusStore) serializeBlockStatus(status externalapi.BlockStatus) ([]byte, error) {
dbBlockStatus := serialization.DomainBlockStatusToDbBlockStatus(status)
return proto.Marshal(dbBlockStatus)
}
func (bss *blockStatusStore) deserializeBlockStatus(statusBytes []byte) (externalapi.BlockStatus, error) {
dbBlockStatus := &serialization.DbBlockStatus{}
err := proto.Unmarshal(statusBytes, dbBlockStatus)
if err != nil {
return 0, err
}
return serialization.DbBlockStatusToDomainBlockStatus(dbBlockStatus), nil
}
func (bss *blockStatusStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
return bucket.Key(hash[:])
}