rippled
Loading...
Searching...
No Matches
PeerReservationTable.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2019 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_OVERLAY_PEER_RESERVATION_TABLE_H_INCLUDED
21#define RIPPLE_OVERLAY_PEER_RESERVATION_TABLE_H_INCLUDED
22
23#include <xrpl/beast/hash/hash_append.h>
24#include <xrpl/beast/hash/uhash.h>
25#include <xrpl/beast/utility/Journal.h>
26#include <xrpl/json/json_forwards.h>
27#include <xrpl/protocol/PublicKey.h>
28
29#include <mutex>
30#include <optional>
31#include <string>
32#include <unordered_set>
33#include <vector>
34
35namespace ripple {
36
37class DatabaseCon;
38
39// Value type for reservations.
40struct PeerReservation final
41{
42public:
45
46 auto
47 toJson() const -> Json::Value;
48
49 template <typename Hasher>
50 friend void
51 hash_append(Hasher& h, PeerReservation const& x) noexcept
52 {
54 hash_append(h, x.nodeId);
55 }
56
57 friend bool
58 operator<(PeerReservation const& a, PeerReservation const& b)
59 {
60 return a.nodeId < b.nodeId;
61 }
62};
63
64// TODO: When C++20 arrives, take advantage of "equivalence" instead of
65// "equality". Add an overload for `(PublicKey, PeerReservation)`, and just
66// pass a `PublicKey` directly to `unordered_set.find`.
67struct KeyEqual final
68{
69 bool
70 operator()(PeerReservation const& lhs, PeerReservation const& rhs) const
71 {
72 return lhs.nodeId == rhs.nodeId;
73 }
74};
75
77{
78public:
81 : journal_(journal)
82 {
83 }
84
86 list() const;
87
88 bool
89 contains(PublicKey const& nodeId)
90 {
91 std::lock_guard lock(this->mutex_);
92 return table_.find({nodeId}) != table_.end();
93 }
94
95 // Because `ApplicationImp` has two-phase initialization, so must we.
96 // Our dependencies are not prepared until the second phase.
97 bool
98 load(DatabaseCon& connection);
99
105 insert_or_assign(PeerReservation const& reservation);
106
111 erase(PublicKey const& nodeId);
112
113private:
118};
119
120} // namespace ripple
121
122#endif
A generic endpoint for log messages.
Definition: Journal.h:59
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:62
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
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.
Definition: hash_append.h:236
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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