Skip to content

Commit

Permalink
Add database prefix (#1750)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
someone235 authored Jun 15, 2021
1 parent 70399da commit 4207c82
Show file tree
Hide file tree
Showing 34 changed files with 727 additions and 174 deletions.
16 changes: 16 additions & 0 deletions app/protocol/flows/testing/handle_relay_invs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ type fakeRelayInvsContext struct {
rwLock sync.RWMutex
}

func (f *fakeRelayInvsContext) DeleteStagingConsensus() error {
panic("implement me")
}

func (f *fakeRelayInvsContext) StagingConsensus() externalapi.Consensus {
panic("implement me")
}

func (f *fakeRelayInvsContext) InitStagingConsensus() error {
panic("implement me")
}

func (f *fakeRelayInvsContext) CommitStagingConsensus() error {
panic("implement me")
}

func (f *fakeRelayInvsContext) EstimateNetworkHashesPerSecond(startHash *externalapi.DomainHash, windowSize int) (uint64, error) {
panic(errors.Errorf("called unimplemented function from test '%s'", f.testName))
}
Expand Down
16 changes: 16 additions & 0 deletions app/rpc/rpchandlers/get_blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ type fakeDomain struct {
testapi.TestConsensus
}

func (d fakeDomain) DeleteStagingConsensus() error {
panic("implement me")
}

func (d fakeDomain) StagingConsensus() externalapi.Consensus {
panic("implement me")
}

func (d fakeDomain) InitStagingConsensus() error {
panic("implement me")
}

func (d fakeDomain) CommitStagingConsensus() error {
panic("implement me")
}

func (d fakeDomain) Consensus() externalapi.Consensus { return d }
func (d fakeDomain) MiningManager() miningmanager.MiningManager { return nil }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/lrucache"
"github.com/kaspanet/kaspad/domain/prefixmanager/prefix"
"google.golang.org/protobuf/proto"
)

var bucket = database.MakeBucket([]byte("acceptance-data"))
var bucketName = []byte("acceptance-data")

// acceptanceDataStore represents a store of AcceptanceData
type acceptanceDataStore struct {
cache *lrucache.LRUCache
cache *lrucache.LRUCache
bucket model.DBBucket
}

