mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-25 08:16:46 +00:00

* [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.
118 lines
3.7 KiB
Go
118 lines
3.7 KiB
Go
package acceptancedatastore
|
|
|
|
import (
|
|
"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"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
var bucket = dbkeys.MakeBucket([]byte("acceptance-data"))
|
|
|
|
// acceptanceDataStore represents a store of AcceptanceData
|
|
type acceptanceDataStore struct {
|
|
staging map[externalapi.DomainHash]model.AcceptanceData
|
|
toDelete map[externalapi.DomainHash]struct{}
|
|
cache *lrucache.LRUCache
|
|
}
|
|
|
|
// New instantiates a new AcceptanceDataStore
|
|
func New(cacheSize int) model.AcceptanceDataStore {
|
|
return &acceptanceDataStore{
|
|
staging: make(map[externalapi.DomainHash]model.AcceptanceData),
|
|
toDelete: make(map[externalapi.DomainHash]struct{}),
|
|
cache: lrucache.New(cacheSize),
|
|
}
|
|
}
|
|
|
|
// Stage stages the given acceptanceData for the given blockHash
|
|
func (ads *acceptanceDataStore) Stage(blockHash *externalapi.DomainHash, acceptanceData model.AcceptanceData) {
|
|
ads.staging[*blockHash] = acceptanceData.Clone()
|
|
}
|
|
|
|
func (ads *acceptanceDataStore) IsStaged() bool {
|
|
return len(ads.staging) != 0 || len(ads.toDelete) != 0
|
|
}
|
|
|
|
func (ads *acceptanceDataStore) Discard() {
|
|
ads.staging = make(map[externalapi.DomainHash]model.AcceptanceData)
|
|
ads.toDelete = make(map[externalapi.DomainHash]struct{})
|
|
}
|
|
|
|
func (ads *acceptanceDataStore) Commit(dbTx model.DBTransaction) error {
|
|
for hash, acceptanceData := range ads.staging {
|
|
acceptanceDataBytes, err := ads.serializeAcceptanceData(acceptanceData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = dbTx.Put(ads.hashAsKey(&hash), acceptanceDataBytes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ads.cache.Add(&hash, acceptanceData)
|
|
}
|
|
|
|
for hash := range ads.toDelete {
|
|
err := dbTx.Delete(ads.hashAsKey(&hash))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ads.cache.Remove(&hash)
|
|
}
|
|
|
|
ads.Discard()
|
|
return nil
|
|
}
|
|
|
|
// Get gets the acceptanceData associated with the given blockHash
|
|
func (ads *acceptanceDataStore) Get(dbContext model.DBReader, blockHash *externalapi.DomainHash) (model.AcceptanceData, error) {
|
|
if acceptanceData, ok := ads.staging[*blockHash]; ok {
|
|
return acceptanceData.Clone(), nil
|
|
}
|
|
|
|
if acceptanceData, ok := ads.cache.Get(blockHash); ok {
|
|
return acceptanceData.(model.AcceptanceData).Clone(), nil
|
|
}
|
|
|
|
acceptanceDataBytes, err := dbContext.Get(ads.hashAsKey(blockHash))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
acceptanceData, err := ads.deserializeAcceptanceData(acceptanceDataBytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ads.cache.Add(blockHash, acceptanceData)
|
|
return acceptanceData.Clone(), nil
|
|
}
|
|
|
|
// Delete deletes the acceptanceData associated with the given blockHash
|
|
func (ads *acceptanceDataStore) Delete(blockHash *externalapi.DomainHash) {
|
|
if _, ok := ads.staging[*blockHash]; ok {
|
|
delete(ads.staging, *blockHash)
|
|
return
|
|
}
|
|
ads.toDelete[*blockHash] = struct{}{}
|
|
}
|
|
|
|
func (ads *acceptanceDataStore) serializeAcceptanceData(acceptanceData model.AcceptanceData) ([]byte, error) {
|
|
dbAcceptanceData := serialization.DomainAcceptanceDataToDbAcceptanceData(acceptanceData)
|
|
return proto.Marshal(dbAcceptanceData)
|
|
}
|
|
|
|
func (ads *acceptanceDataStore) deserializeAcceptanceData(acceptanceDataBytes []byte) (model.AcceptanceData, error) {
|
|
dbAcceptanceData := &serialization.DbAcceptanceData{}
|
|
err := proto.Unmarshal(acceptanceDataBytes, dbAcceptanceData)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return serialization.DbAcceptanceDataToDomainAcceptanceData(dbAcceptanceData)
|
|
}
|
|
|
|
func (ads *acceptanceDataStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
|
|
return bucket.Key(hash[:])
|
|
}
|