xrpld
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::test::csf {
17
20{
22
23public:
25 : Sink(beast::Severity::Disabled, false), clock_{clock}
26 {
27 }
28
29 void
30 write(beast::Severity level, std::string const& text) override
31 {
32 if (level < threshold())
33 return;
34
35 std::cout << clock_.now().time_since_epoch().count() << " " << text << std::endl;
36 }
37
38 void
39 writeAlways(beast::Severity level, std::string const& text) override
40 {
41 std::cout << clock_.now().time_since_epoch().count() << " " << text << std::endl;
42 }
43};
44
45class Sim
46{
47 // Use a deque to have stable pointers even when dynamically adding peers
48 // - Alternatively consider using unique_ptrs allocated from arena
51
52public:
61
68 // NOLINTNEXTLINE(bugprone-random-generator-seed): fixed seed for reproducible test
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())},
94 oracle,
95 net,
98 j);
99 newPeers.emplace_back(&peers_.back());
100 }
101 PeerGroup res{newPeers};
102 allPeers_ = allPeers_ + res;
103 return res;
104 }
105
108 size() const
109 {
110 return peers_.size();
111 }
112
119 void
120 run(int ledgers);
121
123 void
124 run(SimDuration const& dur);
125
131 static bool
132 synchronized(PeerGroup const& g);
133
136 bool
137 synchronized() const;
138
145 branches(PeerGroup const& g) const;
146
150 branches() const;
151};
152
153} // namespace xrpl::test::csf
Abstraction for the underlying message destination.
Definition Journal.h:51
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Sink(Sink const &sink)=default
A generic endpoint for log messages.
Definition Journal.h:38
Peer to peer network simulator.
Sink that prepends simulation time to messages.
Definition Sim.h:20
BasicSink(Scheduler::clock_type const &clock)
Definition Sim.h:24
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition Sim.h:30
void writeAlways(beast::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
Definition Sim.h:39
Scheduler::clock_type const & clock_
Definition Sim.h:21
A container of CollectorRefs.
Oracle maintaining unique ledgers for a simulation.
Definition ledgers.h:227
A group of simulation Peers.
Definition PeerGroup.h:20
Simulated discrete-event scheduler.
beast::ManualClock< std::chrono::steady_clock > clock_type
std::deque< Peer > peers_
Definition Sim.h:49
Sim()
Create a simulation.
Definition Sim.h:69
beast::Journal j
Definition Sim.h:56
LedgerOracle oracle
Definition Sim.h:57
std::mt19937_64 rng
Definition Sim.h:53
void run(int ledgers)
Run consensus protocol to generate the provided number of ledgers.
Definition Sim.cpp:14
BasicSink sink
Definition Sim.h:55
CollectorRefs collectors
Definition Sim.h:60
BasicNetwork< Peer * > net
Definition Sim.h:58
TrustGraph< Peer * > trustGraph
Definition Sim.h:59
std::size_t size() const
The number of peers in the simulation.
Definition Sim.h:108
std::size_t branches() const
Calculate the number of branches in the network.
Definition Sim.cpp:54
Scheduler scheduler
Definition Sim.h:54
PeerGroup allPeers_
Definition Sim.h:50
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:11
SimClock::duration SimDuration
Definition SimTime.h:14
TaggedInteger< std::uint32_t, PeerIDTag > PeerID
Definition Validation.h:15
T reserve(T... args)