rippled
Loading...
Searching...
No Matches
TestBase.h
1#pragma once
2
3#include <xrpl/basics/StringUtilities.h>
4#include <xrpl/basics/random.h>
5#include <xrpl/beast/unit_test.h>
6#include <xrpl/beast/utility/rngfill.h>
7#include <xrpl/beast/xor_shift_engine.h>
8#include <xrpl/nodestore/Backend.h>
9#include <xrpl/nodestore/Database.h>
10#include <xrpl/nodestore/Types.h>
11
12#include <boost/algorithm/string.hpp>
13
14#include <iomanip>
15
16namespace xrpl {
17namespace NodeStore {
18
27{
28 bool
30 const noexcept
31 {
32 return lhs->getHash() < rhs->getHash();
33 }
34};
35
37inline bool
39{
40 return (lhs->getType() == rhs->getType()) && (lhs->getHash() == rhs->getHash()) &&
41 (lhs->getData() == rhs->getData());
42}
43
44// Some common code for the unit tests
45//
47{
48public:
49 // Tunable parameters
50 //
51 static std::size_t const minPayloadBytes = 1;
52 static std::size_t const maxPayloadBytes = 2000;
53 static int const numObjectsToTest = 2000;
54
55public:
56 // Create a predictable batch of objects
57 static Batch
59 {
61 batch.reserve(numObjects);
62
64
65 for (int i = 0; i < numObjects; ++i)
66 {
67 NodeObjectType const type = [&] {
68 switch (rand_int(rng, 3))
69 {
70 case 0:
71 return hotLEDGER;
72 case 1:
73 return hotACCOUNT_NODE;
74 case 2:
76 case 3:
77 return hotUNKNOWN;
78 }
79 // will never happen, but make static analysis tool happy.
80 return hotUNKNOWN;
81 }();
82
83 uint256 hash;
84 beast::rngfill(hash.begin(), hash.size(), rng);
85
87 beast::rngfill(blob.data(), blob.size(), rng);
88
89 batch.push_back(NodeObject::createObject(type, std::move(blob), hash));
90 }
91
92 return batch;
93 }
94
95 // Compare two batches for equality
96 static bool
97 areBatchesEqual(Batch const& lhs, Batch const& rhs)
98 {
99 bool result = true;
100
101 if (lhs.size() == rhs.size())
102 {
103 for (int i = 0; i < lhs.size(); ++i)
104 {
105 if (!isSame(lhs[i], rhs[i]))
106 {
107 result = false;
108 break;
109 }
110 }
111 }
112 else
113 {
114 result = false;
115 }
116
117 return result;
118 }
119
120 // Store a batch in a backend
121 void
122 storeBatch(Backend& backend, Batch const& batch)
123 {
124 for (int i = 0; i < batch.size(); ++i)
125 {
126 backend.store(batch[i]);
127 }
128 }
129
130 // Get a copy of a batch in a backend
131 void
132 fetchCopyOfBatch(Backend& backend, Batch* pCopy, Batch const& batch)
133 {
134 pCopy->clear();
135 pCopy->reserve(batch.size());
136
137 for (int i = 0; i < batch.size(); ++i)
138 {
140
141 Status const status = backend.fetch(batch[i]->getHash(), &object);
142
143 BEAST_EXPECT(status == ok);
144
145 if (status == ok)
146 {
147 BEAST_EXPECT(object != nullptr);
148
149 pCopy->push_back(object);
150 }
151 }
152 }
153
154 void
155 fetchMissing(Backend& backend, Batch const& batch)
156 {
157 for (int i = 0; i < batch.size(); ++i)
158 {
160
161 Status const status = backend.fetch(batch[i]->getHash(), &object);
162
163 BEAST_EXPECT(status == notFound);
164 }
165 }
166
167 // Store all objects in a batch
168 static void
170 {
171 for (int i = 0; i < batch.size(); ++i)
172 {
173 std::shared_ptr<NodeObject> const object(batch[i]);
174
175 Blob data(object->getData());
176
177 db.store(object->getType(), std::move(data), object->getHash(), db.earliestLedgerSeq());
178 }
179 }
180
181 // Fetch all the hashes in one batch, into another batch.
182 static void
184 {
185 pCopy->clear();
186 pCopy->reserve(batch.size());
187
188 for (int i = 0; i < batch.size(); ++i)
189 {
190 std::shared_ptr<NodeObject> const object = db.fetchNodeObject(batch[i]->getHash(), 0);
191
192 if (object != nullptr)
193 pCopy->push_back(object);
194 }
195 }
196};
197
198} // namespace NodeStore
199} // namespace xrpl
A testsuite class.
Definition suite.h:51
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
A backend used for the NodeStore.
Definition Backend.h:20
virtual void store(std::shared_ptr< NodeObject > const &object)=0
Store a single object.
virtual Status fetch(uint256 const &hash, std::shared_ptr< NodeObject > *pObject)=0
Fetch a single object.
Persistency layer for NodeObject.
Definition Database.h:31
std::uint32_t earliestLedgerSeq() const noexcept
Definition Database.h:189
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::synchronous, bool duplicate=false)
Fetch a node object.
Definition Database.cpp:209
virtual void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t ledgerSeq)=0
Store the object.
static bool areBatchesEqual(Batch const &lhs, Batch const &rhs)
Definition TestBase.h:97
void fetchCopyOfBatch(Backend &backend, Batch *pCopy, Batch const &batch)
Definition TestBase.h:132
static void storeBatch(Database &db, Batch const &batch)
Definition TestBase.h:169
void fetchMissing(Backend &backend, Batch const &batch)
Definition TestBase.h:155
static int const numObjectsToTest
Definition TestBase.h:53
void storeBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:122
static std::size_t const minPayloadBytes
Definition TestBase.h:51
static std::size_t const maxPayloadBytes
Definition TestBase.h:52
static void fetchCopyOfBatch(Database &db, Batch *pCopy, Batch const &batch)
Definition TestBase.h:183
static Batch createPredictableBatch(int numObjects, std::uint64_t seed)
Definition TestBase.h:58
iterator begin()
Definition base_uint.h:112
static constexpr std::size_t size()
Definition base_uint.h:499
T clear(T... args)
T data(T... args)
void rngfill(void *const buffer, std::size_t const bytes, Generator &g)
Definition rngfill.h:14
bool isSame(std::shared_ptr< NodeObject > const &lhs, std::shared_ptr< NodeObject > const &rhs)
Returns true if objects are identical.
Definition TestBase.h:38
Status
Return codes from Backend operations.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::enable_if_t< std::is_integral< Integral >::value, Integral > rand_int()
NodeObjectType
The types of node objects.
Definition NodeObject.h:12
@ hotLEDGER
Definition NodeObject.h:14
@ hotTRANSACTION_NODE
Definition NodeObject.h:16
@ hotACCOUNT_NODE
Definition NodeObject.h:15
@ hotUNKNOWN
Definition NodeObject.h:13
T push_back(T... args)
T reserve(T... args)
T size(T... args)
Binary function that satisfies the strict-weak-ordering requirement.
Definition TestBase.h:27
bool operator()(std::shared_ptr< NodeObject > const &lhs, std::shared_ptr< NodeObject > const &rhs) const noexcept
Definition TestBase.h:29