forked from kaspanet/kaspad
-
Notifications
You must be signed in to change notification settings - Fork 3
/
block_relation_store.go
99 lines (79 loc) · 3.26 KB
/
block_relation_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package blockrelationstore
import (
"github.com/golang/protobuf/proto"
"github.com/ixbaseANT/gord/domain/consensus/database/serialization"
"github.com/ixbaseANT/gord/domain/consensus/model"
"github.com/ixbaseANT/gord/domain/consensus/model/externalapi"
"github.com/ixbaseANT/gord/domain/consensus/utils/lrucache"
"github.com/ixbaseANT/gord/util/staging"
)
var bucketName = []byte("block-relations")
// blockRelationStore represents a store of BlockRelations
type blockRelationStore struct {
shardID model.StagingShardID
cache *lrucache.LRUCache
bucket model.DBBucket
}
// New instantiates a new BlockRelationStore
func New(prefixBucket model.DBBucket, cacheSize int, preallocate bool) model.BlockRelationStore {
return &blockRelationStore{
shardID: staging.GenerateShardingID(),
cache: lrucache.New(cacheSize, preallocate),
bucket: prefixBucket.Bucket(bucketName),
}
}
func (brs *blockRelationStore) StageBlockRelation(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash, blockRelations *model.BlockRelations) {
stagingShard := brs.stagingShard(stagingArea)
stagingShard.toAdd[*blockHash] = blockRelations.Clone()
}
func (brs *blockRelationStore) IsStaged(stagingArea *model.StagingArea) bool {
return brs.stagingShard(stagingArea).isStaged()
}
func (brs *blockRelationStore) BlockRelation(dbContext model.DBReader, stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (*model.BlockRelations, error) {
stagingShard := brs.stagingShard(stagingArea)
if blockRelations, ok := stagingShard.toAdd[*blockHash]; ok {
return blockRelations.Clone(), nil
}
if blockRelations, ok := brs.cache.Get(blockHash); ok {
return blockRelations.(*model.BlockRelations).Clone(), nil
}
blockRelationsBytes, err := dbContext.Get(brs.hashAsKey(blockHash))
if err != nil {
return nil, err
}
blockRelations, err := brs.deserializeBlockRelations(blockRelationsBytes)
if err != nil {
return nil, err
}
brs.cache.Add(blockHash, blockRelations)
return blockRelations.Clone(), nil
}
func (brs *blockRelationStore) Has(dbContext model.DBReader, stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (bool, error) {
stagingShard := brs.stagingShard(stagingArea)
if _, ok := stagingShard.toAdd[*blockHash]; ok {
return true, nil
}
if brs.cache.Has(blockHash) {
return true, nil
}
return dbContext.Has(brs.hashAsKey(blockHash))
}
func (brs *blockRelationStore) UnstageAll(stagingArea *model.StagingArea) {
stagingShard := brs.stagingShard(stagingArea)
stagingShard.toAdd = make(map[externalapi.DomainHash]*model.BlockRelations)
}
func (brs *blockRelationStore) hashAsKey(hash *externalapi.DomainHash) model.DBKey {
return brs.bucket.Key(hash.ByteSlice())
}
func (brs *blockRelationStore) serializeBlockRelations(blockRelations *model.BlockRelations) ([]byte, error) {
dbBlockRelations := serialization.DomainBlockRelationsToDbBlockRelations(blockRelations)
return proto.Marshal(dbBlockRelations)
}
func (brs *blockRelationStore) deserializeBlockRelations(blockRelationsBytes []byte) (*model.BlockRelations, error) {
dbBlockRelations := &serialization.DbBlockRelations{}
err := proto.Unmarshal(blockRelationsBytes, dbBlockRelations)
if err != nil {
return nil, err
}
return serialization.DbBlockRelationsToDomainBlockRelations(dbBlockRelations)
}