xrpld
Loading...
Searching...
No Matches
PeerReservationTable.h
1#pragma once
2
3#include <xrpl/beast/hash/hash_append.h>
4#include <xrpl/beast/hash/uhash.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/protocol/PublicKey.h>
7
8#include <mutex>
9#include <optional>
10#include <string>
11#include <unordered_set>
12#include <vector>
13
14namespace xrpl {
15
16class DatabaseCon;
17
18// Value type for reservations.
19struct PeerReservation final
20{
21public:
23 std::string description = {}; // NOLINT(readability-redundant-member-init)
24
25 [[nodiscard]] auto
26 toJson() const -> json::Value;
27
28 template <typename Hasher>
29 friend void
31 Hasher& h,
32 PeerReservation const& x) noexcept // NOLINT(readability-identifier-naming)
33 {
35 hash_append(h, x.nodeId);
36 }
37
38 friend bool
39 operator<(PeerReservation const& a, PeerReservation const& b)
40 {
41 return a.nodeId < b.nodeId;
42 }
43};
44
45// TODO: When C++20 arrives, take advantage of "equivalence" instead of
46// "equality". Add an overload for `(PublicKey, PeerReservation)`, and just
47// pass a `PublicKey` directly to `unordered_set.find`.
48struct KeyEqual final
49{
50 bool
51 operator()(PeerReservation const& lhs, PeerReservation const& rhs) const
52 {
53 return lhs.nodeId == rhs.nodeId;
54 }
55};
56
58{
59public:
65
67 list() const;
68
69 bool
70 contains(PublicKey const& nodeId)
71 {
72 std::scoped_lock const lock(this->mutex_);
73 return table_.contains({.nodeId = nodeId, .description = {}});
74 }
75
76 // Because `ApplicationImp` has two-phase initialization, so must we.
77 // Our dependencies are not prepared until the second phase.
78 bool
79 load(DatabaseCon& connection);
80
86 insertOrAssign(PeerReservation const& reservation);
87
92 erase(PublicKey const& nodeId);
93
94private:
99};
100
101} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
static Sink & getNullSink()
Returns a Sink which does nothing.
std::vector< PeerReservation > list() const
PeerReservationTable(beast::Journal journal=beast::Journal(beast::Journal::getNullSink()))
std::optional< PeerReservation > insertOrAssign(PeerReservation const &reservation)
bool contains(PublicKey const &nodeId)
std::optional< PeerReservation > erase(PublicKey const &nodeId)
std::unordered_set< PeerReservation, beast::Uhash<>, KeyEqual > table_
bool load(DatabaseCon &connection)
A public key.
Definition PublicKey.h:42
std::enable_if_t< IsContiguouslyHashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
JSON (JavaScript Object Notation).
Definition json_errors.h:5
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool operator()(PeerReservation const &lhs, PeerReservation const &rhs) const
friend bool operator<(PeerReservation const &a, PeerReservation const &b)
friend void hash_append(Hasher &h, PeerReservation const &x) noexcept
auto toJson() const -> json::Value