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/protocol/PublicKey.h>
27
28#include <mutex>
29#include <optional>
30#include <string>
31#include <unordered_set>
32#include <vector>
33
34namespace ripple {
35
36class DatabaseCon;
37
38// Value type for reservations.
39struct PeerReservation final
40{
41public:
44
45 auto
46 toJson() const -> Json::Value;
47
48 template <typename Hasher>
49 friend void
50 hash_append(Hasher& h, PeerReservation const& x) noexcept
51 {
53 hash_append(h, x.nodeId);
54 }
55
56 friend bool
57 operator<(PeerReservation const& a, PeerReservation const& b)
58 {
59 return a.nodeId < b.nodeId;
60 }
61};
62
63// TODO: When C++20 arrives, take advantage of "equivalence" instead of
64// "equality". Add an overload for `(PublicKey, PeerReservation)`, and just
65// pass a `PublicKey` directly to `unordered_set.find`.
66struct KeyEqual final
67{
68 bool
69 operator()(PeerReservation const& lhs, PeerReservation const& rhs) const
70 {
71 return lhs.nodeId == rhs.nodeId;
72 }
73};
74
76{
77public:
83
85 list() const;
86
87 bool
88 contains(PublicKey const& nodeId)
89 {
90 std::lock_guard lock(this->mutex_);
91 return table_.find({nodeId}) != table_.end();
92 }
93
94 // Because `ApplicationImp` has two-phase initialization, so must we.
95 // Our dependencies are not prepared until the second phase.
96 bool
97 load(DatabaseCon& connection);
98
104 insert_or_assign(PeerReservation const& reservation);
105
110 erase(PublicKey const& nodeId);
111
112private:
117};
118
119} // namespace ripple
120
121#endif
A generic endpoint for log messages.
Definition Journal.h:60
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:61
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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