xrpld
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::NodeStore {
17
26{
27 bool
29 const noexcept
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 kMinPayloadBytes = 1;
51 static std::size_t const kMaxPayloadBytes = 2000;
52 static int const kNumObjectsToTest = 2000;
53
54public:
55 // Create a predictable batch of objects
56 static Batch
58 {
59 Batch batch;
60 batch.reserve(numObjects);
61
63
64 for (int i = 0; i < numObjects; ++i)
65 {
66 NodeObjectType const type = [&] {
67 switch (randInt(rng, 3))
68 {
69 case 0:
71 case 1:
73 case 2:
75 case 3:
76 default:
78 }
79 }();
80
81 uint256 hash;
82 beast::rngfill(hash.begin(), hash.size(), rng);
83
85 beast::rngfill(blob.data(), blob.size(), rng);
86
87 batch.push_back(NodeObject::createObject(type, std::move(blob), hash));
88 }
89
90 return batch;
91 }
92
93 // Compare two batches for equality
94 static bool
95 areBatchesEqual(Batch const& lhs, Batch const& rhs)
96 {
97 bool result = true;
98
99 if (lhs.size() == rhs.size())
100 {
101 for (int i = 0; i < lhs.size(); ++i)
102 {
103 if (!isSame(lhs[i], rhs[i]))
104 {
105 result = false;
106 break;
107 }
108 }
109 }
110 else
111 {
112 result = false;
113 }
114
115 return result;
116 }
117
118 // Store a batch in a backend
119 static void
120 storeBatch(Backend& backend, Batch const& batch)
121 {
122 for (int i = 0; i < batch.size(); ++i)
123 {
124 backend.store(batch[i]);
125 }
126 }
127
128 // Get a copy of a batch in a backend
129 void
130 fetchCopyOfBatch(Backend& backend, Batch* pCopy, Batch const& batch)
131 {
132 pCopy->clear();
133 pCopy->reserve(batch.size());
134
135 for (int i = 0; i < batch.size(); ++i)
136 {
138
139 Status const status = backend.fetch(batch[i]->getHash(), &object);
140
141 BEAST_EXPECT(status == Status::Ok);
142
143 if (status == Status::Ok)
144 {
145 BEAST_EXPECT(object != nullptr);
146
147 pCopy->push_back(object);
148 }
149 }
150 }
151
152 void
153 fetchMissing(Backend& backend, Batch const& batch)
154 {
155 for (int i = 0; i < batch.size(); ++i)
156 {
158
159 Status const status = backend.fetch(batch[i]->getHash(), &object);
160
161 BEAST_EXPECT(status == Status::NotFound);
162 }
163 }
164
165 // Store all objects in a batch
166 static void
167 storeBatch(Database& db, Batch const& batch)
168 {
169 for (int i = 0; i < batch.size(); ++i)
170 {
171 std::shared_ptr<NodeObject> const object(batch[i]);
172
173 Blob data(object->getData());
174
175 db.store(object->getType(), std::move(data), object->getHash(), db.earliestLedgerSeq());
176 }
177 }
178
179 // Fetch all the hashes in one batch, into another batch.
180 static void
181 fetchCopyOfBatch(Database& db, Batch* pCopy, Batch const& batch)
182 {
183 pCopy->clear();
184 pCopy->reserve(batch.size());
185
186 for (int i = 0; i < batch.size(); ++i)
187 {
188 std::shared_ptr<NodeObject> const object = db.fetchNodeObject(batch[i]->getHash(), 0);
189
190 if (object != nullptr)
191 pCopy->push_back(object);
192 }
193 }
194};
195
196} // namespace xrpl::NodeStore
A testsuite class.
Definition suite.h:50
iterator begin()
Definition base_uint.h:117
static constexpr std::size_t size()
Definition base_uint.h:530
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:19
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:32
std::uint32_t earliestLedgerSeq() const noexcept
Definition Database.h:194
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:231
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:95
static std::size_t const kMinPayloadBytes
Definition TestBase.h:50
void fetchCopyOfBatch(Backend &backend, Batch *pCopy, Batch const &batch)
Definition TestBase.h:130
static void storeBatch(Database &db, Batch const &batch)
Definition TestBase.h:167
void fetchMissing(Backend &backend, Batch const &batch)
Definition TestBase.h:153
static std::size_t const kMaxPayloadBytes
Definition TestBase.h:51
static void storeBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:120
static void fetchCopyOfBatch(Database &db, Batch *pCopy, Batch const &batch)
Definition TestBase.h:181
static Batch createPredictableBatch(int numObjects, std::uint64_t seed)
Definition TestBase.h:57
static int const kNumObjectsToTest
Definition TestBase.h:52
T clear(T... args)
T data(T... args)
detail::XorShiftEngine<> xor_shift_engine
XOR-shift Generator.
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.
std::vector< std::shared_ptr< NodeObject > > Batch
A batch of NodeObjects to write at once.
std::enable_if_t< std::is_integral_v< Integral >, Integral > randInt()
NodeObjectType
The types of node objects.
Definition NodeObject.h:12
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:10
BaseUInt< 256 > uint256
Definition base_uint.h:562
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:26
bool operator()(std::shared_ptr< NodeObject > const &lhs, std::shared_ptr< NodeObject > const &rhs) const noexcept
Definition TestBase.h:28