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:
26 BasicSink(Scheduler::clock_type const& clock) : Sink(beast::severities::kDisabled, false), clock_{clock}
27 {
28 }
29
30 void
31 write(beast::severities::Severity level, std::string const& text) override
32 {
33 if (level < threshold())
34 return;
35
36 std::cout << clock_.now().time_since_epoch().count() << " " << text << std::endl;
37 }
38
39 void
41 {
42 std::cout << clock_.now().time_since_epoch().count() << " " << text << std::endl;
43 }
44};
45
46class Sim
47{
48 // Use a deque to have stable pointers even when dynamically adding peers
49 // - Alternatively consider using unique_ptrs allocated from arena
52
53public:
62
69 Sim() : sink{scheduler.clock()}, j{sink}, net{scheduler}
70 {
71 }
72
86 {
87 std::vector<Peer*> newPeers;
88 newPeers.reserve(numPeers);
89 for (std::size_t i = 0; i < numPeers; ++i)
90 {
91 peers.emplace_back(
92 PeerID{static_cast<std::uint32_t>(peers.size())}, scheduler, oracle, net, trustGraph, collectors, j);
93 newPeers.emplace_back(&peers.back());
94 }
95 PeerGroup res{newPeers};
96 allPeers = allPeers + res;
97 return res;
98 }
99
102 size() const
103 {
104 return peers.size();
105 }
106
113 void
114 run(int ledgers);
115
117 void
118 run(SimDuration const& dur);
119
125 bool
126 synchronized(PeerGroup const& g) const;
127
130 bool
131 synchronized() const;
132
139 branches(PeerGroup const& g) const;
140
144 branches() const;
145};
146
147} // namespace csf
148} // namespace test
149} // 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:31
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:40
A container of CollectorRefs.
Oracle maintaining unique ledgers for a simulation.
Definition ledgers.h:223
A group of simulation Peers.
Definition PeerGroup.h:22
Simulated discrete-event scheduler.
Sim()
Create a simulation.
Definition Sim.h:69
beast::Journal j
Definition Sim.h:57
LedgerOracle oracle
Definition Sim.h:58
std::mt19937_64 rng
Definition Sim.h:54
PeerGroup allPeers
Definition Sim.h:51
void run(int ledgers)
Run consensus protocol to generate the provided number of ledgers.
Definition Sim.cpp:8
BasicSink sink
Definition Sim.h:56
CollectorRefs collectors
Definition Sim.h:61
std::deque< Peer > peers
Definition Sim.h:50
BasicNetwork< Peer * > net
Definition Sim.h:59
TrustGraph< Peer * > trustGraph
Definition Sim.h:60
std::size_t size() const
The number of peers in the simulation.
Definition Sim.h:102
std::size_t branches() const
Calculate the number of branches in the network.
Definition Sim.cpp:48
Scheduler scheduler
Definition Sim.h:55
PeerGroup createGroup(std::size_t numPeers)
Create a new group of peers.
Definition Sim.h:85
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)