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/json/json_value.h>
7#include <xrpl/protocol/PublicKey.h>
8
9#include <mutex>
10#include <optional>
11#include <string>
12#include <unordered_set>
13#include <vector>
14
15namespace xrpl {
16
17class DatabaseCon;
18
19// Value type for reservations.
20struct PeerReservation final
21{
22public:
24 std::string description = {}; // NOLINT(readability-redundant-member-init)
25
26 [[nodiscard]] auto
27 toJson() const -> json::Value;
28
29 template <typename Hasher>
30 friend void
32 Hasher& h,
33 PeerReservation const& x) noexcept // NOLINT(readability-identifier-naming)
34 {
36 hash_append(h, x.nodeId);
37 }
38
39 friend bool
40 operator<(PeerReservation const& a, PeerReservation const& b)
41 {
42 return a.nodeId < b.nodeId;
43 }
44};
45
46// TODO: When C++20 arrives, take advantage of "equivalence" instead of
47// "equality". Add an overload for `(PublicKey, PeerReservation)`, and just
48// pass a `PublicKey` directly to `unordered_set.find`.
49struct KeyEqual final
50{
51 bool
52 operator()(PeerReservation const& lhs, PeerReservation const& rhs) const
53 {
54 return lhs.nodeId == rhs.nodeId;
55 }
56};
57
59{
60public:
66
68 list() const;
69
70 bool
71 contains(PublicKey const& nodeId)
72 {
73 std::scoped_lock const lock(this->mutex_);
74 return table_.contains({.nodeId = nodeId, .description = {}});
75 }
76
77 // Because `ApplicationImp` has two-phase initialization, so must we.
78 // Our dependencies are not prepared until the second phase.
79 bool
80 load(DatabaseCon& connection);
81
87 insertOrAssign(PeerReservation const& reservation);
88
93 erase(PublicKey const& nodeId);
94
95private:
100};
101
102} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
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:53
void 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