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
62class Sim
63{
64 // Use a deque to have stable pointers even when dynamically adding peers
65 // - Alternatively consider using unique_ptrs allocated from arena
68
69public:
78
85 Sim() : sink{scheduler.clock()}, j{sink}, net{scheduler}
86 {
87 }
88
102 {
103 std::vector<Peer*> newPeers;
104 newPeers.reserve(numPeers);
105 for (std::size_t i = 0; i < numPeers; ++i)
106 {
107 peers.emplace_back(
108 PeerID{static_cast<std::uint32_t>(peers.size())},
109 scheduler,
110 oracle,
111 net,
114 j);
115 newPeers.emplace_back(&peers.back());
116 }
117 PeerGroup res{newPeers};
118 allPeers = allPeers + res;
119 return res;
120 }
121
124 size() const
125 {
126 return peers.size();
127 }
128
135 void
136 run(int ledgers);
137
139 void
140 run(SimDuration const& dur);
141
147 bool
148 synchronized(PeerGroup const& g) const;
149
152 bool
153 synchronized() const;
154
161 branches(PeerGroup const& g) const;
162
166 branches() const;
167};
168
169} // namespace csf
170} // namespace test
171} // namespace ripple
172
173#endif
Abstraction for the underlying message destination.
Definition: Journal.h:75
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
A generic endpoint for log messages.
Definition: Journal.h:59
time_point now() const override
Returns the current time.
Definition: manual_clock.h:55
Peer to peer network simulator.
Definition: BasicNetwork.h:84
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
BasicSink(Scheduler::clock_type const &clock)
Definition: Sim.h:46
Scheduler::clock_type const & clock_
Definition: Sim.h:43
A container of CollectorRefs.
Definition: CollectorRef.h:324
Oracle maintaining unique ledgers for a simulation.
Definition: ledgers.h:245
A group of simulation Peers.
Definition: PeerGroup.h:40
Simulated discrete-event scheduler.
PeerGroup allPeers
Definition: Sim.h:67
Sim()
Create a simulation.
Definition: Sim.h:85
LedgerOracle oracle
Definition: Sim.h:74
Scheduler scheduler
Definition: Sim.h:71
std::size_t branches() const
Calculate the number of branches in the network.
Definition: Sim.cpp:66
beast::Journal j
Definition: Sim.h:73
TrustGraph< Peer * > trustGraph
Definition: Sim.h:76
void run(int ledgers)
Run consensus protocol to generate the provided number of ledgers.
Definition: Sim.cpp:26
CollectorRefs collectors
Definition: Sim.h:77
std::mt19937_64 rng
Definition: Sim.h:70
std::deque< Peer > peers
Definition: Sim.h:66
BasicNetwork< Peer * > net
Definition: Sim.h:75
PeerGroup createGroup(std::size_t numPeers)
Create a new group of peers.
Definition: Sim.h:101
std::size_t size() const
The number of peers in the simulation.
Definition: Sim.h:124
BasicSink sink
Definition: Sim.h:72
T emplace_back(T... args)
T endl(T... args)
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
typename SimClock::duration SimDuration
Definition: SimTime.h:35
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
T reserve(T... args)