-
Notifications
You must be signed in to change notification settings - Fork 210
/
mesh.go
773 lines (688 loc) · 24.4 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
// Package mesh defines the main store point for all the block-mesh objects
// such as ballots, blocks, transactions and global state
package mesh
import (
"context"
"errors"
"fmt"
"sync"
"go.uber.org/atomic"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/database"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/layers"
"github.com/spacemeshos/go-spacemesh/sql/transactions"
)
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
state
trtl tortoise
txPool txMemPool
mu sync.Mutex
// latestLayer is the latest layer this node had seen from blocks
latestLayer atomic.Value
// latestLayerInState is the latest layer whose contents have been applied to the state
latestLayerInState atomic.Value
// processedLayer is the latest layer whose votes have been processed
processedLayer atomic.Value
// see doc for MissingLayer()
missingLayer atomic.Value
nextProcessedLayers map[types.LayerID]struct{}
maxProcessedLayer types.LayerID
}
// NewMesh creates a new instant of a mesh.
func NewMesh(db *DB, atxDb AtxDB, trtl tortoise, txPool txMemPool, state state, logger log.Log) *Mesh {
msh := &Mesh{
Log: logger,
trtl: trtl,
txPool: txPool,
state: state,
DB: db,
AtxDB: atxDb,
nextProcessedLayers: make(map[types.LayerID]struct{}),
}
msh.latestLayer.Store(types.GetEffectiveGenesis())
msh.latestLayerInState.Store(types.GetEffectiveGenesis())
msh.processedLayer.Store(types.LayerID{})
gLyr := types.GetEffectiveGenesis()
for i := types.NewLayerID(1); !i.After(gLyr); i = i.Add(1) {
if i.Before(gLyr) {
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(), i, i); err != nil {
msh.With().Panic("failed to persist hashes for layer", i, log.Err(err))
}
msh.setProcessedLayer(i)
}
return msh
}
// NewRecoveredMesh creates new instance of mesh with recovered mesh data fom database.
func NewRecoveredMesh(db *DB, atxDb AtxDB, trtl tortoise, txPool txMemPool, state state, logger log.Log) *Mesh {
msh := NewMesh(db, atxDb, trtl, txPool, state, logger)
latest, err := msh.GetLatestLayer()
if err != nil {
logger.With().Panic("failed to recover latest layer", log.Err(err))
}
msh.setLatestLayer(latest)
lyr, err := msh.GetProcessedLayer()
if err != nil {
logger.With().Panic("failed to recover processed layer", log.Err(err))
}
msh.setProcessedLayerFromRecoveredData(lyr)
verified, err := msh.GetVerifiedLayer()
if err != nil {
logger.With().Panic("failed to recover latest verified layer", log.Err(err))
}
if err = msh.setLatestLayerInState(verified); err != nil {
logger.With().Panic("failed to recover latest layer in state", log.Err(err))
}
_, err = state.Rewind(msh.LatestLayerInState())
if err != nil {
logger.With().Panic("failed to load state for layer", msh.LatestLayerInState(), log.Err(err))
}
msh.With().Info("recovered mesh from disk",
log.FieldNamed("latest", msh.LatestLayer()),
log.FieldNamed("processed", msh.ProcessedLayer()),
log.String("root_hash", state.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 {
return msh.latestLayerInState.Load().(types.LayerID)
}
// LatestLayer - returns the latest layer we saw from the network.
func (msh *Mesh) LatestLayer() types.LayerID {
return msh.latestLayer.Load().(types.LayerID)
}
// MissingLayer is a layer in (latestLayerInState, processLayer].
// this layer is missing critical data (valid blocks or transactions)
// and can't be applied to the state.
//
// First valid layer starts with 1. 0 is empty layer and can be ignored.
func (msh *Mesh) MissingLayer() types.LayerID {
value := msh.missingLayer.Load()
if value == nil {
return types.LayerID{}
}
return value.(types.LayerID)
}
// setLatestLayer sets the latest layer we saw from the network.
func (msh *Mesh) setLatestLayer(lid types.LayerID) {
events.ReportLayerUpdate(events.LayerUpdate{
LayerID: lid,
Status: events.LayerStatusTypeUnknown,
})
for {
current := msh.LatestLayer()
if !lid.After(current) {
return
}
if msh.latestLayer.CompareAndSwap(current, lid) {
events.ReportNodeStatusUpdate()
msh.With().Info("set latest known layer", lid)
if err := layers.SetStatus(msh.db, lid, layers.Latest); 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) {
ballots, err := msh.LayerBallots(i)
if err != nil {
return nil, fmt.Errorf("layer ballots: %w", err)
}
blocks, err := msh.LayerBlocks(i)
if err != nil {
return nil, fmt.Errorf("layer blocks: %w", err)
}
return types.NewExistingLayer(i, ballots, blocks), nil
}
// GetLayerHash returns layer hash.
func (msh *Mesh) GetLayerHash(layerID types.LayerID) types.Hash32 {
h, err := msh.recoverLayerHash(layerID)
if err == nil {
return h
}
if errors.Is(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 {
return msh.processedLayer.Load().(types.LayerID)
}
func (msh *Mesh) setProcessedLayerFromRecoveredData(lid types.LayerID) {
msh.processedLayer.Store(lid)
msh.Event().Info("processed layer set from recovered data", lid)
}
func (msh *Mesh) setProcessedLayer(layerID types.LayerID) {
processed := msh.ProcessedLayer()
if !layerID.After(processed) {
msh.With().Info("trying to set processed layer to an older layer",
log.FieldNamed("processed", processed),
layerID)
return
}
if layerID.After(msh.maxProcessedLayer) {
msh.maxProcessedLayer = layerID
}
if layerID != processed.Add(1) {
msh.With().Info("trying to set processed layer out of order",
log.FieldNamed("processed", processed),
layerID)
msh.nextProcessedLayers[layerID] = struct{}{}
return
}
msh.nextProcessedLayers[layerID] = struct{}{}
for i := layerID; !i.After(msh.maxProcessedLayer); i = i.Add(1) {
_, ok := msh.nextProcessedLayers[i]
if !ok {
break
}
processed = i
delete(msh.nextProcessedLayers, i)
}
msh.processedLayer.Store(processed)
events.ReportNodeStatusUpdate()
msh.Event().Info("processed layer set", processed)
if err := msh.persistProcessedLayer(processed); err != nil {
msh.With().Error("failed to persist processed layer",
log.FieldNamed("processed", processed),
log.Err(err))
}
}
// ProcessLayer 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.
func (msh *Mesh) ProcessLayer(ctx context.Context, layerID types.LayerID) error {
msh.mu.Lock()
defer msh.mu.Unlock()
logger := msh.WithContext(ctx).WithFields(layerID)
logger.Info("processing layer")
// pass the layer to tortoise for processing
oldVerified, newVerified, reverted := msh.trtl.HandleIncomingLayer(ctx, layerID)
logger.With().Info("tortoise results",
log.Bool("reverted", reverted),
log.FieldNamed("old_verified", oldVerified),
log.FieldNamed("new_verified", newVerified))
// set processed layer even if later code will fail, as that failure is not related
// to the layer that is being processed
msh.setProcessedLayer(layerID)
// 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 {
msh.setLatestLayerInState(oldVerified)
if err := msh.revertState(ctx, oldVerified); err != nil {
logger.With().Error("failed to revert state, unable to process layer", log.Err(err))
return err
}
}
// mesh can't skip layer that failed to complete
from := minLayer(oldVerified, msh.LatestLayerInState()).Add(1)
to := newVerified
if !to.Before(from) {
if err := msh.pushLayersToState(ctx, from, to); err != nil {
logger.With().Error("failed to push layers to state", log.Err(err))
return err
}
if err := msh.persistLayerHashes(ctx, from, to); err != nil {
logger.With().Error("failed to persist layer hashes", log.Err(err))
return err
}
for lid := from; !lid.After(to); lid = lid.Add(1) {
events.ReportLayerUpdate(events.LayerUpdate{
LayerID: lid,
Status: events.LayerStatusTypeConfirmed,
})
}
}
logger.Info("done processing layer")
return nil
}
func (msh *Mesh) getAggregatedHash(lid types.LayerID) (types.Hash32, error) {
if !lid.After(types.NewLayerID(1)) {
return types.EmptyLayerHash, nil
}
return layers.GetAggregatedHash(msh.db, lid)
}
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.getAggregatedHash(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)
blocks, err := msh.LayerBlockIds(layerID)
if err != nil {
return nil, err
}
var validBlockIDs []types.BlockID
for _, bID := range blocks {
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
}
// 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, from, to types.LayerID) error {
logger := msh.WithContext(ctx).WithFields(
log.Stringer("from_layer", from),
log.Stringer("to_layer", to))
logger.Info("pushing layers to state")
if from.Before(types.GetEffectiveGenesis()) || to.Before(types.GetEffectiveGenesis()) {
logger.Panic("tried to push genesis layers")
return nil
}
missing := msh.MissingLayer()
// we never reapply the state of oldVerified. note that state reversions must be handled separately.
for layerID := from; !layerID.After(to); layerID = layerID.Add(1) {
if !layerID.After(msh.LatestLayerInState()) {
logger.With().Error("trying to apply layer before currently applied layer",
log.Stringer("applied_layer", msh.LatestLayerInState()),
layerID,
)
continue
}
if err := msh.pushLayer(ctx, layerID); err != nil {
msh.missingLayer.Store(layerID)
return err
}
if layerID == missing {
msh.missingLayer.Store(types.LayerID{})
}
}
return nil
}
func (msh *Mesh) pushLayer(ctx context.Context, layerID types.LayerID) error {
layerBlocks, err := msh.LayerBlocks(layerID)
if err != nil {
return fmt.Errorf("failed to get layer %s: %w", layerID, err)
}
validBlocks, invalidBlocks := msh.BlocksByValidity(layerBlocks)
var (
applied *types.Block
notApplied = invalidBlocks
blocks = types.SortBlocks(validBlocks)
)
if len(blocks) > 0 {
// when tortoise verify multiple blocks in the same layer, we only apply one with the lowest
// lexicographical sort order
applied = blocks[0]
if len(blocks) > 1 {
notApplied = append(notApplied, blocks[1:]...)
}
}
if err = msh.updateStateWithLayer(ctx, layerID, applied); err != nil {
return fmt.Errorf("failed to update state %s: %w", layerID, err)
}
msh.Event().Info("end of layer state root",
layerID,
log.Stringer("state_root", msh.state.GetStateRoot()),
)
if err = msh.reInsertTxsToPool(applied, notApplied, layerID); err != nil {
return fmt.Errorf("failed to reinsert TXs to pool %s: %w", layerID, err)
}
return nil
}
// 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.state.Rewind(layerID); err != nil {
return fmt.Errorf("failed to revert state to layer %v: %w", layerID, err)
}
return nil
}
func (msh *Mesh) reInsertTxsToPool(applied *types.Block, notApplied []*types.Block, l types.LayerID) error {
seenTxIds := make(map[types.TransactionID]struct{})
if applied != nil {
uniqueTxIds([]*types.Block{applied}, seenTxIds) // run for the side effect, updating seenTxIds
}
returnedTxs, missing := msh.GetTransactions(uniqueTxIds(notApplied, seenTxIds))
if len(missing) > 0 {
msh.With().Error("could not reinsert transactions", log.Int("missing", len(missing)), l)
}
if err := msh.markTransactionsDeleted(returnedTxs...); err != nil {
return err
}
for _, tx := range returnedTxs {
if err := msh.ValidateNonceAndBalance(tx); err != nil {
return err
}
if err := msh.AddTxToPool(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())
}
}
return nil
}
func (msh *Mesh) applyState(block *types.Block) error {
var failedTxs []*types.Transaction
var svmErr error
rewardByMiner := map[types.Address]uint64{}
for _, r := range block.Rewards {
rewardByMiner[r.Address] += r.Amount
}
txs, missing := msh.GetTransactions(block.TxIDs)
if len(missing) > 0 {
return fmt.Errorf("could not find transactions %v from layer %v", missing, block.LayerIndex)
}
// TODO: should miner IDs be sorted in a deterministic order prior to applying rewards?
failedTxs, svmErr = msh.state.ApplyLayer(block.LayerIndex, txs, rewardByMiner)
if svmErr != nil {
msh.With().Error("failed to apply transactions",
block.LayerIndex, log.Int("num_failed_txs", len(failedTxs)), log.Err(svmErr))
// 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 `oldVerified`
return fmt.Errorf("apply layer: %w", svmErr)
}
if err := msh.DB.writeTransactionRewards(block.LayerIndex, block.Rewards); err != nil {
msh.With().Error("cannot write reward to db", log.Err(err))
return err
}
reportRewards(block)
if err := msh.updateDBTXWithBlockID(block, txs...); err != nil {
msh.With().Error("failed to update tx block ID in db", log.Err(err))
return err
}
for _, tx := range txs {
if err := transactions.Applied(msh.db, tx.ID()); err != nil {
return err
}
}
msh.With().Info("applied transactions",
block.LayerIndex,
log.Int("valid_block_txs", len(txs)),
log.Int("num_failed_txs", len(failedTxs)),
)
return nil
}
// ProcessLayerPerHareOutput receives hare output once it finishes running for a given layer.
func (msh *Mesh) ProcessLayerPerHareOutput(ctx context.Context, layerID types.LayerID, blockID types.BlockID) error {
logger := msh.WithContext(ctx).WithFields(layerID, blockID)
if blockID == types.EmptyBlockID {
logger.Info("received empty set from hare")
} else {
// double-check we have this block in the mesh
_, err := msh.GetBlock(blockID)
if err != nil {
logger.With().Error("hare terminated with block that is not present in mesh", log.Err(err))
return err
}
}
// report that hare "approved" this layer
events.ReportLayerUpdate(events.LayerUpdate{
LayerID: layerID,
Status: events.LayerStatusTypeApproved,
})
logger.Info("saving hare output for layer")
if err := msh.SaveHareConsensusOutput(ctx, layerID, blockID); err != nil {
logger.Error("saving layer hare output failed")
}
return msh.ProcessLayer(ctx, layerID)
}
// apply the state for a single layer.
func (msh *Mesh) updateStateWithLayer(ctx context.Context, layerID types.LayerID, block *types.Block) error {
if latest := msh.LatestLayerInState(); layerID != latest.Add(1) {
msh.WithContext(ctx).With().Panic("update state out-of-order",
log.FieldNamed("verified", layerID),
log.FieldNamed("latest", latest))
}
if block != nil {
if err := msh.applyState(block); err != nil {
return err
}
}
if err := msh.setLatestLayerInState(layerID); err != nil {
return err
}
return nil
}
func (msh *Mesh) setLatestLayerInState(lyr types.LayerID) error {
// Update validated layer only after applying transactions since loading of
// state depends on processedLayer param.
if err := layers.SetStatus(msh.db, lyr, layers.Applied); err != nil {
// can happen if database already closed
msh.Error("could not persist validated layer index %d: %v", lyr, err.Error())
return fmt.Errorf("put into DB: %w", err)
}
msh.latestLayerInState.Store(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 := layers.GetAggregatedHash(msh.db, layerID)
if err != nil {
return types.EmptyLayerHash
}
return h
}
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
}
var errLayerHasBallot = errors.New("layer has ballot")
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 !errors.Is(err, sql.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 doesn't exist, need to insert new layer
return msh.AddZeroBlockLayer(lyr)
}
// AddTXsFromProposal adds the TXs in a Proposal into the database.
func (msh *Mesh) AddTXsFromProposal(ctx context.Context, layerID types.LayerID, proposalID types.ProposalID, txIDs []types.TransactionID) error {
logger := msh.WithContext(ctx).WithFields(layerID, proposalID, log.Int("num_txs", len(txIDs)))
logger.Debug("adding proposal txs to mesh")
msh.addTransactionsForBlock(logger, layerID, types.EmptyBlockID, txIDs)
logger.Info("added proposal's txs to database")
return nil
}
// AddBallot to the mesh.
func (msh *Mesh) AddBallot(ballot *types.Ballot) error {
if err := msh.DB.AddBallot(ballot); err != nil {
return err
}
msh.trtl.OnBallot(ballot)
return nil
}
// AddBlockWithTXs adds the block and its TXs in into the database.
func (msh *Mesh) AddBlockWithTXs(ctx context.Context, block *types.Block) error {
logger := msh.WithContext(ctx).WithFields(block.LayerIndex, block.ID(), log.Int("num_txs", len(block.TxIDs)))
logger.Debug("adding block txs to mesh")
msh.addTransactionsForBlock(logger, block.LayerIndex, block.ID(), block.TxIDs)
if err := msh.AddBlock(block); err != nil {
return err
}
msh.trtl.OnBlock(block)
return nil
}
func (msh *Mesh) addTransactionsForBlock(logger log.Log, layerID types.LayerID, blockID types.BlockID, txIDs []types.TransactionID) {
if err := msh.storeTransactionsFromPool(layerID, blockID, txIDs); err != nil {
logger.With().Error("not all txs were processed", log.Err(err))
}
msh.setLatestLayer(layerID)
logger.Info("added txs to database")
}
func (msh *Mesh) invalidateFromPools(txIDs []types.TransactionID) {
for _, id := range txIDs {
msh.txPool.Invalidate(id)
}
}
// storeTransactionsFromPool takes declared txs from provided proposal and writes them to DB. it then invalidates
// the transactions from txpool.
func (msh *Mesh) storeTransactionsFromPool(layerID types.LayerID, blockID types.BlockID, txIDs []types.TransactionID) error {
// Store transactions (doesn't have to be rolled back if other writes fail)
if len(txIDs) == 0 {
return nil
}
txs := make([]*types.Transaction, 0, len(txIDs))
for _, txID := range 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 := transactions.Has(msh.db, txID); !has {
return fmt.Errorf("check if tx is in DB: %w", err)
}
continue
}
txs = append(txs, tx)
}
if err := msh.writeTransactions(layerID, blockID, txs...); err != nil {
return fmt.Errorf("write tx: %w", err)
}
// remove txs from pool
msh.invalidateFromPools(txIDs)
return nil
}
func reportRewards(block *types.Block) {
// 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 _, r := range block.Rewards {
events.ReportRewardReceived(events.Reward{
Layer: block.LayerIndex,
Total: r.Amount,
LayerReward: r.LayerReward,
Coinbase: r.Address,
Smesher: r.SmesherID,
})
}
}
// 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
}
// Transactions exports the transactions DB.
func (msh *Mesh) Transactions() database.Getter {
return &txFetcher{m: msh}
}
func minLayer(i, j types.LayerID) types.LayerID {
if i.Before(j) {
return i
}
return j
}
func maxLayer(i, j types.LayerID) types.LayerID {
if i.After(j) {
return i
}
return j
}