rippled
Loading...
Searching...
No Matches
DistributedValidatorsSim_test.cpp
1#include <test/csf.h>
2
3#include <xrpl/beast/unit_test.h>
4
5#include <boost/algorithm/string/classification.hpp>
6#include <boost/algorithm/string/split.hpp>
7
8#include <algorithm>
9#include <fstream>
10#include <sstream>
11#include <string>
12#include <utility>
13
14namespace xrpl {
15namespace test {
16
20{
21 void
23 std::size_t numPeers,
25 bool printHeaders = false)
26 {
27 using namespace csf;
28 using namespace std::chrono;
29
30 // Initialize persistent collector logs specific to this method
31 std::string const prefix =
32 "DistributedValidators_"
33 "completeTrustCompleteConnectFixedDelay";
34 std::fstream txLog(prefix + "_tx.csv", std::ofstream::app),
35 ledgerLog(prefix + "_ledger.csv", std::ofstream::app);
36
37 // title
38 log << prefix << "(" << numPeers << "," << delay.count() << ")" << std::endl;
39
40 // number of peers, UNLs, connections
41 BEAST_EXPECT(numPeers >= 1);
42
43 Sim sim;
44 PeerGroup peers = sim.createGroup(numPeers);
45
46 // complete trust graph
47 peers.trust(peers);
48
49 // complete connect graph with fixed delay
50 peers.connect(peers, delay);
51
52 // Initialize collectors to track statistics to report
53 TxCollector txCollector;
54 LedgerCollector ledgerCollector;
55 auto colls = makeCollectors(txCollector, ledgerCollector);
56 sim.collectors.add(colls);
57
58 // Initial round to set prior state
59 sim.run(1);
60
61 // Run for 10 minutes, submitting 100 tx/second
62 std::chrono::nanoseconds const simDuration = 10min;
63 std::chrono::nanoseconds const quiet = 10s;
64 Rate const rate{100, 1000ms};
65
66 // Initialize timers
67 HeartbeatTimer heart(sim.scheduler);
68
69 // txs, start/stop/step, target
70 auto peerSelector = makeSelector(peers.begin(), peers.end(), std::vector<double>(numPeers, 1.), sim.rng);
71 auto txSubmitter = makeSubmitter(
72 ConstantDistribution{rate.inv()},
73 sim.scheduler.now() + quiet,
74 sim.scheduler.now() + simDuration - quiet,
75 peerSelector,
76 sim.scheduler,
77 sim.rng);
78
79 // run simulation for given duration
80 heart.start();
81 sim.run(simDuration);
82
83 // BEAST_EXPECT(sim.branches() == 1);
84 // BEAST_EXPECT(sim.synchronized());
85
86 log << std::right;
87 log << "| Peers: " << std::setw(2) << peers.size();
88 log << " | Duration: " << std::setw(6) << duration_cast<milliseconds>(simDuration).count() << " ms";
89 log << " | Branches: " << std::setw(1) << sim.branches();
90 log << " | Synchronized: " << std::setw(1) << (sim.synchronized() ? "Y" : "N");
91 log << " |" << std::endl;
92
93 txCollector.report(simDuration, log, true);
94 ledgerCollector.report(simDuration, log, false);
95
96 std::string const tag = std::to_string(numPeers);
97 txCollector.csv(simDuration, txLog, tag, printHeaders);
98 ledgerCollector.csv(simDuration, ledgerLog, tag, printHeaders);
99
100 log << std::endl;
101 }
102
103 void
105 std::size_t numPeers,
107 bool printHeaders = false)
108 {
109 using namespace csf;
110 using namespace std::chrono;
111
112 // Initialize persistent collector logs specific to this method
113 std::string const prefix =
114 "DistributedValidators__"
115 "completeTrustScaleFreeConnectFixedDelay";
116 std::fstream txLog(prefix + "_tx.csv", std::ofstream::app),
117 ledgerLog(prefix + "_ledger.csv", std::ofstream::app);
118
119 // title
120 log << prefix << "(" << numPeers << "," << delay.count() << ")" << std::endl;
121
122 // number of peers, UNLs, connections
123 int const numCNLs = std::max(int(1.00 * numPeers), 1);
124 int const minCNLSize = std::max(int(0.25 * numCNLs), 1);
125 int const maxCNLSize = std::max(int(0.50 * numCNLs), 1);
126 BEAST_EXPECT(numPeers >= 1);
127 BEAST_EXPECT(numCNLs >= 1);
128 BEAST_EXPECT(1 <= minCNLSize && minCNLSize <= maxCNLSize && maxCNLSize <= numPeers);
129
130 Sim sim;
131 PeerGroup peers = sim.createGroup(numPeers);
132
133 // complete trust graph
134 peers.trust(peers);
135
136 // scale-free connect graph with fixed delay
137 std::vector<double> const ranks = sample(peers.size(), PowerLawDistribution{1, 3}, sim.rng);
138 randomRankedConnect(
139 peers, ranks, numCNLs, std::uniform_int_distribution<>{minCNLSize, maxCNLSize}, sim.rng, delay);
140
141 // Initialize collectors to track statistics to report
142 TxCollector txCollector;
143 LedgerCollector ledgerCollector;
144 auto colls = makeCollectors(txCollector, ledgerCollector);
145 sim.collectors.add(colls);
146
147 // Initial round to set prior state
148 sim.run(1);
149
150 // Run for 10 minutes, submitting 100 tx/second
151 std::chrono::nanoseconds simDuration = 10min;
152 std::chrono::nanoseconds quiet = 10s;
153 Rate rate{100, 1000ms};
154
155 // Initialize timers
156 HeartbeatTimer heart(sim.scheduler);
157
158 // txs, start/stop/step, target
159 auto peerSelector = makeSelector(peers.begin(), peers.end(), std::vector<double>(numPeers, 1.), sim.rng);
160 auto txSubmitter = makeSubmitter(
161 ConstantDistribution{rate.inv()},
162 sim.scheduler.now() + quiet,
163 sim.scheduler.now() + simDuration - quiet,
164 peerSelector,
165 sim.scheduler,
166 sim.rng);
167
168 // run simulation for given duration
169 heart.start();
170 sim.run(simDuration);
171
172 // BEAST_EXPECT(sim.branches() == 1);
173 // BEAST_EXPECT(sim.synchronized());
174
175 log << std::right;
176 log << "| Peers: " << std::setw(2) << peers.size();
177 log << " | Duration: " << std::setw(6) << duration_cast<milliseconds>(simDuration).count() << " ms";
178 log << " | Branches: " << std::setw(1) << sim.branches();
179 log << " | Synchronized: " << std::setw(1) << (sim.synchronized() ? "Y" : "N");
180 log << " |" << std::endl;
181
182 txCollector.report(simDuration, log, true);
183 ledgerCollector.report(simDuration, log, false);
184
185 std::string const tag = std::to_string(numPeers);
186 txCollector.csv(simDuration, txLog, tag, printHeaders);
187 ledgerCollector.csv(simDuration, ledgerLog, tag, printHeaders);
188
189 log << std::endl;
190 }
191
192 void
193 run() override
194 {
195 std::string const defaultArgs = "5 200";
196 std::string const args = arg().empty() ? defaultArgs : arg();
197 std::stringstream argStream(args);
198
199 int maxNumValidators = 0;
200 int delayCount(200);
201 argStream >> maxNumValidators;
202 argStream >> delayCount;
203
204 std::chrono::milliseconds const delay(delayCount);
205
206 log << "DistributedValidators: 1 to " << maxNumValidators << " Peers" << std::endl;
207
215 for (int i = 2; i <= maxNumValidators; i++)
216 {
218 }
219
227 for (int i = 2; i <= maxNumValidators; i++)
228 {
230 }
231 }
232};
233
234BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(DistributedValidators, consensus, xrpl, 2);
235
236} // namespace test
237} // namespace xrpl
A testsuite class.
Definition suite.h:52
log_os< char > log
Logging output stream.
Definition suite.h:145
std::string const & arg() const
Return the argument associated with the runner.
Definition suite.h:277
In progress simulations for diversifying and distributing validators.
void completeTrustCompleteConnectFixedDelay(std::size_t numPeers, std::chrono::milliseconds delay=std::chrono::milliseconds(200), bool printHeaders=false)
void completeTrustScaleFreeConnectFixedDelay(std::size_t numPeers, std::chrono::milliseconds delay=std::chrono::milliseconds(200), bool printHeaders=false)
T empty(T... args)
T endl(T... args)
T right(T... args)
T max(T... args)
Json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
Definition rate.cpp:13
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
T setw(T... args)
Represents a transfer rate.
Definition Rate.h:21
T to_string(T... args)