forked from XRPLF/rippled
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSim.h
161 lines (131 loc) · 4.52 KB
/
Sim.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012-2017 Ripple Labs Inc
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_TEST_CSF_SIM_H_INCLUDED
#define RIPPLE_TEST_CSF_SIM_H_INCLUDED
#include <test/csf/Digraph.h>
#include <test/csf/SimTime.h>
#include <test/csf/BasicNetwork.h>
#include <test/csf/Scheduler.h>
#include <test/csf/Peer.h>
#include <test/csf/PeerGroup.h>
#include <test/csf/TrustGraph.h>
#include <test/csf/CollectorRef.h>
#include <iostream>
#include <deque>
#include <random>
namespace ripple {
namespace test {
namespace csf {
/** Sink that prepends simulation time to messages */
class BasicSink : public beast::Journal::Sink
{
Scheduler::clock_type const & clock_;
public:
BasicSink (Scheduler::clock_type const & clock)
: Sink (beast::severities::kDisabled, false)
, clock_{clock}
{
}
void
write (beast::severities::Severity level,
std::string const& text) override
{
if (level < threshold())
return;
std::cout << clock_.now().time_since_epoch().count() << " " << text
<< std::endl;
}
};
class Sim
{
// Use a deque to have stable pointers even when dynamically adding peers
// - Alternatively consider using unique_ptrs allocated from arena
std::deque<Peer> peers;
public:
std::mt19937_64 rng;
Scheduler scheduler;
BasicSink sink;
beast::Journal j;
LedgerOracle oracle;
BasicNetwork<Peer*> net;
TrustGraph<Peer*> trustGraph;
CollectorRefs collectors;
/** Create a simulation
Creates a new simulation. The simulation has no peers, no trust links
and no network connections.
*/
Sim() : sink{scheduler.clock()}, j{sink}, net{scheduler}
{
}
/** Create a new group of peers.
Creates a new group of peers. The peers do not have any trust relations
or network connections by default. Those must be configured by the client.
@param numPeers The number of peers in the group
@return PeerGroup representing these new peers
@note This increases the number of peers in the simulation by numPeers.
*/
PeerGroup
createGroup(std::size_t numPeers)
{
std::vector<Peer*> newPeers;
newPeers.reserve(numPeers);
for (std::size_t i = 0; i < numPeers; ++i)
{
peers.emplace_back(
PeerID{static_cast<std::uint32_t>(peers.size())},
scheduler,
oracle,
net,
trustGraph,
collectors,
j);
newPeers.emplace_back(&peers.back());
}
return PeerGroup{newPeers};
}
//! The number of peers in the simulation
std::size_t
size() const
{
return peers.size();
}
/** Run consensus protocol to generate the provided number of ledgers.
Has each peer run consensus until it closes `ledgers` more ledgers.
@param ledgers The number of additional ledgers to close
*/
void
run(int ledgers);
/** Run consensus for the given duration */
void
run(SimDuration const& dur);
/** Check whether all peers in the network are synchronized.
Nodes in the network are synchronized if they share the same last
fully validated and last generated ledger.
*/
bool
synchronized() const;
/** Calculate the number of branches in the network.
A branch occurs if two peers have fullyValidatedLedgers that are not on
the same chain of ledgers.
*/
std::size_t
branches() const;
};
} // namespace csf
} // namespace test
} // namespace ripple
#endif