xrpld
Loading...
Searching...
No Matches
Consensus.cpp
1#include <xrpld/consensus/Consensus.h>
2
3#include <xrpld/consensus/ConsensusParms.h>
4#include <xrpld/consensus/ConsensusTypes.h>
5
6#include <xrpl/basics/Log.h>
7#include <xrpl/beast/utility/Journal.h>
8
9#include <algorithm>
10#include <chrono>
11#include <cstddef>
12#include <memory>
13#include <sstream>
14
15namespace xrpl {
16
17bool
19 bool anyTransactions,
20 std::size_t prevProposers,
21 std::size_t proposersClosed,
22 std::size_t proposersValidated,
23 std::chrono::milliseconds prevRoundTime,
24 std::chrono::milliseconds timeSincePrevClose, // Time since last ledger's close time
25 std::chrono::milliseconds openTime, // Time waiting to close this ledger
26 std::chrono::milliseconds idleInterval,
27 ConsensusParms const& parms,
30{
31 CLOG(clog) << "shouldCloseLedger params anyTransactions: " << anyTransactions
32 << ", prevProposers: " << prevProposers << ", proposersClosed: " << proposersClosed
33 << ", proposersValidated: " << proposersValidated
34 << ", prevRoundTime: " << prevRoundTime.count() << "ms"
35 << ", timeSincePrevClose: " << timeSincePrevClose.count() << "ms"
36 << ", openTime: " << openTime.count() << "ms"
37 << ", idleInterval: " << idleInterval.count() << "ms"
38 << ", ledgerMIN_CLOSE: " << parms.ledgerMinClose.count() << "ms"
39 << ". ";
40 using namespace std::chrono_literals;
41 if ((prevRoundTime < -1s) || (prevRoundTime > 10min) || (timeSincePrevClose > 10min))
42 {
43 // These are unexpected cases, we just close the ledger
45 ss << "shouldCloseLedger Trans=" << (anyTransactions ? "yes" : "no")
46 << " Prop: " << prevProposers << "/" << proposersClosed
47 << " Secs: " << timeSincePrevClose.count() << " (last: " << prevRoundTime.count() << ")";
48
49 JLOG(j.warn()) << ss.str();
50 CLOG(clog) << "closing ledger: " << ss.str() << ". ";
51 return true;
52 }
53
54 if ((proposersClosed + proposersValidated) > (prevProposers / 2))
55 {
56 // If more than half of the network has closed, we close
57 JLOG(j.trace()) << "Others have closed";
58 CLOG(clog) << "closing ledger because enough others have already. ";
59 return true;
60 }
61
62 if (!anyTransactions)
63 {
64 // Only close at the end of the idle interval
65 CLOG(clog) << "no transactions, returning. ";
66 return timeSincePrevClose >= idleInterval; // normal idle
67 }
68
69 // Preserve minimum ledger open time
70 if (openTime < parms.ledgerMinClose)
71 {
72 JLOG(j.debug()) << "Must wait minimum time before closing";
73 CLOG(clog) << "not closing because under ledgerMIN_CLOSE. ";
74 return false;
75 }
76
77 // Don't let this ledger close more than twice as fast as the previous
78 // ledger reached consensus so that slower validators can slow down
79 // the network
80 if (openTime < (prevRoundTime / 2))
81 {
82 JLOG(j.debug()) << "Ledger has not been open long enough";
83 CLOG(clog) << "not closing because not open long enough. ";
84 return false;
85 }
86
87 // Close the ledger
88 CLOG(clog) << "no reason to not close. ";
89 return true;
90}
91
92bool
94 std::size_t agreeing,
95 std::size_t total,
96 bool countSelf,
97 std::size_t minConsensusPct,
98 bool reachedMax,
99 bool stalled,
101{
102 CLOG(clog) << "checkConsensusReached params: agreeing: " << agreeing << ", total: " << total
103 << ", count_self: " << countSelf << ", minConsensusPct: " << minConsensusPct
104 << ", reachedMax: " << reachedMax << ". ";
105
106 // If we are alone for too long, we have consensus.
107 // Delaying consensus like this avoids a circumstance where a peer
108 // gets ahead of proposers insofar as it has not received any proposals.
109 // This could happen if there's a slowdown in receiving proposals. Reaching
110 // consensus prematurely in this way means that the peer will likely desync.
111 // The check for reachedMax should allow plenty of time for proposals to
112 // arrive, and there should be no downside. If a peer is truly not
113 // receiving any proposals, then there should be no hurry. There's
114 // really nowhere to go.
115 if (total == 0)
116 {
117 if (reachedMax)
118 {
119 CLOG(clog) << "Consensus reached because nobody shares our position and "
120 "maximum duration has passed.";
121 return true;
122 }
123 CLOG(clog) << "Consensus not reached and nobody shares our position. ";
124 return false;
125 }
126
127 // We only get stalled when there are disputed transactions and all of them
128 // unequivocally have 80% (minConsensusPct) agreement, either for or
129 // against. That is: either under 20% or over 80% consensus (respectively
130 // "nay" or "yay"). This prevents manipulation by a minority of byzantine
131 // peers of which transactions make the cut to get into the ledger.
132 if (stalled)
133 {
134 CLOG(clog) << "consensus stalled. ";
135 return true;
136 }
137
138 if (countSelf)
139 {
140 ++agreeing;
141 ++total;
142 CLOG(clog) << "agreeing and total adjusted: " << agreeing << ',' << total << ". ";
143 }
144
145 std::size_t const currentPercentage = (agreeing * 100) / total;
146
147 CLOG(clog) << "currentPercentage: " << currentPercentage;
148 bool const ret = currentPercentage >= minConsensusPct;
149 if (ret)
150 {
151 CLOG(clog) << ", consensus reached. ";
152 }
153 else
154 {
155 CLOG(clog) << ", consensus not reached. ";
156 }
157 return ret;
158}
159
162 std::size_t prevProposers,
163 std::size_t currentProposers,
164 std::size_t currentAgree,
165 std::size_t currentFinished,
166 std::chrono::milliseconds previousAgreeTime,
167 std::chrono::milliseconds currentAgreeTime,
168 bool stalled,
169 ConsensusParms const& parms,
170 bool proposing,
173{
174 CLOG(clog) << "checkConsensus: prop=" << currentProposers << "/" << prevProposers
175 << " agree=" << currentAgree << " validated=" << currentFinished
176 << " time=" << currentAgreeTime.count() << "/" << previousAgreeTime.count()
177 << " proposing? " << proposing
178 << " minimum duration to reach consensus: " << parms.ledgerMinConsensus.count()
179 << "ms"
180 << " max consensus time " << parms.ledgerMaxConsensus.count() << "ms"
181 << " minimum consensus percentage: " << parms.minConsensusPct << ". ";
182
183 if (currentAgreeTime <= parms.ledgerMinConsensus)
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.ledgerMinConsensus))
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.minConsensusPct,
208 currentAgreeTime > parms.ledgerMaxConsensus,
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.minConsensusPct,
225 currentAgreeTime > parms.ledgerMaxConsensus,
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.ledgerAbandonConsensusFactor;
236 if (currentAgreeTime >
237 std::clamp(maxAgreeTime, parms.ledgerMaxConsensus, parms.ledgerAbandonConsensus))
238 {
239 JLOG(j.warn()) << "consensus taken too long";
240 CLOG(clog) << "Consensus taken too long. ";
241 // Note the Expired result may be overridden by the caller.
243 }
244
245 // no consensus yet
246 JLOG(j.trace()) << "no consensus";
247 CLOG(clog) << "No consensus. ";
248 return ConsensusState::No;
249}
250
251} // namespace xrpl
T clamp(T... args)
A generic endpoint for log messages.
Definition Journal.h:38
Stream debug() const
Definition Journal.h:297
Stream trace() const
Severity stream access functions.
Definition Journal.h:291
Stream warn() const
Definition Journal.h:309
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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 checkConsensusReached(std::size_t agreeing, std::size_t total, bool countSelf, std::size_t minConsensusPct, bool reachedMax, bool stalled, std::unique_ptr< std::stringstream > const &clog)
Definition Consensus.cpp:93
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:18
T str(T... args)
Consensus algorithm parameters.
std::chrono::milliseconds const ledgerMinClose
Minimum number of seconds to wait to ensure others have computed the LCL.
std::chrono::milliseconds const ledgerAbandonConsensus
Maximum amount of time to give a consensus round.
std::chrono::milliseconds const ledgerMinConsensus
The number of seconds we wait minimum to ensure participation.
std::chrono::milliseconds const ledgerMaxConsensus
The maximum amount of time to spend pausing for laggards.
std::size_t const minConsensusPct
The percentage threshold above which we can declare consensus.
std::size_t const ledgerAbandonConsensusFactor
How long to wait before completely abandoning consensus.