rippled
Sim.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_TEST_CSF_SIM_H_INCLUDED
21 #define RIPPLE_TEST_CSF_SIM_H_INCLUDED
22 
23 #include <test/csf/BasicNetwork.h>
24 #include <test/csf/CollectorRef.h>
25 #include <test/csf/Digraph.h>
26 #include <test/csf/Peer.h>
27 #include <test/csf/PeerGroup.h>
28 #include <test/csf/Scheduler.h>
29 #include <test/csf/SimTime.h>
30 #include <test/csf/TrustGraph.h>
31 
32 #include <deque>
33 #include <iostream>
34 #include <random>
35 
36 namespace ripple {
37 namespace test {
38 namespace csf {
39 
42 {
44 
45 public:
47  : Sink(beast::severities::kDisabled, false), clock_{clock}
48  {
49  }
50 
51  void
52  write(beast::severities::Severity level, std::string const& text) override
53  {
54  if (level < threshold())
55  return;
56 
57  std::cout << clock_.now().time_since_epoch().count() << " " << text
58  << std::endl;
59  }
60 };
61 
62 class Sim
63 {
64  // Use a deque to have stable pointers even when dynamically adding peers
65  // - Alternatively consider using unique_ptrs allocated from arena
68 
69 public:
78 
86  {
87  }
88 
100  PeerGroup
102  {
103  std::vector<Peer*> newPeers;
104  newPeers.reserve(numPeers);
105  for (std::size_t i = 0; i < numPeers; ++i)
106  {
107  peers.emplace_back(
108  PeerID{static_cast<std::uint32_t>(peers.size())},
109  scheduler,
110  oracle,
111  net,
112  trustGraph,
113  collectors,
114  j);
115  newPeers.emplace_back(&peers.back());
116  }
117  PeerGroup res{newPeers};
118  allPeers = allPeers + res;
119  return res;
120  }
121 
124  size() const
125  {
126  return peers.size();
127  }
128 
135  void
136  run(int ledgers);
137 
139  void
140  run(SimDuration const& dur);
141 
147  bool
148  synchronized(PeerGroup const& g) const;
149 
152  bool
153  synchronized() const;
154 
161  branches(PeerGroup const& g) const;
162 
166  branches() const;
167 };
168 
169 } // namespace csf
170 } // namespace test
171 } // namespace ripple
172 
173 #endif
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition: Journal.h:74
ripple::test::csf::BasicSink::clock_
Scheduler::clock_type const & clock_
Definition: Sim.h:43
std::string
STL class.
ripple::test::csf::TrustGraph
Trust graph.
Definition: TrustGraph.h:43
beast::severities::kDisabled
@ kDisabled
Definition: Journal.h:41
ripple::test::csf::Sim
Definition: Sim.h:62
ripple::test::csf::Sim::peers
std::deque< Peer > peers
Definition: Sim.h:66
std::vector::reserve
T reserve(T... args)
std::vector< Peer * >
ripple::test::csf::Sim::scheduler
Scheduler scheduler
Definition: Sim.h:71
ripple::test::csf::Scheduler::clock
clock_type & clock() const
Return the clock.
Definition: test/csf/Scheduler.h:359
ripple::test::csf::Scheduler
Simulated discrete-event scheduler.
Definition: test/csf/Scheduler.h:47
ripple::test::csf::Sim::net
BasicNetwork< Peer * > net
Definition: Sim.h:75
random
ripple::test::csf::Sim::size
std::size_t size() const
The number of peers in the simulation.
Definition: Sim.h:124
iostream
ripple::test::csf::Sim::oracle
LedgerOracle oracle
Definition: Sim.h:74
std::mt19937_64
std::cout
ripple::test::csf::Sim::collectors
CollectorRefs collectors
Definition: Sim.h:77
ripple::test::csf::Sim::rng
std::mt19937_64 rng
Definition: Sim.h:70
ripple::test::csf::Sim::trustGraph
TrustGraph< Peer * > trustGraph
Definition: Sim.h:76
ripple::test::csf::Sim::branches
std::size_t branches() const
Calculate the number of branches in the network.
Definition: Sim.cpp:66
beast::Journal::Sink::Sink
Sink()=delete
ripple::test::csf::LedgerOracle
Oracle maintaining unique ledgers for a simulation.
Definition: ledgers.h:244
deque
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
ripple::test::csf::Sim::allPeers
PeerGroup allPeers
Definition: Sim.h:67
ripple::test::csf::PeerGroup
A group of simulation Peers.
Definition: PeerGroup.h:39
beast::manual_clock::now
time_point now() const override
Returns the current time.
Definition: manual_clock.h:55
std::vector::emplace_back
T emplace_back(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::endl
T endl(T... args)
beast::Journal::Sink::threshold
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Definition: beast_Journal.cpp:106
ripple::test::csf::BasicSink::write
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition: Sim.h:52
beast::severities::Severity
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
ripple::test::csf::Sim::sink
BasicSink sink
Definition: Sim.h:72
ripple::test::csf::BasicSink
Sink that prepends simulation time to messages.
Definition: Sim.h:41
ripple::test::csf::CollectorRefs
A container of CollectorRefs.
Definition: CollectorRef.h:323
std::size_t
ripple::test::csf::Sim::j
beast::Journal j
Definition: Sim.h:73
ripple::tagged_integer< std::uint32_t, PeerIDTag >
ripple::test::csf::SimDuration
typename SimClock::duration SimDuration
Definition: SimTime.h:35
beast::manual_clock< std::chrono::steady_clock >
ripple::test::csf::Sim::Sim
Sim()
Create a simulation.
Definition: Sim.h:85
ripple::test::csf::Sim::run
void run(int ledgers)
Run consensus protocol to generate the provided number of ledgers.
Definition: Sim.cpp:26
ripple::test::csf::Sim::createGroup
PeerGroup createGroup(std::size_t numPeers)
Create a new group of peers.
Definition: Sim.h:101
ripple::test::csf::BasicNetwork
Peer to peer network simulator.
Definition: BasicNetwork.h:83
ripple::test::csf::BasicSink::BasicSink
BasicSink(Scheduler::clock_type const &clock)
Definition: Sim.h:46
beast
Definition: base_uint.h:641