rippled
Loading...
Searching...
No Matches
ConsensusProposal.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2017 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVID_tED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_CONSENSUS_CONSENSUSPROPOSAL_H_INCLUDED
21#define RIPPLE_CONSENSUS_CONSENSUSPROPOSAL_H_INCLUDED
22
23#include <xrpl/basics/base_uint.h>
24#include <xrpl/basics/chrono.h>
25#include <xrpl/json/json_value.h>
26#include <xrpl/protocol/HashPrefix.h>
27#include <xrpl/protocol/digest.h>
28#include <xrpl/protocol/jss.h>
29
30#include <cstdint>
31#include <optional>
32#include <sstream>
33
34namespace ripple {
58template <class NodeID_t, class LedgerID_t, class Position_t>
60{
61public:
62 using NodeID = NodeID_t;
63
64 //< Sequence value when a peer initially joins consensus
65 static std::uint32_t const seqJoin = 0;
66
67 //< Sequence number when a peer wants to bow out and leave consensus
68 static std::uint32_t const seqLeave = 0xffffffff;
69
80 LedgerID_t const& prevLedger,
81 std::uint32_t seq,
82 Position_t const& position,
85 NodeID_t const& nodeID)
89 , time_(now)
90 , proposeSeq_(seq)
92 {
93 }
94
96 NodeID_t const&
97 nodeID() const
98 {
99 return nodeID_;
100 }
101
103 Position_t const&
104 position() const
105 {
106 return position_;
107 }
108
110 LedgerID_t const&
112 {
113 return previousLedger_;
114 }
115
125 {
126 return proposeSeq_;
127 }
128
131 closeTime() const
132 {
133 return closeTime_;
134 }
135
138 seenTime() const
139 {
140 return time_;
141 }
142
146 bool
147 isInitial() const
148 {
149 return proposeSeq_ == seqJoin;
150 }
151
153 bool
154 isBowOut() const
155 {
156 return proposeSeq_ == seqLeave;
157 }
158
160 bool
162 {
163 return time_ <= cutoff;
164 }
165
173 void
175 Position_t const& newPosition,
176 NetClock::time_point newCloseTime,
178 {
179 signingHash_.reset();
180 position_ = newPosition;
181 closeTime_ = newCloseTime;
182 time_ = now;
183 if (proposeSeq_ != seqLeave)
184 ++proposeSeq_;
185 }
186
193 void
195 {
196 signingHash_.reset();
197 time_ = now;
199 }
200
202 render() const
203 {
205 ss << "proposal: previous_ledger: " << previousLedger_
206 << " proposal_seq: " << proposeSeq_ << " position: " << position_
207 << " close_time: " << to_string(closeTime_)
208 << " now: " << to_string(time_) << " is_bow_out:" << isBowOut()
209 << " node_id: " << nodeID_;
210 return ss.str();
211 }
212
215 getJson() const
216 {
217 using std::to_string;
218
220 ret[jss::previous_ledger] = to_string(prevLedger());
221
222 if (!isBowOut())
223 {
224 ret[jss::transaction_hash] = to_string(position());
225 ret[jss::propose_seq] = proposeSeq();
226 }
227
228 ret[jss::close_time] =
229 to_string(closeTime().time_since_epoch().count());
230
231 return ret;
232 }
233
235 uint256 const&
237 {
238 if (!signingHash_)
239 {
243 closeTime().time_since_epoch().count(),
244 prevLedger(),
245 position());
246 }
247
248 return signingHash_.value();
249 }
250
251private:
253 LedgerID_t previousLedger_;
254
256 Position_t position_;
257
260
261 // !The time this position was last updated
263
266
268 NodeID_t nodeID_;
269
272};
273
274template <class NodeID_t, class LedgerID_t, class Position_t>
275bool
279{
280 return a.nodeID() == b.nodeID() && a.proposeSeq() == b.proposeSeq() &&
281 a.prevLedger() == b.prevLedger() && a.position() == b.position() &&
282 a.closeTime() == b.closeTime() && a.seenTime() == b.seenTime();
283}
284} // namespace ripple
285#endif
Represents a JSON value.
Definition json_value.h:149
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:45
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
@ proposal
proposal for signing
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:585
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:224
T str(T... args)
T to_string(T... args)