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/DisputedTx.h>
25 #include <chrono>
26 #include <map>
27 
28 namespace ripple {
29 
54 enum class ConsensusMode {
56  proposing,
58  observing,
66 };
67 
68 inline std::string
70 {
71  switch (m)
72  {
74  return "proposing";
76  return "observing";
78  return "wrongLedger";
80  return "switchedLedger";
81  default:
82  return "unknown";
83  }
84 }
85 
102 enum class ConsensusPhase {
104  open,
105 
107  establish,
108 
112  accepted,
113 };
114 
115 inline std::string
117 {
118  switch (p)
119  {
121  return "open";
123  return "establish";
125  return "accepted";
126  default:
127  return "unknown";
128  }
129 }
130 
134 {
135  using time_point = std::chrono::steady_clock::time_point;
138 
139 public:
141  read() const
142  {
143  return dur_;
144  }
145 
146  void
148  {
149  dur_ += fixed;
150  }
151 
152  void
154  {
155  start_ = tp;
157  }
158 
159  void
161  {
162  using namespace std::chrono;
163  dur_ = duration_cast<milliseconds>(tp - start_);
164  }
165 };
166 
174 {
175  explicit ConsensusCloseTimes() = default;
176 
179 
182 };
183 
185 enum class ConsensusState {
186  No,
187  MovedOn,
188  Yes
189 };
190 
191 template <class NodeID_t, class LedgerID_t, class Position_t, class Seq>
192 class ConsensusProposal;
201 template <class Traits>
203 {
204  using Ledger_t = typename Traits::Ledger_t;
205  using TxSet_t = typename Traits::TxSet_t;
206  using NodeID_t = typename Traits::NodeID_t;
207 
208  using Tx_t = typename TxSet_t::Tx;
210  NodeID_t,
211  typename Ledger_t::ID,
212  typename TxSet_t::ID,
213  typename Ledger_t::Seq>;
215 
217  : txns{std::move(s)}, position{std::move(p)}
218  {
219  assert(txns.id() == position.position());
220  }
221 
224 
227 
230 
231  // Set of TxSet ids we have already compared/created disputes
233 
234  // Measures the duration of the establish phase for this consensus round
236 
237  // Indicates state in which consensus ended. Once in the accept phase
238  // will be either Yes or MovedOn
240 
241  // The number of peers proposing during the round
242  std::size_t proposers = 0;
243 };
244 } // namespace ripple
245 
246 #endif
ripple::ConsensusState
ConsensusState
Whether we have or don't have a consensus.
Definition: ConsensusTypes.h:185
ripple::ConsensusResult::Ledger_t
typename Traits::Ledger_t Ledger_t
Definition: ConsensusTypes.h:204
ripple::ConsensusTimer::tick
void tick(std::chrono::milliseconds fixed)
Definition: ConsensusTypes.h:147
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:133
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:206
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:235
ripple::ConsensusTimer::time_point
std::chrono::steady_clock::time_point time_point
Definition: ConsensusTypes.h:135
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:202
ripple::ConsensusCloseTimes::peers
std::map< NetClock::time_point, int > peers
Close time estimates, keep ordered for predictable traverse.
Definition: ConsensusTypes.h:178
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:229
ripple::ConsensusTimer::dur_
std::chrono::milliseconds dur_
Definition: ConsensusTypes.h:137
ripple::ConsensusTimer::read
std::chrono::milliseconds read() const
Definition: ConsensusTypes.h:141
ripple::ConsensusTimer::reset
void reset(time_point tp)
Definition: ConsensusTypes.h:153
chrono
ripple::ConsensusPhase
ConsensusPhase
Phases of consensus for a single ledger round.
Definition: ConsensusTypes.h:102
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:160
ripple::ConsensusResult::Tx_t
typename TxSet_t::Tx Tx_t
Definition: ConsensusTypes.h:208
ripple::ConsensusTimer::start_
time_point start_
Definition: ConsensusTypes.h:136
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:54
ripple::ConsensusResult::TxSet_t
typename Traits::TxSet_t TxSet_t
Definition: ConsensusTypes.h:205
ripple::ConsensusResult::position
Proposal_t position
Our proposed position on transactions/close time.
Definition: ConsensusTypes.h:226
ripple::ConsensusResult::compares
hash_set< typename TxSet_t::ID > compares
Definition: ConsensusTypes.h:232
ripple::ConsensusResult::ConsensusResult
ConsensusResult(TxSet_t &&s, Proposal_t &&p)
Definition: ConsensusTypes.h:216
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:223
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:173
ripple::ConsensusProposal< NodeID_t, typename Ledger_t::ID, typename TxSet_t::ID, typename Ledger_t::Seq >
std::chrono