-
Notifications
You must be signed in to change notification settings - Fork 1
/
reachability_data_store.go
executable file
·176 lines (142 loc) · 6.14 KB
/
reachability_data_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package reachabilitydatastore
import (
"github.com/shatll-s/nexelliad/domain/consensus/database/serialization"
"github.com/shatll-s/nexelliad/domain/consensus/model"
"github.com/shatll-s/nexelliad/domain/consensus/model/externalapi"
"github.com/shatll-s/nexelliad/domain/consensus/utils/lrucache"
"github.com/shatll-s/nexelliad/infrastructure/db/database"
"github.com/shatll-s/nexelliad/util/staging"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)
var reachabilityDataBucketName = []byte("reachability-data")
var reachabilityReindexRootKeyName = []byte("reachability-reindex-root")
// reachabilityDataStore represents a store of ReachabilityData
type reachabilityDataStore struct {
shardID model.StagingShardID
reachabilityDataCache *lrucache.LRUCache
reachabilityReindexRootCache *externalapi.DomainHash
reachabilityDataBucket model.DBBucket
reachabilityReindexRootKey model.DBKey
}
// New instantiates a new ReachabilityDataStore
func New(prefixBucket model.DBBucket, cacheSize int, preallocate bool) model.ReachabilityDataStore {
return &reachabilityDataStore{
shardID: staging.GenerateShardingID(),
reachabilityDataCache: lrucache.New(cacheSize, preallocate),
reachabilityDataBucket: prefixBucket.Bucket(reachabilityDataBucketName),
reachabilityReindexRootKey: prefixBucket.Key(reachabilityReindexRootKeyName),
}
}
// StageReachabilityData stages the given reachabilityData for the given blockHash
func (rds *reachabilityDataStore) StageReachabilityData(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash, reachabilityData model.ReachabilityData) {
stagingShard := rds.stagingShard(stagingArea)
stagingShard.reachabilityData[*blockHash] = reachabilityData
}
func (rds *reachabilityDataStore) Delete(dbContext model.DBWriter) error {
cursor, err := dbContext.Cursor(rds.reachabilityDataBucket)
if err != nil {
return err
}
for ok := cursor.First(); ok; ok = cursor.Next() {
key, err := cursor.Key()
if err != nil {
return err
}
err = dbContext.Delete(key)
if err != nil {
return err
}
}
return dbContext.Delete(rds.reachabilityReindexRootKey)
}
// StageReachabilityReindexRoot stages the given reachabilityReindexRoot
func (rds *reachabilityDataStore) StageReachabilityReindexRoot(stagingArea *model.StagingArea, reachabilityReindexRoot *externalapi.DomainHash) {
stagingShard := rds.stagingShard(stagingArea)
stagingShard.reachabilityReindexRoot = reachabilityReindexRoot
}
func (rds *reachabilityDataStore) IsStaged(stagingArea *model.StagingArea) bool {
return rds.stagingShard(stagingArea).isStaged()
}
var errNotFound = errors.Wrap(database.ErrNotFound, "reachability data not found")
// ReachabilityData returns the reachabilityData associated with the given blockHash
func (rds *reachabilityDataStore) ReachabilityData(dbContext model.DBReader, stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (model.ReachabilityData, error) {
stagingShard := rds.stagingShard(stagingArea)
if reachabilityData, ok := stagingShard.reachabilityData[*blockHash]; ok {
return reachabilityData, nil
}
if reachabilityData, ok := rds.reachabilityDataCache.Get(blockHash); ok {
if reachabilityData == nil {
return nil, errNotFound
}
return reachabilityData.(model.ReachabilityData), nil
}
reachabilityDataBytes, err := dbContext.Get(rds.reachabilityDataBlockHashAsKey(blockHash))
if database.IsNotFoundError(err) {
rds.reachabilityDataCache.Add(blockHash, nil)
}
if err != nil {
return nil, err
}
reachabilityData, err := rds.deserializeReachabilityData(reachabilityDataBytes)
if err != nil {
return nil, err
}
rds.reachabilityDataCache.Add(blockHash, reachabilityData)
return reachabilityData, nil
}
func (rds *reachabilityDataStore) HasReachabilityData(dbContext model.DBReader, stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (bool, error) {
_, err := rds.ReachabilityData(dbContext, stagingArea, blockHash)
if database.IsNotFoundError(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// ReachabilityReindexRoot returns the current reachability reindex root
func (rds *reachabilityDataStore) ReachabilityReindexRoot(dbContext model.DBReader, stagingArea *model.StagingArea) (*externalapi.DomainHash, error) {
stagingShard := rds.stagingShard(stagingArea)
if stagingShard.reachabilityReindexRoot != nil {
return stagingShard.reachabilityReindexRoot, nil
}
if rds.reachabilityReindexRootCache != nil {
return rds.reachabilityReindexRootCache, nil
}
reachabilityReindexRootBytes, err := dbContext.Get(rds.reachabilityReindexRootKey)
if err != nil {
return nil, err
}
reachabilityReindexRoot, err := rds.deserializeReachabilityReindexRoot(reachabilityReindexRootBytes)
if err != nil {
return nil, err
}
rds.reachabilityReindexRootCache = reachabilityReindexRoot
return reachabilityReindexRoot, nil
}
func (rds *reachabilityDataStore) reachabilityDataBlockHashAsKey(hash *externalapi.DomainHash) model.DBKey {
return rds.reachabilityDataBucket.Key(hash.ByteSlice())
}
func (rds *reachabilityDataStore) serializeReachabilityData(reachabilityData model.ReachabilityData) ([]byte, error) {
return proto.Marshal(serialization.ReachablityDataToDBReachablityData(reachabilityData))
}
func (rds *reachabilityDataStore) deserializeReachabilityData(reachabilityDataBytes []byte) (model.ReachabilityData, error) {
dbReachabilityData := &serialization.DbReachabilityData{}
err := proto.Unmarshal(reachabilityDataBytes, dbReachabilityData)
if err != nil {
return nil, err
}
return serialization.DBReachablityDataToReachablityData(dbReachabilityData)
}
func (rds *reachabilityDataStore) serializeReachabilityReindexRoot(reachabilityReindexRoot *externalapi.DomainHash) ([]byte, error) {
return proto.Marshal(serialization.DomainHashToDbHash(reachabilityReindexRoot))
}
func (rds *reachabilityDataStore) deserializeReachabilityReindexRoot(reachabilityReindexRootBytes []byte) (*externalapi.DomainHash, error) {
dbHash := &serialization.DbHash{}
err := proto.Unmarshal(reachabilityReindexRootBytes, dbHash)
if err != nil {
return nil, err
}
return serialization.DbHashToDomainHash(dbHash)
}