-
Notifications
You must be signed in to change notification settings - Fork 2
/
utxo_diff_staging_shard.go
74 lines (64 loc) · 2.14 KB
/
utxo_diff_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package utxodiffstore
import (
"github.com/bugnanetwork/bugnad/domain/consensus/model"
"github.com/bugnanetwork/bugnad/domain/consensus/model/externalapi"
)
type utxoDiffStagingShard struct {
store *utxoDiffStore
utxoDiffToAdd map[externalapi.DomainHash]externalapi.UTXODiff
utxoDiffChildToAdd map[externalapi.DomainHash]*externalapi.DomainHash
toDelete map[externalapi.DomainHash]struct{}
}
func (uds *utxoDiffStore) stagingShard(stagingArea *model.StagingArea) *utxoDiffStagingShard {
return stagingArea.GetOrCreateShard(uds.shardID, func() model.StagingShard {
return &utxoDiffStagingShard{
store: uds,
utxoDiffToAdd: make(map[externalapi.DomainHash]externalapi.UTXODiff),
utxoDiffChildToAdd: make(map[externalapi.DomainHash]*externalapi.DomainHash),
toDelete: make(map[externalapi.DomainHash]struct{}),
}
}).(*utxoDiffStagingShard)
}
func (udss *utxoDiffStagingShard) Commit(dbTx model.DBTransaction) error {
for hash, utxoDiff := range udss.utxoDiffToAdd {
utxoDiffBytes, err := udss.store.serializeUTXODiff(utxoDiff)
if err != nil {
return err
}
err = dbTx.Put(udss.store.utxoDiffHashAsKey(&hash), utxoDiffBytes)
if err != nil {
return err
}
udss.store.utxoDiffCache.Add(&hash, utxoDiff)
}
for hash, utxoDiffChild := range udss.utxoDiffChildToAdd {
if utxoDiffChild == nil {
continue
}
utxoDiffChildBytes, err := udss.store.serializeUTXODiffChild(utxoDiffChild)
if err != nil {
return err
}
err = dbTx.Put(udss.store.utxoDiffChildHashAsKey(&hash), utxoDiffChildBytes)
if err != nil {
return err
}
udss.store.utxoDiffChildCache.Add(&hash, utxoDiffChild)
}
for hash := range udss.toDelete {
err := dbTx.Delete(udss.store.utxoDiffHashAsKey(&hash))
if err != nil {
return err
}
udss.store.utxoDiffCache.Remove(&hash)
err = dbTx.Delete(udss.store.utxoDiffChildHashAsKey(&hash))
if err != nil {
return err
}
udss.store.utxoDiffChildCache.Remove(&hash)
}
return nil
}
func (udss *utxoDiffStagingShard) isStaged() bool {
return len(udss.utxoDiffToAdd) != 0 || len(udss.utxoDiffChildToAdd) != 0 || len(udss.toDelete) != 0
}