rippled
Loading...
Searching...
No Matches
Consensus.cpp
1#include <xrpld/consensus/Consensus.h>
2
3#include <xrpl/basics/Log.h>
4
5namespace xrpl {
6
7bool
9 bool anyTransactions,
10 std::size_t prevProposers,
11 std::size_t proposersClosed,
12 std::size_t proposersValidated,
13 std::chrono::milliseconds prevRoundTime,
14 std::chrono::milliseconds timeSincePrevClose, // Time since last ledger's close time
15 std::chrono::milliseconds openTime, // Time waiting to close this ledger
16 std::chrono::milliseconds idleInterval,
17 ConsensusParms const& parms,
20{
21 CLOG(clog) << "shouldCloseLedger params anyTransactions: " << anyTransactions
22 << ", prevProposers: " << prevProposers << ", proposersClosed: " << proposersClosed
23 << ", proposersValidated: " << proposersValidated << ", prevRoundTime: " << prevRoundTime.count() << "ms"
24 << ", timeSincePrevClose: " << timeSincePrevClose.count() << "ms"
25 << ", openTime: " << openTime.count() << "ms"
26 << ", idleInterval: " << idleInterval.count() << "ms"
27 << ", ledgerMIN_CLOSE: " << parms.ledgerMIN_CLOSE.count() << "ms"
28 << ". ";
29 using namespace std::chrono_literals;
30 if ((prevRoundTime < -1s) || (prevRoundTime > 10min) || (timeSincePrevClose > 10min))
31 {
32 // These are unexpected cases, we just close the ledger
34 ss << "shouldCloseLedger Trans=" << (anyTransactions ? "yes" : "no") << " Prop: " << prevProposers << "/"
35 << proposersClosed << " Secs: " << timeSincePrevClose.count() << " (last: " << prevRoundTime.count() << ")";
36
37 JLOG(j.warn()) << ss.str();
38 CLOG(clog) << "closing ledger: " << ss.str() << ". ";
39 return true;
40 }
41
42 if ((proposersClosed + proposersValidated) > (prevProposers / 2))
43 {
44 // If more than half of the network has closed, we close
45 JLOG(j.trace()) << "Others have closed";
46 CLOG(clog) << "closing ledger because enough others have already. ";
47 return true;
48 }
49
50 if (!anyTransactions)
51 {
52 // Only close at the end of the idle interval
53 CLOG(clog) << "no transactions, returning. ";
54 return timeSincePrevClose >= idleInterval; // normal idle
55 }
56
57 // Preserve minimum ledger open time
58 if (openTime < parms.ledgerMIN_CLOSE)
59 {
60 JLOG(j.debug()) << "Must wait minimum time before closing";
61 CLOG(clog) << "not closing because under ledgerMIN_CLOSE. ";
62 return false;
63 }
64
65 // Don't let this ledger close more than twice as fast as the previous
66 // ledger reached consensus so that slower validators can slow down
67 // the network
68 if (openTime < (prevRoundTime / 2))
69 {
70 JLOG(j.debug()) << "Ledger has not been open long enough";
71 CLOG(clog) << "not closing because not open long enough. ";
72 return false;
73 }
74
75 // Close the ledger
76 CLOG(clog) << "no reason to not close. ";
77 return true;
78}
79
80bool
82 std::size_t agreeing,
83 std::size_t total,
84 bool count_self,
85 std::size_t minConsensusPct,
86 bool reachedMax,
87 bool stalled,
89{
90 CLOG(clog) << "checkConsensusReached params: agreeing: " << agreeing << ", total: " << total
91 << ", count_self: " << count_self << ", minConsensusPct: " << minConsensusPct
92 << ", reachedMax: " << reachedMax << ". ";
93
94 // If we are alone for too long, we have consensus.
95 // Delaying consensus like this avoids a circumstance where a peer
96 // gets ahead of proposers insofar as it has not received any proposals.
97 // This could happen if there's a slowdown in receiving proposals. Reaching
98 // consensus prematurely in this way means that the peer will likely desync.
99 // The check for reachedMax should allow plenty of time for proposals to
100 // arrive, and there should be no downside. If a peer is truly not
101 // receiving any proposals, then there should be no hurry. There's
102 // really nowhere to go.
103 if (total == 0)
104 {
105 if (reachedMax)
106 {
107 CLOG(clog) << "Consensus reached because nobody shares our position and "
108 "maximum duration has passed.";
109 return true;
110 }
111 CLOG(clog) << "Consensus not reached and nobody shares our position. ";
112 return false;
113 }
114
115 // We only get stalled when there are disputed transactions and all of them
116 // unequivocally have 80% (minConsensusPct) agreement, either for or
117 // against. That is: either under 20% or over 80% consensus (respectively
118 // "nay" or "yay"). This prevents manipulation by a minority of byzantine
119 // peers of which transactions make the cut to get into the ledger.
120 if (stalled)
121 {
122 CLOG(clog) << "consensus stalled. ";
123 return true;
124 }
125
126 if (count_self)
127 {
128 ++agreeing;
129 ++total;
130 CLOG(clog) << "agreeing and total adjusted: " << agreeing << ',' << total << ". ";
131 }
132
133 std::size_t currentPercentage = (agreeing * 100) / total;
134
135 CLOG(clog) << "currentPercentage: " << currentPercentage;
136 bool const ret = currentPercentage >= minConsensusPct;
137 if (ret)
138 {
139 CLOG(clog) << ", consensus reached. ";
140 }
141 else
142 {
143 CLOG(clog) << ", consensus not reached. ";
144 }
145 return ret;
146}
147
150 std::size_t prevProposers,
151 std::size_t currentProposers,
152 std::size_t currentAgree,
153 std::size_t currentFinished,
154 std::chrono::milliseconds previousAgreeTime,
155 std::chrono::milliseconds currentAgreeTime,
156 bool stalled,
157 ConsensusParms const& parms,
158 bool proposing,
161{
162 CLOG(clog) << "checkConsensus: prop=" << currentProposers << "/" << prevProposers << " agree=" << currentAgree
163 << " validated=" << currentFinished << " time=" << currentAgreeTime.count() << "/"
164 << previousAgreeTime.count() << " proposing? " << proposing
165 << " minimum duration to reach consensus: " << parms.ledgerMIN_CONSENSUS.count() << "ms"
166 << " max consensus time " << parms.ledgerMAX_CONSENSUS.count() << "ms"
167 << " minimum consensus percentage: " << parms.minCONSENSUS_PCT << ". ";
168
169 if (currentAgreeTime <= parms.ledgerMIN_CONSENSUS)
170 {
171 CLOG(clog) << "Not reached. ";
172 return ConsensusState::No;
173 }
174
175 if (currentProposers < (prevProposers * 3 / 4))
176 {
177 // Less than 3/4 of the last ledger's proposers are present; don't
178 // rush: we may need more time.
179 if (currentAgreeTime < (previousAgreeTime + parms.ledgerMIN_CONSENSUS))
180 {
181 JLOG(j.trace()) << "too fast, not enough proposers";
182 CLOG(clog) << "Too fast, not enough proposers. Not reached. ";
183 return ConsensusState::No;
184 }
185 }
186
187 // Have we, together with the nodes on our UNL list, reached the threshold
188 // to declare consensus?
190 currentAgree,
191 currentProposers,
192 proposing,
193 parms.minCONSENSUS_PCT,
194 currentAgreeTime > parms.ledgerMAX_CONSENSUS,
195 stalled,
196 clog))
197 {
198 JLOG((stalled ? j.warn() : j.debug())) << "normal consensus" << (stalled ? ", but stalled" : "");
199 CLOG(clog) << "reached" << (stalled ? ", but stalled." : ".");
200 return ConsensusState::Yes;
201 }
202
203 // Have sufficient nodes on our UNL list moved on and reached the threshold
204 // to declare consensus?
206 currentFinished,
207 currentProposers,
208 false,
209 parms.minCONSENSUS_PCT,
210 currentAgreeTime > parms.ledgerMAX_CONSENSUS,
211 false,
212 clog))
213 {
214 JLOG(j.warn()) << "We see no consensus, but 80% of nodes have moved on";
215 CLOG(clog) << "We see no consensus, but 80% of nodes have moved on";
217 }
218
219 std::chrono::milliseconds const maxAgreeTime = previousAgreeTime * parms.ledgerABANDON_CONSENSUS_FACTOR;
220 if (currentAgreeTime > std::clamp(maxAgreeTime, parms.ledgerMAX_CONSENSUS, parms.ledgerABANDON_CONSENSUS))
221 {
222 JLOG(j.warn()) << "consensus taken too long";
223 CLOG(clog) << "Consensus taken too long. ";
224 // Note the Expired result may be overridden by the caller.
226 }
227
228 // no consensus yet
229 JLOG(j.trace()) << "no consensus";
230 CLOG(clog) << "No consensus. ";
231 return ConsensusState::No;
232}
233
234} // namespace xrpl
T clamp(T... args)
A generic endpoint for log messages.
Definition Journal.h:41
Stream debug() const
Definition Journal.h:301
Stream trace() const
Severity stream access functions.
Definition Journal.h:295
Stream warn() const
Definition Journal.h:313
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ proposing
We are normal participant in consensus and propose our position.
bool checkConsensusReached(std::size_t agreeing, std::size_t total, bool count_self, std::size_t minConsensusPct, bool reachedMax, bool stalled, std::unique_ptr< std::stringstream > const &clog)
Definition Consensus.cpp:81
ConsensusState checkConsensus(std::size_t prevProposers, std::size_t currentProposers, std::size_t currentAgree, std::size_t currentFinished, std::chrono::milliseconds previousAgreeTime, std::chrono::milliseconds currentAgreeTime, bool stalled, ConsensusParms const &parms, bool proposing, beast::Journal j, std::unique_ptr< std::stringstream > const &clog)
Determine whether the network reached consensus and whether we joined.
ConsensusState
Whether we have or don't have a consensus.
@ Expired
Consensus time limit has hard-expired.
@ MovedOn
The network has consensus without us.
@ Yes
We have consensus along with the network.
@ No
We do not have consensus.
bool shouldCloseLedger(bool anyTransactions, std::size_t prevProposers, std::size_t proposersClosed, std::size_t proposersValidated, std::chrono::milliseconds prevRoundTime, std::chrono::milliseconds timeSincePrevClose, std::chrono::milliseconds openTime, std::chrono::milliseconds idleInterval, ConsensusParms const &parms, beast::Journal j, std::unique_ptr< std::stringstream > const &clog)
Determines whether the current ledger should close at this time.
Definition Consensus.cpp:8
T str(T... args)
Consensus algorithm parameters.
std::chrono::milliseconds const ledgerABANDON_CONSENSUS
Maximum amount of time to give a consensus round.
std::size_t const minCONSENSUS_PCT
The percentage threshold above which we can declare consensus.
std::chrono::milliseconds const ledgerMAX_CONSENSUS
The maximum amount of time to spend pausing for laggards.
std::chrono::milliseconds const ledgerMIN_CONSENSUS
The number of seconds we wait minimum to ensure participation.
std::chrono::milliseconds const ledgerMIN_CLOSE
Minimum number of seconds to wait to ensure others have computed the LCL.
std::size_t const ledgerABANDON_CONSENSUS_FACTOR
How long to wait before completely abandoning consensus.