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
154 changePosition(Position_t const& newPosition, NetClock::time_point newCloseTime, NetClock::time_point now)
155 {
156 signingHash_.reset();
157 position_ = newPosition;
158 closeTime_ = newCloseTime;
159 time_ = now;
160 if (proposeSeq_ != seqLeave)
161 ++proposeSeq_;
162 }
163
170 void
172 {
173 signingHash_.reset();
174 time_ = now;
176 }
177
179 render() const
180 {
182 ss << "proposal: previous_ledger: " << previousLedger_ << " proposal_seq: " << proposeSeq_
183 << " position: " << position_ << " close_time: " << to_string(closeTime_) << " now: " << to_string(time_)
184 << " is_bow_out:" << isBowOut() << " node_id: " << nodeID_;
185 return ss.str();
186 }
187
190 getJson() const
191 {
192 using std::to_string;
193
195 ret[jss::previous_ledger] = to_string(prevLedger());
196
197 if (!isBowOut())
198 {
199 ret[jss::transaction_hash] = to_string(position());
200 ret[jss::propose_seq] = proposeSeq();
201 }
202
203 ret[jss::close_time] = to_string(closeTime().time_since_epoch().count());
204
205 return ret;
206 }
207
209 uint256 const&
211 {
212 if (!signingHash_)
213 {
217 closeTime().time_since_epoch().count(),
218 prevLedger(),
219 position());
220 }
221
222 return signingHash_.value();
223 }
224
225private:
227 LedgerID_t previousLedger_;
228
230 Position_t position_;
231
234
235 // !The time this position was last updated
237
240
242 NodeID_t nodeID_;
243
246};
247
248template <class NodeID_t, class LedgerID_t, class Position_t>
249bool
253{
254 return a.nodeID() == b.nodeID() && a.proposeSeq() == b.proposeSeq() && a.prevLedger() == b.prevLedger() &&
255 a.position() == b.position() && a.closeTime() == b.closeTime() && a.seenTime() == b.seenTime();
256}
257} // 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:597
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:552
@ proposal
proposal for signing
T str(T... args)
T to_string(T... args)