-
Notifications
You must be signed in to change notification settings - Fork 211
/
mesh.go
769 lines (660 loc) · 23.5 KB
/
mesh.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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
// Package mesh defines the main store point for all the block-mesh objects
// such as blocks, transactions and global state
package mesh
import (
"encoding/binary"
"errors"
"fmt"
"github.com/seehuhn/mt19937"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/common/util"
"github.com/spacemeshos/go-spacemesh/database"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/signing"
"math/rand"
"math/big"
"sync"
)
const (
layerSize = 200
// Genesis layer index
Genesis = types.LayerID(0)
)
var constTrue = []byte{1}
var constFalse = []byte{0}
var constLATEST = []byte("latest")
var constLAYERHASH = []byte("layer hash")
var constPROCESSED = []byte("processed")
// TORTOISE key for tortoise persistence in database
var TORTOISE = []byte("tortoise")
// VERIFIED refers to layers we pushed into the state
var VERIFIED = []byte("verified")
type tortoise interface {
HandleIncomingLayer(layer *types.Layer) (types.LayerID, types.LayerID)
LatestComplete() types.LayerID
Persist() error
HandleLateBlock(bl *types.Block) (types.LayerID, types.LayerID)
}
// Validator interface to be used in tests to mock validation flow
type Validator interface {
ValidateLayer(layer *types.Layer)
HandleLateBlock(bl *types.Block)
ProcessedLayer() types.LayerID
SetProcessedLayer(lyr types.LayerID)
}
type txProcessor interface {
ApplyTransactions(layer types.LayerID, txs []*types.Transaction) (int, error)
ApplyRewards(layer types.LayerID, miners []types.Address, reward *big.Int)
AddressExists(addr types.Address) bool
ValidateNonceAndBalance(transaction *types.Transaction) error
GetLayerApplied(txID types.TransactionID) *types.LayerID
GetStateRoot() types.Hash32
LoadState(layer types.LayerID) error
}
type txMemPoolInValidator interface {
Invalidate(id types.TransactionID)
}
type atxMemPoolInValidator interface {
Invalidate(id types.ATXID)
}
// AtxDB holds logic for working with atxs
type AtxDB interface {
ProcessAtxs(atxs []*types.ActivationTx) error
GetAtxHeader(id types.ATXID) (*types.ActivationTxHeader, error)
GetFullAtx(id types.ATXID) (*types.ActivationTx, error)
SyntacticallyValidateAtx(atx *types.ActivationTx) error
}
type blockBuilder interface {
ValidateAndAddTxToPool(tx *types.Transaction) error
}
// Mesh is the logic layer above our mesh.DB database
type Mesh struct {
log.Log
*DB
AtxDB
txProcessor
Validator
trtl tortoise
blockBuilder blockBuilder
txInvalidator txMemPoolInValidator
atxInvalidator atxMemPoolInValidator
config Config
latestLayer types.LayerID
latestLayerInState types.LayerID
layerHash []byte
lMutex sync.RWMutex
lkMutex sync.RWMutex
lcMutex sync.RWMutex
lvMutex sync.RWMutex
orphMutex sync.RWMutex
pMutex sync.RWMutex
done chan struct{}
nextValidLayers map[types.LayerID]*types.Layer
maxValidatedLayer types.LayerID
txMutex sync.Mutex
}
// NewMesh creates a new instant of a mesh
func NewMesh(db *DB, atxDb AtxDB, rewardConfig Config, mesh tortoise, txInvalidator txMemPoolInValidator, atxInvalidator atxMemPoolInValidator, pr txProcessor, logger log.Log) *Mesh {
ll := &Mesh{
Log: logger,
trtl: mesh,
txInvalidator: txInvalidator,
atxInvalidator: atxInvalidator,
txProcessor: pr,
done: make(chan struct{}),
DB: db,
config: rewardConfig,
AtxDB: atxDb,
nextValidLayers: make(map[types.LayerID]*types.Layer),
}
ll.Validator = &validator{ll, 0}
return ll
}
// NewRecoveredMesh creates new instance of mesh with recovered mesh data fom database
func NewRecoveredMesh(db *DB, atxDb AtxDB, rewardConfig Config, mesh tortoise, txInvalidator txMemPoolInValidator, atxInvalidator atxMemPoolInValidator, pr txProcessor, logger log.Log) *Mesh {
msh := NewMesh(db, atxDb, rewardConfig, mesh, txInvalidator, atxInvalidator, pr, logger)
latest, err := db.general.Get(constLATEST)
if err != nil {
logger.Panic("could not recover latest layer: %v", err)
}
msh.latestLayer = types.LayerID(util.BytesToUint64(latest))
processed, err := db.general.Get(constPROCESSED)
if err != nil {
logger.Panic("could not recover processed layer: %v", err)
}
msh.SetProcessedLayer(types.LayerID(util.BytesToUint64(processed)))
if msh.layerHash, err = db.general.Get(constLAYERHASH); err != nil {
logger.With().Error("could not recover latest layer hash", log.Err(err))
}
verified, err := db.general.Get(VERIFIED)
if err != nil {
logger.Panic("could not recover latest verified layer: %v", err)
}
msh.latestLayerInState = types.LayerID(util.BytesToUint64(verified))
err = pr.LoadState(msh.LatestLayerInState())
if err != nil {
logger.Panic("cannot load state for layer %v, message: %v", msh.LatestLayerInState(), err)
}
// in case we load a state that was not fully played
if msh.LatestLayerInState()+1 < msh.trtl.LatestComplete() {
// todo: add test for this case, or add random kill test on node
logger.Info("playing layers %v to %v to state", msh.LatestLayerInState()+1, msh.trtl.LatestComplete())
msh.pushLayersToState(msh.LatestLayerInState()+1, msh.trtl.LatestComplete())
}
msh.With().Info("recovered mesh from disc",
log.Uint64("latest_layer", msh.latestLayer.Uint64()),
log.Uint64("validated_layer", msh.ProcessedLayer().Uint64()),
log.String("layer_hash", util.Bytes2Hex(msh.layerHash)),
log.String("root_hash", pr.GetStateRoot().String()))
return msh
}
// CacheWarmUp warms up cache with latest blocks
func (msh *Mesh) CacheWarmUp(layerSize int) {
start := types.LayerID(0)
if msh.ProcessedLayer() > types.LayerID(msh.blockCache.Cap()/layerSize) {
start = msh.ProcessedLayer() - types.LayerID(msh.blockCache.Cap()/layerSize)
}
if err := msh.cacheWarmUpFromTo(start, msh.ProcessedLayer()); err != nil {
msh.Error("cache warm up failed during recovery", err)
}
msh.Info("cache warm up done")
}
// SetBlockBuilder sets the block builder in use by mesh
func (msh *Mesh) SetBlockBuilder(blockBuilder blockBuilder) {
msh.blockBuilder = blockBuilder
}
// LatestLayerInState returns the latest layer we applied to state
func (msh *Mesh) LatestLayerInState() types.LayerID {
defer msh.pMutex.RUnlock()
msh.pMutex.RLock()
return msh.latestLayerInState
}
// LatestLayer - returns the latest layer we saw from the network
func (msh *Mesh) LatestLayer() types.LayerID {
defer msh.lkMutex.RUnlock()
msh.lkMutex.RLock()
return msh.latestLayer
}
// SetLatestLayer sets the latest layer we saw from the network
func (msh *Mesh) SetLatestLayer(idx types.LayerID) {
defer msh.lkMutex.Unlock()
msh.lkMutex.Lock()
if idx > msh.latestLayer {
msh.Info("set latest known layer to %v", idx)
msh.latestLayer = idx
if err := msh.general.Put(constLATEST, idx.ToBytes()); err != nil {
msh.Error("could not persist Latest layer index")
}
}
}
// GetLayer returns Layer i from the database
func (msh *Mesh) GetLayer(i types.LayerID) (*types.Layer, error) {
mBlocks, err := msh.LayerBlocks(i)
if err != nil {
return nil, err
}
l := types.NewLayer(i)
l.SetBlocks(mBlocks)
return l, nil
}
type validator struct {
*Mesh
processedLayer types.LayerID
}
func (vl *validator) ProcessedLayer() types.LayerID {
defer vl.lvMutex.RUnlock()
vl.lvMutex.RLock()
return vl.processedLayer
}
func (vl *validator) SetProcessedLayer(lyr types.LayerID) {
vl.Info("set processed layer to %d", lyr)
defer vl.lvMutex.Unlock()
vl.lvMutex.Lock()
vl.processedLayer = lyr
}
func (vl *validator) HandleLateBlock(b *types.Block) {
vl.Info("Validate late block %s", b.ID())
oldPbase, newPbase := vl.trtl.HandleLateBlock(b)
if err := vl.trtl.Persist(); err != nil {
vl.Error("could not persist Tortoise on late block %s from layer index %d", b.ID(), b.Layer())
}
vl.pushLayersToState(oldPbase, newPbase)
}
func (vl *validator) ValidateLayer(lyr *types.Layer) {
vl.Info("Validate layer %d", lyr.Index())
if len(lyr.Blocks()) == 0 {
vl.Info("skip validation of layer %d with no blocks", lyr.Index())
vl.SetProcessedLayer(lyr.Index())
return
}
oldPbase, newPbase := vl.trtl.HandleIncomingLayer(lyr)
vl.SetProcessedLayer(lyr.Index())
if err := vl.trtl.Persist(); err != nil {
vl.Error("could not persist tortoise layer index %d", lyr.Index())
}
if err := vl.general.Put(constPROCESSED, lyr.Index().ToBytes()); err != nil {
vl.Error("could not persist validated layer index %d", lyr.Index())
}
vl.pushLayersToState(oldPbase, newPbase)
vl.Info("done validating layer %v", lyr.Index())
}
func (msh *Mesh) pushLayersToState(oldPbase types.LayerID, newPbase types.LayerID) {
for layerID := oldPbase; layerID < newPbase; layerID++ {
l, err := msh.GetLayer(layerID)
// TODO: propagate/handle error
if err != nil || l == nil {
msh.With().Error("failed to get layer", log.LayerID(layerID.Uint64()), log.Err(err))
return
}
validBlocks, invalidBlocks := msh.BlocksByValidity(l.Blocks())
msh.updateStateWithLayer(layerID, types.NewExistingLayer(layerID, validBlocks))
msh.logStateRoot(l.Index())
msh.setLayerHash(l)
msh.reInsertTxsToPool(validBlocks, invalidBlocks, l.Index())
}
msh.persistLayerHash()
}
func (msh *Mesh) reInsertTxsToPool(validBlocks, invalidBlocks []*types.Block, l types.LayerID) {
seenTxIds := make(map[types.TransactionID]struct{})
uniqueTxIds(validBlocks, seenTxIds)
returnedTxs := msh.getTxs(uniqueTxIds(invalidBlocks, seenTxIds), l)
grouped, accounts := msh.removeFromUnappliedTxs(returnedTxs)
for account := range accounts {
msh.removeRejectedFromAccountTxs(account, grouped, l)
}
for _, tx := range returnedTxs {
err := msh.blockBuilder.ValidateAndAddTxToPool(tx)
// We ignore errors here, since they mean that the tx is no longer valid and we shouldn't re-add it
if err == nil {
msh.With().Info("transaction from contextually invalid block re-added to mempool",
log.TxID(tx.ID().ShortString()))
}
}
}
func (msh *Mesh) applyState(l *types.Layer) {
msh.accumulateRewards(l, msh.config)
msh.pushTransactions(l)
msh.setLatestLayerInState(l.Index())
}
// HandleValidatedLayer handles layer valid blocks as decided by hare
func (msh *Mesh) HandleValidatedLayer(validatedLayer types.LayerID, layer []types.BlockID) {
var blocks []*types.Block
for _, blockID := range layer {
block, err := msh.GetBlock(blockID)
if err != nil {
// stop processing this hare result, wait until tortoise pushes this layer into state
log.Error("hare terminated with block that is not present in mesh")
return
}
blocks = append(blocks, block)
}
lyr := types.NewExistingLayer(validatedLayer, blocks)
invalidBlocks := msh.getInvalidBlocksByHare(lyr)
msh.updateStateWithLayer(validatedLayer, lyr)
msh.reInsertTxsToPool(blocks, invalidBlocks, lyr.Index())
}
func (msh *Mesh) getInvalidBlocksByHare(hareLayer *types.Layer) (invalid []*types.Block) {
dbLayer, err := msh.GetLayer(hareLayer.Index())
if err != nil {
log.Panic("wtf")
return
}
exists := make(map[types.BlockID]struct{})
for _, block := range hareLayer.Blocks() {
exists[block.ID()] = struct{}{}
}
for _, block := range dbLayer.Blocks() {
if _, has := exists[block.ID()]; !has {
invalid = append(invalid, block)
}
}
return
}
func (msh *Mesh) updateStateWithLayer(validatedLayer types.LayerID, layer *types.Layer) {
msh.txMutex.Lock()
defer msh.txMutex.Unlock()
latest := msh.LatestLayerInState()
if validatedLayer <= latest {
log.Info("result received after state has been advanced for layer %v, latest: %v", validatedLayer, latest)
return
}
if msh.maxValidatedLayer < validatedLayer {
msh.maxValidatedLayer = validatedLayer
}
if validatedLayer > latest+1 {
log.Info("early layer result was received for layer %v, max validated so far %v", validatedLayer, msh.maxValidatedLayer)
msh.nextValidLayers[validatedLayer] = layer
return
}
msh.applyState(layer)
for i := validatedLayer + 1; i <= msh.maxValidatedLayer; i++ {
nxtLayer, has := msh.nextValidLayers[i]
if !has {
break
}
msh.applyState(nxtLayer)
delete(msh.nextValidLayers, i)
}
}
func (msh *Mesh) setLatestLayerInState(lyr types.LayerID) {
// update validated layer only after applying transactions since loading of state depends on processedLayer param.
msh.pMutex.Lock()
if err := msh.general.Put(VERIFIED, lyr.ToBytes()); err != nil {
msh.Panic("could not persist validated layer index %d", lyr)
}
msh.latestLayerInState = lyr
msh.pMutex.Unlock()
}
func (msh *Mesh) logStateRoot(layerID types.LayerID) {
msh.Event().Info("end of layer state root",
log.LayerID(layerID.Uint64()),
log.String("state_root", util.Bytes2Hex(msh.txProcessor.GetStateRoot().Bytes())),
)
}
func (msh *Mesh) setLayerHash(layer *types.Layer) {
validBlocks, _ := msh.BlocksByValidity(layer.Blocks())
msh.layerHash = types.CalcBlocksHash32(types.BlockIDs(validBlocks), msh.layerHash).Bytes()
msh.Event().Info("new layer hash",
log.LayerID(layer.Index().Uint64()),
log.String("layer_hash", util.Bytes2Hex(msh.layerHash)))
}
func (msh *Mesh) persistLayerHash() {
if err := msh.general.Put(constLAYERHASH, msh.layerHash); err != nil {
msh.With().Error("failed to persist layer hash", log.Err(err), log.LayerID(msh.ProcessedLayer().Uint64()),
log.String("layer_hash", util.Bytes2Hex(msh.layerHash)))
}
}
func (msh *Mesh) extractUniqueOrderedTransactions(l *types.Layer) (validBlockTxs []*types.Transaction) {
validBlocks := l.Blocks()
// Deterministically sort valid blocks
types.SortBlocks(validBlocks)
// Initialize a Mersenne Twister seeded with layerHash
blockHash := types.CalcBlockHash32Presorted(types.BlockIDs(validBlocks), nil)
mt := mt19937.New()
mt.SeedFromSlice(toUint64Slice(blockHash.Bytes()))
rng := rand.New(mt)
// Perform a Fisher-Yates shuffle on the blocks
rng.Shuffle(len(validBlocks), func(i, j int) {
validBlocks[i], validBlocks[j] = validBlocks[j], validBlocks[i]
})
// Get and return unique transactions
seenTxIds := make(map[types.TransactionID]struct{})
return msh.getTxs(uniqueTxIds(validBlocks, seenTxIds), l.Index())
}
func toUint64Slice(b []byte) []uint64 {
l := len(b)
var s []uint64
for i := 0; i < l; i += 8 {
s = append(s, binary.LittleEndian.Uint64(b[i:util.Min(l, i+8)]))
}
return s
}
func uniqueTxIds(blocks []*types.Block, seenTxIds map[types.TransactionID]struct{}) []types.TransactionID {
var txIds []types.TransactionID
for _, b := range blocks {
for _, id := range b.TxIDs {
if _, found := seenTxIds[id]; found {
continue
}
txIds = append(txIds, id)
seenTxIds[id] = struct{}{}
}
}
return txIds
}
func (msh *Mesh) getTxs(txIds []types.TransactionID, l types.LayerID) []*types.Transaction {
txs, missing := msh.GetTransactions(txIds)
if len(missing) != 0 {
msh.Panic("could not find transactions %v from layer %v", missing, l)
}
return txs
}
func (msh *Mesh) pushTransactions(l *types.Layer) {
validBlockTxs := msh.extractUniqueOrderedTransactions(l)
numFailedTxs, err := msh.ApplyTransactions(l.Index(), validBlockTxs)
if err != nil {
msh.With().Error("failed to apply transactions",
log.LayerID(l.Index().Uint64()), log.Int("num_failed_txs", numFailedTxs), log.Err(err))
// TODO: We want to panic here once we have a way to "remember" that we didn't apply these txs
// e.g. persist the last layer transactions were applied from and use that instead of `oldBase`
}
msh.removeFromUnappliedTxs(validBlockTxs)
msh.With().Info("applied transactions",
log.Int("valid_block_txs", len(validBlockTxs)),
log.LayerID(l.Index().Uint64()),
log.Int("num_failed_txs", numFailedTxs),
)
}
// GetProcessedLayer returns a layer only if it has already been processed
func (msh *Mesh) GetProcessedLayer(i types.LayerID) (*types.Layer, error) {
msh.lMutex.RLock()
if i > msh.ProcessedLayer() {
msh.lMutex.RUnlock()
msh.Debug("failed to get layer ", i, " layer not verified yet")
return nil, errors.New("layer not verified yet")
}
msh.lMutex.RUnlock()
return msh.GetLayer(i)
}
// AddBlock adds a block to the database ignoring the block txs/atxs
// ***USED ONLY FOR TESTS***
func (msh *Mesh) AddBlock(blk *types.Block) error {
msh.Debug("add block %d", blk.ID())
if err := msh.DB.AddBlock(blk); err != nil {
msh.Warning("failed to add block %v %v", blk.ID(), err)
return err
}
msh.SetLatestLayer(blk.Layer())
// new block add to orphans
msh.handleOrphanBlocks(blk)
// invalidate txs and atxs from pool
msh.invalidateFromPools(&blk.MiniBlock)
return nil
}
// SetZeroBlockLayer tags lyr as a layer without blocks
func (msh *Mesh) SetZeroBlockLayer(lyr types.LayerID) error {
// check database for layer
_, err := msh.GetLayer(lyr)
if err == nil {
// layer exists
msh.Info("layer has blocks, dont set layer to 0 ")
return fmt.Errorf("layer exists")
}
if err != database.ErrNotFound {
// database error
return fmt.Errorf("could not fetch layer from database %s", err)
}
msh.SetLatestLayer(lyr)
lm := msh.getLayerMutex(lyr)
defer msh.endLayerWorker(lyr)
lm.m.Lock()
defer lm.m.Unlock()
// layer doesnt exist, need to insert new layer
blockIds := make([]types.BlockID, 0, 1)
w, err := types.BlockIdsAsBytes(blockIds)
if err != nil {
return errors.New("could not encode layer blk ids")
}
err = msh.layers.Put(lyr.ToBytes(), w)
return err
}
// AddBlockWithTxs adds a block to the database
// blk - the block to add
// txs - block txs that we dont have in our tx database yet
// atxs - block atxs that we dont have in our atx database yet
func (msh *Mesh) AddBlockWithTxs(blk *types.Block, txs []*types.Transaction, atxs []*types.ActivationTx) error {
msh.With().Debug("adding block", log.BlockID(blk.ID().String()))
// Store transactions (doesn't have to be rolled back if other writes fail)
if len(txs) > 0 {
if err := msh.writeTransactions(blk.LayerIndex, txs); err != nil {
return fmt.Errorf("could not write transactions of block %v database: %v", blk.ID(), err)
}
if err := msh.addToUnappliedTxs(txs, blk.LayerIndex); err != nil {
return fmt.Errorf("failed to add to unappliedTxs: %v", err)
}
}
// Store block (delete if storing ATXs fails)
if err := msh.DB.AddBlock(blk); err != nil && err != ErrAlreadyExist {
msh.With().Error("failed to add block", log.BlockID(blk.ID().String()), log.Err(err))
return err
}
// Store ATXs (atomically, delete the block on failure)
if err := msh.AtxDB.ProcessAtxs(atxs); err != nil {
// Roll back adding the block (delete it)
if err := msh.blocks.Delete(blk.ID().ToBytes()); err != nil {
msh.With().Warning("failed to roll back adding a block", log.Err(err), log.BlockID(blk.ID().String()))
}
return fmt.Errorf("failed to process ATXs: %v", err)
}
msh.SetLatestLayer(blk.Layer())
// new block add to orphans
msh.handleOrphanBlocks(blk)
// invalidate txs and atxs from pool
msh.invalidateFromPools(&blk.MiniBlock)
events.Publish(events.NewBlock{ID: blk.ID().String(), Atx: blk.ATXID.ShortString(), Layer: uint64(blk.LayerIndex)})
msh.With().Info("added block to database ", log.BlockID(blk.ID().String()), log.LayerID(uint64(blk.LayerIndex)))
return nil
}
func (msh *Mesh) invalidateFromPools(blk *types.MiniBlock) {
for _, id := range blk.TxIDs {
msh.txInvalidator.Invalidate(id)
}
msh.atxInvalidator.Invalidate(blk.ATXID)
for _, id := range blk.ATXIDs {
msh.atxInvalidator.Invalidate(id)
}
}
// todo better thread safety
func (msh *Mesh) handleOrphanBlocks(blk *types.Block) {
msh.orphMutex.Lock()
defer msh.orphMutex.Unlock()
if _, ok := msh.orphanBlocks[blk.Layer()]; !ok {
msh.orphanBlocks[blk.Layer()] = make(map[types.BlockID]struct{})
}
msh.orphanBlocks[blk.Layer()][blk.ID()] = struct{}{}
msh.Debug("Added block %s to orphans", blk.ID())
for _, b := range blk.ViewEdges {
for layerID, layermap := range msh.orphanBlocks {
if _, has := layermap[b]; has {
msh.Log.Debug("delete block ", b, "from orphans")
delete(layermap, b)
if len(layermap) == 0 {
delete(msh.orphanBlocks, layerID)
}
break
}
}
}
}
// GetOrphanBlocksBefore returns all known orphan blocks with layerID < l
func (msh *Mesh) GetOrphanBlocksBefore(l types.LayerID) ([]types.BlockID, error) {
msh.orphMutex.RLock()
defer msh.orphMutex.RUnlock()
ids := map[types.BlockID]struct{}{}
for key, val := range msh.orphanBlocks {
if key < l {
for bid := range val {
ids[bid] = struct{}{}
}
}
}
blocks, err := msh.LayerBlockIds(l - 1)
if err != nil {
return nil, errors.New(fmt.Sprint("failed getting latest layer ", err))
}
// add last layer blocks
for _, b := range blocks {
ids[b] = struct{}{}
}
idArr := make([]types.BlockID, 0, len(ids))
for i := range ids {
idArr = append(idArr, i)
}
idArr = types.SortBlockIDs(idArr)
msh.Info("orphans for layer %d are %v", l, idArr)
return idArr, nil
}
func (msh *Mesh) accumulateRewards(l *types.Layer, params Config) {
ids := make([]types.Address, 0, len(l.Blocks()))
for _, bl := range l.Blocks() {
if bl.ATXID == *types.EmptyATXID {
msh.With().Info("skipping reward distribution for block with no ATX",
log.LayerID(uint64(bl.LayerIndex)), log.BlockID(bl.ID().String()))
continue
}
atx, err := msh.AtxDB.GetAtxHeader(bl.ATXID)
if err != nil {
msh.With().Warning("Atx from block not found in db", log.Err(err), log.BlockID(bl.ID().String()), log.AtxID(bl.ATXID.ShortString()))
continue
}
ids = append(ids, atx.Coinbase)
}
if len(ids) == 0 {
msh.With().Info("no valid blocks for layer ", log.LayerID(uint64(l.Index())))
return
}
// aggregate all blocks' rewards
txs := msh.extractUniqueOrderedTransactions(l)
totalReward := &big.Int{}
for _, tx := range txs {
totalReward.Add(totalReward, new(big.Int).SetUint64(tx.Fee))
}
layerReward := calculateLayerReward(l.Index(), params)
totalReward.Add(totalReward, layerReward)
numBlocks := big.NewInt(int64(len(ids)))
blockTotalReward := calculateActualRewards(l.Index(), totalReward, numBlocks)
msh.ApplyRewards(l.Index(), ids, blockTotalReward)
blockLayerReward := calculateActualRewards(l.Index(), layerReward, numBlocks)
err := msh.writeTransactionRewards(l.Index(), ids, blockTotalReward, blockLayerReward)
if err != nil {
msh.Error("cannot write reward to db")
}
// todo: should miner id be sorted in a deterministic order prior to applying rewards?
}
// GenesisBlock is a mock genesis block used until genesis flow is implemented
var GenesisBlock = types.NewExistingBlock(0, []byte("genesis"))
// GenesisLayer generates layer 0 should be removed after the genesis flow is implemented
func GenesisLayer() *types.Layer {
l := types.NewLayer(Genesis)
l.AddBlock(GenesisBlock)
return l
}
// GetATXs uses GetFullAtx to return a list of atxs corresponding to atxIds requested
func (msh *Mesh) GetATXs(atxIds []types.ATXID) (map[types.ATXID]*types.ActivationTx, []types.ATXID) {
var mIds []types.ATXID
atxs := make(map[types.ATXID]*types.ActivationTx, len(atxIds))
for _, id := range atxIds {
t, err := msh.GetFullAtx(id)
if err != nil {
msh.Warning("could not get atx %v from database, %v", id.ShortString(), err)
mIds = append(mIds, id)
} else {
atxs[t.ID()] = t
}
}
return atxs, mIds
}
// NewSignedTx is used in TESTS ONLY to generate signed txs
func NewSignedTx(nonce uint64, rec types.Address, amount, gas, fee uint64, signer *signing.EdSigner) (*types.Transaction, error) {
inner := types.InnerTransaction{
AccountNonce: nonce,
Recipient: rec,
Amount: amount,
GasLimit: gas,
Fee: fee,
}
buf, err := types.InterfaceToBytes(&inner)
if err != nil {
return nil, err
}
sst := &types.Transaction{
InnerTransaction: inner,
Signature: [64]byte{},
}
copy(sst.Signature[:], signer.Sign(buf))
addr := types.Address{}
addr.SetBytes(signer.PublicKey().Bytes())
sst.SetOrigin(addr)
return sst, nil
}