-
Notifications
You must be signed in to change notification settings - Fork 202
/
common.go
654 lines (545 loc) · 17 KB
/
common.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
package process
import (
"bytes"
"encoding/hex"
"fmt"
"math"
"sort"
"github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/block"
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/data/transaction"
"github.com/ElrondNetwork/elrond-go/data/typeConverters"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/marshal"
)
var log = logger.GetOrCreate("process")
// GetShardHeader gets the header, which is associated with the given hash, from pool or storage
func GetShardHeader(
hash []byte,
headersCacher dataRetriever.HeadersPool,
marshalizer marshal.Marshalizer,
storageService dataRetriever.StorageService,
) (*block.Header, error) {
err := checkGetHeaderParamsForNil(headersCacher, marshalizer, storageService)
if err != nil {
return nil, err
}
hdr, err := GetShardHeaderFromPool(hash, headersCacher)
if err != nil {
hdr, err = GetShardHeaderFromStorage(hash, marshalizer, storageService)
if err != nil {
return nil, err
}
}
return hdr, nil
}
// GetMetaHeader gets the header, which is associated with the given hash, from pool or storage
func GetMetaHeader(
hash []byte,
headersCacher dataRetriever.HeadersPool,
marshalizer marshal.Marshalizer,
storageService dataRetriever.StorageService,
) (*block.MetaBlock, error) {
err := checkGetHeaderParamsForNil(headersCacher, marshalizer, storageService)
if err != nil {
return nil, err
}
hdr, err := GetMetaHeaderFromPool(hash, headersCacher)
if err != nil {
hdr, err = GetMetaHeaderFromStorage(hash, marshalizer, storageService)
if err != nil {
return nil, err
}
}
return hdr, nil
}
// GetShardHeaderFromPool gets the header, which is associated with the given hash, from pool
func GetShardHeaderFromPool(
hash []byte,
headersCacher dataRetriever.HeadersPool,
) (*block.Header, error) {
obj, err := getHeaderFromPool(hash, headersCacher)
if err != nil {
return nil, err
}
hdr, ok := obj.(*block.Header)
if !ok {
return nil, ErrWrongTypeAssertion
}
return hdr, nil
}
// GetMetaHeaderFromPool gets the header, which is associated with the given hash, from pool
func GetMetaHeaderFromPool(
hash []byte,
headersCacher dataRetriever.HeadersPool,
) (*block.MetaBlock, error) {
obj, err := getHeaderFromPool(hash, headersCacher)
if err != nil {
return nil, err
}
hdr, ok := obj.(*block.MetaBlock)
if !ok {
return nil, ErrWrongTypeAssertion
}
return hdr, nil
}
// GetShardHeaderFromStorage gets the header, which is associated with the given hash, from storage
func GetShardHeaderFromStorage(
hash []byte,
marshalizer marshal.Marshalizer,
storageService dataRetriever.StorageService,
) (*block.Header, error) {
buffHdr, err := GetMarshalizedHeaderFromStorage(dataRetriever.BlockHeaderUnit, hash, marshalizer, storageService)
if err != nil {
return nil, err
}
hdr := &block.Header{}
err = marshalizer.Unmarshal(hdr, buffHdr)
if err != nil {
return nil, ErrUnmarshalWithoutSuccess
}
return hdr, nil
}
// GetMetaHeaderFromStorage gets the header, which is associated with the given hash, from storage
func GetMetaHeaderFromStorage(
hash []byte,
marshalizer marshal.Marshalizer,
storageService dataRetriever.StorageService,
) (*block.MetaBlock, error) {
buffHdr, err := GetMarshalizedHeaderFromStorage(dataRetriever.MetaBlockUnit, hash, marshalizer, storageService)
if err != nil {
return nil, err
}
hdr := &block.MetaBlock{}
err = marshalizer.Unmarshal(hdr, buffHdr)
if err != nil {
return nil, ErrUnmarshalWithoutSuccess
}
return hdr, nil
}
// GetMarshalizedHeaderFromStorage gets the marshalized header, which is associated with the given hash, from storage
func GetMarshalizedHeaderFromStorage(
blockUnit dataRetriever.UnitType,
hash []byte,
marshalizer marshal.Marshalizer,
storageService dataRetriever.StorageService,
) ([]byte, error) {
if check.IfNil(marshalizer) {
return nil, ErrNilMarshalizer
}
if check.IfNil(storageService) {
return nil, ErrNilStorage
}
hdrStore := storageService.GetStorer(blockUnit)
if check.IfNil(hdrStore) {
return nil, ErrNilHeadersStorage
}
buffHdr, err := hdrStore.Get(hash)
if err != nil {
return nil, fmt.Errorf("%w : GetMarshalizedHeaderFromStorage hash = %s",
ErrMissingHeader, logger.DisplayByteSlice(hash))
}
return buffHdr, nil
}
// GetShardHeaderWithNonce method returns a shard block header with a given nonce and shardId
func GetShardHeaderWithNonce(
nonce uint64,
shardId uint32,
headersCacher dataRetriever.HeadersPool,
marshalizer marshal.Marshalizer,
storageService dataRetriever.StorageService,
uint64Converter typeConverters.Uint64ByteSliceConverter,
) (*block.Header, []byte, error) {
err := checkGetHeaderWithNonceParamsForNil(headersCacher, marshalizer, storageService, uint64Converter)
if err != nil {
return nil, nil, err
}
hdr, hash, err := GetShardHeaderFromPoolWithNonce(nonce, shardId, headersCacher)
if err != nil {
hdr, hash, err = GetShardHeaderFromStorageWithNonce(nonce, shardId, storageService, uint64Converter, marshalizer)
if err != nil {
return nil, nil, err
}
}
return hdr, hash, nil
}
// GetMetaHeaderWithNonce method returns a meta block header with a given nonce
func GetMetaHeaderWithNonce(
nonce uint64,
headersCacher dataRetriever.HeadersPool,
marshalizer marshal.Marshalizer,
storageService dataRetriever.StorageService,
uint64Converter typeConverters.Uint64ByteSliceConverter,
) (*block.MetaBlock, []byte, error) {
err := checkGetHeaderWithNonceParamsForNil(headersCacher, marshalizer, storageService, uint64Converter)
if err != nil {
return nil, nil, err
}
hdr, hash, err := GetMetaHeaderFromPoolWithNonce(nonce, headersCacher)
if err != nil {
hdr, hash, err = GetMetaHeaderFromStorageWithNonce(nonce, storageService, uint64Converter, marshalizer)
if err != nil {
return nil, nil, err
}
}
return hdr, hash, nil
}
// GetShardHeaderFromPoolWithNonce method returns a shard block header from pool with a given nonce and shardId
func GetShardHeaderFromPoolWithNonce(
nonce uint64,
shardId uint32,
headersCacher dataRetriever.HeadersPool,
) (*block.Header, []byte, error) {
obj, hash, err := getHeaderFromPoolWithNonce(nonce, shardId, headersCacher)
if err != nil {
return nil, nil, err
}
hdr, ok := obj.(*block.Header)
if !ok {
return nil, nil, ErrWrongTypeAssertion
}
return hdr, hash, nil
}
// GetMetaHeaderFromPoolWithNonce method returns a meta block header from pool with a given nonce
func GetMetaHeaderFromPoolWithNonce(
nonce uint64,
headersCacher dataRetriever.HeadersPool,
) (*block.MetaBlock, []byte, error) {
obj, hash, err := getHeaderFromPoolWithNonce(nonce, core.MetachainShardId, headersCacher)
if err != nil {
return nil, nil, err
}
hdr, ok := obj.(*block.MetaBlock)
if !ok {
return nil, nil, ErrWrongTypeAssertion
}
return hdr, hash, nil
}
// GetHeaderFromStorageWithNonce method returns a block header from storage with a given nonce and shardId
func GetHeaderFromStorageWithNonce(
nonce uint64,
shardId uint32,
storageService dataRetriever.StorageService,
uint64Converter typeConverters.Uint64ByteSliceConverter,
marshalizer marshal.Marshalizer,
) (data.HeaderHandler, []byte, error) {
if shardId == core.MetachainShardId {
return GetMetaHeaderFromStorageWithNonce(nonce, storageService, uint64Converter, marshalizer)
}
return GetShardHeaderFromStorageWithNonce(nonce, shardId, storageService, uint64Converter, marshalizer)
}
// GetShardHeaderFromStorageWithNonce method returns a shard block header from storage with a given nonce and shardId
func GetShardHeaderFromStorageWithNonce(
nonce uint64,
shardId uint32,
storageService dataRetriever.StorageService,
uint64Converter typeConverters.Uint64ByteSliceConverter,
marshalizer marshal.Marshalizer,
) (*block.Header, []byte, error) {
hash, err := getHeaderHashFromStorageWithNonce(
nonce,
storageService,
uint64Converter,
marshalizer,
dataRetriever.ShardHdrNonceHashDataUnit+dataRetriever.UnitType(shardId))
if err != nil {
return nil, nil, err
}
hdr, err := GetShardHeaderFromStorage(hash, marshalizer, storageService)
if err != nil {
return nil, nil, err
}
return hdr, hash, nil
}
// GetMetaHeaderFromStorageWithNonce method returns a meta block header from storage with a given nonce
func GetMetaHeaderFromStorageWithNonce(
nonce uint64,
storageService dataRetriever.StorageService,
uint64Converter typeConverters.Uint64ByteSliceConverter,
marshalizer marshal.Marshalizer,
) (*block.MetaBlock, []byte, error) {
hash, err := getHeaderHashFromStorageWithNonce(
nonce,
storageService,
uint64Converter,
marshalizer,
dataRetriever.MetaHdrNonceHashDataUnit)
if err != nil {
return nil, nil, err
}
hdr, err := GetMetaHeaderFromStorage(hash, marshalizer, storageService)
if err != nil {
return nil, nil, err
}
return hdr, hash, nil
}
// GetTransactionHandler gets the transaction with a given sender/receiver shardId and txHash
func GetTransactionHandler(
senderShardID uint32,
destShardID uint32,
txHash []byte,
shardedDataCacherNotifier dataRetriever.ShardedDataCacherNotifier,
storageService dataRetriever.StorageService,
marshalizer marshal.Marshalizer,
searchFirst bool,
) (data.TransactionHandler, error) {
err := checkGetTransactionParamsForNil(shardedDataCacherNotifier, storageService, marshalizer)
if err != nil {
return nil, err
}
tx, err := GetTransactionHandlerFromPool(senderShardID, destShardID, txHash, shardedDataCacherNotifier, searchFirst)
if err != nil {
tx, err = GetTransactionHandlerFromStorage(txHash, storageService, marshalizer)
if err != nil {
return nil, err
}
}
return tx, nil
}
// GetTransactionHandlerFromPool gets the transaction from pool with a given sender/receiver shardId and txHash
func GetTransactionHandlerFromPool(
senderShardID uint32,
destShardID uint32,
txHash []byte,
shardedDataCacherNotifier dataRetriever.ShardedDataCacherNotifier,
searchFirst bool,
) (data.TransactionHandler, error) {
if shardedDataCacherNotifier == nil {
return nil, ErrNilShardedDataCacherNotifier
}
var val interface{}
ok := false
if searchFirst {
val, ok = shardedDataCacherNotifier.SearchFirstData(txHash)
if !ok {
return nil, ErrTxNotFound
}
} else {
strCache := ShardCacherIdentifier(senderShardID, destShardID)
txStore := shardedDataCacherNotifier.ShardDataStore(strCache)
if txStore == nil {
return nil, ErrNilStorage
}
val, ok = txStore.Peek(txHash)
}
if !ok {
return nil, ErrTxNotFound
}
tx, ok := val.(data.TransactionHandler)
if !ok {
return nil, ErrInvalidTxInPool
}
return tx, nil
}
// GetTransactionHandlerFromStorage gets the transaction from storage with a given sender/receiver shardId and txHash
func GetTransactionHandlerFromStorage(
txHash []byte,
storageService dataRetriever.StorageService,
marshalizer marshal.Marshalizer,
) (data.TransactionHandler, error) {
if storageService == nil || storageService.IsInterfaceNil() {
return nil, ErrNilStorage
}
if marshalizer == nil || marshalizer.IsInterfaceNil() {
return nil, ErrNilMarshalizer
}
txBuff, err := storageService.Get(dataRetriever.TransactionUnit, txHash)
if err != nil {
return nil, err
}
tx := transaction.Transaction{}
err = marshalizer.Unmarshal(&tx, txBuff)
if err != nil {
return nil, err
}
return &tx, nil
}
func checkGetHeaderParamsForNil(
cacher dataRetriever.HeadersPool,
marshalizer marshal.Marshalizer,
storageService dataRetriever.StorageService,
) error {
if cacher == nil || cacher.IsInterfaceNil() {
return ErrNilCacher
}
if marshalizer == nil || marshalizer.IsInterfaceNil() {
return ErrNilMarshalizer
}
if storageService == nil || storageService.IsInterfaceNil() {
return ErrNilStorage
}
return nil
}
func checkGetHeaderWithNonceParamsForNil(
headersCacher dataRetriever.HeadersPool,
marshalizer marshal.Marshalizer,
storageService dataRetriever.StorageService,
uint64Converter typeConverters.Uint64ByteSliceConverter,
) error {
err := checkGetHeaderParamsForNil(headersCacher, marshalizer, storageService)
if err != nil {
return err
}
if check.IfNil(uint64Converter) {
return ErrNilUint64Converter
}
return nil
}
func checkGetTransactionParamsForNil(
shardedDataCacherNotifier dataRetriever.ShardedDataCacherNotifier,
storageService dataRetriever.StorageService,
marshalizer marshal.Marshalizer,
) error {
if shardedDataCacherNotifier == nil || shardedDataCacherNotifier.IsInterfaceNil() {
return ErrNilShardedDataCacherNotifier
}
if storageService == nil || storageService.IsInterfaceNil() {
return ErrNilStorage
}
if marshalizer == nil || marshalizer.IsInterfaceNil() {
return ErrNilMarshalizer
}
return nil
}
func getHeaderFromPool(
hash []byte,
headersCacher dataRetriever.HeadersPool,
) (interface{}, error) {
if check.IfNil(headersCacher) {
return nil, ErrNilCacher
}
obj, err := headersCacher.GetHeaderByHash(hash)
if err != nil {
return nil, fmt.Errorf("%w : getHeaderFromPool hash = %s",
ErrMissingHeader, logger.DisplayByteSlice(hash))
}
return obj, nil
}
func getHeaderFromPoolWithNonce(
nonce uint64,
shardId uint32,
headersCacher dataRetriever.HeadersPool,
) (interface{}, []byte, error) {
if check.IfNil(headersCacher) {
return nil, nil, ErrNilCacher
}
headers, hashes, err := headersCacher.GetHeadersByNonceAndShardId(nonce, shardId)
if err != nil {
return nil, nil, fmt.Errorf("%w : getHeaderFromPoolWithNonce shard = %d nonce = %d",
ErrMissingHeader, shardId, nonce)
}
//TODO what should we do when we get from pool more than one header with same nonce and shardId
return headers[len(headers)-1], hashes[len(hashes)-1], nil
}
func getHeaderHashFromStorageWithNonce(
nonce uint64,
storageService dataRetriever.StorageService,
uint64Converter typeConverters.Uint64ByteSliceConverter,
marshalizer marshal.Marshalizer,
blockUnit dataRetriever.UnitType,
) ([]byte, error) {
if storageService == nil || storageService.IsInterfaceNil() {
return nil, ErrNilStorage
}
if uint64Converter == nil || uint64Converter.IsInterfaceNil() {
return nil, ErrNilUint64Converter
}
if marshalizer == nil || marshalizer.IsInterfaceNil() {
return nil, ErrNilMarshalizer
}
headerStore := storageService.GetStorer(blockUnit)
if headerStore == nil {
return nil, ErrNilHeadersStorage
}
nonceToByteSlice := uint64Converter.ToByteSlice(nonce)
hash, err := headerStore.Get(nonceToByteSlice)
if err != nil {
return nil, ErrMissingHashForHeaderNonce
}
return hash, nil
}
// SortHeadersByNonce will sort a given list of headers by nonce
func SortHeadersByNonce(headers []data.HeaderHandler) {
if len(headers) > 1 {
sort.Slice(headers, func(i, j int) bool {
return headers[i].GetNonce() < headers[j].GetNonce()
})
}
}
// IsInProperRound checks if the given round index satisfies the round modulus trigger
func IsInProperRound(index int64) bool {
return index%RoundModulusTrigger == 0
}
// AddHeaderToBlackList adds a hash to black list handler. Logs if the operation did not succeed
func AddHeaderToBlackList(blackListHandler BlackListHandler, hash []byte) {
blackListHandler.Sweep()
err := blackListHandler.Add(string(hash))
if err != nil {
log.Trace("blackListHandler.Add", "error", err.Error())
}
log.Debug("header has been added to blacklist",
"hash", hash)
}
// ForkInfo hold the data related to a detected fork
type ForkInfo struct {
IsDetected bool
Nonce uint64
Round uint64
Hash []byte
}
// NewForkInfo creates a new ForkInfo object
func NewForkInfo() *ForkInfo {
return &ForkInfo{IsDetected: false, Nonce: math.MaxUint64, Round: math.MaxUint64, Hash: nil}
}
// DisplayProcessTxDetails displays information related to the tx which should be executed
func DisplayProcessTxDetails(
message string,
accountHandler state.AccountHandler,
txHandler data.TransactionHandler,
addressPubkeyConverter core.PubkeyConverter,
) {
if !check.IfNil(accountHandler) {
account, ok := accountHandler.(state.UserAccountHandler)
if ok {
log.Trace(message,
"nonce", account.GetNonce(),
"balance", account.GetBalance(),
)
}
}
if check.IfNil(addressPubkeyConverter) {
return
}
if check.IfNil(txHandler) {
return
}
receiver := ""
if len(txHandler.GetRcvAddr()) == addressPubkeyConverter.Len() {
receiver = addressPubkeyConverter.Encode(txHandler.GetRcvAddr())
}
sender := ""
if len(txHandler.GetSndAddr()) == addressPubkeyConverter.Len() {
sender = addressPubkeyConverter.Encode(txHandler.GetSndAddr())
}
log.Trace("executing transaction",
"nonce", txHandler.GetNonce(),
"value", txHandler.GetValue(),
"gas limit", txHandler.GetGasLimit(),
"gas price", txHandler.GetGasPrice(),
"data", hex.EncodeToString(txHandler.GetData()),
"sender", sender,
"receiver", receiver)
}
// IsAllowedToSaveUnderKey returns if saving key-value in data tries under given key is allowed
func IsAllowedToSaveUnderKey(key []byte) bool {
prefixLen := len(core.ElrondProtectedKeyPrefix)
if len(key) < prefixLen {
return true
}
trimmedKey := key[:prefixLen]
return !bytes.Equal(trimmedKey, []byte(core.ElrondProtectedKeyPrefix))
}