rippled
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 <ripple/basics/base_uint.h>
23 #include <ripple/basics/chrono.h>
24 #include <ripple/beast/clock/abstract_clock.h>
25 #include <ripple/consensus/ConsensusTypes.h>
26 #include <ripple/json/json_value.h>
27 #include <ripple/protocol/HashPrefix.h>
28 #include <ripple/protocol/Protocol.h>
29 #include <ripple/protocol/jss.h>
30 #include <chrono>
31 #include <cstdint>
32 #include <optional>
33 
34 namespace ripple {
58 template <class NodeID_t, class LedgerID_t, class Position_t, class Seq>
60 {
61 public:
62  using NodeID = NodeID_t;
63 
66 
67  //< Sequence value when a peer initially joins consensus
68  static std::uint32_t const seqJoin = 0;
69 
70  //< Sequence number when a peer wants to bow out and leave consensus
71  static std::uint32_t const seqLeave = 0xffffffff;
72 
85  LedgerID_t const& prevLedger,
86  std::uint32_t seq,
87  Position_t const& position,
90  NodeID_t const& nodeID,
92  clock_type const& clock)
96  , time_(now)
97  , proposeSeq_(seq)
98  , nodeID_(nodeID)
100  {
101  // Track the arrive time to know how long our peers have been
102  // sending proposals.
103  arrivalTime_.reset(clock.now());
104  }
105 
107  NodeID_t const&
108  nodeID() const
109  {
110  return nodeID_;
111  }
112 
114  Position_t const&
115  position() const
116  {
117  return position_;
118  }
119 
121  LedgerID_t const&
122  prevLedger() const
123  {
124  return previousLedger_;
125  }
126 
135  proposeSeq() const
136  {
137  return proposeSeq_;
138  }
139 
141  NetClock::time_point const&
142  closeTime() const
143  {
144  return closeTime_;
145  }
146 
148  NetClock::time_point const&
149  seenTime() const
150  {
151  return time_;
152  }
153 
157  bool
158  isInitial() const
159  {
160  return proposeSeq_ == seqJoin;
161  }
162 
164  bool
165  isBowOut() const
166  {
167  return proposeSeq_ == seqLeave;
168  }
169 
171  bool
173  {
174  return time_ <= cutoff;
175  }
176 
184  void
186  Position_t const& newPosition,
187  NetClock::time_point newCloseTime,
189  {
190  signingHash_.reset();
191  position_ = newPosition;
192  closeTime_ = newCloseTime;
193  time_ = now;
194  if (proposeSeq_ != seqLeave)
195  ++proposeSeq_;
196  }
197 
204  void
206  {
207  signingHash_.reset();
208  time_ = now;
210  }
211 
214  getJson() const
215  {
216  using std::to_string;
217 
219  ret[jss::previous_ledger] = to_string(prevLedger());
220 
221  if (!isBowOut())
222  {
223  ret[jss::transaction_hash] = to_string(position());
224  ret[jss::propose_seq] = proposeSeq();
225  }
226 
227  ret[jss::close_time] =
228  to_string(closeTime().time_since_epoch().count());
229 
230  return ret;
231  }
232 
234  uint256 const&
235  signingHash() const
236  {
237  if (!signingHash_)
238  {
242  closeTime().time_since_epoch().count(),
243  prevLedger(),
244  position());
245  }
246 
247  return signingHash_.value();
248  }
249 
250  std::optional<Seq> const&
251  ledgerSeq() const
252  {
253  return ledgerSeq_;
254  }
255 
257  arrivalTime() const
258  {
259  return arrivalTime_;
260  }
261 
262 private:
264  LedgerID_t previousLedger_;
265 
267  Position_t position_;
268 
271 
272  // !The time this position was last updated
274 
277 
279  NodeID_t nodeID_;
280 
282 
285 
287 };
288 
289 template <class NodeID_t, class LedgerID_t, class Position_t, class Seq>
290 bool
294 {
295  return a.nodeID() == b.nodeID() && a.proposeSeq() == b.proposeSeq() &&
296  a.prevLedger() == b.prevLedger() && a.position() == b.position() &&
297  a.closeTime() == b.closeTime() && a.seenTime() == b.seenTime();
298 }
299 } // namespace ripple
300 #endif
ripple::ConsensusProposal::position_
Position_t position_
Unique identifier of the position this proposal is taking.
Definition: ConsensusProposal.h:267
ripple::ConsensusTimer
Measures the duration of phases of consensus.
Definition: ConsensusTypes.h:133
ripple::ConsensusProposal::closeTime_
NetClock::time_point closeTime_
The ledger close time this position is taking.
Definition: ConsensusProposal.h:270
ripple::ConsensusProposal::position
Position_t const & position() const
Get the proposed position.
Definition: ConsensusProposal.h:115
ripple::ConsensusProposal::seqJoin
static const std::uint32_t seqJoin
Definition: ConsensusProposal.h:68
ripple::ConsensusProposal::time_
NetClock::time_point time_
Definition: ConsensusProposal.h:273
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:165
ripple::ConsensusProposal::bowOut
void bowOut(NetClock::time_point now)
Leave consensus.
Definition: ConsensusProposal.h:205
ripple::ConsensusProposal::signingHash
uint256 const & signingHash() const
The digest for this proposal, used for signing purposes.
Definition: ConsensusProposal.h:235
ripple::base_uint< 256 >
ripple::ConsensusProposal::getJson
Json::Value getJson() const
Get JSON representation for debugging.
Definition: ConsensusProposal.h:214
ripple::ConsensusProposal::nodeID
NodeID_t const & nodeID() const
Identifying which peer took this position.
Definition: ConsensusProposal.h:108
ripple::ConsensusProposal::proposeSeq_
std::uint32_t proposeSeq_
The sequence number of these positions taken by this node.
Definition: ConsensusProposal.h:276
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::ConsensusProposal::arrivalTime
ConsensusTimer & arrivalTime() const
Definition: ConsensusProposal.h:257
ripple::ConsensusTimer::reset
void reset(time_point tp)
Definition: ConsensusTypes.h:153
ripple::ConsensusProposal::nodeID_
NodeID_t nodeID_
The identifier of the node taking this position.
Definition: ConsensusProposal.h:279
chrono
ripple::ConsensusProposal::isBowOut
bool isBowOut() const
Get whether this node left the consensus process.
Definition: ConsensusProposal.h:165
ripple::ConsensusProposal::ledgerSeq_
std::optional< Seq > ledgerSeq_
Definition: ConsensusProposal.h:281
std::to_string
T to_string(T... args)
ripple::ConsensusProposal::closeTime
NetClock::time_point const & closeTime() const
The current position on the consensus close time.
Definition: ConsensusProposal.h:142
std::chrono::time_point
ripple::HashPrefix::proposal
@ proposal
proposal for signing
cstdint
ripple::ConsensusProposal::prevLedger
LedgerID_t const & prevLedger() const
Get the prior accepted ledger this position is based on.
Definition: ConsensusProposal.h:122
ripple::ConsensusProposal::isInitial
bool isInitial() const
Whether this is the first position taken during the current consensus round.
Definition: ConsensusProposal.h:158
std::uint32_t
beast::abstract_clock< std::chrono::steady_clock >
ripple::ConsensusProposal::arrivalTime_
ConsensusTimer arrivalTime_
Definition: ConsensusProposal.h:286
ripple::ConsensusProposal::ledgerSeq
std::optional< Seq > const & ledgerSeq() const
Definition: ConsensusProposal.h:251
ripple::ConsensusProposal::seenTime
NetClock::time_point const & seenTime() const
Get when this position was taken.
Definition: ConsensusProposal.h:149
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::sha512Half
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:216
ripple::ConsensusProposal< PeerID, Ledger::ID, TxSet::ID, Ledger::Seq >::NodeID
PeerID NodeID
Definition: ConsensusProposal.h:62
ripple::ConsensusProposal::signingHash_
std::optional< uint256 > signingHash_
The signing hash for this proposal.
Definition: ConsensusProposal.h:284
ripple::ConsensusProposal::proposeSeq
std::uint32_t proposeSeq() const
Get the sequence number of this proposal.
Definition: ConsensusProposal.h:135
optional
ripple::ConsensusProposal::changePosition
void changePosition(Position_t const &newPosition, NetClock::time_point newCloseTime, NetClock::time_point now)
Update the position during the consensus process.
Definition: ConsensusProposal.h:185
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::ConsensusProposal::isStale
bool isStale(NetClock::time_point cutoff) const
Get whether this position is stale relative to the provided cutoff.
Definition: ConsensusProposal.h:172
ripple::ConsensusProposal::previousLedger_
LedgerID_t previousLedger_
Unique identifier of prior ledger this proposal is based on.
Definition: ConsensusProposal.h:264
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::ConsensusProposal::ConsensusProposal
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, std::optional< Seq > const &ledgerSeq, clock_type const &clock)
Constructor.
Definition: ConsensusProposal.h:84
ripple::ConsensusProposal::seqLeave
static const std::uint32_t seqLeave
Definition: ConsensusProposal.h:71
ripple::ConsensusProposal
Represents a proposed position taken during a round of consensus.
Definition: ConsensusProposal.h:59