rippled
Loading...
Searching...
No Matches
Sim.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2017 Ripple Labs Inc
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_TEST_CSF_SIM_H_INCLUDED
21#define RIPPLE_TEST_CSF_SIM_H_INCLUDED
22
23#include <test/csf/BasicNetwork.h>
24#include <test/csf/CollectorRef.h>
25#include <test/csf/Digraph.h>
26#include <test/csf/Peer.h>
27#include <test/csf/PeerGroup.h>
28#include <test/csf/Scheduler.h>
29#include <test/csf/SimTime.h>
30#include <test/csf/TrustGraph.h>
31
32#include <deque>
33#include <iostream>
34#include <random>
35
36namespace ripple {
37namespace test {
38namespace csf {
39
42{
44
45public:
47 : Sink(beast::severities::kDisabled, false), clock_{clock}
48 {
49 }
50
51 void
52 write(beast::severities::Severity level, std::string const& text) override
53 {
54 if (level < threshold())
55 return;
56
57 std::cout << clock_.now().time_since_epoch().count() << " " << text
58 << std::endl;
59 }
60
61 void
63 override
64 {
65 std::cout << clock_.now().time_since_epoch().count() << " " << text
66 << std::endl;
67 }
68};
69
70class Sim
71{
72 // Use a deque to have stable pointers even when dynamically adding peers
73 // - Alternatively consider using unique_ptrs allocated from arena
76
77public:
86
93 Sim() : sink{scheduler.clock()}, j{sink}, net{scheduler}
94 {
95 }
96
110 {
111 std::vector<Peer*> newPeers;
112 newPeers.reserve(numPeers);
113 for (std::size_t i = 0; i < numPeers; ++i)
114 {
115 peers.emplace_back(
116 PeerID{static_cast<std::uint32_t>(peers.size())},
117 scheduler,
118 oracle,
119 net,
122 j);
123 newPeers.emplace_back(&peers.back());
124 }
125 PeerGroup res{newPeers};
126 allPeers = allPeers + res;
127 return res;
128 }
129
132 size() const
133 {
134 return peers.size();
135 }
136
143 void
144 run(int ledgers);
145
147 void
148 run(SimDuration const& dur);
149
155 bool
156 synchronized(PeerGroup const& g) const;
157
160 bool
161 synchronized() const;
162
169 branches(PeerGroup const& g) const;
170
174 branches() const;
175};
176
177} // namespace csf
178} // namespace test
179} // namespace ripple
180
181#endif
Abstraction for the underlying message destination.
Definition Journal.h:76
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
A generic endpoint for log messages.
Definition Journal.h:60
time_point now() const override
Returns the current time.
Peer to peer network simulator.
Sink that prepends simulation time to messages.
Definition Sim.h:42
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition Sim.h:52
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:62
BasicSink(Scheduler::clock_type const &clock)
Definition Sim.h:46
Scheduler::clock_type const & clock_
Definition Sim.h:43
A container of CollectorRefs.
Oracle maintaining unique ledgers for a simulation.
Definition ledgers.h:250
A group of simulation Peers.
Definition PeerGroup.h:42
Simulated discrete-event scheduler.
PeerGroup allPeers
Definition Sim.h:75
Sim()
Create a simulation.
Definition Sim.h:93
LedgerOracle oracle
Definition Sim.h:82
Scheduler scheduler
Definition Sim.h:79
std::size_t branches() const
Calculate the number of branches in the network.
Definition Sim.cpp:67
beast::Journal j
Definition Sim.h:81
TrustGraph< Peer * > trustGraph
Definition Sim.h:84
void run(int ledgers)
Run consensus protocol to generate the provided number of ledgers.
Definition Sim.cpp:27
CollectorRefs collectors
Definition Sim.h:85
std::mt19937_64 rng
Definition Sim.h:78
std::deque< Peer > peers
Definition Sim.h:74
BasicNetwork< Peer * > net
Definition Sim.h:83
PeerGroup createGroup(std::size_t numPeers)
Create a new group of peers.
Definition Sim.h:109
std::size_t size() const
The number of peers in the simulation.
Definition Sim.h:132
BasicSink sink
Definition Sim.h:80
T emplace_back(T... args)
T endl(T... args)
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:32
typename SimClock::duration SimDuration
Definition SimTime.h:36
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
T reserve(T... args)