-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghostadag_data_staging_shard.go
52 lines (44 loc) · 1.34 KB
/
ghostadag_data_staging_shard.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
package ghostdagdatastore
import (
"github.com/Nirvana-Chain/nirvanad/domain/consensus/model"
"github.com/Nirvana-Chain/nirvanad/domain/consensus/model/externalapi"
)
type key struct {
hash externalapi.DomainHash
isTrustedData bool
}
func newKey(hash *externalapi.DomainHash, isTrustedData bool) key {
return key{
hash: *hash,
isTrustedData: isTrustedData,
}
}
type ghostdagDataStagingShard struct {
store *ghostdagDataStore
toAdd map[key]*externalapi.BlockGHOSTDAGData
}
func (gds *ghostdagDataStore) stagingShard(stagingArea *model.StagingArea) *ghostdagDataStagingShard {
return stagingArea.GetOrCreateShard(gds.shardID, func() model.StagingShard {
return &ghostdagDataStagingShard{
store: gds,
toAdd: make(map[key]*externalapi.BlockGHOSTDAGData),
}
}).(*ghostdagDataStagingShard)
}
func (gdss *ghostdagDataStagingShard) Commit(dbTx model.DBTransaction) error {
for key, blockGHOSTDAGData := range gdss.toAdd {
blockGhostdagDataBytes, err := gdss.store.serializeBlockGHOSTDAGData(blockGHOSTDAGData)
if err != nil {
return err
}
err = dbTx.Put(gdss.store.serializeKey(key), blockGhostdagDataBytes)
if err != nil {
return err
}
gdss.store.cache.Add(&key.hash, key.isTrustedData, blockGHOSTDAGData)
}
return nil
}
func (gdss *ghostdagDataStagingShard) isStaged() bool {
return len(gdss.toAdd) != 0
}