rippled
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:
24
25 auto
26 toJson() const -> Json::Value;
27
28 template <typename Hasher>
29 friend void
30 hash_append(Hasher& h, PeerReservation const& x) noexcept
31 {
33 hash_append(h, x.nodeId);
34 }
35
36 friend bool
37 operator<(PeerReservation const& a, PeerReservation const& b)
38 {
39 return a.nodeId < b.nodeId;
40 }
41};
42
43// TODO: When C++20 arrives, take advantage of "equivalence" instead of
44// "equality". Add an overload for `(PublicKey, PeerReservation)`, and just
45// pass a `PublicKey` directly to `unordered_set.find`.
46struct KeyEqual final
47{
48 bool
49 operator()(PeerReservation const& lhs, PeerReservation const& rhs) const
50 {
51 return lhs.nodeId == rhs.nodeId;
52 }
53};
54
56{
57public:
62
64 list() const;
65
66 bool
67 contains(PublicKey const& nodeId)
68 {
69 std::lock_guard lock(this->mutex_);
70 return table_.find({nodeId}) != table_.end();
71 }
72
73 // Because `ApplicationImp` has two-phase initialization, so must we.
74 // Our dependencies are not prepared until the second phase.
75 bool
76 load(DatabaseCon& connection);
77
83 insert_or_assign(PeerReservation const& reservation);
84
89 erase(PublicKey const& nodeId);
90
91private:
96};
97
98} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
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 > insert_or_assign(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
JSON (JavaScript Object Notation).
Definition json_errors.h:5
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:5
bool operator()(PeerReservation const &lhs, PeerReservation const &rhs) const
auto toJson() const -> Json::Value
friend bool operator<(PeerReservation const &a, PeerReservation const &b)
friend void hash_append(Hasher &h, PeerReservation const &x) noexcept