-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
618 lines (511 loc) · 14.2 KB
/
block.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
//go:generate protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/numbatx/protobuf/protobuf --gogoslick_out=$GOPATH/src block.proto
package block
import (
"fmt"
"math/big"
"github.com/ThotaGopichandThota/gn-core3/data"
"github.com/ThotaGopichandThota/gn-core3/data/headerVersionData"
)
// TODO: add access protection through wrappers
var _ = data.HeaderHandler(&Header{})
var _ = data.ShardHeaderHandler(&Header{})
// MiniBlockSlice should be used when referring to subset of mini blocks that is not
//
// necessarily representing a full block body
type MiniBlockSlice []*MiniBlock
// MiniblockAndHash holds the info related to a miniblock and its hash
type MiniblockAndHash struct {
Miniblock *MiniBlock
Hash []byte
}
// SetNonce sets header nonce
func (h *Header) SetNonce(n uint64) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.Nonce = n
return nil
}
// SetEpoch sets header epoch
func (h *Header) SetEpoch(e uint32) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.Epoch = e
return nil
}
// SetRound sets header round
func (h *Header) SetRound(r uint64) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.Round = r
return nil
}
// SetRootHash sets root hash
func (h *Header) SetRootHash(rHash []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.RootHash = rHash
return nil
}
// SetValidatorStatsRootHash sets the root hash for the validator statistics trie
func (h *Header) SetValidatorStatsRootHash(_ []byte) {
}
// SetPrevHash sets prev hash
func (h *Header) SetPrevHash(pvHash []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.PrevHash = pvHash
return nil
}
// GetValidatorStatsRootHash returns the root hash for the validator statistics trie
func (h *Header) GetValidatorStatsRootHash() []byte {
return []byte{}
}
// SetPrevRandSeed sets previous random seed
func (h *Header) SetPrevRandSeed(pvRandSeed []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.PrevRandSeed = pvRandSeed
return nil
}
// SetRandSeed sets previous random seed
func (h *Header) SetRandSeed(randSeed []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.RandSeed = randSeed
return nil
}
// SetPubKeysBitmap sets public key bitmap
func (h *Header) SetPubKeysBitmap(pkbm []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.PubKeysBitmap = pkbm
return nil
}
// SetSignature sets header signature
func (h *Header) SetSignature(sg []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.Signature = sg
return nil
}
// SetLeaderSignature will set the leader's signature
func (h *Header) SetLeaderSignature(sg []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.LeaderSignature = sg
return nil
}
// SetChainID sets the chain ID on which this block is valid on
func (h *Header) SetChainID(chainID []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.ChainID = chainID
return nil
}
// SetSoftwareVersion sets the software version of the header
func (h *Header) SetSoftwareVersion(version []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.SoftwareVersion = version
return nil
}
// SetTimeStamp sets header timestamp
func (h *Header) SetTimeStamp(ts uint64) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.TimeStamp = ts
return nil
}
// SetAccumulatedFees sets the accumulated fees in the header
func (h *Header) SetAccumulatedFees(value *big.Int) error {
if h == nil {
return data.ErrNilPointerReceiver
}
if h.AccumulatedFees == nil {
h.AccumulatedFees = big.NewInt(0)
}
h.AccumulatedFees.Set(value)
return nil
}
// SetDeveloperFees sets the developer fees in the header
func (h *Header) SetDeveloperFees(value *big.Int) error {
if h == nil {
return data.ErrNilPointerReceiver
}
if h.DeveloperFees == nil {
h.DeveloperFees = big.NewInt(0)
}
h.DeveloperFees.Set(value)
return nil
}
// SetTxCount sets the transaction count of the block associated with this header
func (h *Header) SetTxCount(txCount uint32) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.TxCount = txCount
return nil
}
// SetShardID sets header shard ID
func (h *Header) SetShardID(shId uint32) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.ShardID = shId
return nil
}
// GetMiniBlockHeadersWithDst as a map of hashes and sender IDs
func (h *Header) GetMiniBlockHeadersWithDst(destId uint32) map[string]uint32 {
if h == nil {
return nil
}
hashDst := make(map[string]uint32)
for _, val := range h.MiniBlockHeaders {
if val.ReceiverShardID == destId && val.SenderShardID != destId {
hashDst[string(val.Hash)] = val.SenderShardID
}
}
return hashDst
}
// GetOrderedCrossMiniblocksWithDst gets all cross miniblocks with the given destination shard ID, ordered in a
// chronological way, taking into consideration the round in which they were created/executed in the sender shard
func (h *Header) GetOrderedCrossMiniblocksWithDst(destId uint32) []*data.MiniBlockInfo {
if h == nil {
return nil
}
miniBlocks := make([]*data.MiniBlockInfo, 0)
for _, mb := range h.MiniBlockHeaders {
if mb.ReceiverShardID == destId && mb.SenderShardID != destId {
miniBlocks = append(miniBlocks, &data.MiniBlockInfo{
Hash: mb.Hash,
SenderShardID: mb.SenderShardID,
Round: h.Round,
})
}
}
return miniBlocks
}
// GetMiniBlockHeadersHashes gets the miniblock hashes
func (h *Header) GetMiniBlockHeadersHashes() [][]byte {
if h == nil {
return nil
}
result := make([][]byte, 0, len(h.MiniBlockHeaders))
for _, miniblock := range h.MiniBlockHeaders {
result = append(result, miniblock.Hash)
}
return result
}
// MapMiniBlockHashesToShards is a map of mini block hashes and sender IDs
func (h *Header) MapMiniBlockHashesToShards() map[string]uint32 {
if h == nil {
return nil
}
hashDst := make(map[string]uint32)
for _, val := range h.MiniBlockHeaders {
hashDst[string(val.Hash)] = val.SenderShardID
}
return hashDst
}
// ShallowClone returns a clone of the object
func (h *Header) ShallowClone() data.HeaderHandler {
if h == nil {
return nil
}
headerCopy := *h
return &headerCopy
}
// IsInterfaceNil returns true if there is no value under the interface
func (h *Header) IsInterfaceNil() bool {
return h == nil
}
// IsStartOfEpochBlock verifies if the block is of type start of epoch
func (h *Header) IsStartOfEpochBlock() bool {
if h == nil {
return false
}
return len(h.EpochStartMetaHash) > 0
}
// GetBlockBodyTypeInt32 returns the block body type as int32
func (h *Header) GetBlockBodyTypeInt32() int32 {
if h == nil {
return -1
}
return int32(h.GetBlockBodyType())
}
// GetMiniBlockHeaderHandlers returns the miniBlock headers as an array of miniBlock header handlers
func (h *Header) GetMiniBlockHeaderHandlers() []data.MiniBlockHeaderHandler {
if h == nil {
return nil
}
mbHeaders := h.GetMiniBlockHeaders()
mbHeaderHandlers := make([]data.MiniBlockHeaderHandler, len(mbHeaders))
for i := range mbHeaders {
mbHeaderHandlers[i] = &mbHeaders[i]
}
return mbHeaderHandlers
}
// SetMiniBlockHeaderHandlers sets the miniBlock headers from the given miniBlock header handlers
func (h *Header) SetMiniBlockHeaderHandlers(mbHeaderHandlers []data.MiniBlockHeaderHandler) error {
if h == nil {
return data.ErrNilPointerReceiver
}
if len(mbHeaderHandlers) == 0 {
h.MiniBlockHeaders = nil
return nil
}
miniBlockHeaders := make([]MiniBlockHeader, len(mbHeaderHandlers))
for i, mbHeaderHandler := range mbHeaderHandlers {
mbHeader, ok := mbHeaderHandler.(*MiniBlockHeader)
if !ok {
return data.ErrInvalidTypeAssertion
}
if mbHeader == nil {
return data.ErrNilPointerDereference
}
miniBlockHeaders[i] = *mbHeader
}
h.MiniBlockHeaders = miniBlockHeaders
return nil
}
// SetReceiptsHash sets the receipts hash
func (h *Header) SetReceiptsHash(hash []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.ReceiptsHash = hash
return nil
}
// SetMetaBlockHashes sets the metaBlock hashes
func (h *Header) SetMetaBlockHashes(hashes [][]byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.MetaBlockHashes = hashes
return nil
}
// SetScheduledRootHash not supported by V1 header
func (h *Header) SetScheduledRootHash(_ []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
return data.ErrScheduledRootHashNotSupported
}
// SetEpochStartMetaHash sets the epoch start metaBlock hash
func (h *Header) SetEpochStartMetaHash(hash []byte) error {
if h == nil {
return data.ErrNilPointerReceiver
}
h.EpochStartMetaHash = hash
return nil
}
// ValidateHeaderVersion does extra validation for header version
func (h *Header) ValidateHeaderVersion() error {
// no extra validation for v1 header
return nil
}
// IntegrityAndValidity checks if data is valid
func (b *Body) IntegrityAndValidity() error {
if b == nil {
return data.ErrNilPointerReceiver
}
for i := 0; i < len(b.MiniBlocks); i++ {
if len(b.MiniBlocks[i].TxHashes) == 0 {
return data.ErrMiniBlockEmpty
}
}
return nil
}
// Clone returns a clone of the object
func (b *Body) Clone() data.BodyHandler {
if b == nil {
return nil
}
bodyCopy := *b
return &bodyCopy
}
// IsInterfaceNil returns true if there is no value under the interface
func (b *Body) IsInterfaceNil() bool {
return b == nil
}
// Clone the underlying data
func (mb *MiniBlock) Clone() *MiniBlock {
if mb == nil {
return nil
}
newMb := &MiniBlock{
ReceiverShardID: mb.ReceiverShardID,
SenderShardID: mb.SenderShardID,
Type: mb.Type,
}
if mb.TxHashes != nil {
newMb.TxHashes = make([][]byte, len(mb.TxHashes))
copy(newMb.TxHashes, mb.TxHashes)
}
if mb.Reserved != nil {
newMb.Reserved = make([]byte, len(mb.Reserved))
copy(newMb.Reserved, mb.Reserved)
}
return newMb
}
// GetMiniBlockReserved returns the unmarshalled reserved field for the miniblock
func (mb *MiniBlock) GetMiniBlockReserved() (*MiniBlockReserved, error) {
if len(mb.Reserved) > 0 {
mbr := &MiniBlockReserved{}
err := mbr.Unmarshal(mb.Reserved)
if err != nil {
return nil, err
}
return mbr, nil
}
return nil, nil
}
// SetMiniBlockReserved sets the reserved field for the miniBlock with the given parameter
func (mb *MiniBlock) SetMiniBlockReserved(mbr *MiniBlockReserved) error {
if mbr == nil {
mb.Reserved = nil
return nil
}
reserved, err := mbr.Marshal()
if err != nil {
return err
}
mb.Reserved = reserved
return nil
}
// IsScheduledMiniBlock returns if the mini block is of type scheduled or not
func (mb *MiniBlock) IsScheduledMiniBlock() bool {
if mb == nil || len(mb.Reserved) == 0 {
return false
}
mbr := &MiniBlockReserved{}
err := mbr.Unmarshal(mb.Reserved)
if err != nil {
return false
}
return mbr.ExecutionType == Scheduled
}
// GetProcessingType returns the miniBlock processing type as a int32
func (mb *MiniBlock) GetProcessingType() int32 {
miniBlockReserved, err := mb.GetMiniBlockReserved()
if err != nil || miniBlockReserved == nil {
return int32(Normal)
}
return int32(miniBlockReserved.ExecutionType)
}
// GetTxsTypeFromMiniBlock returns all the txs type from mini block
func (mb *MiniBlock) GetTxsTypeFromMiniBlock() ([]Type, error) {
if mb == nil {
return nil, data.ErrNilPointerReceiver
}
if len(mb.Reserved) == 0 {
return mb.getTxsTypeOfMiniBlock()
}
mbr := &MiniBlockReserved{}
err := mbr.Unmarshal(mb.Reserved)
if err != nil {
return nil, err
}
if len(mbr.TransactionsType) == 0 {
return mb.getTxsTypeOfMiniBlock()
}
return mb.getTxsType(mbr)
}
func (mb *MiniBlock) getTxsTypeOfMiniBlock() ([]Type, error) {
numTxs := len(mb.TxHashes)
txsType := make([]Type, numTxs)
for i := 0; i < numTxs; i++ {
txsType[i] = mb.Type
}
return txsType, nil
}
func (mb *MiniBlock) getTxsType(mbr *MiniBlockReserved) ([]Type, error) {
numTxs := len(mb.TxHashes)
err := checkTransactionsTypeValidity(mbr, numTxs)
if err != nil {
return nil, err
}
txsType := make([]Type, numTxs)
for i := 0; i < numTxs; i++ {
isTxBlockType := (mbr.TransactionsType[i/8] & (1 << (uint16(i) % 8))) == 0
if isTxBlockType {
txsType[i] = TxBlock
} else {
// we assume that we have only two types of txs, TxBlock and SmartContractResultBlock, included in mini blocks of type TxBlock
txsType[i] = SmartContractResultBlock
}
}
return txsType, nil
}
func checkTransactionsTypeValidity(mbr *MiniBlockReserved, numTxs int) error {
transactionsTypeSize := numTxs / 8
if numTxs%8 != 0 {
transactionsTypeSize++
}
if len(mbr.TransactionsType) != transactionsTypeSize {
return data.ErrWrongTransactionsTypeSize
}
return nil
}
// SetAdditionalData sets the additional data for the header
func (h *Header) SetAdditionalData(_ headerVersionData.HeaderAdditionalData) error {
// first header version does not have any additional data
return nil
}
// HasScheduledSupport returns false as the first block version does not support scheduled data
func (h *Header) HasScheduledSupport() bool {
return false
}
// HasScheduledMiniBlocks returns false as v1 Header has no support for scheduled data
func (h *Header) HasScheduledMiniBlocks() bool {
return false
}
// GetAdditionalData gets the additional version-related data for the header
func (h *Header) GetAdditionalData() headerVersionData.HeaderAdditionalData {
// no extra data for the initial version of shard block header
return nil
}
// CheckFieldsForNil checks a predefined set of fields for nil values
func (h *Header) CheckFieldsForNil() error {
if h == nil {
return data.ErrNilPointerReceiver
}
if h.PrevHash == nil {
return fmt.Errorf("%w in Header.PrevHash", data.ErrNilValue)
}
if h.PrevRandSeed == nil {
return fmt.Errorf("%w in Header.PrevRandSeed", data.ErrNilValue)
}
if h.RandSeed == nil {
return fmt.Errorf("%w in Header.RandSeed", data.ErrNilValue)
}
if h.RootHash == nil {
return fmt.Errorf("%w in Header.RootHash", data.ErrNilValue)
}
if h.ChainID == nil {
return fmt.Errorf("%w in Header.ChainID", data.ErrNilValue)
}
if h.SoftwareVersion == nil {
return fmt.Errorf("%w in Header.SoftwareVersion", data.ErrNilValue)
}
if h.AccumulatedFees == nil {
return fmt.Errorf("%w in Header.AccumulatedFees", data.ErrNilValue)
}
if h.DeveloperFees == nil {
return fmt.Errorf("%w in Header.DeveloperFees", data.ErrNilValue)
}
return nil
}