rippled
ConsensusTypes.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 PROVIDED "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_CONSENSUS_TYPES_H_INCLUDED
21 #define RIPPLE_CONSENSUS_CONSENSUS_TYPES_H_INCLUDED
22 
23 #include <ripple/basics/chrono.h>
24 #include <ripple/consensus/ConsensusProposal.h>
25 #include <ripple/consensus/DisputedTx.h>
26 #include <chrono>
27 #include <map>
28 
29 namespace ripple {
30 
55 enum class ConsensusMode {
57  proposing,
59  observing,
67 };
68 
69 inline std::string
71 {
72  switch (m)
73  {
75  return "proposing";
77  return "observing";
79  return "wrongLedger";
81  return "switchedLedger";
82  default:
83  return "unknown";
84  }
85 }
86 
103 enum class ConsensusPhase {
105  open,
106 
108  establish,
109 
113  accepted,
114 };
115 
116 inline std::string
118 {
119  switch (p)
120  {
122  return "open";
124  return "establish";
126  return "accepted";
127  default:
128  return "unknown";
129  }
130 }
131 
135 {
136  using time_point = std::chrono::steady_clock::time_point;
139 
140 public:
142  read() const
143  {
144  return dur_;
145  }
146 
147  void
149  {
150  dur_ += fixed;
151  }
152 
153  void
155  {
156  start_ = tp;
158  }
159 
160  void
162  {
163  using namespace std::chrono;
164  dur_ = duration_cast<milliseconds>(tp - start_);
165  }
166 };
167 
175 {
176  explicit ConsensusCloseTimes() = default;
177 
180 
183 };
184 
186 enum class ConsensusState {
187  No,
188  MovedOn,
189  Yes
190 };
191 
200 template <class Traits>
202 {
203  using Ledger_t = typename Traits::Ledger_t;
204  using TxSet_t = typename Traits::TxSet_t;
205  using NodeID_t = typename Traits::NodeID_t;
206 
207  using Tx_t = typename TxSet_t::Tx;
209  NodeID_t,
210  typename Ledger_t::ID,
211  typename TxSet_t::ID>;
213 
215  : txns{std::move(s)}, position{std::move(p)}
216  {
217  assert(txns.id() == position.position());
218  }
219 
222 
225 
228 
229  // Set of TxSet ids we have already compared/created disputes
231 
232  // Measures the duration of the establish phase for this consensus round
234 
235  // Indicates state in which consensus ended. Once in the accept phase
236  // will be either Yes or MovedOn
238 
239  // The number of peers proposing during the round
240  std::size_t proposers = 0;
241 };
242 } // namespace ripple
243 
244 #endif
ripple::ConsensusState
ConsensusState
Whether we have or don't have a consensus.
Definition: ConsensusTypes.h:186
ripple::ConsensusResult::Ledger_t
typename Traits::Ledger_t Ledger_t
Definition: ConsensusTypes.h:203
ripple::ConsensusTimer::tick
void tick(std::chrono::milliseconds fixed)
Definition: ConsensusTypes.h:148
std::string
STL class.
ripple::ConsensusMode::proposing
@ proposing
We are normal participant in consensus and propose our position.
ripple::ConsensusTimer
Measures the duration of phases of consensus.
Definition: ConsensusTypes.h:134
std::unordered_set
STL class.
ripple::ConsensusMode::wrongLedger
@ wrongLedger
We have the wrong ledger and are attempting to acquire it.
ripple::ConsensusResult::NodeID_t
typename Traits::NodeID_t NodeID_t
Definition: ConsensusTypes.h:205
ripple::ConsensusState::Yes
@ Yes
We have consensus along with the network.
std::chrono::milliseconds
ripple::ConsensusPhase::open
@ open
We haven't closed our ledger yet, but others might have.
ripple::ConsensusMode::switchedLedger
@ switchedLedger
We switched ledgers since we started this consensus round but are now running on what we believe is t...
ripple::ConsensusResult::roundTime
ConsensusTimer roundTime
Definition: ConsensusTypes.h:233
ripple::ConsensusTimer::time_point
std::chrono::steady_clock::time_point time_point
Definition: ConsensusTypes.h:136
ripple::ConsensusPhase::accepted
@ accepted
We have accepted a new last closed ledger and are waiting on a call to startRound to begin the next c...
ripple::ConsensusResult
Encapsulates the result of consensus.
Definition: ConsensusTypes.h:201
ripple::ConsensusCloseTimes::peers
std::map< NetClock::time_point, int > peers
Close time estimates, keep ordered for predictable traverse.
Definition: ConsensusTypes.h:179
ripple::ConsensusMode::observing
@ observing
We are observing peer positions, but not proposing our position.
ripple::ConsensusResult::disputes
hash_map< typename Tx_t::ID, Dispute_t > disputes
Transactions which are under dispute with our peers.
Definition: ConsensusTypes.h:227
ripple::ConsensusTimer::dur_
std::chrono::milliseconds dur_
Definition: ConsensusTypes.h:138
ripple::ConsensusTimer::read
std::chrono::milliseconds read() const
Definition: ConsensusTypes.h:142
ripple::ConsensusTimer::reset
void reset(time_point tp)
Definition: ConsensusTypes.h:154
chrono
ripple::ConsensusPhase
ConsensusPhase
Phases of consensus for a single ledger round.
Definition: ConsensusTypes.h:103
std::chrono::time_point
ripple::ConsensusState::No
@ No
We do not have consensus.
ripple::ConsensusState::MovedOn
@ MovedOn
The network has consensus without us.
map
ripple::ConsensusTimer::tick
void tick(time_point tp)
Definition: ConsensusTypes.h:161
ripple::ConsensusResult::Tx_t
typename TxSet_t::Tx Tx_t
Definition: ConsensusTypes.h:207
ripple::ConsensusTimer::start_
time_point start_
Definition: ConsensusTypes.h:137
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ConsensusMode
ConsensusMode
Represents how a node currently participates in Consensus.
Definition: ConsensusTypes.h:55
ripple::ConsensusResult::TxSet_t
typename Traits::TxSet_t TxSet_t
Definition: ConsensusTypes.h:204
ripple::ConsensusResult::position
Proposal_t position
Our proposed position on transactions/close time.
Definition: ConsensusTypes.h:224
ripple::ConsensusResult::compares
hash_set< typename TxSet_t::ID > compares
Definition: ConsensusTypes.h:230
ripple::ConsensusResult::ConsensusResult
ConsensusResult(TxSet_t &&s, Proposal_t &&p)
Definition: ConsensusTypes.h:214
ripple::ConsensusPhase::establish
@ establish
Establishing consensus by exchanging proposals with our peers.
ripple::DisputedTx
A transaction discovered to be in dispute during consensus.
Definition: DisputedTx.h:50
ripple::ConsensusResult::txns
TxSet_t txns
The set of transactions consensus agrees go in the ledger.
Definition: ConsensusTypes.h:221
std::size_t
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
std::unordered_map
STL class.
ripple::ConsensusCloseTimes
Stores the set of initial close times.
Definition: ConsensusTypes.h:174
ripple::ConsensusProposal< NodeID_t, typename Ledger_t::ID, typename TxSet_t::ID >
std::chrono