rippled
Loading...
Searching...
No Matches
Sim.h
1#pragma once
2
3#include <test/csf/BasicNetwork.h>
4#include <test/csf/CollectorRef.h>
5#include <test/csf/Digraph.h>
6#include <test/csf/Peer.h>
7#include <test/csf/PeerGroup.h>
8#include <test/csf/Scheduler.h>
9#include <test/csf/SimTime.h>
10#include <test/csf/TrustGraph.h>
11
12#include <deque>
13#include <iostream>
14#include <random>
15
16namespace xrpl {
17namespace test {
18namespace csf {
19
22{
24
25public:
27 : Sink(beast::severities::kDisabled, false), clock_{clock}
28 {
29 }
30
31 void
32 write(beast::severities::Severity level, std::string const& text) override
33 {
34 if (level < threshold())
35 return;
36
37 std::cout << clock_.now().time_since_epoch().count() << " " << text << std::endl;
38 }
39
40 void
42 {
43 std::cout << clock_.now().time_since_epoch().count() << " " << text << std::endl;
44 }
45};
46
47class Sim
48{
49 // Use a deque to have stable pointers even when dynamically adding peers
50 // - Alternatively consider using unique_ptrs allocated from arena
53
54public:
63
70 Sim() : sink{scheduler.clock()}, j{sink}, net{scheduler}
71 {
72 }
73
87 {
88 std::vector<Peer*> newPeers;
89 newPeers.reserve(numPeers);
90 for (std::size_t i = 0; i < numPeers; ++i)
91 {
92 peers.emplace_back(
93 PeerID{static_cast<std::uint32_t>(peers.size())},
95 oracle,
96 net,
99 j);
100 newPeers.emplace_back(&peers.back());
101 }
102 PeerGroup res{newPeers};
103 allPeers = allPeers + res;
104 return res;
105 }
106
109 size() const
110 {
111 return peers.size();
112 }
113
120 void
121 run(int ledgers);
122
124 void
125 run(SimDuration const& dur);
126
132 static bool
133 synchronized(PeerGroup const& g);
134
137 bool
138 synchronized() const;
139
146 branches(PeerGroup const& g) const;
147
151 branches() const;
152};
153
154} // namespace csf
155} // namespace test
156} // namespace xrpl
Abstraction for the underlying message destination.
Definition Journal.h:56
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
A generic endpoint for log messages.
Definition Journal.h:40
time_point now() const override
Returns the current time.
Peer to peer network simulator.
Sink that prepends simulation time to messages.
Definition Sim.h:22
BasicSink(Scheduler::clock_type const &clock)
Definition Sim.h:26
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition Sim.h:32
Scheduler::clock_type const & clock_
Definition Sim.h:23
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:41
A container of CollectorRefs.
Oracle maintaining unique ledgers for a simulation.
Definition ledgers.h:229
A group of simulation Peers.
Definition PeerGroup.h:22
Simulated discrete-event scheduler.
Sim()
Create a simulation.
Definition Sim.h:70
beast::Journal j
Definition Sim.h:58
LedgerOracle oracle
Definition Sim.h:59
std::mt19937_64 rng
Definition Sim.h:55
PeerGroup allPeers
Definition Sim.h:52
void run(int ledgers)
Run consensus protocol to generate the provided number of ledgers.
Definition Sim.cpp:8
BasicSink sink
Definition Sim.h:57
CollectorRefs collectors
Definition Sim.h:62
std::deque< Peer > peers
Definition Sim.h:51
BasicNetwork< Peer * > net
Definition Sim.h:60
TrustGraph< Peer * > trustGraph
Definition Sim.h:61
std::size_t size() const
The number of peers in the simulation.
Definition Sim.h:109
std::size_t branches() const
Calculate the number of branches in the network.
Definition Sim.cpp:48
Scheduler scheduler
Definition Sim.h:56
PeerGroup createGroup(std::size_t numPeers)
Create a new group of peers.
Definition Sim.h:86
T emplace_back(T... args)
T endl(T... args)
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:12
typename SimClock::duration SimDuration
Definition SimTime.h:16
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T reserve(T... args)