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 {
31 return lhs->getHash() < rhs->getHash();
32 }
33};
34
36inline bool
38{
39 return (lhs->getType() == rhs->getType()) && (lhs->getHash() == rhs->getHash()) &&
40 (lhs->getData() == rhs->getData());
41}
42
43// Some common code for the unit tests
44//
46{
47public:
48 // Tunable parameters
49 //
50 static std::size_t const minPayloadBytes = 1;
51 static std::size_t const maxPayloadBytes = 2000;
52 static int const numObjectsToTest = 2000;
53
54public:
55 // Create a predictable batch of objects
56 static Batch
58 {
60 batch.reserve(numObjects);
61
63
64 for (int i = 0; i < numObjects; ++i)
65 {
66 NodeObjectType const type = [&] {
67 switch (rand_int(rng, 3))
68 {
69 case 0:
70 return hotLEDGER;
71 case 1:
72 return hotACCOUNT_NODE;
73 case 2:
75 case 3:
76 return hotUNKNOWN;
77 }
78 // will never happen, but make static analysis tool happy.
79 return hotUNKNOWN;
80 }();
81
82 uint256 hash;
83 beast::rngfill(hash.begin(), hash.size(), rng);
84
86 beast::rngfill(blob.data(), blob.size(), rng);
87
88 batch.push_back(NodeObject::createObject(type, std::move(blob), hash));
89 }
90
91 return batch;
92 }
93
94 // Compare two batches for equality
95 static bool
96 areBatchesEqual(Batch const& lhs, Batch const& rhs)
97 {
98 bool result = true;
99
100 if (lhs.size() == rhs.size())
101 {
102 for (int i = 0; i < lhs.size(); ++i)
103 {
104 if (!isSame(lhs[i], rhs[i]))
105 {
106 result = false;
107 break;
108 }
109 }
110 }
111 else
112 {
113 result = false;
114 }
115
116 return result;
117 }
118
119 // Store a batch in a backend
120 void
121 storeBatch(Backend& backend, Batch const& batch)
122 {
123 for (int i = 0; i < batch.size(); ++i)
124 {
125 backend.store(batch[i]);
126 }
127 }
128
129 // Get a copy of a batch in a backend
130 void
131 fetchCopyOfBatch(Backend& backend, Batch* pCopy, Batch const& batch)
132 {
133 pCopy->clear();
134 pCopy->reserve(batch.size());
135
136 for (int i = 0; i < batch.size(); ++i)
137 {
139
140 Status const status = backend.fetch(batch[i]->getHash().cbegin(), &object);
141
142 BEAST_EXPECT(status == ok);
143
144 if (status == ok)
145 {
146 BEAST_EXPECT(object != nullptr);
147
148 pCopy->push_back(object);
149 }
150 }
151 }
152
153 void
154 fetchMissing(Backend& backend, Batch const& batch)
155 {
156 for (int i = 0; i < batch.size(); ++i)
157 {
159
160 Status const status = backend.fetch(batch[i]->getHash().cbegin(), &object);
161
162 BEAST_EXPECT(status == notFound);
163 }
164 }
165
166 // Store all objects in a batch
167 static void
169 {
170 for (int i = 0; i < batch.size(); ++i)
171 {
172 std::shared_ptr<NodeObject> const object(batch[i]);
173
174 Blob data(object->getData());
175
176 db.store(object->getType(), std::move(data), object->getHash(), db.earliestLedgerSeq());
177 }
178 }
179
180 // Fetch all the hashes in one batch, into another batch.
181 static void
183 {
184 pCopy->clear();
185 pCopy->reserve(batch.size());
186
187 for (int i = 0; i < batch.size(); ++i)
188 {
189 std::shared_ptr<NodeObject> object = db.fetchNodeObject(batch[i]->getHash(), 0);
190
191 if (object != nullptr)
192 pCopy->push_back(object);
193 }
194 }
195};
196
197} // namespace NodeStore
198} // 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(void const *key, 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:202
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:96
void fetchCopyOfBatch(Backend &backend, Batch *pCopy, Batch const &batch)
Definition TestBase.h:131
static void storeBatch(Database &db, Batch const &batch)
Definition TestBase.h:168
void fetchMissing(Backend &backend, Batch const &batch)
Definition TestBase.h:154
static int const numObjectsToTest
Definition TestBase.h:52
void storeBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:121
static std::size_t const minPayloadBytes
Definition TestBase.h:50
static std::size_t const maxPayloadBytes
Definition TestBase.h:51
static void fetchCopyOfBatch(Database &db, Batch *pCopy, Batch const &batch)
Definition TestBase.h:182
static Batch createPredictableBatch(int numObjects, std::uint64_t seed)
Definition TestBase.h:57
iterator begin()
Definition base_uint.h:112
static constexpr std::size_t size()
Definition base_uint.h:494
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:37
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