forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBallotProtocol.cpp
2189 lines (1965 loc) · 58.4 KB
/
BallotProtocol.cpp
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
// Copyright 2014 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "BallotProtocol.h"
#include "Slot.h"
#include "crypto/Hex.h"
#include "crypto/SHA.h"
#include "lib/json/json.h"
#include "scp/LocalNode.h"
#include "scp/QuorumSetUtils.h"
#include "util/GlobalChecks.h"
#include "util/Logging.h"
#include "util/XDROperators.h"
#include "xdrpp/marshal.h"
#include <functional>
namespace stellar
{
using namespace std::placeholders;
// max number of transitions that can occur from processing one message
static const int MAX_ADVANCE_SLOT_RECURSION = 50;
BallotProtocol::BallotProtocol(Slot& slot)
: mSlot(slot)
, mHeardFromQuorum(false)
, mPhase(SCP_PHASE_PREPARE)
, mCurrentMessageLevel(0)
{
}
bool
BallotProtocol::isNewerStatement(NodeID const& nodeID, SCPStatement const& st)
{
auto oldp = mLatestEnvelopes.find(nodeID);
bool res = false;
if (oldp == mLatestEnvelopes.end())
{
res = true;
}
else
{
res = isNewerStatement(oldp->second.statement, st);
}
return res;
}
bool
BallotProtocol::isNewerStatement(SCPStatement const& oldst,
SCPStatement const& st)
{
bool res = false;
// total ordering described in SCP paper.
auto t = st.pledges.type();
// statement type (PREPARE < CONFIRM < EXTERNALIZE)
if (oldst.pledges.type() != t)
{
res = (oldst.pledges.type() < t);
}
else
{
// can't have duplicate EXTERNALIZE statements
if (t == SCPStatementType::SCP_ST_EXTERNALIZE)
{
res = false;
}
else if (t == SCPStatementType::SCP_ST_CONFIRM)
{
// sorted by (b, p, p', h) (p' = 0 implicitely)
auto const& oldC = oldst.pledges.confirm();
auto const& c = st.pledges.confirm();
int compBallot = compareBallots(oldC.ballot, c.ballot);
if (compBallot < 0)
{
res = true;
}
else if (compBallot == 0)
{
if (oldC.nPrepared == c.nPrepared)
{
res = (oldC.nH < c.nH);
}
else
{
res = (oldC.nPrepared < c.nPrepared);
}
}
}
else
{
// Lexicographical order between PREPARE statements:
// (b, p, p', h)
auto const& oldPrep = oldst.pledges.prepare();
auto const& prep = st.pledges.prepare();
int compBallot = compareBallots(oldPrep.ballot, prep.ballot);
if (compBallot < 0)
{
res = true;
}
else if (compBallot == 0)
{
compBallot = compareBallots(oldPrep.prepared, prep.prepared);
if (compBallot < 0)
{
res = true;
}
else if (compBallot == 0)
{
compBallot = compareBallots(oldPrep.preparedPrime,
prep.preparedPrime);
if (compBallot < 0)
{
res = true;
}
else if (compBallot == 0)
{
res = (oldPrep.nH < prep.nH);
}
}
}
}
}
return res;
}
void
BallotProtocol::recordEnvelope(SCPEnvelope const& env)
{
auto const& st = env.statement;
auto oldp = mLatestEnvelopes.find(st.nodeID);
if (oldp == mLatestEnvelopes.end())
{
mLatestEnvelopes.insert(std::make_pair(st.nodeID, env));
}
else
{
oldp->second = env;
}
mSlot.recordStatement(env.statement);
}
SCP::EnvelopeState
BallotProtocol::processEnvelope(SCPEnvelope const& envelope, bool self)
{
SCP::EnvelopeState res = SCP::EnvelopeState::INVALID;
dbgAssert(envelope.statement.slotIndex == mSlot.getSlotIndex());
SCPStatement const& statement = envelope.statement;
NodeID const& nodeID = statement.nodeID;
if (!isStatementSane(statement, self))
{
if (self)
{
CLOG(ERROR, "SCP") << "not sane statement from self, skipping "
<< " e: " << mSlot.getSCP().envToStr(envelope);
}
return SCP::EnvelopeState::INVALID;
}
if (!isNewerStatement(nodeID, statement))
{
if (self)
{
CLOG(ERROR, "SCP") << "stale statement from self, skipping "
<< " e: " << mSlot.getSCP().envToStr(envelope);
}
else
{
CLOG(TRACE, "SCP") << "stale statement, skipping "
<< " i: " << mSlot.getSlotIndex();
}
return SCP::EnvelopeState::INVALID;
}
auto validationRes = validateValues(statement);
if (validationRes != SCPDriver::kInvalidValue)
{
bool processed = false;
if (mPhase != SCP_PHASE_EXTERNALIZE)
{
if (validationRes == SCPDriver::kMaybeValidValue)
{
mSlot.setFullyValidated(false);
}
recordEnvelope(envelope);
processed = true;
advanceSlot(statement);
res = SCP::EnvelopeState::VALID;
}
if (!processed)
{
// note: this handles also our own messages
// in particular our final EXTERNALIZE message
if (mPhase == SCP_PHASE_EXTERNALIZE &&
mCommit->value == getWorkingBallot(statement).value)
{
recordEnvelope(envelope);
res = SCP::EnvelopeState::VALID;
}
else
{
if (self)
{
CLOG(ERROR, "SCP")
<< "externalize statement with invalid value from "
"self, skipping "
<< " e: " << mSlot.getSCP().envToStr(envelope);
}
res = SCP::EnvelopeState::INVALID;
}
}
}
else
{
// If the value is not valid, we just ignore it.
if (self)
{
CLOG(ERROR, "SCP") << "invalid value from self, skipping "
<< " e: " << mSlot.getSCP().envToStr(envelope);
}
else
{
CLOG(TRACE, "SCP") << "invalid value "
<< " i: " << mSlot.getSlotIndex();
}
res = SCP::EnvelopeState::INVALID;
}
return res;
}
bool
BallotProtocol::isStatementSane(SCPStatement const& st, bool self)
{
auto qSet = mSlot.getQuorumSetFromStatement(st);
bool res = qSet != nullptr && isQuorumSetSane(*qSet, false);
if (!res)
{
CLOG(DEBUG, "SCP") << "Invalid quorum set received";
return false;
}
switch (st.pledges.type())
{
case SCPStatementType::SCP_ST_PREPARE:
{
auto const& p = st.pledges.prepare();
// self is allowed to have b = 0 (as long as it never gets emitted)
bool isOK = self || p.ballot.counter > 0;
isOK = isOK &&
((!p.preparedPrime || !p.prepared) ||
(areBallotsLessAndIncompatible(*p.preparedPrime, *p.prepared)));
isOK =
isOK && (p.nH == 0 || (p.prepared && p.nH <= p.prepared->counter));
// c != 0 -> c <= h <= b
isOK = isOK && (p.nC == 0 || (p.nH != 0 && p.ballot.counter >= p.nH &&
p.nH >= p.nC));
if (!isOK)
{
CLOG(TRACE, "SCP") << "Malformed PREPARE message";
res = false;
}
}
break;
case SCPStatementType::SCP_ST_CONFIRM:
{
auto const& c = st.pledges.confirm();
// c <= h <= b
res = c.ballot.counter > 0;
res = res && (c.nH <= c.ballot.counter);
res = res && (c.nCommit <= c.nH);
if (!res)
{
CLOG(TRACE, "SCP") << "Malformed CONFIRM message";
}
}
break;
case SCPStatementType::SCP_ST_EXTERNALIZE:
{
auto const& e = st.pledges.externalize();
res = e.commit.counter > 0;
res = res && e.nH >= e.commit.counter;
if (!res)
{
CLOG(TRACE, "SCP") << "Malformed EXTERNALIZE message";
}
}
break;
default:
dbgAbort();
}
return res;
}
bool
BallotProtocol::abandonBallot(uint32 n)
{
CLOG(TRACE, "SCP") << "BallotProtocol::abandonBallot";
bool res = false;
Value v = mSlot.getLatestCompositeCandidate();
if (v.empty())
{
if (mCurrentBallot)
{
v = mCurrentBallot->value;
}
}
if (!v.empty())
{
if (n == 0)
{
res = bumpState(v, true);
}
else
{
res = bumpState(v, n);
}
}
return res;
}
bool
BallotProtocol::bumpState(Value const& value, bool force)
{
uint32 n;
if (!force && mCurrentBallot)
{
return false;
}
n = mCurrentBallot ? (mCurrentBallot->counter + 1) : 1;
return bumpState(value, n);
}
bool
BallotProtocol::bumpState(Value const& value, uint32 n)
{
if (mPhase != SCP_PHASE_PREPARE && mPhase != SCP_PHASE_CONFIRM)
{
return false;
}
SCPBallot newb;
newb.counter = n;
if (mValueOverride)
{
// we use the value that we saw confirmed prepared
// or that we at least voted to commit to
newb.value = *mValueOverride;
}
else
{
newb.value = value;
}
if (Logging::logTrace("SCP"))
CLOG(TRACE, "SCP") << "BallotProtocol::bumpState"
<< " i: " << mSlot.getSlotIndex()
<< " v: " << mSlot.getSCP().ballotToStr(newb);
bool updated = updateCurrentValue(newb);
if (updated)
{
emitCurrentStateStatement();
checkHeardFromQuorum();
}
return updated;
}
// updates the local state based to the specified ballot
// (that could be a prepared ballot) enforcing invariants
bool
BallotProtocol::updateCurrentValue(SCPBallot const& ballot)
{
if (mPhase != SCP_PHASE_PREPARE && mPhase != SCP_PHASE_CONFIRM)
{
return false;
}
bool updated = false;
if (!mCurrentBallot)
{
bumpToBallot(ballot, true);
updated = true;
}
else
{
dbgAssert(compareBallots(*mCurrentBallot, ballot) <= 0);
if (mCommit && !areBallotsCompatible(*mCommit, ballot))
{
return false;
}
int comp = compareBallots(*mCurrentBallot, ballot);
if (comp < 0)
{
bumpToBallot(ballot, true);
updated = true;
}
else if (comp > 0)
{
// this code probably changes with the final version
// of the conciliator
// this case may happen if the other nodes are not
// following the protocol (and we end up with a smaller value)
// not sure what is the best way to deal
// with this situation
CLOG(ERROR, "SCP")
<< "BallotProtocol::updateCurrentValue attempt to bump to "
"a smaller value";
// can't just bump to the value as we may already have
// statements at counter+1
return false;
}
}
if (updated)
{
CLOG(TRACE, "SCP") << "BallotProtocol::updateCurrentValue updated";
}
checkInvariants();
return updated;
}
void
BallotProtocol::bumpToBallot(SCPBallot const& ballot, bool check)
{
if (Logging::logTrace("SCP"))
CLOG(TRACE, "SCP") << "BallotProtocol::bumpToBallot"
<< " i: " << mSlot.getSlotIndex()
<< " b: " << mSlot.getSCP().ballotToStr(ballot);
// `bumpToBallot` should be never called once we committed.
dbgAssert(mPhase != SCP_PHASE_EXTERNALIZE);
if (check)
{
// We should move mCurrentBallot monotonically only
dbgAssert(!mCurrentBallot ||
compareBallots(ballot, *mCurrentBallot) >= 0);
}
bool gotBumped =
!mCurrentBallot || (mCurrentBallot->counter != ballot.counter);
if (!mCurrentBallot)
{
mSlot.getSCPDriver().startedBallotProtocol(mSlot.getSlotIndex(),
ballot);
}
mCurrentBallot = std::make_unique<SCPBallot>(ballot);
// invariant: h.value = b.value
if (mHighBallot && !areBallotsCompatible(*mCurrentBallot, *mHighBallot))
{
mHighBallot.reset();
}
if (gotBumped)
{
mHeardFromQuorum = false;
}
}
void
BallotProtocol::startBallotProtocolTimer()
{
std::chrono::milliseconds timeout =
mSlot.getSCPDriver().computeTimeout(mCurrentBallot->counter);
std::shared_ptr<Slot> slot = mSlot.shared_from_this();
mSlot.getSCPDriver().setupTimer(
mSlot.getSlotIndex(), Slot::BALLOT_PROTOCOL_TIMER, timeout,
[slot]() { slot->getBallotProtocol().ballotProtocolTimerExpired(); });
}
void
BallotProtocol::stopBallotProtocolTimer()
{
std::shared_ptr<Slot> slot = mSlot.shared_from_this();
mSlot.getSCPDriver().setupTimer(mSlot.getSlotIndex(),
Slot::BALLOT_PROTOCOL_TIMER,
std::chrono::seconds::zero(), nullptr);
}
void
BallotProtocol::ballotProtocolTimerExpired()
{
abandonBallot(0);
}
SCPStatement
BallotProtocol::createStatement(SCPStatementType const& type)
{
SCPStatement statement;
checkInvariants();
statement.pledges.type(type);
switch (type)
{
case SCPStatementType::SCP_ST_PREPARE:
{
auto& p = statement.pledges.prepare();
p.quorumSetHash = getLocalNode()->getQuorumSetHash();
if (mCurrentBallot)
{
p.ballot = *mCurrentBallot;
}
if (mCommit)
{
p.nC = mCommit->counter;
}
if (mPrepared)
{
p.prepared.activate() = *mPrepared;
}
if (mPreparedPrime)
{
p.preparedPrime.activate() = *mPreparedPrime;
}
if (mHighBallot)
{
p.nH = mHighBallot->counter;
}
}
break;
case SCPStatementType::SCP_ST_CONFIRM:
{
auto& c = statement.pledges.confirm();
c.quorumSetHash = getLocalNode()->getQuorumSetHash();
c.ballot = *mCurrentBallot;
c.nPrepared = mPrepared->counter;
c.nCommit = mCommit->counter;
c.nH = mHighBallot->counter;
}
break;
case SCPStatementType::SCP_ST_EXTERNALIZE:
{
auto& e = statement.pledges.externalize();
e.commit = *mCommit;
e.nH = mHighBallot->counter;
e.commitQuorumSetHash = getLocalNode()->getQuorumSetHash();
}
break;
default:
dbgAbort();
}
return statement;
}
void
BallotProtocol::emitCurrentStateStatement()
{
SCPStatementType t;
switch (mPhase)
{
case SCP_PHASE_PREPARE:
t = SCP_ST_PREPARE;
break;
case SCP_PHASE_CONFIRM:
t = SCP_ST_CONFIRM;
break;
case SCP_PHASE_EXTERNALIZE:
t = SCP_ST_EXTERNALIZE;
break;
default:
dbgAbort();
}
SCPStatement statement = createStatement(t);
SCPEnvelope envelope = mSlot.createEnvelope(statement);
bool canEmit = (mCurrentBallot != nullptr);
// if we generate the same envelope, don't process it again
// this can occur when updating h in PREPARE phase
// as statements only keep track of h.n (but h.x could be different)
auto lastEnv = mLatestEnvelopes.find(mSlot.getSCP().getLocalNodeID());
if (lastEnv == mLatestEnvelopes.end() || !(lastEnv->second == envelope))
{
if (mSlot.processEnvelope(envelope, true) == SCP::EnvelopeState::VALID)
{
if (canEmit &&
(!mLastEnvelope || isNewerStatement(mLastEnvelope->statement,
envelope.statement)))
{
mLastEnvelope = std::make_shared<SCPEnvelope>(envelope);
// this will no-op if invoked from advanceSlot
// as advanceSlot consolidates all messages sent
sendLatestEnvelope();
}
}
else
{
// there is a bug in the application if it queued up
// a statement for itself that it considers invalid
throw std::runtime_error("moved to a bad state (ballot protocol)");
}
}
}
void
BallotProtocol::checkInvariants()
{
if (mCurrentBallot)
{
dbgAssert(mCurrentBallot->counter != 0);
}
if (mPrepared && mPreparedPrime)
{
dbgAssert(areBallotsLessAndIncompatible(*mPreparedPrime, *mPrepared));
}
if (mHighBallot)
{
dbgAssert(mCurrentBallot);
dbgAssert(areBallotsLessAndCompatible(*mHighBallot, *mCurrentBallot));
}
if (mCommit)
{
dbgAssert(mCurrentBallot);
dbgAssert(areBallotsLessAndCompatible(*mCommit, *mHighBallot));
dbgAssert(areBallotsLessAndCompatible(*mHighBallot, *mCurrentBallot));
}
switch (mPhase)
{
case SCP_PHASE_PREPARE:
break;
case SCP_PHASE_CONFIRM:
dbgAssert(mCommit);
break;
case SCP_PHASE_EXTERNALIZE:
dbgAssert(mCommit);
dbgAssert(mHighBallot);
break;
default:
dbgAbort();
}
}
std::set<SCPBallot>
BallotProtocol::getPrepareCandidates(SCPStatement const& hint)
{
std::set<SCPBallot> hintBallots;
switch (hint.pledges.type())
{
case SCP_ST_PREPARE:
{
auto const& prep = hint.pledges.prepare();
hintBallots.insert(prep.ballot);
if (prep.prepared)
{
hintBallots.insert(*prep.prepared);
}
if (prep.preparedPrime)
{
hintBallots.insert(*prep.preparedPrime);
}
}
break;
case SCP_ST_CONFIRM:
{
auto const& con = hint.pledges.confirm();
hintBallots.insert(SCPBallot(con.nPrepared, con.ballot.value));
hintBallots.insert(SCPBallot(UINT32_MAX, con.ballot.value));
}
break;
case SCP_ST_EXTERNALIZE:
{
auto const& ext = hint.pledges.externalize();
hintBallots.insert(SCPBallot(UINT32_MAX, ext.commit.value));
}
break;
default:
abort();
};
std::set<SCPBallot> candidates;
while (!hintBallots.empty())
{
auto last = --hintBallots.end();
SCPBallot topVote = *last;
hintBallots.erase(last);
auto const& val = topVote.value;
// find candidates that may have been prepared
for (auto const& e : mLatestEnvelopes)
{
SCPStatement const& st = e.second.statement;
switch (st.pledges.type())
{
case SCP_ST_PREPARE:
{
auto const& prep = st.pledges.prepare();
if (areBallotsLessAndCompatible(prep.ballot, topVote))
{
candidates.insert(prep.ballot);
}
if (prep.prepared &&
areBallotsLessAndCompatible(*prep.prepared, topVote))
{
candidates.insert(*prep.prepared);
}
if (prep.preparedPrime &&
areBallotsLessAndCompatible(*prep.preparedPrime, topVote))
{
candidates.insert(*prep.preparedPrime);
}
}
break;
case SCP_ST_CONFIRM:
{
auto const& con = st.pledges.confirm();
if (areBallotsCompatible(topVote, con.ballot))
{
candidates.insert(topVote);
if (con.nPrepared < topVote.counter)
{
candidates.insert(SCPBallot(con.nPrepared, val));
}
}
}
break;
case SCP_ST_EXTERNALIZE:
{
auto const& ext = st.pledges.externalize();
if (areBallotsCompatible(topVote, ext.commit))
{
candidates.insert(topVote);
}
}
break;
default:
abort();
}
}
}
return candidates;
}
bool
BallotProtocol::updateCurrentIfNeeded(SCPBallot const& h)
{
bool didWork = false;
if (!mCurrentBallot || compareBallots(*mCurrentBallot, h) < 0)
{
bumpToBallot(h, true);
didWork = true;
}
return didWork;
}
bool
BallotProtocol::attemptPreparedAccept(SCPStatement const& hint)
{
if (mPhase != SCP_PHASE_PREPARE && mPhase != SCP_PHASE_CONFIRM)
{
return false;
}
auto candidates = getPrepareCandidates(hint);
// see if we can accept any of the candidates, starting with the highest
for (auto cur = candidates.rbegin(); cur != candidates.rend(); cur++)
{
SCPBallot ballot = *cur;
if (mPhase == SCP_PHASE_CONFIRM)
{
// only consider the ballot if it may help us increase
// p (note: at this point, p ~ c)
if (!areBallotsLessAndCompatible(*mPrepared, ballot))
{
continue;
}
dbgAssert(areBallotsCompatible(*mCommit, ballot));
}
// if we already prepared this ballot, don't bother checking again
// if ballot <= p' ballot is neither a candidate for p nor p'
if (mPreparedPrime && compareBallots(ballot, *mPreparedPrime) <= 0)
{
continue;
}
if (mPrepared)
{
// if ballot is already covered by p, skip
if (areBallotsLessAndCompatible(ballot, *mPrepared))
{
continue;
}
// otherwise, there is a chance it increases p'
}
bool accepted = federatedAccept(
// checks if any node is voting for this ballot
[&ballot](SCPStatement const& st) {
bool res;
switch (st.pledges.type())
{
case SCP_ST_PREPARE:
{
auto const& p = st.pledges.prepare();
res = areBallotsLessAndCompatible(ballot, p.ballot);
}
break;
case SCP_ST_CONFIRM:
{
auto const& c = st.pledges.confirm();
res = areBallotsCompatible(ballot, c.ballot);
}
break;
case SCP_ST_EXTERNALIZE:
{
auto const& e = st.pledges.externalize();
res = areBallotsCompatible(ballot, e.commit);
}
break;
default:
res = false;
dbgAbort();
}
return res;
},
std::bind(&BallotProtocol::hasPreparedBallot, ballot, _1));
if (accepted)
{
return setPreparedAccept(ballot);
}
}
return false;
}
bool
BallotProtocol::setPreparedAccept(SCPBallot const& ballot)
{
if (Logging::logTrace("SCP"))
CLOG(TRACE, "SCP") << "BallotProtocol::setPreparedAccept"
<< " i: " << mSlot.getSlotIndex()
<< " b: " << mSlot.getSCP().ballotToStr(ballot);
// update our state
bool didWork = setPrepared(ballot);
// check if we also need to clear 'c'
if (mCommit && mHighBallot)
{
if ((mPrepared &&
areBallotsLessAndIncompatible(*mHighBallot, *mPrepared)) ||
(mPreparedPrime &&
areBallotsLessAndIncompatible(*mHighBallot, *mPreparedPrime)))
{
dbgAssert(mPhase == SCP_PHASE_PREPARE);
mCommit.reset();
didWork = true;
}
}
if (didWork)
{
mSlot.getSCPDriver().acceptedBallotPrepared(mSlot.getSlotIndex(),
ballot);
emitCurrentStateStatement();
}
return didWork;
}
bool
BallotProtocol::attemptPreparedConfirmed(SCPStatement const& hint)
{
if (mPhase != SCP_PHASE_PREPARE)
{
return false;
}
// check if we could accept this ballot as prepared
if (!mPrepared)
{
return false;
}
auto candidates = getPrepareCandidates(hint);
// see if we can accept any of the candidates, starting with the highest
SCPBallot newH;
bool newHfound = false;
auto cur = candidates.rbegin();
for (; cur != candidates.rend(); cur++)
{
SCPBallot ballot = *cur;
// only consider it if we can potentially raise h
if (mHighBallot && compareBallots(*mHighBallot, ballot) >= 0)
{
break;
}
bool ratified = federatedRatify(
std::bind(&BallotProtocol::hasPreparedBallot, ballot, _1));
if (ratified)
{
newH = ballot;
newHfound = true;
break;
}
}
bool res = false;
if (newHfound)
{
SCPBallot newC;
// now, look for newC (left as 0 if no update)
// step (3) from the paper
SCPBallot b = mCurrentBallot ? *mCurrentBallot : SCPBallot();
if (!mCommit &&
(!mPrepared || !areBallotsLessAndIncompatible(newH, *mPrepared)) &&
(!mPreparedPrime ||
!areBallotsLessAndIncompatible(newH, *mPreparedPrime)))
{
// continue where we left off (cur is at newH at this point)
for (; cur != candidates.rend(); cur++)
{
SCPBallot ballot = *cur;
if (compareBallots(ballot, b) < 0)
{
break;
}
// c and h must be compatible
if (!areBallotsLessAndCompatible(*cur, newH))
{
continue;
}
bool ratified = federatedRatify(
std::bind(&BallotProtocol::hasPreparedBallot, ballot, _1));
if (ratified)
{
newC = ballot;
}
else
{
break;
}
}
}
res = setPreparedConfirmed(newC, newH);
}
return res;
}
bool
BallotProtocol::commitPredicate(SCPBallot const& ballot, Interval const& check,
SCPStatement const& st)
{
bool res = false;