-
Notifications
You must be signed in to change notification settings - Fork 202
/
peerMiniBlocksSyncer.go
379 lines (301 loc) · 11.1 KB
/
peerMiniBlocksSyncer.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package shardchain
import (
"sync"
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go-core/data/block"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/epochStart"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/state"
"github.com/ElrondNetwork/elrond-go/storage"
)
var _ process.ValidatorInfoSyncer = (*peerMiniBlockSyncer)(nil)
// waitTime defines the time in seconds to wait after a request has been done
const waitTime = 5 * time.Second
// ArgPeerMiniBlockSyncer holds all dependencies required to create a peerMiniBlockSyncer
type ArgPeerMiniBlockSyncer struct {
MiniBlocksPool storage.Cacher
ValidatorsInfoPool dataRetriever.ShardedDataCacherNotifier
RequestHandler epochStart.RequestHandler
}
// peerMiniBlockSyncer implements validator info processing for mini blocks of type PeerMiniBlock
type peerMiniBlockSyncer struct {
miniBlocksPool storage.Cacher
validatorsInfoPool dataRetriever.ShardedDataCacherNotifier
requestHandler epochStart.RequestHandler
mapAllPeerMiniBlocks map[string]*block.MiniBlock
mapAllValidatorsInfo map[string]*state.ShardValidatorInfo
chRcvAllMiniBlocks chan struct{}
chRcvAllValidatorsInfo chan struct{}
mutMiniBlocksForBlock sync.RWMutex
mutValidatorsInfoForBlock sync.RWMutex
numMissingPeerMiniBlocks uint32
numMissingValidatorsInfo uint32
}
// NewPeerMiniBlockSyncer creates a new peerMiniBlockSyncer object
func NewPeerMiniBlockSyncer(arguments ArgPeerMiniBlockSyncer) (*peerMiniBlockSyncer, error) {
if check.IfNil(arguments.MiniBlocksPool) {
return nil, epochStart.ErrNilMiniBlockPool
}
if check.IfNil(arguments.ValidatorsInfoPool) {
return nil, epochStart.ErrNilValidatorsInfoPool
}
if check.IfNil(arguments.RequestHandler) {
return nil, epochStart.ErrNilRequestHandler
}
p := &peerMiniBlockSyncer{
miniBlocksPool: arguments.MiniBlocksPool,
validatorsInfoPool: arguments.ValidatorsInfoPool,
requestHandler: arguments.RequestHandler,
}
//TODO: change the registerHandler for the miniblockPool to call
//directly with hash and value - like func (sp *shardProcessor) receivedMetaBlock
p.miniBlocksPool.RegisterHandler(p.receivedMiniBlock, core.UniqueIdentifier())
p.validatorsInfoPool.RegisterOnAdded(p.receivedValidatorInfo)
return p, nil
}
func (p *peerMiniBlockSyncer) initMiniBlocks() {
p.mutMiniBlocksForBlock.Lock()
p.mapAllPeerMiniBlocks = make(map[string]*block.MiniBlock)
p.chRcvAllMiniBlocks = make(chan struct{})
p.mutMiniBlocksForBlock.Unlock()
}
func (p *peerMiniBlockSyncer) initValidatorsInfo() {
p.mutValidatorsInfoForBlock.Lock()
p.mapAllValidatorsInfo = make(map[string]*state.ShardValidatorInfo)
p.chRcvAllValidatorsInfo = make(chan struct{})
p.mutValidatorsInfoForBlock.Unlock()
}
// SyncMiniBlocks synchronizes peers mini blocks from an epoch start meta block
func (p *peerMiniBlockSyncer) SyncMiniBlocks(headerHandler data.HeaderHandler) ([][]byte, data.BodyHandler, error) {
if check.IfNil(headerHandler) {
return nil, nil, epochStart.ErrNilMetaBlock
}
p.initMiniBlocks()
p.computeMissingPeerBlocks(headerHandler)
allMissingPeerMiniBlocksHashes, err := p.retrieveMissingMiniBlocks()
if err != nil {
return allMissingPeerMiniBlocksHashes, nil, err
}
peerBlockBody := p.getAllPeerMiniBlocks(headerHandler)
return nil, peerBlockBody, nil
}
// SyncValidatorsInfo synchronizes validators info from a block body of an epoch start meta block
func (p *peerMiniBlockSyncer) SyncValidatorsInfo(bodyHandler data.BodyHandler) ([][]byte, map[string]*state.ShardValidatorInfo, error) {
if check.IfNil(bodyHandler) {
return nil, nil, epochStart.ErrNilBlockBody
}
body, ok := bodyHandler.(*block.Body)
if !ok {
return nil, nil, epochStart.ErrWrongTypeAssertion
}
p.initValidatorsInfo()
p.computeMissingValidatorsInfo(body)
allMissingValidatorsInfoHashes, err := p.retrieveMissingValidatorsInfo()
if err != nil {
return allMissingValidatorsInfoHashes, nil, err
}
validatorsInfo := p.getAllValidatorsInfo(body)
return nil, validatorsInfo, nil
}
func (p *peerMiniBlockSyncer) receivedMiniBlock(key []byte, val interface{}) {
peerMiniBlock, ok := val.(*block.MiniBlock)
if !ok {
log.Error("receivedMiniBlock", "key", key, "error", epochStart.ErrWrongTypeAssertion)
return
}
if peerMiniBlock.Type != block.PeerBlock {
return
}
log.Debug("peerMiniBlockSyncer.receivedMiniBlock", "mb type", peerMiniBlock.Type)
p.mutMiniBlocksForBlock.Lock()
havingPeerMb, ok := p.mapAllPeerMiniBlocks[string(key)]
if !ok || havingPeerMb != nil {
p.mutMiniBlocksForBlock.Unlock()
return
}
p.mapAllPeerMiniBlocks[string(key)] = peerMiniBlock
p.numMissingPeerMiniBlocks--
numMissingPeerMiniBlocks := p.numMissingPeerMiniBlocks
p.mutMiniBlocksForBlock.Unlock()
log.Debug("peerMiniBlockSyncer.receivedMiniBlock", "mb hash", key, "num missing peer mini blocks", numMissingPeerMiniBlocks)
if numMissingPeerMiniBlocks == 0 {
p.chRcvAllMiniBlocks <- struct{}{}
}
}
func (p *peerMiniBlockSyncer) receivedValidatorInfo(key []byte, val interface{}) {
validatorInfo, ok := val.(*state.ShardValidatorInfo)
if !ok {
log.Error("receivedValidatorInfo", "key", key, "error", epochStart.ErrWrongTypeAssertion)
return
}
log.Debug("peerMiniBlockSyncer.receivedValidatorInfo", "pk", validatorInfo.PublicKey)
p.mutValidatorsInfoForBlock.Lock()
havingValidatorInfo, ok := p.mapAllValidatorsInfo[string(key)]
if !ok || havingValidatorInfo != nil {
p.mutValidatorsInfoForBlock.Unlock()
return
}
p.mapAllValidatorsInfo[string(key)] = validatorInfo
p.numMissingValidatorsInfo--
numMissingValidatorsInfo := p.numMissingValidatorsInfo
p.mutValidatorsInfoForBlock.Unlock()
log.Debug("peerMiniBlockSyncer.receivedValidatorInfo", "tx hash", key, "num missing validators info", numMissingValidatorsInfo)
if numMissingValidatorsInfo == 0 {
p.chRcvAllValidatorsInfo <- struct{}{}
}
}
func (p *peerMiniBlockSyncer) getAllPeerMiniBlocks(metaBlock data.HeaderHandler) data.BodyHandler {
p.mutMiniBlocksForBlock.Lock()
defer p.mutMiniBlocksForBlock.Unlock()
peerBlockBody := &block.Body{
MiniBlocks: make([]*block.MiniBlock, 0),
}
for _, peerMiniBlock := range metaBlock.GetMiniBlockHeaderHandlers() {
if peerMiniBlock.GetTypeInt32() != int32(block.PeerBlock) {
continue
}
mb := p.mapAllPeerMiniBlocks[string(peerMiniBlock.GetHash())]
peerBlockBody.MiniBlocks = append(peerBlockBody.MiniBlocks, mb)
}
return peerBlockBody
}
func (p *peerMiniBlockSyncer) getAllValidatorsInfo(body *block.Body) map[string]*state.ShardValidatorInfo {
p.mutValidatorsInfoForBlock.Lock()
defer p.mutValidatorsInfoForBlock.Unlock()
validatorsInfo := make(map[string]*state.ShardValidatorInfo)
for _, mb := range body.MiniBlocks {
if mb.Type != block.PeerBlock {
continue
}
for _, txHash := range mb.TxHashes {
validatorInfo := p.mapAllValidatorsInfo[string(txHash)]
validatorsInfo[string(txHash)] = validatorInfo
}
}
return validatorsInfo
}
func (p *peerMiniBlockSyncer) computeMissingPeerBlocks(metaBlock data.HeaderHandler) {
p.mutMiniBlocksForBlock.Lock()
defer p.mutMiniBlocksForBlock.Unlock()
numMissingPeerMiniBlocks := uint32(0)
for _, mb := range metaBlock.GetMiniBlockHeaderHandlers() {
if mb.GetTypeInt32() != int32(block.PeerBlock) {
continue
}
p.mapAllPeerMiniBlocks[string(mb.GetHash())] = nil
mbObjectFound, ok := p.miniBlocksPool.Peek(mb.GetHash())
if !ok {
numMissingPeerMiniBlocks++
continue
}
mbFound, ok := mbObjectFound.(*block.MiniBlock)
if !ok {
numMissingPeerMiniBlocks++
continue
}
p.mapAllPeerMiniBlocks[string(mb.GetHash())] = mbFound
}
p.numMissingPeerMiniBlocks = numMissingPeerMiniBlocks
}
func (p *peerMiniBlockSyncer) computeMissingValidatorsInfo(body *block.Body) {
p.mutValidatorsInfoForBlock.Lock()
defer p.mutValidatorsInfoForBlock.Unlock()
numMissingValidatorsInfo := uint32(0)
for _, miniBlock := range body.MiniBlocks {
if miniBlock.Type != block.PeerBlock {
continue
}
numMissingValidatorsInfo += p.setMissingValidatorsInfo(miniBlock)
}
p.numMissingValidatorsInfo = numMissingValidatorsInfo
}
func (p *peerMiniBlockSyncer) setMissingValidatorsInfo(miniBlock *block.MiniBlock) uint32 {
numMissingValidatorsInfo := uint32(0)
for _, txHash := range miniBlock.TxHashes {
p.mapAllValidatorsInfo[string(txHash)] = nil
val, ok := p.validatorsInfoPool.SearchFirstData(txHash)
if !ok {
numMissingValidatorsInfo++
continue
}
validatorInfo, ok := val.(*state.ShardValidatorInfo)
if !ok {
numMissingValidatorsInfo++
continue
}
p.mapAllValidatorsInfo[string(txHash)] = validatorInfo
}
return numMissingValidatorsInfo
}
func (p *peerMiniBlockSyncer) retrieveMissingMiniBlocks() ([][]byte, error) {
p.mutMiniBlocksForBlock.Lock()
missingMiniBlocks := make([][]byte, 0)
for mbHash, mb := range p.mapAllPeerMiniBlocks {
if mb == nil {
missingMiniBlocks = append(missingMiniBlocks, []byte(mbHash))
}
}
p.numMissingPeerMiniBlocks = uint32(len(missingMiniBlocks))
p.mutMiniBlocksForBlock.Unlock()
if len(missingMiniBlocks) == 0 {
return nil, nil
}
go p.requestHandler.RequestMiniBlocks(core.MetachainShardId, missingMiniBlocks)
select {
case <-p.chRcvAllMiniBlocks:
return nil, nil
case <-time.After(waitTime):
return p.getAllMissingPeerMiniBlocksHashes(), process.ErrTimeIsOut
}
}
func (p *peerMiniBlockSyncer) retrieveMissingValidatorsInfo() ([][]byte, error) {
p.mutValidatorsInfoForBlock.Lock()
missingValidatorsInfo := make([][]byte, 0)
for validatorInfoHash, validatorInfo := range p.mapAllValidatorsInfo {
if validatorInfo == nil {
missingValidatorsInfo = append(missingValidatorsInfo, []byte(validatorInfoHash))
}
}
p.numMissingValidatorsInfo = uint32(len(missingValidatorsInfo))
p.mutValidatorsInfoForBlock.Unlock()
if len(missingValidatorsInfo) == 0 {
return nil, nil
}
go p.requestHandler.RequestValidatorsInfo(missingValidatorsInfo)
select {
case <-p.chRcvAllValidatorsInfo:
return nil, nil
case <-time.After(waitTime):
return p.getAllMissingValidatorsInfoHashes(), process.ErrTimeIsOut
}
}
func (p *peerMiniBlockSyncer) getAllMissingPeerMiniBlocksHashes() [][]byte {
p.mutMiniBlocksForBlock.RLock()
defer p.mutMiniBlocksForBlock.RUnlock()
missingPeerMiniBlocksHashes := make([][]byte, 0)
for hash, mb := range p.mapAllPeerMiniBlocks {
if mb == nil {
missingPeerMiniBlocksHashes = append(missingPeerMiniBlocksHashes, []byte(hash))
}
}
return missingPeerMiniBlocksHashes
}
func (p *peerMiniBlockSyncer) getAllMissingValidatorsInfoHashes() [][]byte {
p.mutValidatorsInfoForBlock.RLock()
defer p.mutValidatorsInfoForBlock.RUnlock()
missingValidatorsInfoHashes := make([][]byte, 0)
for validatorInfoHash, validatorInfo := range p.mapAllValidatorsInfo {
if validatorInfo == nil {
missingValidatorsInfoHashes = append(missingValidatorsInfoHashes, []byte(validatorInfoHash))
}
}
return missingValidatorsInfoHashes
}
// IsInterfaceNil returns true if underlying object is nil
func (p *peerMiniBlockSyncer) IsInterfaceNil() bool {
return p == nil
}