-
Notifications
You must be signed in to change notification settings - Fork 202
/
processedMiniBlocks.go
146 lines (117 loc) · 4.96 KB
/
processedMiniBlocks.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
package processedMb
import (
"sync"
"github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/process/block/bootstrapStorage"
)
var log = logger.GetOrCreate("process/processedMb")
// MiniBlockHashes will keep a list of miniblock hashes as keys in a map for easy access
type MiniBlockHashes map[string]struct{}
// ProcessedMiniBlockTracker is used to store all processed mini blocks hashes grouped by a metahash
type ProcessedMiniBlockTracker struct {
processedMiniBlocks map[string]MiniBlockHashes
mutProcessedMiniBlocks sync.RWMutex
}
// NewProcessedMiniBlocks will create a complex type of processedMb
func NewProcessedMiniBlocks() *ProcessedMiniBlockTracker {
return &ProcessedMiniBlockTracker{
processedMiniBlocks: make(map[string]MiniBlockHashes),
}
}
// AddMiniBlockHash will add a miniblock hash
func (pmb *ProcessedMiniBlockTracker) AddMiniBlockHash(metaBlockHash string, miniBlockHash string) {
pmb.mutProcessedMiniBlocks.Lock()
defer pmb.mutProcessedMiniBlocks.Unlock()
miniBlocksProcessed, ok := pmb.processedMiniBlocks[metaBlockHash]
if !ok {
miniBlocksProcessed = make(MiniBlockHashes)
miniBlocksProcessed[miniBlockHash] = struct{}{}
pmb.processedMiniBlocks[metaBlockHash] = miniBlocksProcessed
return
}
miniBlocksProcessed[miniBlockHash] = struct{}{}
}
// RemoveMetaBlockHash will remove a meta block hash
func (pmb *ProcessedMiniBlockTracker) RemoveMetaBlockHash(metaBlockHash string) {
pmb.mutProcessedMiniBlocks.Lock()
delete(pmb.processedMiniBlocks, metaBlockHash)
pmb.mutProcessedMiniBlocks.Unlock()
}
// RemoveMiniBlockHash will remove a mini block hash
func (pmb *ProcessedMiniBlockTracker) RemoveMiniBlockHash(miniBlockHash string) {
pmb.mutProcessedMiniBlocks.Lock()
for metaHash, miniBlocksProcessed := range pmb.processedMiniBlocks {
delete(miniBlocksProcessed, miniBlockHash)
if len(miniBlocksProcessed) == 0 {
delete(pmb.processedMiniBlocks, metaHash)
}
}
pmb.mutProcessedMiniBlocks.Unlock()
}
// GetProcessedMiniBlocksHashes will return all processed miniblocks for a metablock
func (pmb *ProcessedMiniBlockTracker) GetProcessedMiniBlocksHashes(metaBlockHash string) map[string]struct{} {
pmb.mutProcessedMiniBlocks.RLock()
processedMiniBlocksHashes := make(map[string]struct{})
for hash, value := range pmb.processedMiniBlocks[metaBlockHash] {
processedMiniBlocksHashes[hash] = value
}
pmb.mutProcessedMiniBlocks.RUnlock()
return processedMiniBlocksHashes
}
// IsMiniBlockProcessed will return true if a mini block is processed
func (pmb *ProcessedMiniBlockTracker) IsMiniBlockProcessed(metaBlockHash string, miniBlockHash string) bool {
pmb.mutProcessedMiniBlocks.RLock()
defer pmb.mutProcessedMiniBlocks.RUnlock()
miniBlocksProcessed, ok := pmb.processedMiniBlocks[metaBlockHash]
if !ok {
return false
}
_, isProcessed := miniBlocksProcessed[miniBlockHash]
return isProcessed
}
// ConvertProcessedMiniBlocksMapToSlice will convert a map[string]map[string]struct{} in a slice of MiniBlocksInMeta
func (pmb *ProcessedMiniBlockTracker) ConvertProcessedMiniBlocksMapToSlice() []bootstrapStorage.MiniBlocksInMeta {
pmb.mutProcessedMiniBlocks.RLock()
defer pmb.mutProcessedMiniBlocks.RUnlock()
if len(pmb.processedMiniBlocks) == 0 {
return nil
}
miniBlocksInMetaBlocks := make([]bootstrapStorage.MiniBlocksInMeta, 0, len(pmb.processedMiniBlocks))
for metaHash, miniBlocksHashes := range pmb.processedMiniBlocks {
miniBlocksInMeta := bootstrapStorage.MiniBlocksInMeta{
MetaHash: []byte(metaHash),
MiniBlocksHashes: make([][]byte, 0, len(miniBlocksHashes)),
}
for miniBlockHash := range miniBlocksHashes {
miniBlocksInMeta.MiniBlocksHashes = append(miniBlocksInMeta.MiniBlocksHashes, []byte(miniBlockHash))
}
miniBlocksInMetaBlocks = append(miniBlocksInMetaBlocks, miniBlocksInMeta)
}
return miniBlocksInMetaBlocks
}
// ConvertSliceToProcessedMiniBlocksMap will convert a slice of MiniBlocksInMeta in an map[string]MiniBlockHashes
func (pmb *ProcessedMiniBlockTracker) ConvertSliceToProcessedMiniBlocksMap(miniBlocksInMetaBlocks []bootstrapStorage.MiniBlocksInMeta) {
pmb.mutProcessedMiniBlocks.Lock()
defer pmb.mutProcessedMiniBlocks.Unlock()
for _, miniBlocksInMeta := range miniBlocksInMetaBlocks {
miniBlocksHashes := make(MiniBlockHashes)
for _, miniBlockHash := range miniBlocksInMeta.MiniBlocksHashes {
miniBlocksHashes[string(miniBlockHash)] = struct{}{}
}
pmb.processedMiniBlocks[string(miniBlocksInMeta.MetaHash)] = miniBlocksHashes
}
}
// DisplayProcessedMiniBlocks will display all miniblocks hashes and meta block hash from the map
func (pmb *ProcessedMiniBlockTracker) DisplayProcessedMiniBlocks() {
log.Debug("processed mini blocks applied")
pmb.mutProcessedMiniBlocks.RLock()
for metaBlockHash, miniBlocksHashes := range pmb.processedMiniBlocks {
log.Debug("processed",
"meta hash", []byte(metaBlockHash))
for miniBlockHash := range miniBlocksHashes {
log.Debug("processed",
"mini block hash", []byte(miniBlockHash))
}
}
pmb.mutProcessedMiniBlocks.RUnlock()
}