xrpld
Loading...
Searching...
No Matches
ConsensusTypes.h
1#pragma once
2
3#include <xrpl/basics/UnorderedContainers.h>
4#include <xrpl/basics/chrono.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/consensus/ConsensusProposal.h>
7#include <xrpl/consensus/DisputedTx.h>
8
9#include <chrono>
10#include <cstddef>
11#include <cstdint>
12#include <map>
13#include <optional>
14#include <string>
15
16namespace xrpl {
17
64
65inline std::string
67{
68 switch (m)
69 {
71 return "proposing";
73 return "observing";
75 return "wrongLedger";
77 return "switchedLedger";
78 default:
79 return "unknown";
80 }
81}
82
100enum class ConsensusPhase {
105
110
117};
118
119inline std::string
121{
122 switch (p)
123 {
125 return "open";
127 return "establish";
129 return "accepted";
130 default:
131 return "unknown";
132 }
133}
134
139{
140 using time_point = std::chrono::steady_clock::time_point;
143
144public:
145 [[nodiscard]] std::chrono::milliseconds
146 read() const
147 {
148 return dur_;
149 }
150
151 void
156
157 void
159 {
160 start_ = tp;
162 }
163
164 void
166 {
167 using namespace std::chrono;
169 }
170};
171
193
209{
210 using namespace std::chrono;
212
213 std::int64_t totalWeight = 1;
214 for (auto const& [_, w] : times.peers)
215 totalWeight += w;
216
217 std::int64_t const halfWeight = (totalWeight + 1) / 2;
218
220 std::int64_t tally = 0;
221 bool selfPlaced = false;
222
223 // Accumulate weight in time order; the first bin to reach halfWeight is
224 // the (lower) weighted median. Returns true once that bin is found.
225 auto step = [&](time_point t, std::int64_t w) {
226 XRPL_ASSERT(tally < halfWeight, "xrpl::medianCloseOffset::step : median not yet found");
227 tally += w;
228 if (tally >= halfWeight)
229 {
230 median = t;
231 return true;
232 }
233 return false;
234 };
235
236 for (auto const& [t, w] : times.peers)
237 {
238 if (!selfPlaced && times.self <= t)
239 {
240 selfPlaced = true;
241 if (step(times.self, 1))
242 break;
243 }
244 if (step(t, w))
245 break;
246 }
247 if (!selfPlaced && !median)
248 step(times.self, 1);
249
250 if (!median)
251 {
252 // LCOV_EXCL_START
253 UNREACHABLE("xrpl::medianCloseOffset : median not found");
254 median = times.self;
255 // LCOV_EXCL_STOP
256 }
257
259 duration<std::int64_t>{median->time_since_epoch().count()} -
261}
262
272
282template <class Traits>
284{
285 using Ledger_t = Traits::Ledger_t;
286 using TxSet_t = Traits::TxSet_t;
287 using NodeID_t = Traits::NodeID_t;
288
289 using Tx_t = TxSet_t::Tx;
292
293 ConsensusResult(TxSet_t&& s, Proposal_t&& p) : txns{std::move(s)}, position{std::move(p)}
294 {
295 XRPL_ASSERT(txns.id() == position.position(), "xrpl::ConsensusResult : valid inputs");
296 }
297
302
307
312
313 // Set of TxSet ids we have already compared/created disputes
315
316 // Measures the duration of the establish phase for this consensus round
318
319 // Indicates state in which consensus ended. Once in the accept phase
320 // will be either Yes or MovedOn or Expired
322
323 // The number of peers proposing during the round
325};
326} // namespace xrpl
Represents a proposed position taken during a round of consensus.
Measures the duration of phases of consensus.
std::chrono::milliseconds dur_
void reset(time_point tp)
void tick(std::chrono::milliseconds fixed)
std::chrono::steady_clock::time_point time_point
std::chrono::milliseconds read() const
void tick(time_point tp)
A transaction discovered to be in dispute during consensus.
Definition DisputedTx.h:36
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
T duration_cast(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
ConsensusMode
Represents how a node currently participates in Consensus.
@ WrongLedger
We have the wrong ledger and are attempting to acquire it.
@ SwitchedLedger
We switched ledgers since we started this consensus round but are now running on what we believe is t...
@ Proposing
We are normal participant in consensus and propose our position.
@ Observing
We are observing peer positions, but not proposing our position.
std::unordered_set< Value, Hash, Pred, Allocator > hash_set
static FunctionType fixed(Keylet const &keylet)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
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.
std::chrono::seconds medianCloseOffset(ConsensusCloseTimes const &times)
Offset of the network's close time relative to ours, using a weighted median.
ConsensusPhase
Phases of consensus for a single ledger round.
@ Accepted
We have accepted a new last closed ledger and are waiting on a call to startRound to begin the next c...
@ Establish
Establishing consensus by exchanging proposals with our peers.
@ Open
We haven't closed our ledger yet, but others might have.
std::unordered_map< Key, Value, Hash, Pred, Allocator > hash_map
Stores the set of initial close times.
NetClock::time_point self
Our close time estimate.
std::map< NetClock::time_point, int > peers
Close time estimates, keep ordered for predictable traverse.
ConsensusResult(TxSet_t &&s, Proposal_t &&p)
hash_map< typename Tx_t::ID, Dispute_t > disputes
ConsensusProposal< NodeID_t, typename Ledger_t::ID, typename TxSet_t::ID > Proposal_t
DisputedTx< Tx_t, NodeID_t > Dispute_t
hash_set< typename TxSet_t::ID > compares
T time_since_epoch(T... args)