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#ifndef RIPPLE_CONSENSUS_CONSENSUSPROPOSAL_H_INCLUDED
20#define RIPPLE_CONSENSUS_CONSENSUSPROPOSAL_H_INCLUDED
21
22#include <xrpl/basics/base_uint.h>
23#include <xrpl/basics/chrono.h>
24#include <xrpl/json/json_value.h>
25#include <xrpl/protocol/HashPrefix.h>
26#include <xrpl/protocol/jss.h>
27#include <cstdint>
28#include <optional>
29#include <sstream>
30
31namespace ripple {
55template <class NodeID_t, class LedgerID_t, class Position_t>
57{
58public:
59 using NodeID = NodeID_t;
60
61 //< Sequence value when a peer initially joins consensus
62 static std::uint32_t const seqJoin = 0;
63
64 //< Sequence number when a peer wants to bow out and leave consensus
65 static std::uint32_t const seqLeave = 0xffffffff;
66
77 LedgerID_t const& prevLedger,
78 std::uint32_t seq,
79 Position_t const& position,
82 NodeID_t const& nodeID)
86 , time_(now)
87 , proposeSeq_(seq)
89 {
90 }
91
93 NodeID_t const&
94 nodeID() const
95 {
96 return nodeID_;
97 }
98
100 Position_t const&
101 position() const
102 {
103 return position_;
104 }
105
107 LedgerID_t const&
109 {
110 return previousLedger_;
111 }
112
122 {
123 return proposeSeq_;
124 }
125
128 closeTime() const
129 {
130 return closeTime_;
131 }
132
135 seenTime() const
136 {
137 return time_;
138 }
139
143 bool
144 isInitial() const
145 {
146 return proposeSeq_ == seqJoin;
147 }
148
150 bool
151 isBowOut() const
152 {
153 return proposeSeq_ == seqLeave;
154 }
155
157 bool
159 {
160 return time_ <= cutoff;
161 }
162
170 void
172 Position_t const& newPosition,
173 NetClock::time_point newCloseTime,
175 {
176 signingHash_.reset();
177 position_ = newPosition;
178 closeTime_ = newCloseTime;
179 time_ = now;
180 if (proposeSeq_ != seqLeave)
181 ++proposeSeq_;
182 }
183
190 void
192 {
193 signingHash_.reset();
194 time_ = now;
196 }
197
199 render() const
200 {
202 ss << "proposal: previous_ledger: " << previousLedger_
203 << " proposal_seq: " << proposeSeq_ << " position: " << position_
204 << " close_time: " << to_string(closeTime_)
205 << " now: " << to_string(time_) << " is_bow_out:" << isBowOut()
206 << " node_id: " << nodeID_;
207 return ss.str();
208 }
209
212 getJson() const
213 {
214 using std::to_string;
215
217 ret[jss::previous_ledger] = to_string(prevLedger());
218
219 if (!isBowOut())
220 {
221 ret[jss::transaction_hash] = to_string(position());
222 ret[jss::propose_seq] = proposeSeq();
223 }
224
225 ret[jss::close_time] =
226 to_string(closeTime().time_since_epoch().count());
227
228 return ret;
229 }
230
232 uint256 const&
234 {
235 if (!signingHash_)
236 {
240 closeTime().time_since_epoch().count(),
241 prevLedger(),
242 position());
243 }
244
245 return signingHash_.value();
246 }
247
248private:
250 LedgerID_t previousLedger_;
251
253 Position_t position_;
254
257
258 // !The time this position was last updated
260
263
265 NodeID_t nodeID_;
266
269};
270
271template <class NodeID_t, class LedgerID_t, class Position_t>
272bool
276{
277 return a.nodeID() == b.nodeID() && a.proposeSeq() == b.proposeSeq() &&
278 a.prevLedger() == b.prevLedger() && a.position() == b.position() &&
279 a.closeTime() == b.closeTime() && a.seenTime() == b.seenTime();
280}
281} // namespace ripple
282#endif
Represents a JSON value.
Definition: json_value.h:147
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::string render() const
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:43
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
@ proposal
proposal for signing
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition: base_uint.h:584
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:223
T str(T... args)
T to_string(T... args)