xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
csf
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
22
namespace
xrpl::test::csf
{
23
27
class
BasicSink
:
public
beast::Journal::Sink
28
{
29
Scheduler::ClockType
const
&
clock_
;
30
31
public
:
32
BasicSink
(
Scheduler::ClockType
const
& clock)
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
53
class
Sim
54
{
55
// Use a deque to have stable pointers even when dynamically adding peers
56
// - Alternatively consider using unique_ptrs allocated from arena
57
std::deque<Peer>
peers_
;
58
PeerGroup
allPeers_
;
59
60
public
:
61
std::mt19937_64
rng
;
62
Scheduler
scheduler
;
63
BasicSink
sink
;
64
beast::Journal
j
;
65
LedgerOracle
oracle
;
66
BasicNetwork<Peer*>
net
;
67
TrustGraph<Peer*>
trustGraph
;
68
CollectorRefs
collectors
;
69
76
// NOLINTNEXTLINE(bugprone-random-generator-seed): fixed seed for reproducible test
77
Sim
() :
sink
{
scheduler
.clock()},
j
{
sink
},
net
{
scheduler
}
78
{
79
}
80
93
PeerGroup
94
createGroup
(
std::size_t
numPeers)
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
,
105
trustGraph
,
106
collectors
,
107
j
);
108
newPeers.
emplace_back
(&
peers_
.back());
109
}
110
PeerGroup
res{newPeers};
111
allPeers_
=
allPeers_
+ res;
112
return
res;
113
}
114
118
std::size_t
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
161
std::size_t
162
branches
(
PeerGroup
const
& g)
const
;
163
167
std::size_t
168
branches
()
const
;
169
};
170
171
}
// namespace xrpl::test::csf
std::string
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition
Journal.h:59
beast::Journal::Sink::threshold
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Definition
beast_Journal.cpp:96
beast::Journal::Sink::Sink
Sink(Sink const &sink)=default
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::test::csf::BasicNetwork
Peer to peer network simulator.
Definition
BasicNetwork.h:64
xrpl::test::csf::BasicSink
Sink that prepends simulation time to messages.
Definition
Sim.h:28
xrpl::test::csf::BasicSink::write
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition
Sim.h:38
xrpl::test::csf::BasicSink::BasicSink
BasicSink(Scheduler::ClockType const &clock)
Definition
Sim.h:32
xrpl::test::csf::BasicSink::writeAlways
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
xrpl::test::csf::BasicSink::clock_
Scheduler::ClockType const & clock_
Definition
Sim.h:29
xrpl::test::csf::CollectorRefs
A container of CollectorRefs.
Definition
CollectorRef.h:311
xrpl::test::csf::LedgerOracle
Oracle maintaining unique ledgers for a simulation.
Definition
ledgers.h:249
xrpl::test::csf::PeerGroup
A group of simulation Peers.
Definition
PeerGroup.h:30
xrpl::test::csf::Scheduler
Simulated discrete-event scheduler.
Definition
src/tests/libxrpl/csf/Scheduler.h:28
xrpl::test::csf::Scheduler::ClockType
beast::ManualClock< std::chrono::steady_clock > ClockType
Definition
src/tests/libxrpl/csf/Scheduler.h:30
xrpl::test::csf::Sim::peers_
std::deque< Peer > peers_
Definition
Sim.h:57
xrpl::test::csf::Sim::Sim
Sim()
Create a simulation.
Definition
Sim.h:77
xrpl::test::csf::Sim::j
beast::Journal j
Definition
Sim.h:64
xrpl::test::csf::Sim::oracle
LedgerOracle oracle
Definition
Sim.h:65
xrpl::test::csf::Sim::rng
std::mt19937_64 rng
Definition
Sim.h:61
xrpl::test::csf::Sim::run
void run(int ledgers)
Run consensus protocol to generate the provided number of ledgers.
Definition
Sim.cpp:14
xrpl::test::csf::Sim::sink
BasicSink sink
Definition
Sim.h:63
xrpl::test::csf::Sim::collectors
CollectorRefs collectors
Definition
Sim.h:68
xrpl::test::csf::Sim::net
BasicNetwork< Peer * > net
Definition
Sim.h:66
xrpl::test::csf::Sim::trustGraph
TrustGraph< Peer * > trustGraph
Definition
Sim.h:67
xrpl::test::csf::Sim::size
std::size_t size() const
The number of peers in the simulation.
Definition
Sim.h:119
xrpl::test::csf::Sim::branches
std::size_t branches() const
Calculate the number of branches in the network.
Definition
Sim.cpp:54
xrpl::test::csf::Sim::scheduler
Scheduler scheduler
Definition
Sim.h:62
xrpl::test::csf::Sim::allPeers_
PeerGroup allPeers_
Definition
Sim.h:58
xrpl::test::csf::Sim::createGroup
PeerGroup createGroup(std::size_t numPeers)
Create a new group of peers.
Definition
Sim.h:94
xrpl::test::csf::TrustGraph
Trust graph.
Definition
TrustGraph.h:23
std::cout
cstddef
cstdint
deque
std::vector::emplace_back
T emplace_back(T... args)
std::endl
T endl(T... args)
std::uint32_t
iostream
std::mt19937_64
beast
Definition
base_uint.h:697
beast::Severity
Severity
Severity level / threshold of a Journal message.
Definition
Journal.h:16
xrpl::test::csf
Definition
Validations.cpp:23
xrpl::test::csf::SimDuration
SimClock::duration SimDuration
Definition
SimTime.h:14
xrpl::test::csf::PeerID
TaggedInteger< std::uint32_t, PeerIDTag > PeerID
Definition
Validation.h:17
random
std::vector::reserve
T reserve(T... args)
std::size_t
string
vector
Generated by
1.17.0