xrpld
Loading...
Searching...
No Matches
ScaleFreeSim_test.cpp
1#include <test/csf/PeerGroup.h>
2#include <test/csf/Sim.h>
3#include <test/csf/collectors.h>
4#include <test/csf/random.h>
5#include <test/csf/submitters.h>
6#include <test/csf/timers.h>
7
8#include <xrpld/consensus/ConsensusParms.h>
9
10#include <xrpl/beast/unit_test/suite.h>
11
12#include <chrono>
13#include <ostream>
14#include <random>
15#include <vector>
16
17namespace xrpl::test {
18
20{
21 void
22 run() override
23 {
24 using namespace std::chrono;
25 using namespace csf;
26
27 // Generate a quasi-random scale free network and simulate consensus
28 // as we vary transaction submission rates
29
30 int const n = 100; // Peers
31
32 int const numUNLs = 15; // UNL lists
33 int const minUNLSize = n / 4, maxUNLSize = n / 2;
34
35 ConsensusParms const parms{};
36 Sim sim;
37 PeerGroup network = sim.createGroup(n);
38
39 // generate trust ranks
40 std::vector<double> const ranks =
41 sample(network.size(), PowerLawDistribution{1, 3}, sim.rng);
42
43 // generate scale-free trust graph
44 randomRankedTrust(
45 network,
46 ranks,
47 numUNLs,
48 std::uniform_int_distribution<>{minUNLSize, maxUNLSize},
49 sim.rng);
50
51 // nodes with a trust line in either direction are network-connected
52 network.connectFromTrust(round<milliseconds>(0.2 * parms.ledgerGRANULARITY));
53
54 // Initialize collectors to track statistics to report
55 TxCollector txCollector;
56 LedgerCollector ledgerCollector;
57 auto colls = makeCollectors(txCollector, ledgerCollector);
58 sim.collectors.add(colls);
59
60 // Initial round to set prior state
61 sim.run(1);
62
63 // Initialize timers
64 HeartbeatTimer heart(sim.scheduler, seconds(10s));
65
66 // Run for 10 minutes, submitting 100 tx/second
67 std::chrono::nanoseconds const simDuration = 10min;
68 std::chrono::nanoseconds const quiet = 10s;
69 Rate const rate{.count = 100, .duration = 1000ms};
70
71 // txs, start/stop/step, target
72 auto peerSelector = makeSelector(network.begin(), network.end(), ranks, sim.rng);
73 auto txSubmitter = makeSubmitter(
74 ConstantDistribution{rate.inv()},
75 sim.scheduler.now() + quiet,
76 sim.scheduler.now() + (simDuration - quiet),
77 peerSelector,
78 sim.scheduler,
79 sim.rng);
80
81 // run simulation for given duration
82 heart.start();
83 sim.run(simDuration);
84
85 BEAST_EXPECT(sim.branches() == 1);
86 BEAST_EXPECT(sim.synchronized());
87
88 // TODO: Clean up this formatting mess!!
89
90 log << "Peers: " << network.size() << std::endl;
91 log << "Simulated Duration: " << duration_cast<milliseconds>(simDuration).count() << " ms"
92 << std::endl;
93 log << "Branches: " << sim.branches() << std::endl;
94 log << "Synchronized: " << (sim.synchronized() ? "Y" : "N") << std::endl;
95 log << std::endl;
96
97 txCollector.report(simDuration, log);
98 ledgerCollector.report(simDuration, log);
99 // Print summary?
100 // # forks? # of LCLs?
101 // # peers
102 // # tx submitted
103 // # ledgers/sec etc.?
104 }
105};
106
107BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(ScaleFreeSim, consensus, xrpl, 80);
108
109} // namespace xrpl::test
A testsuite class.
Definition suite.h:50
LogOs< char > log
Logging output stream.
Definition suite.h:146
void run() override
Runs the suite.
T duration_cast(T... args)
T endl(T... args)
T min(T... args)
json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
Definition rate.cpp:15
BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(CrossingLimits, app, xrpl, 10)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T round(T... args)
T sample(T... args)
Consensus algorithm parameters.
std::chrono::milliseconds const ledgerGRANULARITY
How often we check state or change positions.
Represents a transfer rate.
Definition Rate.h:20