xrpld
Loading...
Searching...
No Matches
Sim.h
1#pragma once
2
3#include <xrpl/beast/utility/Journal.h>
4
5#include <csf/BasicNetwork.h>
6#include <csf/CollectorRef.h>
7#include <csf/Peer.h>
8#include <csf/PeerGroup.h>
9#include <csf/Scheduler.h>
10#include <csf/SimTime.h>
11#include <csf/TrustGraph.h>
12#include <csf/ledgers.h>
13
14#include <cstddef>
15#include <cstdint>
16#include <deque>
17#include <iostream>
18#include <random>
19#include <string>
20#include <vector>
21
22namespace xrpl::test::csf {
23
28{
30
31public:
33 : Sink(beast::Severity::Disabled, false), clock_{clock}
34 {
35 }
36
37 void
38 write(beast::Severity level, std::string const& text) override
39 {
40 if (level < threshold())
41 return;
42
43 std::cout << clock_.now().time_since_epoch().count() << " " << text << std::endl;
44 }
45
46 void
47 writeAlways(beast::Severity level, std::string const& text) override
48 {
49 std::cout << clock_.now().time_since_epoch().count() << " " << text << std::endl;
50 }
51};
52
53class Sim
54{
55 // Use a deque to have stable pointers even when dynamically adding peers
56 // - Alternatively consider using unique_ptrs allocated from arena
59
60public:
69
76 // NOLINTNEXTLINE(bugprone-random-generator-seed): fixed seed for reproducible test
77 Sim() : sink{scheduler.clock()}, j{sink}, net{scheduler}
78 {
79 }
80
95 {
96 std::vector<Peer*> newPeers;
97 newPeers.reserve(numPeers);
98 for (std::size_t i = 0; i < numPeers; ++i)
99 {
100 peers_.emplace_back(
101 PeerID{static_cast<std::uint32_t>(peers_.size())},
102 scheduler,
103 oracle,
104 net,
107 j);
108 newPeers.emplace_back(&peers_.back());
109 }
110 PeerGroup res{newPeers};
111 allPeers_ = allPeers_ + res;
112 return res;
113 }
114
119 size() const
120 {
121 return peers_.size();
122 }
123
131 void
132 run(int ledgers);
133
137 void
138 run(SimDuration const& dur);
139
146 static bool
147 synchronized(PeerGroup const& g);
148
152 bool
153 synchronized() const;
154
162 branches(PeerGroup const& g) const;
163
168 branches() const;
169};
170
171} // namespace xrpl::test::csf
Abstraction for the underlying message destination.
Definition Journal.h:59
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:44
Peer to peer network simulator.
Sink that prepends simulation time to messages.
Definition Sim.h:28
BasicSink(Scheduler::clock_type const &clock)
Definition Sim.h:32
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition Sim.h:38
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:47
Scheduler::clock_type const & clock_
Definition Sim.h:29
A container of CollectorRefs.
Oracle maintaining unique ledgers for a simulation.
Definition ledgers.h:249
A group of simulation Peers.
Definition PeerGroup.h:30
Simulated discrete-event scheduler.
beast::ManualClock< std::chrono::steady_clock > clock_type
std::deque< Peer > peers_
Definition Sim.h:57
Sim()
Create a simulation.
Definition Sim.h:77
beast::Journal j
Definition Sim.h:64
LedgerOracle oracle
Definition Sim.h:65
std::mt19937_64 rng
Definition Sim.h:61
void run(int ledgers)
Run consensus protocol to generate the provided number of ledgers.
Definition Sim.cpp:14
BasicSink sink
Definition Sim.h:63
CollectorRefs collectors
Definition Sim.h:68
BasicNetwork< Peer * > net
Definition Sim.h:66
TrustGraph< Peer * > trustGraph
Definition Sim.h:67
std::size_t size() const
The number of peers in the simulation.
Definition Sim.h:119
std::size_t branches() const
Calculate the number of branches in the network.
Definition Sim.cpp:54
Scheduler scheduler
Definition Sim.h:62
PeerGroup allPeers_
Definition Sim.h:58
PeerGroup createGroup(std::size_t numPeers)
Create a new group of peers.
Definition Sim.h:94
T emplace_back(T... args)
T endl(T... args)
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:16
SimClock::duration SimDuration
Definition SimTime.h:14
TaggedInteger< std::uint32_t, PeerIDTag > PeerID
Definition Validation.h:17
T reserve(T... args)