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