-
Notifications
You must be signed in to change notification settings - Fork 210
/
tortoise_beacon.go
1031 lines (876 loc) · 30.3 KB
/
tortoise_beacon.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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tortoisebeacon
import (
"context"
"errors"
"fmt"
"math/big"
"sync"
"sync/atomic"
"time"
"github.com/ALTree/bigfloat"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
"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/log"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/system"
"github.com/spacemeshos/go-spacemesh/timesync"
"github.com/spacemeshos/go-spacemesh/tortoisebeacon/metrics"
"github.com/spacemeshos/go-spacemesh/tortoisebeacon/weakcoin"
)
const (
protoName = "TORTOISE_BEACON_PROTOCOL"
proposalPrefix = "TBP"
proposalChanCapacity = 1024
numEpochsToKeep = 10
)
// Tortoise Beacon errors.
var (
ErrBeaconNotCalculated = errors.New("beacon is not calculated for this epoch")
ErrZeroEpochWeight = errors.New("zero epoch weight provided")
ErrZeroEpoch = errors.New("zero epoch provided")
)
//go:generate mockgen -package=mocks -destination=./mocks/mocks.go -source=./tortoise_beacon.go
type coin interface {
StartEpoch(context.Context, types.EpochID, weakcoin.UnitAllowances)
StartRound(context.Context, types.RoundID) error
FinishRound(context.Context)
Get(context.Context, types.EpochID, types.RoundID) bool
FinishEpoch(context.Context, types.EpochID)
}
type eligibilityChecker interface {
IsProposalEligible(proposal []byte) bool
}
type (
proposals = struct{ valid, potentiallyValid [][]byte }
allVotes = struct{ valid, invalid proposalSet }
ballotWeight = struct {
weight uint64
ballots map[types.BallotID]struct{}
}
)
type layerClock interface {
Subscribe() timesync.LayerTimer
Unsubscribe(timesync.LayerTimer)
LayerToTime(types.LayerID) time.Time
}
// New returns a new TortoiseBeacon.
func New(
conf Config,
nodeID types.NodeID,
publisher pubsub.Publisher,
atxDB activationDB,
edSigner signing.Signer,
edVerifier signing.VerifyExtractor,
vrfSigner signing.Signer,
vrfVerifier signing.Verifier,
weakCoin coin,
db database.Database,
clock layerClock,
logger log.Log,
) *TortoiseBeacon {
tb := &TortoiseBeacon{
logger: logger,
config: conf,
nodeID: nodeID,
theta: new(big.Float).SetRat(conf.Theta),
publisher: publisher,
atxDB: atxDB,
edSigner: edSigner,
edVerifier: edVerifier,
vrfSigner: vrfSigner,
vrfVerifier: vrfVerifier,
weakCoin: weakCoin,
db: db,
clock: clock,
beacons: make(map[types.EpochID]types.Beacon),
beaconsFromBallots: make(map[types.EpochID]map[types.Beacon]*ballotWeight),
hasProposed: make(map[string]struct{}),
hasVoted: make([]map[string]struct{}, conf.RoundsNumber),
firstRoundIncomingVotes: make(map[string]proposals),
proposalChans: make(map[types.EpochID]chan *proposalMessageWithReceiptData),
votesMargin: map[string]*big.Int{},
}
tb.metricsCollector = metrics.NewBeaconMetricsCollector(tb.gatherMetricsData, logger.WithName("metrics"))
return tb
}
// TortoiseBeacon represents Tortoise Beacon.
type TortoiseBeacon struct {
running uint64
inProtocol uint64
eg errgroup.Group
cancel context.CancelFunc
logger log.Log
config Config
nodeID types.NodeID
sync system.SyncStateProvider
publisher pubsub.Publisher
atxDB activationDB
edSigner signing.Signer
edVerifier signing.VerifyExtractor
vrfSigner signing.Signer
vrfVerifier signing.Verifier
weakCoin coin
theta *big.Float
clock layerClock
layerTicker chan types.LayerID
db database.Database
mu sync.RWMutex
lastTickedEpoch types.EpochID
epochInProgress types.EpochID
epochWeight uint64
// beacons store calculated beacons as the result of the tortoise beacon protocol.
// the map key is the epoch when beacon is calculated. the beacon is used in the following epoch
beacons map[types.EpochID]types.Beacon
// beaconsFromBallots store beacons collected from ballots.
// the map key is the epoch when the block is published. the beacon value is calculated in the
// previous epoch and used in the current epoch
beaconsFromBallots map[types.EpochID]map[types.Beacon]*ballotWeight
// TODO(nkryuchkov): have a mixed list of all sorted proposals
// have one bit vector: valid proposals
incomingProposals proposals
firstRoundIncomingVotes map[string]proposals // sorted votes for bit vector decoding
// TODO(nkryuchkov): For every round excluding first round consider having a vector of opinions.
votesMargin map[string]*big.Int
hasProposed map[string]struct{}
hasVoted []map[string]struct{}
proposalPhaseFinishedTime time.Time
proposalChans map[types.EpochID]chan *proposalMessageWithReceiptData
proposalChecker eligibilityChecker
// metrics
metricsCollector *metrics.BeaconMetricsCollector
metricsRegistry *prometheus.Registry
}
// SetSyncState updates sync state provider. Must be executed only once.
func (tb *TortoiseBeacon) SetSyncState(sync system.SyncStateProvider) {
if tb.sync != nil {
tb.logger.Panic("sync state provider can be updated only once")
}
tb.sync = sync
}
// for testing.
func (tb *TortoiseBeacon) setMetricsRegistry(registry *prometheus.Registry) {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.metricsRegistry = registry
}
// Start starts listening for layers and outputs.
func (tb *TortoiseBeacon) Start(ctx context.Context) error {
if !atomic.CompareAndSwapUint64(&tb.running, 0, 1) {
tb.logger.WithContext(ctx).Warning("attempt to start tortoise beacon more than once")
return nil
}
tb.logger.Info("starting %v with the following config: %+v", protoName, tb.config)
if tb.sync == nil {
tb.logger.Panic("update sync state provider can't be nil")
}
ctx, cancel := context.WithCancel(ctx)
tb.cancel = cancel
tb.initGenesisBeacons()
tb.layerTicker = tb.clock.Subscribe()
tb.metricsCollector.Start(nil)
tb.eg.Go(func() error {
tb.listenLayers(ctx)
return fmt.Errorf("context error: %w", ctx.Err())
})
return nil
}
// Close closes TortoiseBeacon.
func (tb *TortoiseBeacon) Close() {
if !atomic.CompareAndSwapUint64(&tb.running, 1, 0) {
return
}
tb.logger.Info("closing %v", protoName)
tb.cancel()
tb.metricsCollector.Stop()
tb.clock.Unsubscribe(tb.layerTicker)
tb.logger.Info("waiting for tortoise beacon goroutines to finish")
if err := tb.eg.Wait(); err != nil {
tb.logger.With().Info("received error waiting for goroutines to finish", log.Err(err))
}
tb.logger.Info("tortoise beacon goroutines finished")
}
// isClosed returns true if background workers are not running.
func (tb *TortoiseBeacon) isClosed() bool {
return atomic.LoadUint64(&tb.running) == 0
}
// ReportBeaconFromBallot reports the beacon value in a ballot along with the smesher's weight unit.
func (tb *TortoiseBeacon) ReportBeaconFromBallot(epoch types.EpochID, bid types.BallotID, beacon types.Beacon, weight uint64) {
tb.recordBeacon(epoch, bid, beacon, weight)
if _, err := tb.GetBeacon(epoch); err == nil {
// already has beacon. i.e. we had participated in tortoise beacon protocol during last epoch
return
}
if eBeacon := tb.findMostWeightedBeaconForEpoch(epoch); eBeacon != types.EmptyBeacon {
tb.setBeacon(epoch-1, eBeacon)
}
}
func (tb *TortoiseBeacon) recordBeacon(epochID types.EpochID, bid types.BallotID, beacon types.Beacon, weight uint64) {
tb.mu.Lock()
defer tb.mu.Unlock()
if _, ok := tb.beaconsFromBallots[epochID]; !ok {
tb.beaconsFromBallots[epochID] = make(map[types.Beacon]*ballotWeight)
}
entry, ok := tb.beaconsFromBallots[epochID][beacon]
if !ok {
tb.beaconsFromBallots[epochID][beacon] = &ballotWeight{
weight: weight,
ballots: map[types.BallotID]struct{}{bid: {}},
}
tb.logger.With().Debug("added beacon from ballot",
epochID,
bid,
log.String("beacon", beacon.ShortString()),
log.Uint64("weight", weight))
return
}
// checks if we have recorded this blockID before
if _, ok := entry.ballots[bid]; ok {
tb.logger.With().Warning("ballot already reported beacon", epochID, bid)
return
}
entry.ballots[bid] = struct{}{}
entry.weight += weight
tb.logger.With().Debug("added beacon from ballot",
epochID,
bid,
log.String("beacon", beacon.ShortString()),
log.Uint64("weight", weight))
}
func (tb *TortoiseBeacon) findMostWeightedBeaconForEpoch(epoch types.EpochID) types.Beacon {
tb.mu.RLock()
defer tb.mu.RUnlock()
epochBeacons, ok := tb.beaconsFromBallots[epoch]
if !ok {
return types.EmptyBeacon
}
var mostWeight uint64
var beacon types.Beacon
numBallots := 0
for k, v := range epochBeacons {
if v.weight > mostWeight {
beacon = k
mostWeight = v.weight
}
numBallots += len(v.ballots)
}
logger := tb.logger.WithFields(epoch, log.Int("num_ballots", numBallots))
if uint32(numBallots) < tb.config.BeaconSyncNumBallots {
logger.Debug("not enough ballots to determine beacon")
return types.EmptyBeacon
}
logger.With().Info("beacon determined for epoch",
log.String("beacon", beacon.ShortString()),
log.Uint64("weight", mostWeight))
return beacon
}
// GetBeacon returns a Tortoise Beacon value as types.Beacon for a certain epoch or an error if it doesn't exist.
// TODO(nkryuchkov): consider not using (using DB instead).
func (tb *TortoiseBeacon) GetBeacon(epochID types.EpochID) (types.Beacon, error) {
if epochID == 0 {
return types.EmptyBeacon, ErrZeroEpoch
}
beaconEpoch := epochID - 1
beacon := tb.getBeacon(beaconEpoch)
if beacon != types.EmptyBeacon {
return beacon, nil
}
beacon, err := tb.getPersistedBeacon(beaconEpoch)
if err == nil {
return beacon, nil
}
if errors.Is(err, database.ErrNotFound) {
tb.logger.With().Warning("beacon not available",
epochID,
log.Uint32("beacon_epoch", uint32(beaconEpoch)))
return types.EmptyBeacon, ErrBeaconNotCalculated
}
tb.logger.With().Error("failed to get beacon from db",
epochID,
log.Uint32("beacon_epoch", uint32(beaconEpoch)),
log.Err(err))
return types.EmptyBeacon, fmt.Errorf("get beacon from DB: %w", err)
}
func (tb *TortoiseBeacon) getBeacon(epoch types.EpochID) types.Beacon {
tb.mu.RLock()
defer tb.mu.RUnlock()
if beacon, ok := tb.beacons[epoch]; ok {
return beacon
}
return types.EmptyBeacon
}
func (tb *TortoiseBeacon) setBeacon(epoch types.EpochID, beacon types.Beacon) error {
if err := tb.persistBeacon(epoch, beacon); err != nil {
return err
}
tb.mu.Lock()
defer tb.mu.Unlock()
tb.beacons[epoch] = beacon
return nil
}
func (tb *TortoiseBeacon) persistBeacon(epoch types.EpochID, beacon types.Beacon) error {
if err := tb.db.Put(epoch.ToBytes(), beacon.Bytes()); err != nil {
tb.logger.With().Error("failed to persist beacon",
epoch,
log.String("beacon", beacon.ShortString()), log.Err(err))
return fmt.Errorf("persist beacon: %w", err)
}
return nil
}
func (tb *TortoiseBeacon) getPersistedBeacon(epoch types.EpochID) (types.Beacon, error) {
data, err := tb.db.Get(epoch.ToBytes())
if err != nil {
return types.EmptyBeacon, fmt.Errorf("get from DB: %w", err)
}
return types.BytesToBeacon(data), nil
}
func (tb *TortoiseBeacon) initGenesisBeacons() {
tb.mu.Lock()
defer tb.mu.Unlock()
for epoch := types.EpochID(0); epoch.IsGenesis(); epoch++ {
genesis := types.HexToBeacon(types.GenesisBeacon)
tb.beacons[epoch] = genesis
}
}
func (tb *TortoiseBeacon) setBeginProtocol(ctx context.Context) {
if !atomic.CompareAndSwapUint64(&tb.inProtocol, 0, 1) {
tb.logger.WithContext(ctx).Error("attempt to begin tortoise beacon protocol more than once")
}
}
func (tb *TortoiseBeacon) setEndProtocol(ctx context.Context) {
if !atomic.CompareAndSwapUint64(&tb.inProtocol, 1, 0) {
tb.logger.WithContext(ctx).Error("attempt to end tortoise beacon protocol more than once")
}
}
func (tb *TortoiseBeacon) isInProtocol() bool {
return atomic.LoadUint64(&tb.inProtocol) == 1
}
func (tb *TortoiseBeacon) setupEpoch(epoch types.EpochID, epochWeight uint64, logger log.Log) chan *proposalMessageWithReceiptData {
tb.cleanupEpoch(epoch - 1) // just in case we processed any gossip messages before the protocol started
tb.mu.Lock()
defer tb.mu.Unlock()
tb.epochWeight = epochWeight
tb.proposalChecker = createProposalChecker(tb.config.Kappa, tb.config.Q, epochWeight, logger)
ch := tb.getOrCreateProposalChannel(epoch)
// allow proposals for the next epoch to come in early
_ = tb.getOrCreateProposalChannel(epoch + 1)
return ch
}
func (tb *TortoiseBeacon) cleanupEpoch(epoch types.EpochID) {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.incomingProposals = proposals{}
tb.firstRoundIncomingVotes = map[string]proposals{}
tb.votesMargin = map[string]*big.Int{}
tb.hasProposed = make(map[string]struct{})
tb.hasVoted = make([]map[string]struct{}, tb.config.RoundsNumber)
tb.proposalPhaseFinishedTime = time.Time{}
if ch, ok := tb.proposalChans[epoch]; ok {
close(ch)
delete(tb.proposalChans, epoch)
}
if epoch <= numEpochsToKeep {
return
}
oldest := epoch - numEpochsToKeep
if _, ok := tb.beacons[oldest]; ok {
delete(tb.beacons, oldest)
}
if _, ok := tb.beaconsFromBallots[oldest]; ok {
delete(tb.beaconsFromBallots, oldest)
}
}
// listens to new layers.
func (tb *TortoiseBeacon) listenLayers(ctx context.Context) {
tb.logger.With().Info("starting listening layers")
for {
select {
case <-ctx.Done():
return
case layer := <-tb.layerTicker:
tb.logger.With().Debug("received tick", layer)
tb.eg.Go(func() error {
tb.handleLayer(ctx, layer)
return nil
})
}
}
}
func (tb *TortoiseBeacon) setTickedEpoch(epoch types.EpochID) {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.lastTickedEpoch = epoch
}
func (tb *TortoiseBeacon) setEpochInProgress(epoch types.EpochID) {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.epochInProgress = epoch
}
// the logic that happens when a new layer arrives.
// this function triggers the start of new CPs.
func (tb *TortoiseBeacon) handleLayer(ctx context.Context, layer types.LayerID) {
epoch := layer.GetEpoch()
logger := tb.logger.WithContext(ctx).WithFields(layer, epoch)
tb.setTickedEpoch(epoch)
if !layer.FirstInEpoch() {
logger.Debug("not first layer in epoch, skipping")
return
}
if tb.isInProtocol() {
logger.Error("last beacon protocol is still running, skipping")
return
}
logger.Info("first layer in epoch, proceeding")
tb.handleEpoch(ctx, epoch)
}
func (tb *TortoiseBeacon) handleEpoch(ctx context.Context, epoch types.EpochID) {
ctx = log.WithNewSessionID(ctx)
logger := tb.logger.WithContext(ctx).WithFields(epoch)
// TODO(nkryuchkov): check when epoch started, adjust waiting time for this timestamp
if epoch.IsGenesis() {
logger.Debug("not starting tortoise beacon since we are in genesis epoch")
return
}
if !tb.sync.IsSynced(ctx) {
logger.Info("tortoise beacon protocol is skipped while node is not synced")
return
}
// make sure this node has ATX in the last epoch and is eligible to participate in tortoise beacon
atxID, err := tb.atxDB.GetNodeAtxIDForEpoch(tb.nodeID, epoch-1)
if err != nil {
logger.With().Info("node has no ATX in last epoch, not participating in tortoise beacon", log.Err(err))
return
}
logger.With().Info("participating in tortoise beacon protocol with ATX", atxID)
epochWeight, atxs, err := tb.atxDB.GetEpochWeight(epoch)
if err != nil {
logger.With().Error("failed to get weight targeting epoch", log.Err(err))
return
}
if epochWeight == 0 {
logger.With().Error("zero weight targeting epoch", log.Err(ErrZeroEpochWeight))
return
}
tb.setEpochInProgress(epoch)
tb.setBeginProtocol(ctx)
defer tb.setEndProtocol(ctx)
ch := tb.setupEpoch(epoch, epochWeight, logger)
defer tb.cleanupEpoch(epoch)
tb.eg.Go(func() error {
tb.readProposalMessagesLoop(ctx, ch)
return nil
})
tb.startWeakCoinEpoch(ctx, epoch, atxs)
defer tb.weakCoin.FinishEpoch(ctx, epoch)
tb.runProposalPhase(ctx, epoch)
lastRoundOwnVotes, err := tb.runConsensusPhase(ctx, epoch)
if err != nil {
logger.With().Warning("consensus execution canceled", log.Err(err))
return
}
// K rounds passed
// After K rounds had passed, tally up votes for proposals using simple tortoise vote counting
if err := tb.calcBeacon(ctx, epoch, lastRoundOwnVotes); err != nil {
logger.With().Error("failed to calculate beacon", log.Err(err))
}
logger.With().Debug("finished handling epoch")
}
func (tb *TortoiseBeacon) getOrCreateProposalChannel(epoch types.EpochID) chan *proposalMessageWithReceiptData {
ch, ok := tb.proposalChans[epoch]
if !ok {
ch = make(chan *proposalMessageWithReceiptData, proposalChanCapacity)
tb.proposalChans[epoch] = ch
}
return ch
}
func (tb *TortoiseBeacon) runProposalPhase(ctx context.Context, epoch types.EpochID) {
logger := tb.logger.WithContext(ctx).WithFields(epoch)
logger.Debug("starting proposal phase")
var cancel func()
ctx, cancel = context.WithTimeout(ctx, tb.config.ProposalDuration)
defer cancel()
tb.eg.Go(func() error {
logger.Debug("starting proposal message sender")
if err := tb.proposalPhaseImpl(ctx, epoch); err != nil {
logger.With().Error("failed to send proposal message", log.Err(err))
}
logger.Debug("proposal message sender finished")
return nil
})
<-ctx.Done()
tb.markProposalPhaseFinished(epoch)
logger.Debug("proposal phase finished")
}
func (tb *TortoiseBeacon) proposalPhaseImpl(ctx context.Context, epoch types.EpochID) error {
if tb.isClosed() {
return nil
}
logger := tb.logger.WithContext(ctx).WithFields(epoch)
proposedSignature := buildSignedProposal(ctx, tb.vrfSigner, epoch, tb.logger)
tb.mu.RLock()
logger.With().Debug("calculated proposal signature",
log.String("signature", string(proposedSignature)),
log.Uint64("total_weight", tb.epochWeight))
passes := tb.proposalChecker.IsProposalEligible(proposedSignature)
tb.mu.RUnlock()
if !passes {
logger.With().Debug("proposal to be sent doesn't pass threshold",
log.String("proposal", string(proposedSignature)))
// proposal is not sent
return nil
}
logger.With().Debug("Proposal to be sent passes threshold",
log.String("proposal", string(proposedSignature)))
// concat them into a single proposal message
m := ProposalMessage{
EpochID: epoch,
NodeID: tb.nodeID,
VRFSignature: proposedSignature,
}
logger.With().Debug("going to send proposal", log.String("message", m.String()))
if err := tb.sendToGossip(ctx, TBProposalProtocol, m); err != nil {
return fmt.Errorf("broadcast proposal message: %w", err)
}
logger.With().Info("sent proposal", log.String("message", m.String()))
return nil
}
func (tb *TortoiseBeacon) getProposalChannel(ctx context.Context, epoch types.EpochID) chan *proposalMessageWithReceiptData {
tb.mu.RLock()
defer tb.mu.RUnlock()
logger := tb.logger.WithContext(ctx).WithFields(
log.FieldNamed("current_epoch", tb.epochInProgress),
log.FieldNamed("proposal_epoch", epoch))
switch {
case epoch < tb.epochInProgress:
logger.With().Debug("proposal too old, do not accept")
return nil
case epoch == tb.epochInProgress:
ongoing := tb.proposalPhaseFinishedTime == time.Time{}
if ongoing {
return tb.getOrCreateProposalChannel(epoch)
}
logger.With().Debug("proposal phase ended, do not accept")
return nil
case epoch == tb.epochInProgress+1:
// always accept proposals for the next epoch, but not too far in the future
logger.Debug("accepting proposal for the next epoch")
ch := tb.getOrCreateProposalChannel(epoch)
if len(ch) == proposalChanCapacity {
// the reader loop is not started for the next epoch yet. drop old messages if it's already full
// channel receive is not synchronous with length check, use select+default here to prevent potential blocking
select {
case msg := <-ch:
logger.With().Warning("proposal channel for next epoch is full, dropping oldest msg",
log.String("message", msg.message.String()))
default:
}
}
return ch
default:
return nil
}
}
// runConsensusPhase runs K voting rounds and returns result from last weak coin round.
func (tb *TortoiseBeacon) runConsensusPhase(ctx context.Context, epoch types.EpochID) (allVotes, error) {
logger := tb.logger.WithContext(ctx).WithFields(epoch)
logger.Debug("starting consensus phase")
// For K rounds: In each round that lasts δ, wait for votes to come in.
// For next rounds,
// wait for δ time, and construct a message that points to all messages from previous round received by δ.
// rounds 1 to K
timer := time.NewTimer(tb.config.FirstVotingRoundDuration)
defer timer.Stop()
var (
ownVotes allVotes
undecided []string
err error
)
for round := types.FirstRound; round <= tb.lastRound(); round++ {
round := round
rLogger := logger.WithFields(round)
votes := ownVotes
tb.eg.Go(func() error {
if round == types.FirstRound {
if err := tb.sendProposalVote(ctx, epoch); err != nil {
rLogger.With().Error("failed to send proposal vote", log.Err(err))
}
} else {
if err := tb.sendFollowingVote(ctx, epoch, round, votes); err != nil {
rLogger.With().Error("failed to send following vote", log.Err(err))
}
}
return nil
})
select {
case <-timer.C:
case <-ctx.Done():
return allVotes{}, fmt.Errorf("context done: %w", ctx.Err())
}
// note that votes after this calcVotes() call will _not_ be counted towards our votes
// for this round, as the late votes can be cast after the weak coin is revealed. we
// count them towards our votes in the next round.
ownVotes, undecided, err = tb.calcVotes(ctx, epoch, round)
if err != nil {
logger.With().Error("failed to calculate votes", log.Err(err))
}
if round != types.FirstRound {
timer.Reset(tb.config.WeakCoinRoundDuration)
tb.eg.Go(func() error {
if err := tb.weakCoin.StartRound(ctx, round); err != nil {
rLogger.With().Error("failed to publish weak coin proposal", log.Err(err))
}
return nil
})
select {
case <-timer.C:
case <-ctx.Done():
return allVotes{}, fmt.Errorf("context done: %w", ctx.Err())
}
tb.weakCoin.FinishRound(ctx)
tallyUndecided(&ownVotes, undecided, tb.weakCoin.Get(ctx, epoch, round))
}
timer.Reset(tb.config.VotingRoundDuration)
}
logger.Debug("consensus phase finished")
return ownVotes, nil
}
func (tb *TortoiseBeacon) startWeakCoinEpoch(ctx context.Context, epoch types.EpochID, atxs []types.ATXID) {
// we need to pass a map with spacetime unit allowances before any round is started
ua := weakcoin.UnitAllowances{}
for _, id := range atxs {
header, err := tb.atxDB.GetAtxHeader(id)
if err != nil {
tb.logger.WithContext(ctx).With().Panic("unable to load atx header", log.Err(err))
}
ua[string(header.NodeID.VRFPublicKey)] += uint64(header.NumUnits)
}
tb.weakCoin.StartEpoch(ctx, epoch, ua)
}
func (tb *TortoiseBeacon) markProposalPhaseFinished(epoch types.EpochID) {
finishedAt := time.Now()
tb.mu.Lock()
tb.proposalPhaseFinishedTime = finishedAt
tb.mu.Unlock()
tb.logger.Debug("marked proposal phase for epoch %v finished at %v", epoch, finishedAt.String())
}
func (tb *TortoiseBeacon) receivedBeforeProposalPhaseFinished(epoch types.EpochID, receivedAt time.Time) bool {
tb.mu.RLock()
finishedAt := tb.proposalPhaseFinishedTime
tb.mu.RUnlock()
hasFinished := !finishedAt.IsZero()
tb.logger.Debug("checking if timestamp %v was received before proposal phase finished in epoch %v, is phase finished: %v, finished at: %v", receivedAt.String(), epoch, hasFinished, finishedAt.String())
return !hasFinished || receivedAt.Before(finishedAt)
}
func (tb *TortoiseBeacon) sendProposalVote(ctx context.Context, epoch types.EpochID) error {
// round 1, send hashed proposal
// create a voting message that references all seen proposals within δ time frame and send it
// TODO(nkryuchkov): also send a bit vector
// TODO(nkryuchkov): initialize margin vector to initial votes
// TODO(nkryuchkov): use weight
tb.mu.RLock()
defer tb.mu.RUnlock()
return tb.sendFirstRoundVote(ctx, epoch, tb.incomingProposals)
}
func (tb *TortoiseBeacon) sendFirstRoundVote(ctx context.Context, epoch types.EpochID, proposals proposals) error {
mb := FirstVotingMessageBody{
EpochID: epoch,
ValidProposals: proposals.valid,
PotentiallyValidProposals: proposals.potentiallyValid,
}
sig := signMessage(tb.edSigner, mb, tb.logger)
m := FirstVotingMessage{
FirstVotingMessageBody: mb,
Signature: sig,
}
tb.logger.WithContext(ctx).With().Debug("sending first round vote",
epoch,
types.FirstRound,
log.String("message", m.String()))
if err := tb.sendToGossip(ctx, TBFirstVotingProtocol, m); err != nil {
return fmt.Errorf("sendToGossip: %w", err)
}
return nil
}
func (tb *TortoiseBeacon) sendFollowingVote(ctx context.Context, epoch types.EpochID, round types.RoundID, ownCurrentRoundVotes allVotes) error {
tb.mu.RLock()
bitVector := tb.encodeVotes(ownCurrentRoundVotes, tb.incomingProposals)
tb.mu.RUnlock()
mb := FollowingVotingMessageBody{
EpochID: epoch,
RoundID: round,
VotesBitVector: bitVector,
}
sig := signMessage(tb.edSigner, mb, tb.logger)
m := FollowingVotingMessage{
FollowingVotingMessageBody: mb,
Signature: sig,
}
tb.logger.WithContext(ctx).With().Debug("sending following round vote",
epoch,
round,
log.String("message", m.String()))
if err := tb.sendToGossip(ctx, TBFollowingVotingProtocol, m); err != nil {
return fmt.Errorf("broadcast voting message: %w", err)
}
return nil
}
func (tb *TortoiseBeacon) votingThreshold(epochWeight uint64) *big.Int {
v, _ := new(big.Float).Mul(
tb.theta,
new(big.Float).SetUint64(epochWeight),
).Int(nil)
return v
}
type proposalChecker struct {
threshold *big.Int
}
func createProposalChecker(kappa uint64, q *big.Rat, epochWeight uint64, logger log.Log) *proposalChecker {
if epochWeight == 0 {
logger.Panic("creating proposal checker with zero weight")
}
threshold := atxThreshold(kappa, q, epochWeight)
logger.With().Info("created proposal checker with ATX threshold",
log.Uint64("epoch_weight", epochWeight),
log.String("threshold", threshold.String()))
return &proposalChecker{threshold: threshold}
}
func (pc *proposalChecker) IsProposalEligible(proposal []byte) bool {
proposalInt := new(big.Int).SetBytes(proposal)
return proposalInt.Cmp(pc.threshold) == -1
}
// TODO(nkryuchkov): Consider replacing github.com/ALTree/bigfloat.
func atxThresholdFraction(kappa uint64, q *big.Rat, epochWeight uint64) *big.Float {
if epochWeight == 0 {
return big.NewFloat(0)
}
// threshold(k, q, W) = 1 - (2 ^ (- (k/((1-q)*W))))
// Floating point: 1 - math.Pow(2.0, -(float64(tb.config.Kappa)/((1.0-tb.config.Q)*float64(epochWeight))))
// Fixed point:
v := new(big.Float).Sub(
new(big.Float).SetInt64(1),
bigfloat.Pow(
new(big.Float).SetInt64(2),
new(big.Float).SetRat(
new(big.Rat).Neg(
new(big.Rat).Quo(
new(big.Rat).SetUint64(kappa),
new(big.Rat).Mul(
new(big.Rat).Sub(
new(big.Rat).SetInt64(1.0),
q,
),
new(big.Rat).SetUint64(epochWeight),
),
),
),
),
),
)
return v
}
// TODO(nkryuchkov): Consider having a generic function for probabilities.
func atxThreshold(kappa uint64, q *big.Rat, epochWeight uint64) *big.Int {
const signatureLength = 64 * 8
fraction := atxThresholdFraction(kappa, q, epochWeight)
two := big.NewInt(2)
signatureLengthBigInt := big.NewInt(signatureLength)
maxPossibleNumberBigInt := new(big.Int).Exp(two, signatureLengthBigInt, nil)
maxPossibleNumberBigFloat := new(big.Float).SetInt(maxPossibleNumberBigInt)
thresholdBigFloat := new(big.Float).Mul(maxPossibleNumberBigFloat, fraction)
threshold, _ := thresholdBigFloat.Int(nil)
return threshold
}
func buildSignedProposal(ctx context.Context, signer signing.Signer, epoch types.EpochID, logger log.Log) []byte {
p := buildProposal(epoch, logger)
signature := signer.Sign(p)
logger.WithContext(ctx).With().Debug("calculated signature",
epoch,
log.String("proposal", util.Bytes2Hex(p)),
log.String("signature", string(signature)))
return signature
}
func signMessage(signer signing.Signer, message interface{}, logger log.Log) []byte {
encoded, err := types.InterfaceToBytes(message)
if err != nil {
logger.With().Panic("failed to serialize message for signing", log.Err(err))
}
return signer.Sign(encoded)
}
func buildProposal(epoch types.EpochID, logger log.Log) []byte {
message := &struct {
Prefix string
Epoch uint32
}{
Prefix: proposalPrefix,
Epoch: uint32(epoch),
}
b, err := types.InterfaceToBytes(message)
if err != nil {
logger.With().Panic("failed to serialize proposal", log.Err(err))
}
return b
}
func (tb *TortoiseBeacon) sendToGossip(ctx context.Context, protocol string, data interface{}) error {
serialized, err := types.InterfaceToBytes(data)
if err != nil {
tb.logger.With().Panic("failed to serialize message for gossip", log.Err(err))
}
// NOTE(dshulyak) moved to goroutine because self-broadcast is applied synchronously
tb.eg.Go(func() error {
if err := tb.publisher.Publish(ctx, protocol, serialized); err != nil {
tb.logger.With().Error("failed to broadcast",
log.String("protocol", protocol),
log.Err(err),
)
}
return nil
})
return nil
}
func (tb *TortoiseBeacon) getOwnWeight(epoch types.EpochID) uint64 {
atxID, err := tb.atxDB.GetNodeAtxIDForEpoch(tb.nodeID, epoch)
if err != nil {
tb.logger.With().Error("failed to look up own ATX for epoch", epoch, log.Err(err))
return 0
}
hdr, err := tb.atxDB.GetAtxHeader(atxID)
if err != nil {
tb.logger.With().Error("failed to look up own weight for epoch", epoch, log.Err(err))
return 0
}
return hdr.GetWeight()
}
func (tb *TortoiseBeacon) gatherMetricsData() ([]*metrics.BeaconStats, *metrics.BeaconStats) {
tb.mu.RLock()