xrpld
Loading...
Searching...
No Matches
ConsensusProposal.h
1#pragma once
2
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/basics/chrono.h>
5#include <xrpl/json/json_value.h>
6#include <xrpl/protocol/HashPrefix.h>
7#include <xrpl/protocol/digest.h>
8#include <xrpl/protocol/jss.h>
9
10#include <cstdint>
11#include <optional>
12#include <sstream>
13#include <string>
14
15namespace xrpl {
40template <class NodeId, class LedgerId, class Position>
42{
43public:
44 using NodeID = NodeId;
45
46 //< Sequence value when a peer initially joins consensus
47 static std::uint32_t const kSeqJoin = 0;
48
49 //< Sequence number when a peer wants to bow out and leave consensus
50 static std::uint32_t const kSeqLeave = 0xffffffff;
51
63 LedgerId const& prevLedger,
64 std::uint32_t seq,
65 Position const& position,
68 NodeId const& nodeID)
72 , time_(now)
73 , proposeSeq_(seq)
75 {
76 }
77
81 NodeId const&
82 nodeID() const
83 {
84 return nodeID_;
85 }
86
90 Position const&
91 position() const
92 {
93 return position_;
94 }
95
99 LedgerId const&
101 {
102 return previousLedger_;
103 }
104
115 {
116 return proposeSeq_;
117 }
118
123 closeTime() const
124 {
125 return closeTime_;
126 }
127
132 seenTime() const
133 {
134 return time_;
135 }
136
141 bool
142 isInitial() const
143 {
144 return proposeSeq_ == kSeqJoin;
145 }
146
150 bool
151 isBowOut() const
152 {
153 return proposeSeq_ == kSeqLeave;
154 }
155
159 bool
161 {
162 return time_ <= cutoff;
163 }
164
173 void
175 Position const& newPosition,
176 NetClock::time_point newCloseTime,
178 {
179 signingHash_.reset();
180 position_ = newPosition;
181 closeTime_ = newCloseTime;
182 time_ = now;
183 if (proposeSeq_ != kSeqLeave)
184 ++proposeSeq_;
185 }
186
194 void
196 {
197 signingHash_.reset();
198 time_ = now;
200 }
201
203 render() const
204 {
206 ss << "proposal: previous_ledger: " << previousLedger_ << " proposal_seq: " << proposeSeq_
207 << " position: " << position_ << " close_time: " << to_string(closeTime_)
208 << " now: " << to_string(time_) << " is_bow_out:" << isBowOut()
209 << " node_id: " << nodeID_;
210 return ss.str();
211 }
212
217 getJson() const
218 {
219 using std::to_string;
220
222 ret[jss::previous_ledger] = to_string(prevLedger());
223
224 if (!isBowOut())
225 {
226 ret[jss::transaction_hash] = to_string(position());
227 ret[jss::propose_seq] = proposeSeq();
228 }
229
230 ret[jss::close_time] = to_string(closeTime().time_since_epoch().count());
231
232 return ret;
233 }
234
238 uint256 const&
240 {
241 if (!signingHash_)
242 {
246 closeTime().time_since_epoch().count(),
247 prevLedger(),
248 position());
249 }
250
251 return signingHash_.value();
252 }
253
254private:
259
263 Position position_;
264
269
270 // !The time this position was last updated
272
277
281 NodeId nodeID_;
282
287};
288
289template <class NodeId, class LedgerId, class Position>
290bool
294{
295 return a.nodeID() == b.nodeID() && a.proposeSeq() == b.proposeSeq() &&
296 a.prevLedger() == b.prevLedger() && a.position() == b.position() &&
297 a.closeTime() == b.closeTime() && a.seenTime() == b.seenTime();
298}
299} // namespace xrpl
Represents a JSON value.
Definition json_value.h:117
Represents a proposed position taken during a round of consensus.
json::Value getJson() const
Get JSON representation for debugging.
bool isInitial() const
Whether this is the first position taken during the current consensus round.
NetClock::time_point const & seenTime() const
Get when this position was taken.
uint256 const & signingHash() const
The digest for this proposal, used for signing purposes.
std::uint32_t proposeSeq() const
Get the sequence number of this proposal.
std::string render() const
ConsensusProposal(LedgerId const &prevLedger, std::uint32_t seq, Position const &position, NetClock::time_point closeTime, NetClock::time_point now, NodeId const &nodeID)
Constructor.
bool isBowOut() const
Get whether this node left the consensus process.
void bowOut(NetClock::time_point now)
Leave consensus.
void changePosition(Position const &newPosition, NetClock::time_point newCloseTime, NetClock::time_point now)
Update the position during the consensus process.
bool isStale(NetClock::time_point cutoff) const
Get whether this position is stale relative to the provided cutoff.
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
@ Object
object value (collection of name/value pairs).
Definition json_value.h:29
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition base_uint.h:606
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:215
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
@ Proposal
proposal for signing
Definition HashPrefix.h:79
BaseUInt< 256 > uint256
Definition base_uint.h:580
T str(T... args)
T to_string(T... args)