rippled
Loading...
Searching...
No Matches
ConsensusProposal.h
1#ifndef XRPL_CONSENSUS_CONSENSUSPROPOSAL_H_INCLUDED
2#define XRPL_CONSENSUS_CONSENSUSPROPOSAL_H_INCLUDED
3
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/basics/chrono.h>
6#include <xrpl/json/json_value.h>
7#include <xrpl/protocol/HashPrefix.h>
8#include <xrpl/protocol/digest.h>
9#include <xrpl/protocol/jss.h>
10
11#include <cstdint>
12#include <optional>
13#include <sstream>
14
15namespace ripple {
39template <class NodeID_t, class LedgerID_t, class Position_t>
41{
42public:
43 using NodeID = NodeID_t;
44
45 //< Sequence value when a peer initially joins consensus
46 static std::uint32_t const seqJoin = 0;
47
48 //< Sequence number when a peer wants to bow out and leave consensus
49 static std::uint32_t const seqLeave = 0xffffffff;
50
61 LedgerID_t const& prevLedger,
62 std::uint32_t seq,
63 Position_t const& position,
66 NodeID_t const& nodeID)
70 , time_(now)
71 , proposeSeq_(seq)
73 {
74 }
75
77 NodeID_t const&
78 nodeID() const
79 {
80 return nodeID_;
81 }
82
84 Position_t const&
85 position() const
86 {
87 return position_;
88 }
89
91 LedgerID_t const&
92 prevLedger() const
93 {
94 return previousLedger_;
95 }
96
106 {
107 return proposeSeq_;
108 }
109
112 closeTime() const
113 {
114 return closeTime_;
115 }
116
119 seenTime() const
120 {
121 return time_;
122 }
123
127 bool
128 isInitial() const
129 {
130 return proposeSeq_ == seqJoin;
131 }
132
134 bool
135 isBowOut() const
136 {
137 return proposeSeq_ == seqLeave;
138 }
139
141 bool
143 {
144 return time_ <= cutoff;
145 }
146
154 void
156 Position_t const& newPosition,
157 NetClock::time_point newCloseTime,
159 {
160 signingHash_.reset();
161 position_ = newPosition;
162 closeTime_ = newCloseTime;
163 time_ = now;
164 if (proposeSeq_ != seqLeave)
165 ++proposeSeq_;
166 }
167
174 void
176 {
177 signingHash_.reset();
178 time_ = now;
180 }
181
183 render() const
184 {
186 ss << "proposal: previous_ledger: " << previousLedger_
187 << " proposal_seq: " << proposeSeq_ << " position: " << position_
188 << " close_time: " << to_string(closeTime_)
189 << " now: " << to_string(time_) << " is_bow_out:" << isBowOut()
190 << " node_id: " << nodeID_;
191 return ss.str();
192 }
193
196 getJson() const
197 {
198 using std::to_string;
199
201 ret[jss::previous_ledger] = to_string(prevLedger());
202
203 if (!isBowOut())
204 {
205 ret[jss::transaction_hash] = to_string(position());
206 ret[jss::propose_seq] = proposeSeq();
207 }
208
209 ret[jss::close_time] =
210 to_string(closeTime().time_since_epoch().count());
211
212 return ret;
213 }
214
216 uint256 const&
218 {
219 if (!signingHash_)
220 {
224 closeTime().time_since_epoch().count(),
225 prevLedger(),
226 position());
227 }
228
229 return signingHash_.value();
230 }
231
232private:
234 LedgerID_t previousLedger_;
235
237 Position_t position_;
238
241
242 // !The time this position was last updated
244
247
249 NodeID_t nodeID_;
250
253};
254
255template <class NodeID_t, class LedgerID_t, class Position_t>
256bool
260{
261 return a.nodeID() == b.nodeID() && a.proposeSeq() == b.proposeSeq() &&
262 a.prevLedger() == b.prevLedger() && a.position() == b.position() &&
263 a.closeTime() == b.closeTime() && a.seenTime() == b.seenTime();
264}
265} // namespace ripple
266#endif
Represents a JSON value.
Definition json_value.h:131
Represents a proposed position taken during a round of consensus.
void changePosition(Position_t const &newPosition, NetClock::time_point newCloseTime, NetClock::time_point now)
Update the position during the consensus process.
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.
LedgerID_t const & prevLedger() const
Get the prior accepted ledger this position is based on.
NetClock::time_point time_
bool isBowOut() const
Get whether this node left the consensus process.
uint256 const & signingHash() const
The digest for this proposal, used for signing purposes.
NetClock::time_point closeTime_
The ledger close time this position is taking.
NodeID_t const & nodeID() const
Identifying which peer took this position.
std::uint32_t proposeSeq_
The sequence number of these positions taken by this node.
std::optional< uint256 > signingHash_
The signing hash for this proposal.
static std::uint32_t const seqJoin
NetClock::time_point const & closeTime() const
The current position on the consensus close time.
LedgerID_t previousLedger_
Unique identifier of prior ledger this proposal is based on.
Json::Value getJson() const
Get JSON representation for debugging.
void bowOut(NetClock::time_point now)
Leave consensus.
static std::uint32_t const seqLeave
NodeID_t nodeID_
The identifier of the node taking this position.
Position_t position_
Unique identifier of the position this proposal is taking.
Position_t const & position() const
Get the proposed position.
NetClock::time_point const & seenTime() const
Get when this position was taken.
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.
bool isInitial() const
Whether this is the first position taken during the current consensus round.
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:27
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
@ proposal
proposal for signing
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:566
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:205
T str(T... args)
T to_string(T... args)