-
Notifications
You must be signed in to change notification settings - Fork 199
/
startInEpochScheduled.go
535 lines (450 loc) · 17.8 KB
/
startInEpochScheduled.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package bootstrap
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/data/scheduled"
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/update"
)
type startInEpochWithScheduledDataSyncer struct {
scheduledTxsHandler process.ScheduledTxsExecutionHandler
scheduledHeadersSyncer epochStart.HeadersByHashSyncer
scheduledMiniBlocksSyncer epochStart.PendingMiniBlocksSyncHandler
txSyncer update.TransactionsSyncHandler
scheduledEnableEpoch uint32
}
func newStartInEpochShardHeaderDataSyncerWithScheduled(
scheduledTxsHandler process.ScheduledTxsExecutionHandler,
headersSyncer epochStart.HeadersByHashSyncer,
miniBlocksSyncer epochStart.PendingMiniBlocksSyncHandler,
txSyncer update.TransactionsSyncHandler,
scheduledEnableEpoch uint32,
) (*startInEpochWithScheduledDataSyncer, error) {
if check.IfNil(scheduledTxsHandler) {
return nil, epochStart.ErrNilScheduledTxsHandler
}
if check.IfNil(headersSyncer) {
return nil, epochStart.ErrNilHeadersSyncer
}
if check.IfNil(miniBlocksSyncer) {
return nil, epochStart.ErrNilMiniBlocksSyncer
}
if check.IfNil(txSyncer) {
return nil, epochStart.ErrNilTransactionsSyncer
}
return &startInEpochWithScheduledDataSyncer{
scheduledTxsHandler: scheduledTxsHandler,
scheduledMiniBlocksSyncer: miniBlocksSyncer,
scheduledHeadersSyncer: headersSyncer,
txSyncer: txSyncer,
scheduledEnableEpoch: scheduledEnableEpoch,
}, nil
}
// UpdateSyncDataIfNeeded checks if according to the header, there is additional or different data required to be synchronized
// and returns that data.
func (ses *startInEpochWithScheduledDataSyncer) UpdateSyncDataIfNeeded(
notarizedShardHeader data.ShardHeaderHandler,
) (data.ShardHeaderHandler, map[string]data.HeaderHandler, map[string]*block.MiniBlock, error) {
if ses.scheduledEnableEpoch > notarizedShardHeader.GetEpoch() {
return notarizedShardHeader, nil, nil, nil
}
headerToBeProcessed, headers, err := ses.getRequiredHeaderByHash(notarizedShardHeader)
if err != nil {
return nil, nil, nil, err
}
allMiniBlocks, err := ses.getMiniBlocks(notarizedShardHeader)
if err != nil {
return nil, nil, nil, err
}
err = ses.prepareScheduledIntermediateTxs(headerToBeProcessed, notarizedShardHeader, allMiniBlocks)
if err != nil {
return nil, nil, nil, err
}
return headerToBeProcessed, headers, allMiniBlocks, nil
}
// IsInterfaceNil returns true if the receiver is nil, false otherwise
func (ses *startInEpochWithScheduledDataSyncer) IsInterfaceNil() bool {
return ses == nil
}
func (ses *startInEpochWithScheduledDataSyncer) getRequiredHeaderByHash(
notarizedShardHeader data.ShardHeaderHandler,
) (data.ShardHeaderHandler, map[string]data.HeaderHandler, error) {
shardIDs, hashesToRequest := getShardIDAndHashesForIncludedMetaBlocks(notarizedShardHeader)
shardIDs = append(shardIDs, notarizedShardHeader.GetShardID())
hashesToRequest = append(hashesToRequest, notarizedShardHeader.GetPrevHash())
headers, err := ses.syncHeaders(shardIDs, hashesToRequest)
if err != nil {
return nil, nil, err
}
headerToBeProcessed, ok := headers[string(notarizedShardHeader.GetPrevHash())].(data.ShardHeaderHandler)
if !ok {
return nil, nil, fmt.Errorf("%w in getRequiredHeaderByHash: shard header hash: %s",
epochStart.ErrMissingHeader,
hex.EncodeToString(notarizedShardHeader.GetPrevHash()))
}
shardIDs, hashesToRequest = getShardIDAndHashesForIncludedMetaBlocks(headerToBeProcessed)
additionalMetaHashToRequest := getPreviousToFirstReferencedMetaHeaderHash(notarizedShardHeader, headers)
if len(additionalMetaHashToRequest) != 0 {
shardIDs = append(shardIDs, core.MetachainShardId)
hashesToRequest = append(hashesToRequest, additionalMetaHashToRequest)
}
prevHeaders, err := ses.syncHeaders(shardIDs, hashesToRequest)
if err != nil {
return nil, nil, err
}
for hash, hdr := range prevHeaders {
headers[hash] = hdr
}
// get also the previous meta block to the first used meta block, which should be completed
if len(hashesToRequest) > 0 {
var prevPrevHeaders map[string]data.HeaderHandler
shardIDs = []uint32{core.MetachainShardId}
header := prevHeaders[string(hashesToRequest[0])]
if header == nil {
return nil, nil, fmt.Errorf("%w in getRequiredHeaderByHash: metaBlock hash: %s",
epochStart.ErrMissingHeader,
hex.EncodeToString(hashesToRequest[0]))
}
hashesToRequest = [][]byte{header.GetPrevHash()}
prevPrevHeaders, err = ses.syncHeaders(shardIDs, hashesToRequest)
if err != nil {
return nil, nil, err
}
for hash, hdr := range prevPrevHeaders {
headers[hash] = hdr
}
}
return headerToBeProcessed, headers, nil
}
func getPreviousToFirstReferencedMetaHeaderHash(shardHeader data.ShardHeaderHandler, headers map[string]data.HeaderHandler) []byte {
hashes := shardHeader.GetMetaBlockHashes()
if len(hashes) == 0 {
return nil
}
firstReferencedMetaHash := hashes[0]
firstReferencedMetaHeader := headers[string(firstReferencedMetaHash)]
if firstReferencedMetaHeader == nil {
log.Error("getPreviousToFirstReferencedMetaHeaderHash", "hash", firstReferencedMetaHash, "error", epochStart.ErrMissingHeader)
return nil
}
metaHeader, ok := firstReferencedMetaHeader.(data.MetaHeaderHandler)
if !ok {
log.Error("getPreviousToFirstReferencedMetaHeaderHash", "hash", firstReferencedMetaHash, "error", epochStart.ErrWrongTypeAssertion)
return nil
}
return metaHeader.GetPrevHash()
}
func getShardIDAndHashesForIncludedMetaBlocks(notarizedShardHeader data.ShardHeaderHandler) ([]uint32, [][]byte) {
shardIDs := make([]uint32, 0)
hashesToRequest := make([][]byte, 0)
// if there were processed metaBlocks in the notarized header, these need to be synced, so they can get again processed
metaBlockHashes := notarizedShardHeader.GetMetaBlockHashes()
for i := range metaBlockHashes {
shardIDs = append(shardIDs, core.MetachainShardId)
hashesToRequest = append(hashesToRequest, metaBlockHashes[i])
}
return shardIDs, hashesToRequest
}
func (ses *startInEpochWithScheduledDataSyncer) syncHeaders(
shardIDs []uint32,
hashesToRequest [][]byte,
) (map[string]data.HeaderHandler, error) {
ses.scheduledHeadersSyncer.ClearFields()
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeToWaitForRequestedData)
err := ses.scheduledHeadersSyncer.SyncMissingHeadersByHash(shardIDs, hashesToRequest, ctx)
cancel()
if err != nil {
return nil, err
}
return ses.scheduledHeadersSyncer.GetHeaders()
}
func (ses *startInEpochWithScheduledDataSyncer) getMiniBlocks(
notarizedShardHeader data.ShardHeaderHandler,
) (map[string]*block.MiniBlock, error) {
return ses.getRequiredMiniBlocksByMbHeader(notarizedShardHeader.GetMiniBlockHeaderHandlers())
}
func (ses *startInEpochWithScheduledDataSyncer) getRequiredMiniBlocksByMbHeader(
mbHeaders []data.MiniBlockHeaderHandler,
) (map[string]*block.MiniBlock, error) {
ses.scheduledMiniBlocksSyncer.ClearFields()
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeToWaitForRequestedData)
err := ses.scheduledMiniBlocksSyncer.SyncPendingMiniBlocks(mbHeaders, ctx)
cancel()
if err != nil {
return nil, err
}
return ses.scheduledMiniBlocksSyncer.GetMiniBlocks()
}
// GetRootHashToSync checks according to the header what root hash is required to be synchronized
func (ses *startInEpochWithScheduledDataSyncer) GetRootHashToSync(notarizedShardHeader data.ShardHeaderHandler) []byte {
if ses.scheduledEnableEpoch > notarizedShardHeader.GetEpoch() {
return notarizedShardHeader.GetRootHash()
}
additionalData := notarizedShardHeader.GetAdditionalData()
if additionalData != nil {
return additionalData.GetScheduledRootHash()
}
return notarizedShardHeader.GetRootHash()
}
func (ses *startInEpochWithScheduledDataSyncer) prepareScheduledIntermediateTxs(
prevHeader data.HeaderHandler,
header data.HeaderHandler,
miniBlocks map[string]*block.MiniBlock,
) error {
scheduledTxHashes, prevHeaderMiniblocks, err := ses.getScheduledTransactionHashes(prevHeader)
if err != nil {
return err
}
allTxs, err := ses.getAllTransactionsForMiniBlocks(miniBlocks, header.GetEpoch())
if err != nil {
return err
}
scheduledIntermediateTxs, err := ses.filterScheduledIntermediateTxs(miniBlocks, scheduledTxHashes, allTxs, prevHeader.GetShardID())
if err != nil {
return err
}
additionalData := header.GetAdditionalData()
if additionalData != nil {
scheduledIntermediateTxsMap := getScheduledIntermediateTxsMapInOrder(
header.GetMiniBlockHeaderHandlers(),
miniBlocks,
scheduledIntermediateTxs,
)
gasAndFees := scheduled.GasAndFees{
AccumulatedFees: additionalData.GetScheduledAccumulatedFees(),
DeveloperFees: additionalData.GetScheduledDeveloperFees(),
GasProvided: additionalData.GetScheduledGasProvided(),
GasPenalized: additionalData.GetScheduledGasPenalized(),
GasRefunded: additionalData.GetScheduledGasRefunded(),
}
scheduledMiniBlocks := getScheduledMiniBlocks(header, miniBlocks)
scheduledInfo := &process.ScheduledInfo{
RootHash: additionalData.GetScheduledRootHash(),
IntermediateTxs: scheduledIntermediateTxsMap,
GasAndFees: gasAndFees,
MiniBlocks: scheduledMiniBlocks,
}
ses.saveScheduledInfo(header.GetPrevHash(), scheduledInfo)
}
if miniBlocks == nil {
miniBlocks = make(map[string]*block.MiniBlock)
}
for hash, mb := range prevHeaderMiniblocks {
miniBlocks[hash] = mb
}
return nil
}
func (ses *startInEpochWithScheduledDataSyncer) filterScheduledIntermediateTxs(
miniBlocks map[string]*block.MiniBlock,
scheduledTxHashes map[string]uint32,
allTxs map[string]data.TransactionHandler,
selfShardID uint32,
) (map[string]data.TransactionHandler, error) {
scheduledIntermediateTxs := make(map[string]data.TransactionHandler)
for txHash, txHandler := range allTxs {
if isScheduledIntermediateTx(miniBlocks, scheduledTxHashes, []byte(txHash), txHandler, selfShardID) {
scheduledIntermediateTxs[txHash] = txHandler
log.Debug("startInEpochWithScheduledDataSyncer.filterScheduledIntermediateTxs",
"intermediate tx hash", []byte(txHash),
"intermediate tx nonce", txHandler.GetNonce(),
"intermediate tx sender address", txHandler.GetSndAddr(),
"intermediate tx receiver address", txHandler.GetRcvAddr(),
"intermediate tx data", string(txHandler.GetData()),
)
}
}
return scheduledIntermediateTxs, nil
}
func isScheduledIntermediateTx(
miniBlocks map[string]*block.MiniBlock,
scheduledTxHashes map[string]uint32,
txHash []byte,
txHandler data.TransactionHandler,
selfShardID uint32,
) bool {
blockType := getBlockTypeOfTx(txHash, miniBlocks)
if blockType != block.SmartContractResultBlock && blockType != block.InvalidBlock {
log.Debug("isScheduledIntermediateTx", "blockType", blockType, "txHash", txHash, "ret", false)
return false
}
var scheduledTxHash []byte
if blockType == block.SmartContractResultBlock {
scr, ok := txHandler.(*smartContractResult.SmartContractResult)
if !ok {
log.Error("isScheduledIntermediateTx", "error", epochStart.ErrWrongTypeAssertion)
return false
}
scheduledTxHash = scr.PrevTxHash
} else {
scheduledTxHash = txHash
}
receiverShardID, isScheduledIntermediateTransaction := scheduledTxHashes[string(scheduledTxHash)]
isTxExecutedInSelfShard := receiverShardID == selfShardID || blockType == block.InvalidBlock
log.Debug("isScheduledIntermediateTx",
"blockType", blockType,
"txHash", txHash,
"isScheduledIntermediateTransaction", isScheduledIntermediateTransaction,
"isTxExecutedInSelfShard", isTxExecutedInSelfShard)
return isScheduledIntermediateTransaction && isTxExecutedInSelfShard
}
func getScheduledIntermediateTxsMapInOrder(
miniBlockHeaderHandlerList []data.MiniBlockHeaderHandler,
miniBlocks map[string]*block.MiniBlock,
intermediateTxs map[string]data.TransactionHandler,
) map[block.Type][]data.TransactionHandler {
intermediateTxsMap := make(map[block.Type][]data.TransactionHandler)
for _, mbHeader := range miniBlockHeaderHandlerList {
miniBlock, ok := miniBlocks[string(mbHeader.GetHash())]
if !ok {
continue
}
for _, hash := range miniBlock.TxHashes {
txHandler, ok := intermediateTxs[string(hash)]
if !ok {
continue
}
intermediateTxsMap[miniBlock.Type] = append(intermediateTxsMap[miniBlock.Type], txHandler)
}
}
return intermediateTxsMap
}
func getBlockTypeOfTx(txHash []byte, miniBlocks map[string]*block.MiniBlock) block.Type {
for _, miniBlock := range miniBlocks {
for _, hash := range miniBlock.TxHashes {
if bytes.Equal(hash, txHash) {
return miniBlock.Type
}
}
}
log.Warn("getBlockTypeOfTx: tx not found in mini blocks", "tx hash", txHash)
return block.SmartContractResultBlock
}
func getScheduledMiniBlocks(
header data.HeaderHandler,
miniBlocks map[string]*block.MiniBlock,
) block.MiniBlockSlice {
scheduledMiniBlocks := make(block.MiniBlockSlice, 0)
mbHeaders := header.GetMiniBlockHeaderHandlers()
for _, mbHeader := range mbHeaders {
if mbHeader.GetProcessingType() != int32(block.Processed) {
continue
}
miniBlock, ok := miniBlocks[string(mbHeader.GetHash())]
if !ok {
log.Warn("getScheduledMiniBlocks: mini block was not found", "mb hash", mbHeader.GetHash())
continue
}
scheduledMiniBlocks = append(scheduledMiniBlocks, miniBlock)
}
return scheduledMiniBlocks
}
func (ses *startInEpochWithScheduledDataSyncer) saveScheduledInfo(headerHash []byte, scheduledInfo *process.ScheduledInfo) {
if scheduledInfo.RootHash == nil {
return
}
log.Debug("startInEpochWithScheduledDataSyncer.saveScheduledInfo",
"headerHash", headerHash,
"scheduledRootHash", scheduledInfo.RootHash,
"num of scheduled mbs", len(scheduledInfo.MiniBlocks),
"num of scheduled intermediate txs", getNumScheduledIntermediateTxs(scheduledInfo.IntermediateTxs),
"gasAndFees.AccumulatedFees", scheduledInfo.GasAndFees.AccumulatedFees.String(),
"gasAndFees.DeveloperFees", scheduledInfo.GasAndFees.DeveloperFees.String(),
"gasAndFees.GasProvided", scheduledInfo.GasAndFees.GasProvided,
"gasAndFees.GasPenalized", scheduledInfo.GasAndFees.GasPenalized,
"gasAndFees.GasRefunded", scheduledInfo.GasAndFees.GasRefunded,
)
ses.scheduledTxsHandler.SaveState(headerHash, scheduledInfo)
}
func (ses *startInEpochWithScheduledDataSyncer) getAllTransactionsForMiniBlocks(miniBlocks map[string]*block.MiniBlock, epoch uint32) (map[string]data.TransactionHandler, error) {
ctx := context.Background()
err := ses.txSyncer.SyncTransactionsFor(miniBlocks, epoch, ctx)
if err != nil {
return nil, err
}
return ses.txSyncer.GetTransactions()
}
func (ses *startInEpochWithScheduledDataSyncer) getScheduledMiniBlockHeaders(header data.HeaderHandler) []data.MiniBlockHeaderHandler {
miniBlockHeaders := header.GetMiniBlockHeaderHandlers()
schMiniBlockHeaders := make([]data.MiniBlockHeaderHandler, 0)
for i, mbh := range miniBlockHeaders {
if mbh.GetProcessingType() == int32(block.Scheduled) {
schMiniBlockHeaders = append(schMiniBlockHeaders, miniBlockHeaders[i])
}
}
return schMiniBlockHeaders
}
func (ses *startInEpochWithScheduledDataSyncer) getScheduledTransactionHashes(header data.HeaderHandler) (map[string]uint32, map[string]*block.MiniBlock, error) {
miniBlockHeaders := ses.getScheduledMiniBlockHeaders(header)
miniBlocks, err := ses.getRequiredMiniBlocksByMbHeader(miniBlockHeaders)
if err != nil {
return nil, nil, err
}
scheduledTxsForShard := make(map[string]uint32)
for _, miniBlockHeader := range miniBlockHeaders {
pi, miniBlock, miniBlockHash, shouldSkip := getMiniBlockAndProcessedIndexes(miniBlockHeader, miniBlocks)
if shouldSkip {
continue
}
createScheduledTxsForShardMap(pi, miniBlock, miniBlockHash, scheduledTxsForShard)
}
return scheduledTxsForShard, miniBlocks, nil
}
func getMiniBlockAndProcessedIndexes(
miniBlockHeader data.MiniBlockHeaderHandler,
miniBlocks map[string]*block.MiniBlock,
) (*processedIndexes, *block.MiniBlock, []byte, bool) {
pi := &processedIndexes{}
miniBlockHash := miniBlockHeader.GetHash()
miniBlock, ok := miniBlocks[string(miniBlockHash)]
if !ok {
log.Warn("startInEpochWithScheduledDataSyncer.getMiniBlockAndProcessedIndexes: mini block was not found", "mb hash", miniBlockHash)
return nil, nil, nil, true
}
pi.firstIndex = miniBlockHeader.GetIndexOfFirstTxProcessed()
pi.lastIndex = miniBlockHeader.GetIndexOfLastTxProcessed()
if pi.firstIndex > pi.lastIndex {
log.Warn("startInEpochWithScheduledDataSyncer.getMiniBlockAndProcessedIndexes: wrong first/last index",
"mb hash", miniBlockHash,
"index of first tx processed", pi.firstIndex,
"index of last tx processed", pi.lastIndex,
"num txs", len(miniBlock.TxHashes),
)
return nil, nil, nil, true
}
return pi, miniBlock, miniBlockHash, false
}
func createScheduledTxsForShardMap(
pi *processedIndexes,
miniBlock *block.MiniBlock,
miniBlockHash []byte,
scheduledTxsForShard map[string]uint32,
) {
for index := pi.firstIndex; index <= pi.lastIndex; index++ {
if index >= int32(len(miniBlock.TxHashes)) {
log.Warn("startInEpochWithScheduledDataSyncer.createScheduledTxsForShardMap: index out of bound",
"mb hash", miniBlockHash,
"index", index,
"num txs", len(miniBlock.TxHashes),
)
break
}
txHash := miniBlock.TxHashes[index]
scheduledTxsForShard[string(txHash)] = miniBlock.GetReceiverShardID()
log.Debug("startInEpochWithScheduledDataSyncer.createScheduledTxsForShardMap", "hash", txHash)
}
}
func getNumScheduledIntermediateTxs(mapScheduledIntermediateTxs map[block.Type][]data.TransactionHandler) int {
numScheduledIntermediateTxs := 0
for _, scheduledIntermediateTxs := range mapScheduledIntermediateTxs {
numScheduledIntermediateTxs += len(scheduledIntermediateTxs)
}
return numScheduledIntermediateTxs
}