rippled
Loading...
Searching...
No Matches
PeerReservationTable.h
1#ifndef XRPL_OVERLAY_PEER_RESERVATION_TABLE_H_INCLUDED
2#define XRPL_OVERLAY_PEER_RESERVATION_TABLE_H_INCLUDED
3
4#include <xrpl/beast/hash/hash_append.h>
5#include <xrpl/beast/hash/uhash.h>
6#include <xrpl/beast/utility/Journal.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 ripple {
16
17class DatabaseCon;
18
19// Value type for reservations.
20struct PeerReservation final
21{
22public:
25
26 auto
27 toJson() const -> Json::Value;
28
29 template <typename Hasher>
30 friend void
31 hash_append(Hasher& h, PeerReservation const& x) noexcept
32 {
34 hash_append(h, x.nodeId);
35 }
36
37 friend bool
38 operator<(PeerReservation const& a, PeerReservation const& b)
39 {
40 return a.nodeId < b.nodeId;
41 }
42};
43
44// TODO: When C++20 arrives, take advantage of "equivalence" instead of
45// "equality". Add an overload for `(PublicKey, PeerReservation)`, and just
46// pass a `PublicKey` directly to `unordered_set.find`.
47struct KeyEqual final
48{
49 bool
50 operator()(PeerReservation const& lhs, PeerReservation const& rhs) const
51 {
52 return lhs.nodeId == rhs.nodeId;
53 }
54};
55
57{
58public:
64
66 list() const;
67
68 bool
69 contains(PublicKey const& nodeId)
70 {
71 std::lock_guard lock(this->mutex_);
72 return table_.find({nodeId}) != table_.end();
73 }
74
75 // Because `ApplicationImp` has two-phase initialization, so must we.
76 // Our dependencies are not prepared until the second phase.
77 bool
78 load(DatabaseCon& connection);
79
85 insert_or_assign(PeerReservation const& reservation);
86
91 erase(PublicKey const& nodeId);
92
93private:
98};
99
100} // namespace ripple
101
102#endif
A generic endpoint for log messages.
Definition Journal.h:41
static Sink & getNullSink()
Returns a Sink which does nothing.
std::optional< PeerReservation > insert_or_assign(PeerReservation const &reservation)
std::vector< PeerReservation > list() const
bool contains(PublicKey const &nodeId)
std::unordered_set< PeerReservation, beast::uhash<>, KeyEqual > table_
std::optional< PeerReservation > erase(PublicKey const &nodeId)
bool load(DatabaseCon &connection)
PeerReservationTable(beast::Journal journal=beast::Journal(beast::Journal::getNullSink()))
A public key.
Definition PublicKey.h:43
JSON (JavaScript Object Notation).
Definition json_errors.h:6
std::enable_if_t< is_contiguously_hashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
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