rippled
Loading...
Searching...
No Matches
TestBase.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef XRPL_NODESTORE_BASE_H_INCLUDED
21#define XRPL_NODESTORE_BASE_H_INCLUDED
22
23#include <xrpl/basics/StringUtilities.h>
24#include <xrpl/basics/random.h>
25#include <xrpl/beast/unit_test.h>
26#include <xrpl/beast/utility/rngfill.h>
27#include <xrpl/beast/xor_shift_engine.h>
28#include <xrpl/nodestore/Backend.h>
29#include <xrpl/nodestore/Database.h>
30#include <xrpl/nodestore/Types.h>
31
32#include <boost/algorithm/string.hpp>
33
34#include <iomanip>
35
36namespace ripple {
37namespace NodeStore {
38
47{
48 bool
51 std::shared_ptr<NodeObject> const& rhs) const noexcept
52 {
53 return lhs->getHash() < rhs->getHash();
54 }
55};
56
58inline bool
62{
63 return (lhs->getType() == rhs->getType()) &&
64 (lhs->getHash() == rhs->getHash()) &&
65 (lhs->getData() == rhs->getData());
66}
67
68// Some common code for the unit tests
69//
71{
72public:
73 // Tunable parameters
74 //
75 static std::size_t const minPayloadBytes = 1;
76 static std::size_t const maxPayloadBytes = 2000;
77 static int const numObjectsToTest = 2000;
78
79public:
80 // Create a predictable batch of objects
81 static Batch
83 {
85 batch.reserve(numObjects);
86
88
89 for (int i = 0; i < numObjects; ++i)
90 {
91 NodeObjectType const type = [&] {
92 switch (rand_int(rng, 3))
93 {
94 case 0:
95 return hotLEDGER;
96 case 1:
97 return hotACCOUNT_NODE;
98 case 2:
100 case 3:
101 return hotUNKNOWN;
102 }
103 // will never happen, but make static analysys tool happy.
104 return hotUNKNOWN;
105 }();
106
107 uint256 hash;
108 beast::rngfill(hash.begin(), hash.size(), rng);
109
111 beast::rngfill(blob.data(), blob.size(), rng);
112
113 batch.push_back(
114 NodeObject::createObject(type, std::move(blob), hash));
115 }
116
117 return batch;
118 }
119
120 // Compare two batches for equality
121 static bool
122 areBatchesEqual(Batch const& lhs, Batch const& rhs)
123 {
124 bool result = true;
125
126 if (lhs.size() == rhs.size())
127 {
128 for (int i = 0; i < lhs.size(); ++i)
129 {
130 if (!isSame(lhs[i], rhs[i]))
131 {
132 result = false;
133 break;
134 }
135 }
136 }
137 else
138 {
139 result = false;
140 }
141
142 return result;
143 }
144
145 // Store a batch in a backend
146 void
147 storeBatch(Backend& backend, Batch const& batch)
148 {
149 for (int i = 0; i < batch.size(); ++i)
150 {
151 backend.store(batch[i]);
152 }
153 }
154
155 // Get a copy of a batch in a backend
156 void
157 fetchCopyOfBatch(Backend& backend, Batch* pCopy, Batch const& batch)
158 {
159 pCopy->clear();
160 pCopy->reserve(batch.size());
161
162 for (int i = 0; i < batch.size(); ++i)
163 {
165
166 Status const status =
167 backend.fetch(batch[i]->getHash().cbegin(), &object);
168
169 BEAST_EXPECT(status == ok);
170
171 if (status == ok)
172 {
173 BEAST_EXPECT(object != nullptr);
174
175 pCopy->push_back(object);
176 }
177 }
178 }
179
180 void
181 fetchMissing(Backend& backend, Batch const& batch)
182 {
183 for (int i = 0; i < batch.size(); ++i)
184 {
186
187 Status const status =
188 backend.fetch(batch[i]->getHash().cbegin(), &object);
189
190 BEAST_EXPECT(status == notFound);
191 }
192 }
193
194 // Store all objects in a batch
195 static void
197 {
198 for (int i = 0; i < batch.size(); ++i)
199 {
200 std::shared_ptr<NodeObject> const object(batch[i]);
201
202 Blob data(object->getData());
203
204 db.store(
205 object->getType(),
206 std::move(data),
207 object->getHash(),
208 db.earliestLedgerSeq());
209 }
210 }
211
212 // Fetch all the hashes in one batch, into another batch.
213 static void
215 {
216 pCopy->clear();
217 pCopy->reserve(batch.size());
218
219 for (int i = 0; i < batch.size(); ++i)
220 {
222 db.fetchNodeObject(batch[i]->getHash(), 0);
223
224 if (object != nullptr)
225 pCopy->push_back(object);
226 }
227 }
228};
229
230} // namespace NodeStore
231} // namespace ripple
232
233#endif
A testsuite class.
Definition suite.h:55
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:40
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:51
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:240
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:221
static int const numObjectsToTest
Definition TestBase.h:77
static Batch createPredictableBatch(int numObjects, std::uint64_t seed)
Definition TestBase.h:82
static bool areBatchesEqual(Batch const &lhs, Batch const &rhs)
Definition TestBase.h:122
static void fetchCopyOfBatch(Database &db, Batch *pCopy, Batch const &batch)
Definition TestBase.h:214
static void storeBatch(Database &db, Batch const &batch)
Definition TestBase.h:196
void fetchCopyOfBatch(Backend &backend, Batch *pCopy, Batch const &batch)
Definition TestBase.h:157
static std::size_t const maxPayloadBytes
Definition TestBase.h:76
void storeBatch(Backend &backend, Batch const &batch)
Definition TestBase.h:147
static std::size_t const minPayloadBytes
Definition TestBase.h:75
void fetchMissing(Backend &backend, Batch const &batch)
Definition TestBase.h:181
iterator begin()
Definition base_uint.h:136
static constexpr std::size_t size()
Definition base_uint.h:526
T clear(T... args)
T data(T... args)
void rngfill(void *const buffer, std::size_t const bytes, Generator &g)
Definition rngfill.h:34
bool isSame(std::shared_ptr< NodeObject > const &lhs, std::shared_ptr< NodeObject > const &rhs)
Returns true if objects are identical.
Definition TestBase.h:59
Status
Return codes from Backend operations.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::enable_if_t< std::is_integral< Integral >::value, Integral > rand_int()
NodeObjectType
The types of node objects.
Definition NodeObject.h:32
@ hotACCOUNT_NODE
Definition NodeObject.h:35
@ hotTRANSACTION_NODE
Definition NodeObject.h:36
@ hotUNKNOWN
Definition NodeObject.h:33
@ hotLEDGER
Definition NodeObject.h:34
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:47
bool operator()(std::shared_ptr< NodeObject > const &lhs, std::shared_ptr< NodeObject > const &rhs) const noexcept
Definition TestBase.h:49