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

* [NOD-1492] Implement some data stores * [NOD-1492] Remove pointers to acceptance data * [NOD-1492] Fix receiver names * [NOD-1492] Implement delete for acceptanceDataStore * [NOD-1492] In blockRelationStore rename IsAnythingStaged to IsStaged * [NOD-1492] Rename bucket name
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package blockrelationstore
|
|
|
|
import (
|
|
"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-relations"))
|
|
|
|
// blockRelationStore represents a store of BlockRelations
|
|
type blockRelationStore struct {
|
|
staging map[externalapi.DomainHash]*model.BlockRelations
|
|
}
|
|
|
|
// New instantiates a new BlockRelationStore
|
|
func New() model.BlockRelationStore {
|
|
return &blockRelationStore{
|
|
staging: make(map[externalapi.DomainHash]*model.BlockRelations),
|
|
}
|
|
}
|
|
|
|
func (brs *blockRelationStore) StageBlockRelation(blockHash *externalapi.DomainHash, blockRelations *model.BlockRelations) {
|
|
brs.staging[*blockHash] = blockRelations
|
|
}
|
|
|
|
func (brs *blockRelationStore) IsStaged() bool {
|
|
return len(brs.staging) != 0
|
|
}
|
|
|
|
func (brs *blockRelationStore) Discard() {
|
|
brs.staging = make(map[externalapi.DomainHash]*model.BlockRelations)
|
|
}
|
|
|
|
func (brs *blockRelationStore) Commit(dbTx model.DBTransaction) error {
|
|
for hash, blockRelations := range brs.staging {
|
|
err := dbTx.Put(brs.hashAsKey(&hash), brs.serializeBlockRelations(blockRelations))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
brs.Discard()
|
|
return nil
|
|
}
|
|
|
|
func (brs *blockRelationStore) BlockRelation(dbContext model.DBReader, blockHash *externalapi.DomainHash) (*model.BlockRelations, error) {
|
|
if blockRelations, ok := brs.staging[*blockHash]; ok {
|
|
return blockRelations, nil
|
|
}
|
|
|
|
blockRelationsBytes, err := dbContext.Get(brs.hashAsKey(blockHash))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return brs.deserializeBlockRelations(blockRelationsBytes)
|
|
}
|
|
|
|
func (brs *blockRelationStore) serializeBlockRelations(blockRelations *model.BlockRelations) []byte {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (brs *blockRelationStore) deserializeBlockRelations(blockRelationsBytes []byte) (*model.BlockRelations, error) {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (brs *blockRelationStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
|
|
return bucket.Key(hash[:])
|
|
}
|