-
Notifications
You must be signed in to change notification settings - Fork 232
/
utxo_diff_store.go
190 lines (154 loc) · 6.05 KB
/
utxo_diff_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package utxodiffstore
import (
"github.com/golang/protobuf/proto"
"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/lrucache"
"github.com/kaspanet/kaspad/util/staging"
"github.com/pkg/errors"
)
var utxoDiffBucketName = []byte("utxo-diffs")
var utxoDiffChildBucketName = []byte("utxo-diff-children")
// utxoDiffStore represents a store of UTXODiffs
type utxoDiffStore struct {
shardID model.StagingShardID
utxoDiffCache *lrucache.LRUCache
utxoDiffChildCache *lrucache.LRUCache
utxoDiffBucket model.DBBucket
utxoDiffChildBucket model.DBBucket
}
// New instantiates a new UTXODiffStore
func New(prefixBucket model.DBBucket, cacheSize int, preallocate bool) model.UTXODiffStore {
return &utxoDiffStore{
shardID: staging.GenerateShardingID(),
utxoDiffCache: lrucache.New(cacheSize, preallocate),
utxoDiffChildCache: lrucache.New(cacheSize, preallocate),
utxoDiffBucket: prefixBucket.Bucket(utxoDiffBucketName),
utxoDiffChildBucket: prefixBucket.Bucket(utxoDiffChildBucketName),
}
}
// Stage stages the given utxoDiff for the given blockHash
func (uds *utxoDiffStore) Stage(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash,
utxoDiff externalapi.UTXODiff, utxoDiffChild *externalapi.DomainHash) {
stagingShard := uds.stagingShard(stagingArea)
stagingShard.utxoDiffToAdd[*blockHash] = utxoDiff
if utxoDiffChild != nil {
stagingShard.utxoDiffChildToAdd[*blockHash] = utxoDiffChild
}
}
func (uds *utxoDiffStore) IsStaged(stagingArea *model.StagingArea) bool {
return uds.stagingShard(stagingArea).isStaged()
}
func (uds *utxoDiffStore) isBlockHashStaged(stagingShard *utxoDiffStagingShard, blockHash *externalapi.DomainHash) bool {
if _, ok := stagingShard.utxoDiffToAdd[*blockHash]; ok {
return true
}
_, ok := stagingShard.utxoDiffChildToAdd[*blockHash]
return ok
}
// UTXODiff gets the utxoDiff associated with the given blockHash
func (uds *utxoDiffStore) UTXODiff(dbContext model.DBReader, stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (externalapi.UTXODiff, error) {
stagingShard := uds.stagingShard(stagingArea)
if utxoDiff, ok := stagingShard.utxoDiffToAdd[*blockHash]; ok {
return utxoDiff, nil
}
if utxoDiff, ok := uds.utxoDiffCache.Get(blockHash); ok {
return utxoDiff.(externalapi.UTXODiff), nil
}
utxoDiffBytes, err := dbContext.Get(uds.utxoDiffHashAsKey(blockHash))
if err != nil {
return nil, err
}
utxoDiff, err := uds.deserializeUTXODiff(utxoDiffBytes)
if err != nil {
return nil, err
}
uds.utxoDiffCache.Add(blockHash, utxoDiff)
return utxoDiff, nil
}
// UTXODiffChild gets the utxoDiff child associated with the given blockHash
func (uds *utxoDiffStore) UTXODiffChild(dbContext model.DBReader, stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (*externalapi.DomainHash, error) {
stagingShard := uds.stagingShard(stagingArea)
if utxoDiffChild, ok := stagingShard.utxoDiffChildToAdd[*blockHash]; ok {
return utxoDiffChild, nil
}
if utxoDiffChild, ok := uds.utxoDiffChildCache.Get(blockHash); ok {
return utxoDiffChild.(*externalapi.DomainHash), nil
}
utxoDiffChildBytes, err := dbContext.Get(uds.utxoDiffChildHashAsKey(blockHash))
if err != nil {
return nil, err
}
utxoDiffChild, err := uds.deserializeUTXODiffChild(utxoDiffChildBytes)
if err != nil {
return nil, err
}
uds.utxoDiffChildCache.Add(blockHash, utxoDiffChild)
return utxoDiffChild, nil
}
// HasUTXODiffChild returns true if the given blockHash has a UTXODiffChild
func (uds *utxoDiffStore) HasUTXODiffChild(dbContext model.DBReader, stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (bool, error) {
stagingShard := uds.stagingShard(stagingArea)
if _, ok := stagingShard.utxoDiffChildToAdd[*blockHash]; ok {
return true, nil
}
if uds.utxoDiffChildCache.Has(blockHash) {
return true, nil
}
return dbContext.Has(uds.utxoDiffChildHashAsKey(blockHash))
}
// Delete deletes the utxoDiff associated with the given blockHash
func (uds *utxoDiffStore) Delete(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) {
stagingShard := uds.stagingShard(stagingArea)
if uds.isBlockHashStaged(stagingShard, blockHash) {
if _, ok := stagingShard.utxoDiffToAdd[*blockHash]; ok {
delete(stagingShard.utxoDiffToAdd, *blockHash)
}
if _, ok := stagingShard.utxoDiffChildToAdd[*blockHash]; ok {
delete(stagingShard.utxoDiffChildToAdd, *blockHash)
}
return
}
stagingShard.toDelete[*blockHash] = struct{}{}
}
func (uds *utxoDiffStore) utxoDiffHashAsKey(hash *externalapi.DomainHash) model.DBKey {
return uds.utxoDiffBucket.Key(hash.ByteSlice())
}
func (uds *utxoDiffStore) utxoDiffChildHashAsKey(hash *externalapi.DomainHash) model.DBKey {
return uds.utxoDiffChildBucket.Key(hash.ByteSlice())
}
func (uds *utxoDiffStore) serializeUTXODiff(utxoDiff externalapi.UTXODiff) ([]byte, error) {
dbUtxoDiff, err := serialization.UTXODiffToDBUTXODiff(utxoDiff)
if err != nil {
return nil, err
}
bytes, err := proto.Marshal(dbUtxoDiff)
if err != nil {
return nil, errors.WithStack(err)
}
return bytes, nil
}
func (uds *utxoDiffStore) deserializeUTXODiff(utxoDiffBytes []byte) (externalapi.UTXODiff, error) {
dbUTXODiff := &serialization.DbUtxoDiff{}
err := proto.Unmarshal(utxoDiffBytes, dbUTXODiff)
if err != nil {
return nil, errors.WithStack(err)
}
return serialization.DBUTXODiffToUTXODiff(dbUTXODiff)
}
func (uds *utxoDiffStore) serializeUTXODiffChild(utxoDiffChild *externalapi.DomainHash) ([]byte, error) {
bytes, err := proto.Marshal(serialization.DomainHashToDbHash(utxoDiffChild))
if err != nil {
return nil, errors.WithStack(err)
}
return bytes, nil
}
func (uds *utxoDiffStore) deserializeUTXODiffChild(utxoDiffChildBytes []byte) (*externalapi.DomainHash, error) {
dbHash := &serialization.DbHash{}
err := proto.Unmarshal(utxoDiffChildBytes, dbHash)
if err != nil {
return nil, errors.WithStack(err)
}
return serialization.DbHashToDomainHash(dbHash)
}