xrpld
Loading...
Searching...
No Matches
common.h
1#pragma once
2
3#include <xrpl/basics/ByteUtilities.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/basics/chrono.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/config/BasicConfig.h>
9#include <xrpl/config/Constants.h>
10#include <xrpl/nodestore/Database.h>
11#include <xrpl/nodestore/DummyScheduler.h>
12#include <xrpl/nodestore/Manager.h>
13#include <xrpl/shamap/Family.h>
14#include <xrpl/shamap/FullBelowCache.h>
15#include <xrpl/shamap/TreeNodeCache.h>
16
17#include <atomic>
18#include <chrono>
19#include <cstddef>
20#include <cstdint>
21#include <memory>
22#include <stdexcept>
23
24namespace xrpl::tests {
25
26class TestNodeFamily : public Family
27{
28private:
30
31 // Declared before the two caches, which both bind a reference to it in the initializer list.
33
36
38
40
41 // Written from whichever nodestore reader thread reports the miss, so read back atomically.
44
45public:
51 explicit TestNodeFamily(beast::Journal j, int readThreads = 1)
52 : fbCache_(std::make_shared<FullBelowCache>("App family full below cache", clock_, j))
53 , tnCache_(
54 std::make_shared<TreeNodeCache>(
55 "App family tree node cache",
56 65536,
57 std::chrono::minutes{1},
58 clock_,
59 j))
60 , j_(j)
61 {
62 Section testSection;
63 testSection.set(Keys::kType, "memory");
64 testSection.set(Keys::kPath, "SHAMap_test");
66 megabytes(4), scheduler_, readThreads, testSection, j);
67 }
68
70 db() override
71 {
72 return *db_;
73 }
74
75 [[nodiscard]] node_store::Database const&
76 db() const override
77 {
78 return *db_;
79 }
80
81 beast::Journal const&
82 journal() override
83 {
84 return j_;
85 }
86
89 {
90 return fbCache_;
91 }
92
95 {
96 return tnCache_;
97 }
98
99 void
100 sweep() override
101 {
102 fbCache_->sweep();
103 tnCache_->sweep();
104 }
105
114 void
115 missingNodeAcquireBySeq(std::uint32_t refNum, [[maybe_unused]] UInt256 const& nodeHash) override
116 {
117 missingBySeqRefNum_.store(refNum, std::memory_order_release);
119 Throw<std::runtime_error>("missing node");
120 }
121
128 void
130 [[maybe_unused]] UInt256 const& refHash,
131 [[maybe_unused]] std::uint32_t refNum) override
132 {
133 Throw<std::runtime_error>("missing node");
134 }
135
142 [[nodiscard]] std::size_t
144 {
145 return missingBySeqReports_.load(std::memory_order_acquire);
146 }
147
153 [[nodiscard]] std::uint32_t
155 {
156 return missingBySeqRefNum_.load(std::memory_order_acquire);
157 }
158
159 void
160 reset() override
161 {
162 (*fbCache_).reset();
163 (*tnCache_).reset();
164 }
165
168 {
169 return clock_;
170 }
171};
172
173} // namespace xrpl::tests
A generic endpoint for log messages.
Definition Journal.h:44
Family(Family const &)=delete
Holds a collection of configuration values.
Definition BasicConfig.h:28
void set(std::string const &key, std::string const &value)
Set a key/value pair.
Persistency layer for NodeObject.
Definition Database.h:45
Simple NodeStore Scheduler that just performs the tasks synchronously.
virtual std::unique_ptr< Database > makeDatabase(std::size_t burstSize, Scheduler &scheduler, int readThreads, Section const &backendParameters, beast::Journal journal)=0
Construct a NodeStore database.
static Manager & instance()
Returns the instance of the manager singleton.
node_store::Database const & db() const override
Definition common.h:76
std::shared_ptr< TreeNodeCache > tnCache_
Definition common.h:35
TestStopwatch & clock()
Definition common.h:167
std::atomic< std::size_t > missingBySeqReports_
Definition common.h:42
node_store::Database & db() override
Definition common.h:70
void missingNodeAcquireByHash(UInt256 const &refHash, std::uint32_t refNum) override
Throw, standing in for Family's real acquisition machinery.
Definition common.h:129
std::shared_ptr< FullBelowCache > getFullBelowCache() override
Return a pointer to the Family Full Below Cache.
Definition common.h:88
std::shared_ptr< TreeNodeCache > getTreeNodeCache() override
Return a pointer to the Family Tree Node Cache.
Definition common.h:94
beast::Journal const j_
Definition common.h:39
TestNodeFamily(beast::Journal j, int readThreads=1)
Definition common.h:51
void missingNodeAcquireBySeq(std::uint32_t refNum, UInt256 const &nodeHash) override
Record the report and throw, standing in for Family's real acquisition machinery.
Definition common.h:115
std::shared_ptr< FullBelowCache > fbCache_
Definition common.h:34
void sweep() override
Definition common.h:100
TestStopwatch clock_
Definition common.h:32
std::atomic< std::uint32_t > missingBySeqRefNum_
Definition common.h:43
std::unique_ptr< node_store::Database > db_
Definition common.h:29
beast::Journal const & journal() override
Definition common.h:82
std::uint32_t missingBySeqRefNum() const
The ledger sequence the most recent such report named.
Definition common.h:154
void reset() override
Definition common.h:160
std::size_t missingBySeqReports() const
How many times a map of this family has withdrawn its claim of being complete in the database.
Definition common.h:143
node_store::DummyScheduler scheduler_
Definition common.h:37
STL namespace.
detail::BasicFullBelowCache FullBelowCache
TaggedCache< UInt256, SHAMapTreeNode, false, intr_ptr::SharedWeakUnionPtr< SHAMapTreeNode >, SHAMapTreeNodePtr > TreeNodeCache
BaseUInt< 256 > UInt256
Definition base_uint.h:581
constexpr auto megabytes(T value) noexcept
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Definition chrono.h:95
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
static constexpr auto kType
Definition Constants.h:177
static constexpr auto kPath
Definition Constants.h:147