/
tips.go
75 lines (59 loc) · 1.89 KB
/
tips.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
package consensusstatestore
import (
"github.com/golang/protobuf/proto"
"github.com/BTCGhostdag/BTCD/domain/consensus/database/serialization"
"github.com/BTCGhostdag/BTCD/domain/consensus/model"
"github.com/BTCGhostdag/BTCD/domain/consensus/model/externalapi"
)
var tipsKeyName = []byte("tips")
func (css *consensusStateStore) Tips(stagingArea *model.StagingArea, dbContext model.DBReader) ([]*externalapi.DomainHash, error) {
stagingShard := css.stagingShard(stagingArea)
if stagingShard.tipsStaging != nil {
return externalapi.CloneHashes(stagingShard.tipsStaging), nil
}
if css.tipsCache != nil {
return externalapi.CloneHashes(css.tipsCache), nil
}
tipsBytes, err := dbContext.Get(css.tipsKey)
if err != nil {
return nil, err
}
tips, err := css.deserializeTips(tipsBytes)
if err != nil {
return nil, err
}
css.tipsCache = tips
return externalapi.CloneHashes(tips), nil
}
func (css *consensusStateStore) StageTips(stagingArea *model.StagingArea, tipHashes []*externalapi.DomainHash) {
stagingShard := css.stagingShard(stagingArea)
stagingShard.tipsStaging = externalapi.CloneHashes(tipHashes)
}
func (css *consensusStateStore) serializeTips(tips []*externalapi.DomainHash) ([]byte, error) {
dbTips := serialization.TipsToDBTips(tips)
return proto.Marshal(dbTips)
}
func (css *consensusStateStore) deserializeTips(tipsBytes []byte) ([]*externalapi.DomainHash,
error) {
dbTips := &serialization.DbTips{}
err := proto.Unmarshal(tipsBytes, dbTips)
if err != nil {
return nil, err
}
return serialization.DBTipsToTips(dbTips)
}
func (csss *consensusStateStagingShard) commitTips(dbTx model.DBTransaction) error {
if csss.tipsStaging == nil {
return nil
}
tipsBytes, err := csss.store.serializeTips(csss.tipsStaging)
if err != nil {
return err
}
err = dbTx.Put(csss.store.tipsKey, tipsBytes)
if err != nil {
return err
}
csss.store.tipsCache = csss.tipsStaging
return nil
}