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/chrono.h>
23 #include <ripple/json/json_value.h>
24 #include <ripple/protocol/jss.h>
25 #include <cstdint>
26 
27 namespace ripple {
51 template <class NodeID_t, class LedgerID_t, class Position_t>
53 {
54 public:
55  using NodeID = NodeID_t;
56 
57  //< Sequence value when a peer initially joins consensus
58  static std::uint32_t const seqJoin = 0;
59 
60  //< Sequence number when a peer wants to bow out and leave consensus
61  static std::uint32_t const seqLeave = 0xffffffff;
62 
73  LedgerID_t const& prevLedger,
74  std::uint32_t seq,
75  Position_t const& position,
78  NodeID_t const& nodeID)
82  , time_(now)
83  , proposeSeq_(seq)
84  , nodeID_(nodeID)
85  {
86  }
87 
89  NodeID_t const&
90  nodeID() const
91  {
92  return nodeID_;
93  }
94 
96  Position_t const&
97  position() const
98  {
99  return position_;
100  }
101 
103  LedgerID_t const&
104  prevLedger() const
105  {
106  return previousLedger_;
107  }
108 
117  proposeSeq() const
118  {
119  return proposeSeq_;
120  }
121 
123  NetClock::time_point const&
124  closeTime() const
125  {
126  return closeTime_;
127  }
128 
130  NetClock::time_point const&
131  seenTime() const
132  {
133  return time_;
134  }
135 
139  bool
140  isInitial() const
141  {
142  return proposeSeq_ == seqJoin;
143  }
144 
146  bool
147  isBowOut() const
148  {
149  return proposeSeq_ == seqLeave;
150  }
151 
153  bool
155  {
156  return time_ <= cutoff;
157  }
158 
166  void
168  Position_t const& newPosition,
169  NetClock::time_point newCloseTime,
171  {
172  position_ = newPosition;
173  closeTime_ = newCloseTime;
174  time_ = now;
175  if (proposeSeq_ != seqLeave)
176  ++proposeSeq_;
177  }
178 
185  void
187  {
188  time_ = now;
190  }
191 
194  getJson() const
195  {
196  using std::to_string;
197 
199  ret[jss::previous_ledger] = to_string(prevLedger());
200 
201  if (!isBowOut())
202  {
203  ret[jss::transaction_hash] = to_string(position());
204  ret[jss::propose_seq] = proposeSeq();
205  }
206 
207  ret[jss::close_time] =
208  to_string(closeTime().time_since_epoch().count());
209 
210  return ret;
211  }
212 
213 private:
215  LedgerID_t previousLedger_;
216 
218  Position_t position_;
219 
222 
223  // !The time this position was last updated
225 
228 
230  NodeID_t nodeID_;
231 };
232 
233 template <class NodeID_t, class LedgerID_t, class Position_t>
234 bool
238 {
239  return a.nodeID() == b.nodeID() && a.proposeSeq() == b.proposeSeq() &&
240  a.prevLedger() == b.prevLedger() && a.position() == b.position() &&
241  a.closeTime() == b.closeTime() && a.seenTime() == b.seenTime();
242 }
243 } // namespace ripple
244 #endif
ripple::ConsensusProposal::time_
NetClock::time_point time_
Definition: ConsensusProposal.h:224
ripple::ConsensusProposal::proposeSeq
std::uint32_t proposeSeq() const
Get the sequence number of this proposal.
Definition: ConsensusProposal.h:117
ripple::ConsensusProposal::isStale
bool isStale(NetClock::time_point cutoff) const
Get whether this position is stale relative to the provided cutoff.
Definition: ConsensusProposal.h:154
ripple::ConsensusProposal::closeTime_
NetClock::time_point closeTime_
The ledger close time this position is taking.
Definition: ConsensusProposal.h:221
ripple::ConsensusProposal::getJson
Json::Value getJson() const
Get JSON representation for debugging.
Definition: ConsensusProposal.h:194
ripple::ConsensusProposal::nodeID_
NodeID_t nodeID_
The identifier of the node taking this position.
Definition: ConsensusProposal.h:230
ripple::ConsensusProposal::seqLeave
static const std::uint32_t seqLeave
Definition: ConsensusProposal.h:61
ripple::ConsensusProposal::bowOut
void bowOut(NetClock::time_point now)
Leave consensus.
Definition: ConsensusProposal.h:186
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:167
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:159
ripple::ConsensusProposal::prevLedger
LedgerID_t const & prevLedger() const
Get the prior accepted ledger this position is based on.
Definition: ConsensusProposal.h:104
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)
Constructor.
Definition: ConsensusProposal.h:72
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
std::to_string
T to_string(T... args)
std::chrono::time_point
cstdint
std::uint32_t
ripple::ConsensusProposal::proposeSeq_
std::uint32_t proposeSeq_
The sequence number of these positions taken by this node.
Definition: ConsensusProposal.h:227
ripple::ConsensusProposal::previousLedger_
LedgerID_t previousLedger_
Unique identifier of prior ledger this proposal is based on.
Definition: ConsensusProposal.h:215
ripple::ConsensusProposal::nodeID
NodeID_t const & nodeID() const
Identifying which peer took this position.
Definition: ConsensusProposal.h:90
ripple::ConsensusProposal::closeTime
NetClock::time_point const & closeTime() const
The current position on the consensus close time.
Definition: ConsensusProposal.h:124
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ConsensusProposal::isBowOut
bool isBowOut() const
Get whether this node left the consensus process.
Definition: ConsensusProposal.h:147
ripple::ConsensusProposal::seqJoin
static const std::uint32_t seqJoin
Definition: ConsensusProposal.h:58
ripple::ConsensusProposal::seenTime
NetClock::time_point const & seenTime() const
Get when this position was taken.
Definition: ConsensusProposal.h:131
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:38
ripple::ConsensusProposal::position_
Position_t position_
Unique identifier of the position this proposal is taking.
Definition: ConsensusProposal.h:218
ripple::ConsensusProposal< NodeID_t, typename Ledger_t::ID, typename TxSet_t::ID >::NodeID
NodeID_t NodeID
Definition: ConsensusProposal.h:55
ripple::ConsensusProposal::position
Position_t const & position() const
Get the proposed position.
Definition: ConsensusProposal.h:97
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::ConsensusProposal::isInitial
bool isInitial() const
Whether this is the first position taken during the current consensus round.
Definition: ConsensusProposal.h:140
ripple::ConsensusProposal
Represents a proposed position taken during a round of consensus.
Definition: ConsensusProposal.h:52