rippled
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
14namespace xrpl {
38template <class NodeID_t, class LedgerID_t, class Position_t>
40{
41public:
42 using NodeID = NodeID_t;
43
44 //< Sequence value when a peer initially joins consensus
45 static std::uint32_t const seqJoin = 0;
46
47 //< Sequence number when a peer wants to bow out and leave consensus
48 static std::uint32_t const seqLeave = 0xffffffff;
49
60 LedgerID_t const& prevLedger,
61 std::uint32_t seq,
62 Position_t const& position,
65 NodeID_t const& nodeID)
69 , time_(now)
70 , proposeSeq_(seq)
72 {
73 }
74
76 NodeID_t const&
77 nodeID() const
78 {
79 return nodeID_;
80 }
81
83 Position_t const&
84 position() const
85 {
86 return position_;
87 }
88
90 LedgerID_t const&
91 prevLedger() const
92 {
93 return previousLedger_;
94 }
95
105 {
106 return proposeSeq_;
107 }
108
111 closeTime() const
112 {
113 return closeTime_;
114 }
115
118 seenTime() const
119 {
120 return time_;
121 }
122
126 bool
127 isInitial() const
128 {
129 return proposeSeq_ == seqJoin;
130 }
131
133 bool
134 isBowOut() const
135 {
136 return proposeSeq_ == seqLeave;
137 }
138
140 bool
142 {
143 return time_ <= cutoff;
144 }
145
153 void
155 Position_t const& newPosition,
156 NetClock::time_point newCloseTime,
158 {
159 signingHash_.reset();
160 position_ = newPosition;
161 closeTime_ = newCloseTime;
162 time_ = now;
163 if (proposeSeq_ != seqLeave)
164 ++proposeSeq_;
165 }
166
173 void
175 {
176 signingHash_.reset();
177 time_ = now;
179 }
180
182 render() const
183 {
185 ss << "proposal: previous_ledger: " << previousLedger_ << " proposal_seq: " << proposeSeq_
186 << " position: " << position_ << " close_time: " << to_string(closeTime_)
187 << " now: " << to_string(time_) << " is_bow_out:" << isBowOut()
188 << " node_id: " << nodeID_;
189 return ss.str();
190 }
191
194 getJson() const
195 {
196 using std::to_string;
197
199 ret[jss::previous_ledger] = to_string(prevLedger());
200
201 if (!isBowOut())
202 {
203 ret[jss::transaction_hash] = to_string(position());
204 ret[jss::propose_seq] = proposeSeq();
205 }
206
207 ret[jss::close_time] = to_string(closeTime().time_since_epoch().count());
208
209 return ret;
210 }
211
213 uint256 const&
215 {
216 if (!signingHash_)
217 {
221 closeTime().time_since_epoch().count(),
222 prevLedger(),
223 position());
224 }
225
226 return signingHash_.value();
227 }
228
229private:
231 LedgerID_t previousLedger_;
232
234 Position_t position_;
235
238
239 // !The time this position was last updated
241
244
246 NodeID_t nodeID_;
247
250};
251
252template <class NodeID_t, class LedgerID_t, class Position_t>
253bool
257{
258 return a.nodeID() == b.nodeID() && a.proposeSeq() == b.proposeSeq() &&
259 a.prevLedger() == b.prevLedger() && a.position() == b.position() &&
260 a.closeTime() == b.closeTime() && a.seenTime() == b.seenTime();
261}
262} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Represents a proposed position taken during a round of consensus.
LedgerID_t const & prevLedger() const
Get the prior accepted ledger this position is based on.
std::optional< uint256 > signingHash_
The signing hash for this proposal.
NetClock::time_point const & seenTime() const
Get when this position was taken.
void bowOut(NetClock::time_point now)
Leave consensus.
Json::Value getJson() const
Get JSON representation for debugging.
void changePosition(Position_t const &newPosition, NetClock::time_point newCloseTime, NetClock::time_point now)
Update the position during the consensus process.
NetClock::time_point closeTime_
The ledger close time this position is taking.
NetClock::time_point time_
NetClock::time_point const & closeTime() const
The current position on the consensus close time.
bool isInitial() const
Whether this is the first position taken during the current consensus round.
static std::uint32_t const seqJoin
bool isBowOut() const
Get whether this node left the consensus process.
LedgerID_t previousLedger_
Unique identifier of prior ledger this proposal is based on.
bool isStale(NetClock::time_point cutoff) const
Get whether this position is stale relative to the provided cutoff.
std::uint32_t proposeSeq() const
Get the sequence number of this proposal.
std::uint32_t proposeSeq_
The sequence number of these positions taken by this node.
static std::uint32_t const seqLeave
Position_t const & position() const
Get the proposed position.
std::string render() const
uint256 const & signingHash() const
The digest for this proposal, used for signing purposes.
NodeID_t nodeID_
The identifier of the node taking this position.
NodeID_t const & nodeID() const
Identifying which peer took this position.
ConsensusProposal(LedgerID_t const &prevLedger, std::uint32_t seq, Position_t const &position, NetClock::time_point closeTime, NetClock::time_point now, NodeID_t const &nodeID)
Constructor.
Position_t position_
Unique identifier of the position this proposal is taking.
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:204
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:557
@ proposal
proposal for signing
T str(T... args)
T to_string(T... args)