rippled
Loading...
Searching...
No Matches
Sim.h
1#ifndef XRPL_TEST_CSF_SIM_H_INCLUDED
2#define XRPL_TEST_CSF_SIM_H_INCLUDED
3
4#include <test/csf/BasicNetwork.h>
5#include <test/csf/CollectorRef.h>
6#include <test/csf/Digraph.h>
7#include <test/csf/Peer.h>
8#include <test/csf/PeerGroup.h>
9#include <test/csf/Scheduler.h>
10#include <test/csf/SimTime.h>
11#include <test/csf/TrustGraph.h>
12
13#include <deque>
14#include <iostream>
15#include <random>
16
17namespace ripple {
18namespace test {
19namespace csf {
20
23{
25
26public:
28 : Sink(beast::severities::kDisabled, false), clock_{clock}
29 {
30 }
31
32 void
33 write(beast::severities::Severity level, std::string const& text) override
34 {
35 if (level < threshold())
36 return;
37
38 std::cout << clock_.now().time_since_epoch().count() << " " << text
39 << std::endl;
40 }
41
42 void
44 override
45 {
46 std::cout << clock_.now().time_since_epoch().count() << " " << text
47 << std::endl;
48 }
49};
50
51class Sim
52{
53 // Use a deque to have stable pointers even when dynamically adding peers
54 // - Alternatively consider using unique_ptrs allocated from arena
57
58public:
67
74 Sim() : sink{scheduler.clock()}, j{sink}, net{scheduler}
75 {
76 }
77
91 {
92 std::vector<Peer*> newPeers;
93 newPeers.reserve(numPeers);
94 for (std::size_t i = 0; i < numPeers; ++i)
95 {
96 peers.emplace_back(
97 PeerID{static_cast<std::uint32_t>(peers.size())},
99 oracle,
100 net,
103 j);
104 newPeers.emplace_back(&peers.back());
105 }
106 PeerGroup res{newPeers};
107 allPeers = allPeers + res;
108 return res;
109 }
110
113 size() const
114 {
115 return peers.size();
116 }
117
124 void
125 run(int ledgers);
126
128 void
129 run(SimDuration const& dur);
130
136 bool
137 synchronized(PeerGroup const& g) const;
138
141 bool
142 synchronized() const;
143
150 branches(PeerGroup const& g) const;
151
155 branches() const;
156};
157
158} // namespace csf
159} // namespace test
160} // namespace ripple
161
162#endif
Abstraction for the underlying message destination.
Definition Journal.h:57
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
A generic endpoint for log messages.
Definition Journal.h:41
time_point now() const override
Returns the current time.
Peer to peer network simulator.
Sink that prepends simulation time to messages.
Definition Sim.h:23
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition Sim.h:33
void writeAlways(beast::severities::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
Definition Sim.h:43
BasicSink(Scheduler::clock_type const &clock)
Definition Sim.h:27
Scheduler::clock_type const & clock_
Definition Sim.h:24
A container of CollectorRefs.
Oracle maintaining unique ledgers for a simulation.
Definition ledgers.h:231
A group of simulation Peers.
Definition PeerGroup.h:23
Simulated discrete-event scheduler.
PeerGroup allPeers
Definition Sim.h:56
Sim()
Create a simulation.
Definition Sim.h:74
LedgerOracle oracle
Definition Sim.h:63
Scheduler scheduler
Definition Sim.h:60
std::size_t branches() const
Calculate the number of branches in the network.
Definition Sim.cpp:48
beast::Journal j
Definition Sim.h:62
TrustGraph< Peer * > trustGraph
Definition Sim.h:65
void run(int ledgers)
Run consensus protocol to generate the provided number of ledgers.
Definition Sim.cpp:8
CollectorRefs collectors
Definition Sim.h:66
std::mt19937_64 rng
Definition Sim.h:59
std::deque< Peer > peers
Definition Sim.h:55
BasicNetwork< Peer * > net
Definition Sim.h:64
PeerGroup createGroup(std::size_t numPeers)
Create a new group of peers.
Definition Sim.h:90
std::size_t size() const
The number of peers in the simulation.
Definition Sim.h:113
BasicSink sink
Definition Sim.h:61
T emplace_back(T... args)
T endl(T... args)
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:13
typename SimClock::duration SimDuration
Definition SimTime.h:17
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
T reserve(T... args)