-
Notifications
You must be signed in to change notification settings - Fork 211
/
mesh.go
985 lines (877 loc) · 32.9 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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
// Package mesh defines the main store point for all the block-mesh objects
// such as blocks, transactions and global state
package mesh
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"math/big"
"math/rand"
"sync"
"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"
)
const (
layerSize = 200
)
var (
constTrue = []byte{1}
constFalse = []byte{0}
constLATEST = []byte("latest")
constPROCESSED = []byte("processed")
)
// VERIFIED refers to layers we pushed into the state
var VERIFIED = []byte("verified")
type tortoise interface {
HandleIncomingLayer(context.Context, types.LayerID) (oldPbase, newPbase types.LayerID, reverted bool)
LatestComplete() types.LayerID
Persist(context.Context) error
HandleLateBlocks(context.Context, []*types.Block) (types.LayerID, types.LayerID)
}
// Validator interface to be used in tests to mock validation flow
type Validator interface {
ValidateLayer(context.Context, *types.Layer)
}
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
GetLayerStateRoot(layer types.LayerID) (types.Hash32, error)
GetStateRoot() types.Hash32
LoadState(layer types.LayerID) error
ValidateAndAddTxToPool(tx *types.Transaction) error
GetBalance(addr types.Address) uint64
GetNonce(addr types.Address) uint64
GetAllAccounts() (*types.MultipleAccountsState, error)
}
type txMemPool interface {
Invalidate(id types.TransactionID)
Get(id types.TransactionID) (*types.Transaction, error)
Put(id types.TransactionID, tx *types.Transaction)
}
// AtxDB holds logic for working with atxs
type AtxDB interface {
GetAtxHeader(id types.ATXID) (*types.ActivationTxHeader, error)
GetFullAtx(id types.ATXID) (*types.ActivationTx, error)
SyntacticallyValidateAtx(ctx context.Context, atx *types.ActivationTx) error
}
// Mesh is the logic layer above our mesh.DB database
type Mesh struct {
log.Log
*DB
AtxDB
txProcessor
Validator
trtl tortoise
txPool txMemPool
config Config
// latestLayer is the latest layer this node had seen from blocks
latestLayer types.LayerID
// latestLayerInState is the latest layer whose contents have been applied to the state
latestLayerInState types.LayerID
// processedLayer is the latest layer whose votes have been processed
processedLayer types.LayerID
nextProcessedLayers map[types.LayerID]struct{}
maxProcessedLayer types.LayerID
mutex 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, trtl tortoise, txPool txMemPool, pr txProcessor, logger log.Log) *Mesh {
msh := &Mesh{
Log: logger,
trtl: trtl,
txPool: txPool,
txProcessor: pr,
done: make(chan struct{}),
DB: db,
config: rewardConfig,
AtxDB: atxDb,
nextProcessedLayers: make(map[types.LayerID]struct{}),
nextValidLayers: make(map[types.LayerID]*types.Layer),
latestLayer: types.GetEffectiveGenesis(),
latestLayerInState: types.GetEffectiveGenesis(),
}
msh.Validator = &validator{Mesh: msh}
gLyr := types.GetEffectiveGenesis()
for i := types.NewLayerID(1); i.Before(gLyr); i = i.Add(1) {
if err := msh.SetZeroBlockLayer(i); err != nil {
msh.With().Panic("failed to set zero-block for genesis layer", i, log.Err(err))
}
}
if err := msh.persistLayerHashes(context.Background(), types.NewLayerID(1), gLyr); err != nil {
msh.With().Panic("failed to persist hashes for genesis layers", log.Err(err))
}
return msh
}
// NewRecoveredMesh creates new instance of mesh with recovered mesh data fom database
func NewRecoveredMesh(ctx context.Context, db *DB, atxDb AtxDB, rewardConfig Config, trtl tortoise, txPool txMemPool, pr txProcessor, logger log.Log) *Mesh {
msh := NewMesh(db, atxDb, rewardConfig, trtl, txPool, pr, logger)
latest, err := db.general.Get(constLATEST)
if err != nil {
logger.With().Panic("failed to recover latest layer", log.Err(err))
}
msh.setLatestLayer(types.BytesToLayerID(latest))
lyr, err := msh.recoverProcessedLayer()
if err != nil {
logger.With().Panic("failed to recover processed layer", log.Err(err))
}
msh.setProcessedLayerFromRecoveredData(lyr)
verified, err := db.general.Get(VERIFIED)
if err != nil {
logger.With().Panic("failed to recover latest verified layer", log.Err(err))
}
if err = msh.setLatestLayerInState(types.BytesToLayerID(verified)); err != nil {
logger.With().Panic("failed to recover latest layer in state", log.Err(err))
}
err = pr.LoadState(msh.LatestLayerInState())
if err != nil {
logger.With().Panic("failed to load state for layer", msh.LatestLayerInState(), log.Err(err))
}
// in case we load a state that was not fully played
nextLayer := msh.LatestLayerInState().Add(1)
lastComplete := msh.trtl.LatestComplete()
if nextLayer.Before(msh.trtl.LatestComplete()) {
// todo: add test for this case, or add random kill test on node
logger.With().Info("playing layers to state",
log.FieldNamed("from_layer", nextLayer),
log.FieldNamed("to_layer", lastComplete))
msh.pushLayersToState(ctx, nextLayer, lastComplete)
}
msh.With().Info("recovered mesh from disk",
log.FieldNamed("latest", msh.LatestLayer()),
log.FieldNamed("processed", msh.ProcessedLayer()),
log.FieldNamed("verified", msh.trtl.LatestComplete()),
log.String("root_hash", pr.GetStateRoot().String()))
return msh
}
// CacheWarmUp warms up cache with latest blocks
func (msh *Mesh) CacheWarmUp(layerSize int) {
start := types.NewLayerID(0)
if msh.ProcessedLayer().Uint32() > uint32(msh.blockCache.Cap()/layerSize) {
start = msh.ProcessedLayer().Sub(uint32(msh.blockCache.Cap() / layerSize))
}
if err := msh.cacheWarmUpFromTo(start, msh.ProcessedLayer()); err != nil {
msh.With().Error("cache warm up failed during recovery", log.Err(err))
}
msh.Info("cache warm up done")
}
// LatestLayerInState returns the latest layer we applied to state
func (msh *Mesh) LatestLayerInState() types.LayerID {
defer msh.mutex.RUnlock()
msh.mutex.RLock()
return msh.latestLayerInState
}
// LatestLayer - returns the latest layer we saw from the network
func (msh *Mesh) LatestLayer() types.LayerID {
defer msh.mutex.RUnlock()
msh.mutex.RLock()
return msh.latestLayer
}
// setLatestLayer sets the latest layer we saw from the network
func (msh *Mesh) setLatestLayer(idx types.LayerID) {
events.ReportLayerUpdate(events.LayerUpdate{
LayerID: idx,
Status: events.LayerStatusTypeUnknown,
})
defer msh.mutex.Unlock()
msh.mutex.Lock()
if idx.After(msh.latestLayer) {
events.ReportNodeStatusUpdate()
msh.With().Info("set latest known layer", idx)
msh.latestLayer = idx
if err := msh.general.Put(constLATEST, idx.Bytes()); 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
}
// GetLayerHash returns layer hash for received blocks
func (msh *Mesh) GetLayerHash(layerID types.LayerID) types.Hash32 {
h, err := msh.recoverLayerHash(layerID)
if err == nil {
return h
}
if err == database.ErrNotFound {
// layer hash not persisted. i.e. contextual validity not yet determined
lyr, err := msh.GetLayer(layerID)
if err == nil {
return lyr.Hash()
}
}
return types.EmptyLayerHash
}
// ProcessedLayer returns the last processed layer ID
func (msh *Mesh) ProcessedLayer() types.LayerID {
msh.mutex.RLock()
defer msh.mutex.RUnlock()
return msh.processedLayer
}
func (msh *Mesh) setProcessedLayerFromRecoveredData(pLayer types.LayerID) {
msh.mutex.Lock()
defer msh.mutex.Unlock()
msh.processedLayer = pLayer
msh.Event().Info("processed layer set from recovered data", pLayer)
}
func (msh *Mesh) setProcessedLayer(layer *types.Layer) {
msh.mutex.Lock()
defer msh.mutex.Unlock()
if !layer.Index().After(msh.processedLayer) {
msh.With().Info("trying to set processed layer to an older layer",
log.FieldNamed("processed", msh.processedLayer),
layer.Index())
return
}
if layer.Index().After(msh.maxProcessedLayer) {
msh.maxProcessedLayer = layer.Index()
}
if layer.Index() != msh.processedLayer.Add(1) {
msh.With().Info("trying to set processed layer out of order",
log.FieldNamed("processed", msh.processedLayer),
layer.Index())
msh.nextProcessedLayers[layer.Index()] = struct{}{}
return
}
msh.nextProcessedLayers[layer.Index()] = struct{}{}
lastProcessed := msh.processedLayer
for i := layer.Index(); !i.After(msh.maxProcessedLayer); i = i.Add(1) {
_, ok := msh.nextProcessedLayers[i]
if !ok {
break
}
lastProcessed = i
delete(msh.nextProcessedLayers, i)
}
msh.processedLayer = lastProcessed
events.ReportNodeStatusUpdate()
msh.Event().Info("processed layer set", msh.processedLayer)
if err := msh.persistProcessedLayer(msh.processedLayer); err != nil {
msh.With().Error("failed to persist processed layer",
log.FieldNamed("processed", lastProcessed),
log.Err(err))
}
}
type validator struct {
*Mesh
}
// ValidateLayer performs fairly heavy lifting: it triggers tortoise to process the full contents of the layer (i.e.,
// all of its blocks), then to attempt to validate all unvalidated layers up to this layer. It also applies state for
// newly-validated layers.
// TODO: rename this. When tortoise passes a layer, we call that "verify." "Validate" sounds too similar and it's
// confusing that a layer can be validated without being verified.
// See https://github.com/spacemeshos/go-spacemesh/issues/2669
func (vl *validator) ValidateLayer(ctx context.Context, lyr *types.Layer) {
layerID := lyr.Index()
logger := vl.WithContext(ctx).WithFields(layerID)
logger.Info("validate layer")
// pass the layer to tortoise for processing
oldPbase, newPbase, reverted := vl.trtl.HandleIncomingLayer(ctx, layerID)
logger.With().Info("tortoise results",
log.Bool("reverted", reverted),
log.FieldNamed("old_pbase", oldPbase),
log.FieldNamed("new_pbase", newPbase))
// check for a state reversion: if tortoise reran and detected changes to historical data, it will request that
// state be reverted and reapplied. pushLayersToState, below, will handle the reapplication.
if reverted {
if err := vl.revertState(ctx, oldPbase); err != nil {
logger.With().Error("failed to revert state, unable to validate layer", log.Err(err))
return
}
}
if err := vl.trtl.Persist(ctx); err != nil {
logger.With().Error("could not persist tortoise", log.Err(err))
}
vl.pushLayersToState(ctx, oldPbase, newPbase)
if newPbase.After(oldPbase) {
if err := vl.persistLayerHashes(ctx, oldPbase.Add(1), newPbase); err != nil {
logger.With().Error("failed to persist layer hashes", log.Err(err))
}
}
vl.setProcessedLayer(lyr)
for newlyVerifiedLayer := oldPbase.Add(1); !newlyVerifiedLayer.After(newPbase); newlyVerifiedLayer = newlyVerifiedLayer.Add(1) {
events.ReportLayerUpdate(events.LayerUpdate{
LayerID: newlyVerifiedLayer,
Status: events.LayerStatusTypeConfirmed,
})
}
logger.Info("done validating layer")
}
func (msh *Mesh) persistLayerHashes(ctx context.Context, from, to types.LayerID) error {
logger := msh.WithContext(ctx)
if to.Before(from) {
logger.With().Panic("verified layer went backward",
log.FieldNamed("fromLayer", from),
log.FieldNamed("toLayer", to))
}
logger.With().Debug("persisting layer hashes",
log.FieldNamed("from_layer", from),
log.FieldNamed("to_layer", to))
for i := from; !i.After(to); i = i.Add(1) {
validBlockIDs, err := msh.getValidBlockIDs(ctx, i)
if err != nil {
logger.With().Error("failed to get valid block IDs", i, log.Err(err))
return err
}
hash := types.EmptyLayerHash
if len(validBlockIDs) > 0 {
hash = types.CalcBlocksHash32(validBlockIDs, nil)
}
if err := msh.persistLayerHash(i, hash); err != nil {
logger.With().Error("failed to persist layer hash", i, log.Err(err))
return err
}
prevHash, err := msh.getAggregatedLayerHash(i.Sub(1))
if err != nil {
logger.With().Debug("failed to get previous aggregated hash", i, log.Err(err))
return err
}
logger.With().Debug("got previous aggregatedHash", i, log.String("prevAggHash", prevHash.ShortString()))
newAggHash := types.CalcBlocksHash32(validBlockIDs, prevHash.Bytes())
if err := msh.persistAggregatedLayerHash(i, newAggHash); err != nil {
logger.With().Error("failed to persist aggregated layer hash", i, log.Err(err))
return err
}
logger.With().Info("aggregated hash updated for layer",
i,
log.String("hash", hash.ShortString()),
log.String("aggHash", newAggHash.ShortString()))
}
return nil
}
func (msh *Mesh) getValidBlockIDs(ctx context.Context, layerID types.LayerID) ([]types.BlockID, error) {
logger := msh.WithContext(ctx)
lyr, err := msh.GetLayer(layerID)
if err != nil {
logger.With().Error("failed to get layer", layerID, log.Err(err))
return nil, err
}
var validBlockIDs []types.BlockID
for _, bID := range lyr.BlocksIDs() {
valid, err := msh.ContextualValidity(bID)
if err != nil {
// block contextual validity is determined by layer. if one block in the layer is not determined,
// the whole layer is not yet verified.
logger.With().Warning("block contextual validity not yet determined", layerID, bID, log.Err(err))
return nil, err
}
if valid {
validBlockIDs = append(validBlockIDs, bID)
}
}
return validBlockIDs, nil
}
// HandleLateBlock process a late (contextually invalid) block.
func (msh *Mesh) HandleLateBlock(ctx context.Context, b *types.Block) {
msh.WithContext(ctx).With().Info("validate late block", b.ID())
// TODO: handle late blocks in batches, see https://github.com/spacemeshos/go-spacemesh/issues/2412
oldPbase, newPbase := msh.trtl.HandleLateBlocks(ctx, []*types.Block{b})
if err := msh.trtl.Persist(ctx); err != nil {
msh.WithContext(ctx).With().Error("could not persist tortoise on late block", b.ID(), b.Layer())
}
msh.pushLayersToState(ctx, oldPbase, newPbase)
}
// apply the state of a range of layers, including re-adding transactions from invalid blocks to the mempool
func (msh *Mesh) pushLayersToState(ctx context.Context, oldPbase, newPbase types.LayerID) {
logger := msh.WithContext(ctx).WithFields(
log.FieldNamed("old_pbase", oldPbase),
log.FieldNamed("new_pbase", newPbase))
logger.Info("pushing layers to state")
// TODO: does this need to be hardcoded? can we use types.GetEffectiveGenesis instead?
// see https://github.com/spacemeshos/go-spacemesh/issues/2670
layerTwo := types.NewLayerID(2)
if oldPbase.Before(layerTwo) {
msh.With().Warning("tried to push layer < 2",
log.FieldNamed("old_pbase", oldPbase),
log.FieldNamed("new_pbase", newPbase))
if newPbase.Before(types.NewLayerID(3)) {
return
}
oldPbase = layerTwo.Sub(1) // since we add one, below
}
// we never reapply the state of oldPbase. note that state reversions must be handled separately.
for layerID := oldPbase.Add(1); !layerID.After(newPbase); layerID = layerID.Add(1) {
l, err := msh.GetLayer(layerID)
// TODO: propagate/handle error
if err != nil || l == nil {
if layerID.GetEpoch().IsGenesis() {
logger.With().Info("failed to get layer (expected for genesis layers)", layerID, log.Err(err))
} else {
logger.With().Error("failed to get layer", layerID, log.Err(err))
}
return
}
validBlocks, invalidBlocks := msh.BlocksByValidity(l.Blocks())
msh.updateStateWithLayer(ctx, types.NewExistingLayer(layerID, validBlocks))
msh.Event().Info("end of layer state root",
layerID,
log.String("state_root", util.Bytes2Hex(msh.txProcessor.GetStateRoot().Bytes())),
)
msh.reInsertTxsToPool(validBlocks, invalidBlocks, l.Index())
}
}
// RevertState reverts to state as of a previous layer
func (msh *Mesh) revertState(ctx context.Context, layerID types.LayerID) error {
logger := msh.WithContext(ctx).WithFields(layerID)
logger.Info("attempting to roll back state to previous layer")
if err := msh.LoadState(layerID); err != nil {
return fmt.Errorf("failed to revert state to layer %v: %w", layerID, err)
}
return nil
}
func (msh *Mesh) reInsertTxsToPool(validBlocks, invalidBlocks []*types.Block, l types.LayerID) {
seenTxIds := make(map[types.TransactionID]struct{})
uniqueTxIds(validBlocks, seenTxIds) // run for the side effect, updating 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 {
if err := msh.ValidateAndAddTxToPool(tx); err == nil {
// We ignore errors here, since they mean that the tx is no longer valid and we shouldn't re-add it
msh.With().Info("transaction from contextually invalid block re-added to mempool", tx.ID())
}
}
}
func (msh *Mesh) applyState(l *types.Layer) {
msh.accumulateRewards(l, msh.config)
msh.pushTransactions(l)
msh.setLatestLayerInState(l.Index())
}
// HandleValidatedLayer receives hare output once it finishes running for a given layer
func (msh *Mesh) HandleValidatedLayer(ctx context.Context, validatedLayer types.LayerID, layer []types.BlockID) {
logger := msh.WithContext(ctx).WithFields(validatedLayer)
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
logger.Error("hare terminated with block that is not present in mesh")
return
}
blocks = append(blocks, block)
}
// report that hare "approved" this layer
events.ReportLayerUpdate(events.LayerUpdate{
LayerID: validatedLayer,
Status: events.LayerStatusTypeApproved,
})
logger.With().Info("saving input vector for layer", log.Int("count_valid_blocks", len(blocks)))
if err := msh.SaveLayerInputVectorByID(ctx, validatedLayer, types.BlockIDs(blocks)); err != nil {
logger.Error("saving layer input vector failed")
}
lyr := types.NewExistingLayer(validatedLayer, blocks)
msh.ValidateLayer(ctx, lyr)
}
func (msh *Mesh) getInvalidBlocksByHare(ctx context.Context, hareLayer *types.Layer) (invalid []*types.Block) {
dbLayer, err := msh.GetLayer(hareLayer.Index())
if err != nil {
msh.WithContext(ctx).With().Error("failed to get layer", log.Err(err))
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
}
// apply the state for a single layer. stores layer results for early layers, and applies previously stored results for
// now-older layers.
func (msh *Mesh) updateStateWithLayer(ctx context.Context, layer *types.Layer) {
msh.txMutex.Lock()
defer msh.txMutex.Unlock()
latest := msh.LatestLayerInState()
if !layer.Index().After(latest) {
msh.WithContext(ctx).With().Warning("result received after state has advanced, late block received?",
log.FieldNamed("validated", layer.Index()),
log.FieldNamed("latest", latest))
return
}
if msh.maxValidatedLayer.Before(layer.Index()) {
msh.maxValidatedLayer = layer.Index()
}
if layer.Index().After(latest.Add(1)) {
msh.WithContext(ctx).With().Warning("early layer result received",
log.FieldNamed("validated", layer.Index()),
log.FieldNamed("max_validated", msh.maxValidatedLayer),
log.FieldNamed("latest", latest))
msh.nextValidLayers[layer.Index()] = layer
return
}
msh.applyState(layer)
for i := layer.Index().Add(1); !i.After(msh.maxValidatedLayer); i = i.Add(1) {
nxtLayer, has := msh.nextValidLayers[i]
if !has {
break
}
msh.applyState(nxtLayer)
delete(msh.nextValidLayers, i)
}
}
func (msh *Mesh) setLatestLayerInState(lyr types.LayerID) error {
// update validated layer only after applying transactions since loading of state depends on processedLayer param.
msh.mutex.Lock()
defer msh.mutex.Unlock()
if err := msh.general.Put(VERIFIED, lyr.Bytes()); err != nil {
// can happen if database already closed
msh.Error("could not persist validated layer index %d: %v", lyr, err.Error())
return err
}
msh.latestLayerInState = lyr
return nil
}
// GetAggregatedLayerHash returns the aggregated layer hash up to the specified layer
func (msh *Mesh) GetAggregatedLayerHash(layerID types.LayerID) types.Hash32 {
h, err := msh.getAggregatedLayerHash(layerID)
if err != nil {
return types.EmptyLayerHash
}
return h
}
func (msh *Mesh) getAggregatedLayerHash(layerID types.LayerID) (types.Hash32, error) {
if layerID.Before(types.NewLayerID(1)) {
return types.EmptyLayerHash, nil
}
var hash types.Hash32
bts, err := msh.general.Get(getAggregatedLayerHashKey(layerID))
if err == nil {
hash.SetBytes(bts)
return hash, nil
}
return hash, err
}
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",
l.Index(), 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 `oldPbase`
}
msh.removeFromUnappliedTxs(validBlockTxs)
msh.With().Info("applied transactions",
log.Int("valid_block_txs", len(validBlockTxs)),
l.Index(),
log.Int("num_failed_txs", numFailedTxs),
)
}
var errLayerHasBlock = errors.New("layer has block")
// SetZeroBlockLayer tags lyr as a layer without blocks
func (msh *Mesh) SetZeroBlockLayer(lyr types.LayerID) error {
msh.With().Info("tagging zero block layer", lyr)
// check database for layer
if l, err := msh.GetLayer(lyr); err != nil {
// database error
if err != database.ErrNotFound {
msh.With().Error("error trying to fetch layer from database", lyr, log.Err(err))
return err
}
} else if len(l.Blocks()) != 0 {
// layer exists
msh.With().Error("layer has blocks, cannot tag as zero block layer",
lyr,
l,
log.Int("num_blocks", len(l.Blocks())))
return errLayerHasBlock
}
msh.setLatestLayer(lyr)
// layer doesnt exist, need to insert new layer
return msh.AddZeroBlockLayer(lyr)
}
// AddBlockWithTxs adds a block to the database
// blk - the block to add
// txs - block txs that we dont have in our tx database yet
func (msh *Mesh) AddBlockWithTxs(ctx context.Context, blk *types.Block) error {
logger := msh.WithContext(ctx).WithFields(blk.ID())
logger.With().Debug("adding block to mesh", blk.Fields()...)
if err := msh.StoreTransactionsFromPool(blk); err != nil {
logger.With().Error("not all txs were processed", log.Err(err))
}
// Store block (delete if storing ATXs fails)
if err := msh.DB.AddBlock(blk); err != nil {
if err == ErrAlreadyExist {
return nil
}
logger.With().Error("failed to add block", log.Err(err))
return err
}
msh.setLatestLayer(blk.Layer())
// add new block to orphans
msh.handleOrphanBlocks(blk)
events.ReportNewBlock(blk)
logger.Info("added block to database")
return nil
}
func (msh *Mesh) invalidateFromPools(blk *types.MiniBlock) {
for _, id := range blk.TxIDs {
msh.txPool.Invalidate(id)
}
}
// StoreTransactionsFromPool takes declared txs from provided block blk and writes them to DB. it then invalidates
// the transactions from txpool
func (msh *Mesh) StoreTransactionsFromPool(blk *types.Block) error {
// Store transactions (doesn't have to be rolled back if other writes fail)
if len(blk.TxIDs) == 0 {
return nil
}
txs := make([]*types.Transaction, 0, len(blk.TxIDs))
for _, txID := range blk.TxIDs {
tx, err := msh.txPool.Get(txID)
if err != nil {
// if the transaction is not in the pool it could have been
// invalidated by another block
if has, err := msh.transactions.Has(txID.Bytes()); !has {
return err
}
continue
}
if err = tx.CalcAndSetOrigin(); err != nil {
return err
}
txs = append(txs, tx)
}
if err := msh.writeTransactions(blk, 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)
}
// remove txs from pool
msh.invalidateFromPools(&blk.MiniBlock)
return nil
}
// todo better thread safety
func (msh *Mesh) handleOrphanBlocks(blk *types.Block) {
msh.mutex.Lock()
defer msh.mutex.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.With().Debug("added block to orphans", blk.ID())
for _, b := range append(blk.ForDiff, append(blk.AgainstDiff, blk.NeutralDiff...)...) {
for layerID, layermap := range msh.orphanBlocks {
if _, has := layermap[b]; has {
msh.With().Debug("delete block from orphans", b)
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.mutex.RLock()
defer msh.mutex.RUnlock()
ids := map[types.BlockID]struct{}{}
for key, val := range msh.orphanBlocks {
if key.Before(l) {
for bid := range val {
ids[bid] = struct{}{}
}
}
}
blocks, err := msh.LayerBlockIds(l.Sub(1))
if err != nil {
return nil, fmt.Errorf("failed getting latest layer %v err %v", l.Sub(1), 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) {
coinbases := make([]types.Address, 0, len(l.Blocks()))
// the reason we are serializing the types.NodeID to a string instead of using it directly as a
// key in the map is due to Golang's restriction on only Comparable types used as map keys. Since
// the types.NodeID contains a slice, it is not comparable and hence cannot be used as a map key
// TODO: fix this when changing the types.NodeID struct, see https://github.com/spacemeshos/go-spacemesh/issues/2269
coinbasesAndSmeshers := make(map[types.Address]map[string]uint64)
for _, bl := range l.Blocks() {
if bl.ATXID == *types.EmptyATXID {
msh.With().Info("skipping reward distribution for block with no atx", bl.LayerIndex, bl.ID())
continue
}
atx, err := msh.AtxDB.GetAtxHeader(bl.ATXID)
if err != nil {
msh.With().Warning("atx from block not found in db", log.Err(err), bl.ID(), bl.ATXID)
continue
}
coinbases = append(coinbases, atx.Coinbase)
// create a 2 dimensional map where the entries are
// coinbasesAndSmeshers[coinbase_id][smesher_id] = number of blocks this pair has created
if _, exists := coinbasesAndSmeshers[atx.Coinbase]; !exists {
coinbasesAndSmeshers[atx.Coinbase] = make(map[string]uint64)
}
coinbasesAndSmeshers[atx.Coinbase][atx.NodeID.String()]++
}
if len(coinbases) == 0 {
msh.With().Info("no valid blocks for layer", 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(coinbases)))
blockTotalReward, blockTotalRewardMod := calculateActualRewards(l.Index(), totalReward, numBlocks)
// NOTE: We don't _report_ rewards when we apply them. This is because applying rewards just requires
// the recipient (i.e., coinbase) account, whereas reporting requires knowing the associated smesherid
// as well. We report rewards below once we unpack the data structure containing the association between
// rewards and smesherids.
// Applying rewards (here), reporting them, and adding them to the database (below) should be atomic. Right now,
// they're not. Also, ApplyRewards does not return an error if it fails.
// TODO: fix this.
msh.ApplyRewards(l.Index(), coinbases, blockTotalReward)
blockLayerReward, blockLayerRewardMod := calculateActualRewards(l.Index(), layerReward, numBlocks)
msh.With().Info("reward calculated",
l.Index(),
log.Uint64("num_blocks", numBlocks.Uint64()),
log.Uint64("total_reward", totalReward.Uint64()),
log.Uint64("layer_reward", layerReward.Uint64()),
log.Uint64("block_total_reward", blockTotalReward.Uint64()),
log.Uint64("block_layer_reward", blockLayerReward.Uint64()),
log.Uint64("total_reward_remainder", blockTotalRewardMod.Uint64()),
log.Uint64("layer_reward_remainder", blockLayerRewardMod.Uint64()),
)
// Report the rewards for each coinbase and each smesherID within each coinbase.
// This can be thought of as a partition of the reward amongst all the smesherIDs
// that added the coinbase into the block.
for account, smesherAccountEntry := range coinbasesAndSmeshers {
for smesherString, cnt := range smesherAccountEntry {
smesherEntry, err := types.StringToNodeID(smesherString)
if err != nil {
msh.With().Error("unable to convert bytes to nodeid", log.Err(err),
log.String("smesher_string", smesherString))
return
}
events.ReportRewardReceived(events.Reward{
Layer: l.Index(),
Total: cnt * blockTotalReward.Uint64(),
LayerReward: cnt * blockLayerReward.Uint64(),
Coinbase: account,
Smesher: *smesherEntry,
})
}
}
if err := msh.writeTransactionRewards(l.Index(), coinbasesAndSmeshers, blockTotalReward, blockLayerReward); 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 is the first static block that xists at the beginning of each network. it exist one layer before actual blocks could be created
func GenesisBlock() *types.Block {
return types.NewExistingBlock(types.GetEffectiveGenesis(), []byte("genesis"), nil)
}
// GenesisLayer generates layer 0 should be removed after the genesis flow is implemented
func GenesisLayer() *types.Layer {
l := types.NewLayer(types.GetEffectiveGenesis())
l.AddBlock(GenesisBlock())
return l
}
// GetATXs uses GetFullAtx to return a list of atxs corresponding to atxIds requested
func (msh *Mesh) GetATXs(ctx context.Context, 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.WithContext(ctx).With().Warning("could not get atx from database", id, log.Err(err))
mIds = append(mIds, id)
} else {
atxs[t.ID()] = t
}
}
return atxs, mIds
}
func getAggregatedLayerHashKey(layerID types.LayerID) []byte {
var b bytes.Buffer
b.WriteString("ag")
b.Write(layerID.Bytes())
return b.Bytes()
}