// New instantiates a new AcceptanceDataStore
func New(cacheSize int, preallocate bool) model.AcceptanceDataStore {
func New(prefix *prefix.Prefix, cacheSize int, preallocate bool) model.AcceptanceDataStore {
return &acceptanceDataStore{
cache: lrucache.New(cacheSize, preallocate),
cache: lrucache.New(cacheSize, preallocate),
bucket: database.MakeBucket(prefix.Serialize()).Bucket(bucketName),
}
}

Expand Down Expand Up @@ -84,5 +87,5 @@ func (ads *acceptanceDataStore) deserializeAcceptanceData(acceptanceDataBytes []
}

func (ads *acceptanceDataStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
return bucket.Key(hash.ByteSlice())
return ads.bucket.Key(hash.ByteSlice())
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (bhss *blockHeaderStagingShard) commitCount(dbTx model.DBTransaction) error
if err != nil {
return err
}
err = dbTx.Put(countKey, countBytes)
err = dbTx.Put(bhss.store.countKey, countBytes)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/lrucache"
"github.com/kaspanet/kaspad/domain/prefixmanager/prefix"
)

var bucket = database.MakeBucket([]byte("block-headers"))
var countKey = database.MakeBucket(nil).Key([]byte("block-headers-count"))
var bucketName = []byte("block-headers")
var countKeyName = []byte("block-headers-count")

// blockHeaderStore represents a store of blocks
type blockHeaderStore struct {
cache *lrucache.LRUCache
countCached uint64
bucket model.DBBucket
countKey model.DBKey
}

// New instantiates a new BlockHeaderStore
func New(dbContext model.DBReader, cacheSize int, preallocate bool) (model.BlockHeaderStore, error) {
func New(dbContext model.DBReader, prefix *prefix.Prefix, cacheSize int, preallocate bool) (model.BlockHeaderStore, error) {
blockHeaderStore := &blockHeaderStore{
cache: lrucache.New(cacheSize, preallocate),
cache: lrucache.New(cacheSize, preallocate),
bucket: database.MakeBucket(prefix.Serialize()).Bucket(bucketName),
countKey: database.MakeBucket(prefix.Serialize()).Key(countKeyName),
}

err := blockHeaderStore.initializeCount(dbContext)
Expand All @@ -34,12 +39,12 @@ func New(dbContext model.DBReader, cacheSize int, preallocate bool) (model.Block

func (bhs *blockHeaderStore) initializeCount(dbContext model.DBReader) error {
count := uint64(0)
hasCountBytes, err := dbContext.Has(countKey)
hasCountBytes, err := dbContext.Has(bhs.countKey)
if err != nil {
return err
}
if hasCountBytes {
countBytes, err := dbContext.Get(countKey)
countBytes, err := dbContext.Get(bhs.countKey)
if err != nil {
return err
}
Expand Down Expand Up @@ -144,7 +149,7 @@ func (bhs *blockHeaderStore) Delete(stagingArea *model.StagingArea, blockHash *e
}

func (bhs *blockHeaderStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
return bucket.Key(hash.ByteSlice())
return bhs.bucket.Key(hash.ByteSlice())
}

func (bhs *blockHeaderStore) serializeHeader(header externalapi.BlockHeader) ([]byte, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/lrucache"
"github.com/kaspanet/kaspad/domain/prefixmanager/prefix"
)

var bucket = database.MakeBucket([]byte("block-relations"))
var bucketName = []byte("block-relations")

// blockRelationStore represents a store of BlockRelations
type blockRelationStore struct {
cache *lrucache.LRUCache
cache *lrucache.LRUCache
bucket model.DBBucket
}

// New instantiates a new BlockRelationStore
func New(cacheSize int, preallocate bool) model.BlockRelationStore {
func New(prefix *prefix.Prefix, cacheSize int, preallocate bool) model.BlockRelationStore {
return &blockRelationStore{
cache: lrucache.New(cacheSize, preallocate),
cache: lrucache.New(cacheSize, preallocate),
bucket: database.MakeBucket(prefix.Serialize()).Bucket(bucketName),
}
}

Expand Down Expand Up @@ -72,7 +75,7 @@ func (brs *blockRelationStore) Has(dbContext model.DBReader, stagingArea *model.
}

func (brs *blockRelationStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
return bucket.Key(hash.ByteSlice())
return brs.bucket.Key(hash.ByteSlice())
}

func (brs *blockRelationStore) serializeBlockRelations(blockRelations *model.BlockRelations) ([]byte, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/lrucache"
"github.com/kaspanet/kaspad/domain/prefixmanager/prefix"
)

var bucket = database.MakeBucket([]byte("block-statuses"))
var bucketName = []byte("block-statuses")

// blockStatusStore represents a store of BlockStatuses
type blockStatusStore struct {
cache *lrucache.LRUCache
cache *lrucache.LRUCache
bucket model.DBBucket
}

// New instantiates a new BlockStatusStore
func New(cacheSize int, preallocate bool) model.BlockStatusStore {
func New(prefix *prefix.Prefix, cacheSize int, preallocate bool) model.BlockStatusStore {
return &blockStatusStore{
cache: lrucache.New(cacheSize, preallocate),
cache: lrucache.New(cacheSize, preallocate),
bucket: database.MakeBucket(prefix.Serialize()).Bucket(bucketName),
}
}

Expand Down Expand Up @@ -93,5 +96,5 @@ func (bss *blockStatusStore) deserializeBlockStatus(statusBytes []byte) (externa
}

func (bss *blockStatusStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
return bucket.Key(hash.ByteSlice())
return bss.bucket.Key(hash.ByteSlice())
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (bss *blockStagingShard) commitCount(dbTx model.DBTransaction) error {
if err != nil {
return err
}
err = dbTx.Put(countKey, countBytes)
err = dbTx.Put(bss.store.countKey, countBytes)
if err != nil {
return err
}
Expand Down
20 changes: 12 additions & 8 deletions domain/consensus/datastructures/blockstore/block_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/lrucache"
"github.com/kaspanet/kaspad/domain/prefixmanager/prefix"
"github.com/pkg/errors"
)

var bucket = database.MakeBucket([]byte("blocks"))
var countKey = database.MakeBucket(nil).Key([]byte("blocks-count"))
var bucketName = []byte("blocks")

// blockStore represents a store of blocks
type blockStore struct {
cache *lrucache.LRUCache
countCached uint64
bucket model.DBBucket
countKey model.DBKey
}

// New instantiates a new BlockStore
func New(dbContext model.DBReader, cacheSize int, preallocate bool) (model.BlockStore, error) {
func New(dbContext model.DBReader, prefix *prefix.Prefix, cacheSize int, preallocate bool) (model.BlockStore, error) {
blockStore := &blockStore{
cache: lrucache.New(cacheSize, preallocate),
cache: lrucache.New(cacheSize, preallocate),
bucket: database.MakeBucket(prefix.Serialize()).Bucket(bucketName),
countKey: database.MakeBucket(prefix.Serialize()).Key([]byte("blocks-count")),
}

err := blockStore.initializeCount(dbContext)
Expand All @@ -35,12 +39,12 @@ func New(dbContext model.DBReader, cacheSize int, preallocate bool) (model.Block

func (bs *blockStore) initializeCount(dbContext model.DBReader) error {
count := uint64(0)
hasCountBytes, err := dbContext.Has(countKey)
hasCountBytes, err := dbContext.Has(bs.countKey)
if err != nil {
return err
}
if hasCountBytes {
countBytes, err := dbContext.Get(countKey)
countBytes, err := dbContext.Get(bs.countKey)
if err != nil {
return err
}
Expand Down Expand Up @@ -153,7 +157,7 @@ func (bs *blockStore) deserializeBlock(blockBytes []byte) (*externalapi.DomainBl
}

func (bs *blockStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
return bucket.Key(hash.ByteSlice())
return bs.bucket.Key(hash.ByteSlice())
}

func (bs *blockStore) Count(stagingArea *model.StagingArea) uint64 {
Expand Down Expand Up @@ -225,7 +229,7 @@ func (a allBlockHashesIterator) Close() error {
}

func (bs *blockStore) AllBlockHashesIterator(dbContext model.DBReader) (model.BlockIterator, error) {
cursor, err := dbContext.Cursor(bucket)
cursor, err := dbContext.Cursor(bs.bucket)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package consensusstatestore

import (
"github.com/kaspanet/kaspad/domain/consensus/database"
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/utxolrucache"
"github.com/kaspanet/kaspad/domain/prefixmanager/prefix"
)

var importingPruningPointUTXOSetKeyName = []byte("importing-pruning-point-utxo-set")

// consensusStateStore represents a store for the current consensus state
type consensusStateStore struct {
virtualUTXOSetCache *utxolrucache.LRUCache

tipsCache []*externalapi.DomainHash
virtualUTXOSetCache *utxolrucache.LRUCache
tipsCache []*externalapi.DomainHash
tipsKey model.DBKey
utxoSetBucket model.DBBucket
importingPruningPointUTXOSetKey model.DBKey
}

// New instantiates a new ConsensusStateStore
func New(utxoSetCacheSize int, preallocate bool) model.ConsensusStateStore {
func New(prefix *prefix.Prefix, utxoSetCacheSize int, preallocate bool) model.ConsensusStateStore {
return &consensusStateStore{
virtualUTXOSetCache: utxolrucache.New(utxoSetCacheSize, preallocate),
virtualUTXOSetCache: utxolrucache.New(utxoSetCacheSize, preallocate),
tipsKey: database.MakeBucket(prefix.Serialize()).Key(tipsKeyName),
importingPruningPointUTXOSetKey: database.MakeBucket(prefix.Serialize()).Key(importingPruningPointUTXOSetKeyName),
utxoSetBucket: database.MakeBucket(prefix.Serialize()).Bucket(utxoSetBucketName),
}
}

Expand Down
7 changes: 3 additions & 4 deletions domain/consensus/datastructures/consensusstatestore/tips.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package consensusstatestore

import (
"github.com/golang/protobuf/proto"
"github.com/kaspanet/kaspad/domain/consensus/database"
"github.com/kaspanet/kaspad/domain/consensus/database/serialization"
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
)

var tipsKey = database.MakeBucket(nil).Key([]byte("tips"))
var tipsKeyName = []byte("tips")

func (css *consensusStateStore) Tips(stagingArea *model.StagingArea, dbContext model.DBReader) ([]*externalapi.DomainHash, error) {
stagingShard := css.stagingShard(stagingArea)
Expand All @@ -21,7 +20,7 @@ func (css *consensusStateStore) Tips(stagingArea *model.StagingArea, dbContext m
return externalapi.CloneHashes(css.tipsCache), nil
}

tipsBytes, err := dbContext.Get(tipsKey)
tipsBytes, err := dbContext.Get(css.tipsKey)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -66,7 +65,7 @@ func (csss *consensusStateStagingShard) commitTips(dbTx model.DBTransaction) err
if err != nil {
return err
}
err = dbTx.Put(tipsKey, tipsBytes)
err = dbTx.Put(csss.store.tipsKey, tipsBytes)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 4207c82

Please sign in to comment.