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 #define SOCI_USE_BOOST
30 #include <soci/soci.h>
31 
32 #include <mutex>
33 #include <optional>
34 #include <string>
35 #include <unordered_set>
36 #include <vector>
37 
38 namespace ripple {
39 
40 class DatabaseCon;
41 
42 // Value type for reservations.
43 struct PeerReservation final
44 {
45 public:
48 
49  auto
50  toJson() const -> Json::Value;
51 
52  template <typename Hasher>
53  friend void
54  hash_append(Hasher& h, PeerReservation const& x) noexcept
55  {
56  using beast::hash_append;
57  hash_append(h, x.nodeId);
58  }
59 
60  friend bool
62  {
63  return a.nodeId < b.nodeId;
64  }
65 };
66 
67 // TODO: When C++20 arrives, take advantage of "equivalence" instead of
68 // "equality". Add an overload for `(PublicKey, PeerReservation)`, and just
69 // pass a `PublicKey` directly to `unordered_set.find`.
70 struct KeyEqual final
71 {
72  bool
73  operator()(PeerReservation const& lhs, PeerReservation const& rhs) const
74  {
75  return lhs.nodeId == rhs.nodeId;
76  }
77 };
78 
80 {
81 public:
84  : journal_(journal)
85  {
86  }
87 
89  list() const;
90 
91  bool
92  contains(PublicKey const& nodeId)
93  {
94  std::lock_guard lock(this->mutex_);
95  return table_.find({nodeId}) != table_.end();
96  }
97 
98  // Because `ApplicationImp` has two-phase initialization, so must we.
99  // Our dependencies are not prepared until the second phase.
100  bool
101  load(DatabaseCon& connection);
102 
108  insert_or_assign(PeerReservation const& reservation);
109 
114  erase(PublicKey const& nodeId);
115 
116 private:
121 };
122 
123 } // namespace ripple
124 
125 #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:120
unordered_set
vector
std::lock_guard
STL class.
ripple::PeerReservationTable::connection_
DatabaseCon * connection_
Definition: PeerReservationTable.h:119
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: json_reader.cpp:27
ripple::PeerReservation::nodeId
PublicKey nodeId
Definition: PeerReservationTable.h:46
ripple::PeerReservationTable::journal_
beast::Journal journal_
Definition: PeerReservationTable.h:117
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::PeerReservationTable::PeerReservationTable
PeerReservationTable(beast::Journal journal=beast::Journal(beast::Journal::getNullSink()))
Definition: PeerReservationTable.h:82
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::PeerReservation
Definition: PeerReservationTable.h:43
ripple::KeyEqual::operator()
bool operator()(PeerReservation const &lhs, PeerReservation const &rhs) const
Definition: PeerReservationTable.h:73
ripple::PeerReservationTable
Definition: PeerReservationTable.h:79
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:118
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:61
ripple::PeerReservationTable::contains
bool contains(PublicKey const &nodeId)
Definition: PeerReservationTable.h:92
ripple::KeyEqual
Definition: PeerReservationTable.h:70
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:47
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:54
string