xrpld
Loading...
Searching...
No Matches
PeerReservationTable.cpp
1#include <xrpl/core/PeerReservationTable.h>
2
3#include <xrpl/json/json_value.h>
4#include <xrpl/protocol/PublicKey.h>
5#include <xrpl/protocol/jss.h>
6#include <xrpl/protocol/tokens.h>
7#include <xrpl/server/Wallet.h>
8
9#include <algorithm>
10#include <iterator>
11#include <mutex>
12#include <optional>
13#include <string>
14#include <vector>
15
16namespace xrpl {
17
18auto
20{
22 result[jss::node] = toBase58(TokenType::NodePublic, nodeId);
23 if (!description.empty())
24 {
25 result[jss::description] = description;
26 }
27 return result;
28}
29
30auto
32{
34 {
35 std::scoped_lock const lock(mutex_);
36 list.reserve(table_.size());
38 }
39 std::sort(list.begin(), list.end()); // NOLINT(modernize-use-ranges)
40 return list;
41}
42
43// See `include/xrpl/rdb/DBInit.h` for the `CREATE TABLE` statement.
44// It is unfortunate that we do not get to define a function for it.
45
46// We choose a `bool` return type to fit in with the error handling scheme
47// of other functions called from `ApplicationImp::setup`, but we always
48// return "no error" (`true`) because we can always return an empty table.
49bool
51{
52 std::scoped_lock const lock(mutex_);
53
54 connection_ = &connection;
55 auto db = connection.checkoutDb();
56 auto table = getPeerReservationTable(*db, journal_);
57 table_.insert(table.begin(), table.end());
58
59 return true;
60}
61
64{
66
67 std::scoped_lock const lock(mutex_);
68
69 auto hint = table_.find(reservation);
70 if (hint != table_.end())
71 {
72 // The node already has a reservation. Remove it.
73 // `std::unordered_set` does not have an `insertOrAssign` method,
74 // and sadly makes it impossible for us to implement one efficiently:
75 // https://stackoverflow.com/q/49651835/618906
76 // Regardless, we don't expect this function to be called often, or
77 // for the table to be very large, so this less-than-ideal
78 // remove-then-insert is acceptable in order to present a better API.
79 previous = *hint;
80 // We should pick an adjacent location for the insertion hint.
81 // Decrementing may be illegal if the found reservation is at the
82 // beginning. Incrementing is always legal; at worst we'll point to
83 // the end.
84 auto const deleteme = hint;
85 ++hint;
86 table_.erase(deleteme);
87 }
88 table_.insert(hint, reservation);
89
90 auto db = connection_->checkoutDb();
91 insertPeerReservation(*db, reservation.nodeId, reservation.description);
92
93 return previous;
94}
95
98{
100
101 std::scoped_lock const lock(mutex_);
102
103 auto const it = table_.find({.nodeId = nodeId});
104 if (it != table_.end())
105 {
106 previous = *it;
107 table_.erase(it);
108 auto db = connection_->checkoutDb();
109 deletePeerReservation(*db, nodeId);
110 }
111
112 return previous;
113}
114
115} // namespace xrpl
T back_inserter(T... args)
Represents a JSON value.
Definition json_value.h:130
LockedSociSession checkoutDb()
std::vector< PeerReservation > list() const
std::optional< PeerReservation > insertOrAssign(PeerReservation const &reservation)
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
T copy(T... args)
JSON (JavaScript Object Notation).
Definition json_errors.h:5
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
void deletePeerReservation(soci::session &session, PublicKey const &nodeId)
deletePeerReservation Deletes an entry from the peer reservation table.
Definition Wallet.cpp:222
void insertPeerReservation(soci::session &session, PublicKey const &nodeId, std::string const &description)
insertPeerReservation Adds an entry to the peer reservation table.
Definition Wallet.cpp:208
std::unordered_set< PeerReservation, beast::Uhash<>, KeyEqual > getPeerReservationTable(soci::session &session, beast::Journal j)
getPeerReservationTable Returns the peer reservation table.
Definition Wallet.cpp:173
T sort(T... args)
auto toJson() const -> json::Value