rippled
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 <ripple/beast/hash/hash_append.h>
24 #include <ripple/beast/hash/uhash.h>
25 #include <ripple/beast/utility/Journal.h>
26 #include <ripple/json/json_forwards.h>
27 #include <ripple/protocol/PublicKey.h>
28 
29 #include <mutex>
30 #include <optional>
31 #include <string>
32 #include <unordered_set>
33 #include <vector>
34 
35 namespace ripple {
36 
37 class DatabaseCon;
38 
39 // Value type for reservations.
40 struct PeerReservation final
41 {
42 public:
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  {
53  using beast::hash_append;
54  hash_append(h, x.nodeId);
55  }
56 
57  friend bool
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`.
67 struct 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 {
78 public:
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 
113 private:
118 };
119 
120 } // namespace ripple
121 
122 #endif
ripple::PeerReservationTable::erase
std::optional< PeerReservation > erase(PublicKey const &nodeId)
Definition: PeerReservationTable.cpp:118
std::string
STL class.
ripple::PeerReservationTable::table_
std::unordered_set< PeerReservation, beast::uhash<>, KeyEqual > table_
Definition: PeerReservationTable.h:117
unordered_set
vector
std::lock_guard
STL class.
ripple::PeerReservationTable::connection_
DatabaseCon * connection_
Definition: PeerReservationTable.h:116
beast::Journal::getNullSink
static Sink & getNullSink()
Returns a Sink which does nothing.
Definition: beast_Journal.cpp:72
ripple::PeerReservation::toJson
auto toJson() const -> Json::Value
Definition: PeerReservationTable.cpp:40
Json
JSON (JavaScript Object Notation).
Definition: DeliverMax.h:28
ripple::PeerReservation::nodeId
PublicKey nodeId
Definition: PeerReservationTable.h:43
ripple::PeerReservationTable::journal_
beast::Journal journal_
Definition: PeerReservationTable.h:114
ripple::PublicKey
A public key.
Definition: PublicKey.h:61
ripple::PeerReservationTable::PeerReservationTable
PeerReservationTable(beast::Journal journal=beast::Journal(beast::Journal::getNullSink()))
Definition: PeerReservationTable.h:79
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::PeerReservation
Definition: PeerReservationTable.h:40
ripple::KeyEqual::operator()
bool operator()(PeerReservation const &lhs, PeerReservation const &rhs) const
Definition: PeerReservationTable.h:70
ripple::PeerReservationTable
Definition: PeerReservationTable.h:76
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerReservationTable::mutex_
std::mutex mutex_
Definition: PeerReservationTable.h:115
ripple::PeerReservationTable::list
std::vector< PeerReservation > list() const
Definition: PeerReservationTable.cpp:52
beast::hash_append
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
ripple::DatabaseCon
Definition: DatabaseCon.h:81
optional
mutex
ripple::PeerReservation::operator<
friend bool operator<(PeerReservation const &a, PeerReservation const &b)
Definition: PeerReservationTable.h:58
ripple::PeerReservationTable::contains
bool contains(PublicKey const &nodeId)
Definition: PeerReservationTable.h:89
ripple::KeyEqual
Definition: PeerReservationTable.h:67
ripple::PeerReservationTable::insert_or_assign
std::optional< PeerReservation > insert_or_assign(PeerReservation const &reservation)
Definition: PeerReservationTable.cpp:84
ripple::PeerReservation::description
std::string description
Definition: PeerReservationTable.h:44
ripple::PeerReservationTable::load
bool load(DatabaseCon &connection)
Definition: PeerReservationTable.cpp:71
ripple::PeerReservation::hash_append
friend void hash_append(Hasher &h, PeerReservation const &x) noexcept
Definition: PeerReservationTable.h:51
string