rippled
Sim.cpp
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 #include <test/csf/Sim.h>
20 
21 namespace ripple {
22 namespace test {
23 namespace csf {
24 
25 void
26 Sim::run(int ledgers)
27 {
28  for (auto& p : peers)
29  {
30  p.targetLedgers = p.completedLedgers + ledgers;
31  p.start();
32  }
33  scheduler.step();
34 }
35 
36 void
37 Sim::run(SimDuration const& dur)
38 {
39  for (auto& p : peers)
40  {
41  p.targetLedgers = std::numeric_limits<decltype(p.targetLedgers)>::max();
42  p.start();
43  }
44  scheduler.step_for(dur);
45 }
46 
47 bool
49 {
50  return synchronized(allPeers);
51 }
52 
53 bool
55 {
56  if (g.size() < 1)
57  return true;
58  Peer const* ref = g[0];
59  return std::all_of(g.begin(), g.end(), [&ref](Peer const* p) {
60  return p->lastClosedLedger.id() == ref->lastClosedLedger.id() &&
61  p->fullyValidatedLedger.id() == ref->fullyValidatedLedger.id();
62  });
63 }
64 
67 {
68  return branches(allPeers);
69 }
71 Sim::branches(PeerGroup const& g) const
72 {
73  if (g.size() < 1)
74  return 0;
75  std::set<Ledger> ledgers;
76  for (auto const& peer : g)
77  ledgers.insert(peer->fullyValidatedLedger);
78 
79  return oracle.branches(ledgers);
80 }
81 
82 } // namespace csf
83 } // namespace test
84 } // namespace ripple
ripple::test::csf::PeerGroup::size
std::size_t size() const
Definition: PeerGroup.h:112
ripple::test::csf::Sim::peers
std::deque< Peer > peers
Definition: Sim.h:66
ripple::test::csf::Sim::scheduler
Scheduler scheduler
Definition: Sim.h:71
std::all_of
T all_of(T... args)
ripple::test::csf::Sim::oracle
LedgerOracle oracle
Definition: Sim.h:74
ripple::test::csf::PeerGroup::end
iterator end()
Definition: PeerGroup.h:74
ripple::test::csf::PeerGroup::begin
iterator begin()
Definition: PeerGroup.h:68
ripple::test::csf::Sim::branches
std::size_t branches() const
Calculate the number of branches in the network.
Definition: Sim.cpp:66
ripple::test::csf::Sim::allPeers
PeerGroup allPeers
Definition: Sim.h:67
ripple::test::csf::PeerGroup
A group of simulation Peers.
Definition: PeerGroup.h:39
ripple::test::csf::Peer
A single peer in the simulation.
Definition: test/csf/Peer.h:54
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::csf::Scheduler::step
bool step()
Run the scheduler until no events remain.
Definition: test/csf/Scheduler.h:403
ripple::test::csf::LedgerOracle::branches
std::size_t branches(std::set< Ledger > const &ledgers) const
Determine the number of distinct branches for the set of ledgers.
Definition: ledgers.cpp:140
std::set::insert
T insert(T... args)
ripple::test::csf::Sim::synchronized
bool synchronized() const
Check whether all peers in the network are synchronized.
Definition: Sim.cpp:48
std::size_t
ripple::test::csf::SimDuration
typename SimClock::duration SimDuration
Definition: SimTime.h:35
ripple::test::csf::Scheduler::step_for
bool step_for(std::chrono::duration< Period, Rep > const &amount)
Run the scheduler until time has elapsed.
Definition: test/csf/Scheduler.h:449
ripple::test::csf::Sim::run
void run(int ledgers)
Run consensus protocol to generate the provided number of ledgers.
Definition: Sim.cpp:26
std::numeric_limits
std::set
STL class.