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